Skip to content

Commit

Permalink
feat: display which filters/keywords are used in publication
Browse files Browse the repository at this point in the history
  • Loading branch information
sparky-raccoon committed Mar 16, 2024
1 parent 4fc6e58 commit e82d907
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 15 deletions.
9 changes: 6 additions & 3 deletions src/cron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,17 @@ const initCronJob = async (client: Client) => {
if (testChannel && testChannel.type === ChannelType.GuildText) {
const filters = await FirestoreChannel.getFilters(channelId);
const noFiltersDefined = filters.length === 0;
const someFiltersMatch = filters.some((f) => {
const someFiltersMatch = filters.filter((f) => {
const regex = new RegExp(`[\\s,;.\\-_'"]${f}[\\s,;.\\-_'"]`, "i");
return regex.test(pub.title) || regex.test(pub.contentSnippet);
});

if (noFiltersDefined || someFiltersMatch) {
if (noFiltersDefined || someFiltersMatch.length > 0) {
logger.info(`Nouvelle publication sur ${testChannel.id}: ${pub.title}`)
const message = getMessage(Message.POST, pub);
const message = getMessage(Message.POST, {
...pub,
...(someFiltersMatch.length > 0 ? { filters: someFiltersMatch } : {})
});
await testChannel.send(message[0]);
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/utils/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { blockQuote } from "discord.js";
import { Source, isSource } from "@/utils/types";
import { Publication, Source, isSource } from "@/utils/types";

const formatSourceToBlockQuote = (source: Source): `>>> ${string}` => {
const { type, name, url } = source;
Expand Down Expand Up @@ -68,6 +68,16 @@ const formatTagListToString = (list: string[]): string => {
return description;
};

const getPublicationDescription = (publication: Publication): string => {
const { contentSnippet, date, author, link, duplicateSources, filters } = publication;
return `${contentSnippet}\n\n` +
`Date de publication : ${date}\n` +
(author ? `Auteur.rice : ${author}\n` : "") +
`Source : ${link}` +
(duplicateSources ? `\nAussi visible sur : ${duplicateSources.join(", ")}` : "") +
(filters ? `\nFiltres concernés : ${filters.join(", ")}` : "");
};

const splitDescriptionInMultipleMessages = (description: string): string[] => {
const messages: string[] = [];
const descriptionLines = description.split("\n");
Expand All @@ -90,5 +100,6 @@ export {
formatSourceListToBlockQuotes,
formatFullListToDescription,
formatTagListToString,
getPublicationDescription,
splitDescriptionInMultipleMessages,
};
13 changes: 2 additions & 11 deletions src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
formatSourceListToBlockQuotes,
formatSourceToBlockQuote,
formatTagListToString,
getPublicationDescription,
splitDescriptionInMultipleMessages,
} from "@/utils/formatters";
import { confirmOrCancelButton } from "@/components/confirm-button";
Expand Down Expand Up @@ -115,19 +116,9 @@ const getMessage = (type: Message, data?: MessageData): any => {
type,
name,
title: pTitle,
link,
contentSnippet,
author,
date,
duplicateSources,
} = data;
title += `[${name}] ${pTitle}`;
description =
`${contentSnippet}\n\n` +
`Date de publication : ${date}\n` +
(author ? `Auteur.rice : ${author}\n` : "") +
`Source : ${link}` +
(duplicateSources ? `\nAussi visible sur : ${duplicateSources.join(", ")}` : "");
description = getPublicationDescription(data);
color = getColorForSourceType(type);
break;
}
Expand Down
1 change: 1 addition & 0 deletions src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ interface Publication {
author?: string;
sourceId: string;
duplicateSources?: string[];
filters?: string[];
}

const isSource = (source: unknown): source is Source => {
Expand Down

0 comments on commit e82d907

Please sign in to comment.