[calendar] handle messages too long for a tweet

This commit is contained in:
Andrea 2022-02-07 15:43:58 +01:00
parent d9a962dd62
commit 307befffd2
1 changed files with 25 additions and 5 deletions

View File

@ -48,6 +48,20 @@ const getEventName = (name) => {
return name; return name;
} }
const buildMessage = (events, locale, day, link) => {
if (events.length === 0) {
return null;
}
let message = `[${locale.name}] ${day.toString()}\n\n${translations.calendar.banner}:\n`;
for (let event of events) {
message += ` - ${event}\n`;
}
message += `\n${link}`;
return message;
}
const eventsSummary = (day, locale) => { const eventsSummary = (day, locale) => {
const eventsRaw = calendar.getCurrentYear().eventsByDate[day.toString()]; const eventsRaw = calendar.getCurrentYear().eventsByDate[day.toString()];
@ -57,14 +71,20 @@ const eventsSummary = (day, locale) => {
let message = null; let message = null;
let events = []; let events = [];
if (eventsRaw !== undefined && eventsRaw.length > 0) { if (eventsRaw !== undefined && eventsRaw.length > 0) {
message = `[${locale.name}] ${day.toString()}\n\n${translations.calendar.banner}:\n`;
for (let event of eventsRaw) { for (let event of eventsRaw) {
const name = getEventName(event.name); events.push(getEventName(event.name));
message += ` - ${name}\n`;
events.push(name);
delete event.daysMemoise; delete event.daysMemoise;
} }
message += `\n${link}`; }
let eventsForMessage = [...events];
while (true) {
message = buildMessage(eventsForMessage, locale, day, link);
if (message === null || message.length <= 280) {
break;
} else {
eventsForMessage = eventsForMessage.slice(0, eventsForMessage.length - 1);
}
} }
return { return {