{ "version": 3, "sources": ["../../../../server/chat-plugins/daily-spotlight.ts"], "sourcesContent": ["import {FS, Utils} from '../../lib';\r\n\r\nconst DAY = 24 * 60 * 60 * 1000;\r\nconst SPOTLIGHT_FILE = 'config/chat-plugins/spotlights.json';\r\nconst NUMBER_REGEX = /^\\s*[0-9]+\\s*$/;\r\n\r\n/** legacy - string = just url, arr is [url, width, height] */\r\ntype StoredImage = string | [string, number, number];\r\n\r\ninterface Spotlight {\r\n\timage?: StoredImage;\r\n\tdescription: string;\r\n\ttime: number;\r\n}\r\n\r\nexport let spotlights: {\r\n\t[roomid: string]: {[k: string]: Spotlight[]},\r\n} = {};\r\n\r\ntry {\r\n\tspotlights = JSON.parse(FS(SPOTLIGHT_FILE).readIfExistsSync() || \"{}\");\r\n\tfor (const roomid in spotlights) {\r\n\t\tfor (const k in spotlights[roomid]) {\r\n\t\t\tfor (const spotlight of spotlights[roomid][k]) {\r\n\t\t\t\tif (!spotlight.time) {\r\n\t\t\t\t\tspotlight.time = Date.now();\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n} catch (e: any) {\r\n\tif (e.code !== 'MODULE_NOT_FOUND' && e.code !== 'ENOENT') throw e;\r\n}\r\nif (!spotlights || typeof spotlights !== 'object') spotlights = {};\r\n\r\nfunction saveSpotlights() {\r\n\tFS(SPOTLIGHT_FILE).writeUpdate(() => JSON.stringify(spotlights));\r\n}\r\n\r\nfunction nextDaily() {\r\n\tfor (const roomid in spotlights) {\r\n\t\tfor (const key in spotlights[roomid]) {\r\n\t\t\tif (spotlights[roomid][key].length > 1) {\r\n\t\t\t\tspotlights[roomid][key].shift();\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tsaveSpotlights();\r\n\ttimeout = setTimeout(nextDaily, DAY);\r\n}\r\n\r\nconst midnight = new Date();\r\nmidnight.setHours(24, 0, 0, 0);\r\nlet timeout = setTimeout(nextDaily, midnight.valueOf() - Date.now());\r\n\r\nexport async function renderSpotlight(roomid: RoomID, key: string, index: number) {\r\n\tlet imgHTML = '';\r\n\tconst {image, description} = spotlights[roomid][key][index];\r\n\r\n\tif (image) {\r\n\t\tif (Array.isArray(image)) {\r\n\t\t\timgHTML = `
| ${Chat.formatText(description, true)} | ${imgHTML}
This room has no daily spotlights.
${key}: | `;\r\n\t\t\t\tconst keys = Utils.sortBy(spotlights[room.roomid][key].slice(), spotlight => {\r\n\t\t\t\t\tswitch (sortType) {\r\n\t\t\t\t\tcase 'time':\r\n\t\t\t\t\t\treturn -spotlight.time;\r\n\t\t\t\t\tdefault:\r\n\t\t\t\t\t\treturn spotlight.description;\r\n\t\t\t\t\t}\r\n\t\t\t\t});\r\n\t\t\t\tfor (const [i] of keys.entries()) {\r\n\t\t\t\t\tconst html = await renderSpotlight(room.roomid, key, i);\r\n\t\t\t\t\tbuf += `|
|---|---|
| ${i ? i : 'Current'} | ${html} |
/setdaily ${key},${image ? `${image},` : ''}${code}/daily [name]: shows the daily spotlight.!daily [name]: shows the daily spotlight to everyone. Requires: + % @ # &/setdaily [name], [image], [description]: sets the daily spotlight. Image can be left out. Requires: % @ # &/queuedaily [name], [image], [description]: queues a daily spotlight. At midnight, the spotlight with this name will automatically switch to the next queued spotlight. Image can be left out. Requires: % @ # &/queuedailyat [name], [queue number], [image], [description]: inserts a daily spotlight into the queue at the specified number (starting from 1). Requires: % @ # &/replacedaily [name], [queue number], [image], [description]: replaces the daily spotlight queued at the specified number. Requires: % @ # &/removedaily [name][, queue number]: if no queue number is provided, deletes all queued and current spotlights with the given name. If a number is provided, removes a specific future spotlight from the queue. Requires: % @ # &/swapdaily [name], [queue number], [queue number]: swaps the two queued spotlights at the given queue numbers. Requires: % @ # &/viewspotlights [sorter]: shows all current spotlights in the room. For staff, also shows queued spotlights.` +\r\n\t\t\t`[sorter] can either be unset, 'time', or 'alphabet'. These sort by either the time added, or alphabetical order.` +\r\n\t\t\t`