From 91cddfa8d8b588a2619d75a3028f65928dab425d Mon Sep 17 00:00:00 2001 From: John Livingston Date: Fri, 6 Sep 2024 11:53:07 +0200 Subject: [PATCH] Emoji only mode WIP: * Button to enable it on all rooms. --- client/@types/global.d.ts | 6 +++ .../configuration/elements/channel-emojis.ts | 15 ++++++++ .../elements/templates/channel-emojis.ts | 21 +++++++++- .../configuration/services/channel-details.ts | 19 ++++++++++ languages/en.yml | 15 ++++++++ ...mod_http_peertubelivechat_manage_rooms.lua | 4 +- server/lib/routers/api/configuration.ts | 38 +++++++++++++++++++ 7 files changed, 116 insertions(+), 2 deletions(-) diff --git a/client/@types/global.d.ts b/client/@types/global.d.ts index 13bbbbfc..a9415661 100644 --- a/client/@types/global.d.ts +++ b/client/@types/global.d.ts @@ -144,3 +144,9 @@ declare const LOC_PROSODY_FIREWALL_FILE_ENABLED: string declare const LOC_PROSODY_FIREWALL_NAME: string declare const LOC_PROSODY_FIREWALL_NAME_DESC: string declare const LOC_PROSODY_FIREWALL_CONTENT: string + +declare const LOC_EMOJI_ONLY_MODE_TITLE: string +declare const LOC_EMOJI_ONLY_MODE_DESC_1: string +declare const LOC_EMOJI_ONLY_MODE_DESC_2: string +declare const LOC_EMOJI_ONLY_MODE_DESC_3: string +declare const LOC_EMOJI_ONLY_ENABLE_ALL_ROOMS: string diff --git a/client/common/configuration/elements/channel-emojis.ts b/client/common/configuration/elements/channel-emojis.ts index 126e4c67..e7e09de8 100644 --- a/client/common/configuration/elements/channel-emojis.ts +++ b/client/common/configuration/elements/channel-emojis.ts @@ -256,6 +256,21 @@ export class ChannelEmojisElement extends LivechatElement { } } + public async enableEmojisOnlyModeOnAllRooms (ev: Event): Promise { + ev.preventDefault() + if (!this._channelDetailsService || !this.channelId) { + this.ptNotifier.error(await this.ptTranslate(LOC_ERROR)) + return + } + try { + await this._channelDetailsService.enableEmojisOnlyModeOnAllRooms(this.channelId) + this.ptNotifier.info(await this.ptTranslate(LOC_SUCCESSFULLY_SAVED)) + } catch (err) { + console.error(err) + this.ptNotifier.error(await this.ptTranslate(LOC_ERROR)) + } + } + /** * Takes an url (or dataUrl), download the image, and converts to dataUrl. * @param url the url diff --git a/client/common/configuration/elements/templates/channel-emojis.ts b/client/common/configuration/elements/templates/channel-emojis.ts index 12e1e293..ae354500 100644 --- a/client/common/configuration/elements/templates/channel-emojis.ts +++ b/client/common/configuration/elements/templates/channel-emojis.ts @@ -45,13 +45,14 @@ export function tplChannelEmojis (el: ChannelEmojisElement): TemplateResult { +

${ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_EMOJIS_TITLE)}

+

${ptTr(LOC_LIVECHAT_CONFIGURATION_CHANNEL_EMOJIS_DESC)}

-
${ @@ -106,5 +107,23 @@ export function tplChannelEmojis (el: ChannelEmojisElement): TemplateResult {
+ +

${ptTr(LOC_EMOJI_ONLY_MODE_TITLE)}

+ +

+ ${ptTr(LOC_EMOJI_ONLY_MODE_DESC_1, true)} +

+

+ ${ptTr(LOC_EMOJI_ONLY_MODE_DESC_2, true)} +

+

+ ${ptTr(LOC_EMOJI_ONLY_MODE_DESC_3, true)} +

+ +
+ +
` } diff --git a/client/common/configuration/services/channel-details.ts b/client/common/configuration/services/channel-details.ts index 99808d41..16a77153 100644 --- a/client/common/configuration/services/channel-details.ts +++ b/client/common/configuration/services/channel-details.ts @@ -312,4 +312,23 @@ export class ChannelDetailsService { return response.json() } + + public async enableEmojisOnlyModeOnAllRooms (channelId: number): Promise { + const response = await fetch( + getBaseRoute(this._registerClientOptions) + + '/api/configuration/channel/emojis/' + + encodeURIComponent(channelId) + + '/enable_emoji_only', + { + method: 'POST', + headers: this._headers + } + ) + + if (!response.ok) { + throw new Error('Can\'t enable Emojis Only Mode on all rooms.') + } + + return response.json() + } } diff --git a/languages/en.yml b/languages/en.yml index 04c3a539..3b712482 100644 --- a/languages/en.yml +++ b/languages/en.yml @@ -638,3 +638,18 @@ prosody_firewall_name_desc: | prosody_firewall_content: File content emoji_only_info: Emoji only mode is enabled, you can only use emoji in your messages. +emoji_only_mode_title: Emojis only mode +emoji_only_mode_desc_1: | + You can enable an "Emojy only mode" in your chatrooms. + When this mode is enabled, participants can only send emojis (standard, or channel custom emojis). + Moderators are not affected by this limitation. +emoji_only_mode_desc_2: | + This mode can be usefull for Example: +
    +
  • To avoid spam or offensive message when you are not here to moderate.
  • +
  • When there are too many speaking participants, and you can't no more moderate correctly.
  • +
+emoji_only_mode_desc_3: | + To enable or disable this mode, you can use the room configuration form. + If you want to enable it for all your rooms at once, you can use the button bellow. +emoji_only_enable_all_rooms: Enable the emoji only mode on all channel's chatrooms diff --git a/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua b/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua index 94a131e1..69b1713b 100644 --- a/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua +++ b/prosody-modules/mod_http_peertubelivechat_manage_rooms/mod_http_peertubelivechat_manage_rooms.lua @@ -107,7 +107,9 @@ local function update_room(event) end if type(config.livechat_emoji_only) == "boolean" then if set_peertubelivechat_emoji_only_mode then - set_peertubelivechat_emoji_only_mode(room, config.livechat_emoji_only) + if set_peertubelivechat_emoji_only_mode(room, config.livechat_emoji_only) then + must104 = true; + end end end if type(config.livechat_custom_emoji_regexp) == "string" then diff --git a/server/lib/routers/api/configuration.ts b/server/lib/routers/api/configuration.ts index 4127d8a3..23e25382 100644 --- a/server/lib/routers/api/configuration.ts +++ b/server/lib/routers/api/configuration.ts @@ -200,6 +200,44 @@ async function initConfigurationApiRouter (options: RegisterServerOptions, route } } ])) + + router.post('/configuration/channel/emojis/:channelId/enable_emoji_only', asyncMiddleware([ + checkConfigurationEnabledMiddleware(options), + getCheckConfigurationChannelMiddleware(options), + async (req: Request, res: Response, _next: NextFunction): Promise => { + try { + if (!res.locals.channelInfos) { + throw new Error('Missing channelInfos in res.locals, should not happen') + } + + const emojis = Emojis.singleton() + const channelInfos = res.locals.channelInfos as ChannelInfos + + logger.info(`Enabling emoji only mode on each channel ${channelInfos.id} rooms ...`) + + // We can also update the EmojisRegexp, just in case. + const customEmojisRegexp = await emojis.getChannelCustomEmojisRegexp(channelInfos.id) + const roomJIDs = RoomChannel.singleton().getChannelRoomJIDs(channelInfos.id) + for (const roomJID of roomJIDs) { + // No need to await here + logger.info(`Enabling emoji only mode on room ${roomJID} ...`) + updateProsodyRoom(options, roomJID, { + livechat_emoji_only: true, + livechat_custom_emoji_regexp: customEmojisRegexp + }).then( + () => {}, + (err) => logger.error(err) + ) + } + + res.status(200) + res.json({ ok: true }) + } catch (err) { + logger.error(err) + res.sendStatus(500) + } + } + ])) } export {