From e82d907760afcb78bd87f7d0dd01e7ba03f1387c Mon Sep 17 00:00:00 2001 From: Andrea Saez Date: Sat, 16 Mar 2024 12:41:13 +0100 Subject: [PATCH] feat: display which filters/keywords are used in publication --- src/cron.ts | 9 ++++++--- src/utils/formatters.ts | 13 ++++++++++++- src/utils/messages.ts | 13 ++----------- src/utils/types.ts | 1 + 4 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/cron.ts b/src/cron.ts index 1933bc6..731decd 100644 --- a/src/cron.ts +++ b/src/cron.ts @@ -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]); } } diff --git a/src/utils/formatters.ts b/src/utils/formatters.ts index 9d338aa..7a0fa61 100644 --- a/src/utils/formatters.ts +++ b/src/utils/formatters.ts @@ -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; @@ -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"); @@ -90,5 +100,6 @@ export { formatSourceListToBlockQuotes, formatFullListToDescription, formatTagListToString, + getPublicationDescription, splitDescriptionInMultipleMessages, }; diff --git a/src/utils/messages.ts b/src/utils/messages.ts index 2b6ac12..0597164 100644 --- a/src/utils/messages.ts +++ b/src/utils/messages.ts @@ -17,6 +17,7 @@ import { formatSourceListToBlockQuotes, formatSourceToBlockQuote, formatTagListToString, + getPublicationDescription, splitDescriptionInMultipleMessages, } from "@/utils/formatters"; import { confirmOrCancelButton } from "@/components/confirm-button"; @@ -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; } diff --git a/src/utils/types.ts b/src/utils/types.ts index 5492f03..395c65c 100644 --- a/src/utils/types.ts +++ b/src/utils/types.ts @@ -15,6 +15,7 @@ interface Publication { author?: string; sourceId: string; duplicateSources?: string[]; + filters?: string[]; } const isSource = (source: unknown): source is Source => {