Skip to content

Commit

Permalink
New option to only allow registered users to speak WIP (#233):
Browse files Browse the repository at this point in the history
* Display a specific message to muted anonymous users.
  • Loading branch information
JohnXLivingston committed Jun 21, 2024
1 parent 8cac14d commit 92cdf9b
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 27 deletions.
1 change: 1 addition & 0 deletions conversejs/builtin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ async function initConverse (
params.livechat_enable_viewer_mode = autoViewerMode && !isAuthenticated && !isRemoteWithNicknameSet

params.livechat_specific_external_authent = isAuthenticatedWithExternalAccount
params.livechat_specific_is_anonymous = !isAuthenticated

if (tryOIDC && !isAuthenticated) {
params.livechat_external_auth_oidc_buttons = initConverseParams.externalAuthOIDC
Expand Down
6 changes: 5 additions & 1 deletion conversejs/custom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ import '../custom/plugins/size/index.js'
import '../custom/plugins/tasks/index.js'
/* END: Removable components */

// We must add our custom plugins to CORE_PLUGINS (so it is white listed):
import { CORE_PLUGINS } from './headless/shared/constants.js'
import { ROOM_FEATURES } from './headless/plugins/muc/constants.js'
// We must add our custom plugins to CORE_PLUGINS (so it is white listed):
CORE_PLUGINS.push('livechat-converse-size')
CORE_PLUGINS.push('livechat-converse-tasks')
// We must also add our custom ROOM_FEATURES, so that they correctly resets
// (see headless/plugins/muc, getDiscoInfoFeatures, which loops on this const)
ROOM_FEATURES.push('x_peertubelivechat_mute_anonymous')

_converse.CustomElement = CustomElement

Expand Down
61 changes: 41 additions & 20 deletions conversejs/custom/templates/muc-bottom-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,26 +79,21 @@ class SlowMode extends CustomElement {
api.elements.define('livechat-slow-mode', SlowMode)

const tplSlowMode = (o) => {
if (!o.can_edit) { return html`` }
return html`<livechat-slow-mode jid=${o.model.get('jid')}>`
}

export default (o) => {
// ConverseJS 10.x does not handle properly the visitor role in unmoderated rooms.
// See https://github.com/conversejs/converse.js/issues/3428 for more info.
// So we will do a dirty hack here to fix this case.
// Note: ConverseJS 11.x has changed the code, and could be fixed more cleanly (or will be fixed if #3428 is fixed).
if (o.can_edit && o.model.getOwnRole() === 'visitor') {
o.can_edit = false
const tplViewerMode = (o) => {
if (!api.settings.get('livechat_enable_viewer_mode')) {
return html``
}

if (api.settings.get('livechat_enable_viewer_mode')) {
const model = o.model
const i18nNickname = __('Nickname')
const i18nJoin = __('Enter groupchat')
const i18nHeading = __('Choose a nickname to enter')
// eslint-disable-next-line no-undef
const i18nExternalLogin = __(LOC_login_using_external_account)
return html`
const model = o.model
const i18nNickname = __('Nickname')
const i18nJoin = __('Enter groupchat')
const i18nHeading = __('Choose a nickname to enter')
// eslint-disable-next-line no-undef
const i18nExternalLogin = __(LOC_login_using_external_account)
return html`
<div class="livechat-viewer-mode-content chatroom-form-container">
<form class="converse-form chatroom-form" @submit=${ev => setNickname(ev, model)}>
<label>${i18nHeading}</label>
Expand Down Expand Up @@ -129,12 +124,38 @@ export default (o) => {
</div>
`
}
</div>
${tplSlowMode(o)}
${tplMucBottomPanel(o)}`
</div>`
}

export default (o) => {
// ConverseJS 10.x does not handle properly the visitor role in unmoderated rooms.
// See https://github.com/conversejs/converse.js/issues/3428 for more info.
// So we will do a dirty hack here to fix this case.
// Note: ConverseJS 11.x has changed the code, and could be fixed more cleanly (or will be fixed if #3428 is fixed).
if (o.can_edit && o.model.getOwnRole() === 'visitor') {
o.can_edit = false
}

let mutedAnonymousMessage
if (
!o.can_edit &&
o.model.features?.get?.('x_peertubelivechat_mute_anonymous') &&
_converse.api.settings.get('livechat_specific_is_anonymous') === true
) {
// If we are moderated because we are anonymous, we want to display a custom message.
// FIXME: if x_peertubelivechat_mute_anonymous changes, user are first muted/voiced, and then only the
// status 104 is sent. But we don't listen to 'change:x_peertubelivechat_mute_anonymous'.
// So the custom message won't be correct. But this is not a big issue.
// eslint-disable-next-line no-undef
mutedAnonymousMessage = __(LOC_muted_anonymous_message)
}

return html`
${tplViewerMode(o)}
${tplSlowMode(o)}
${tplMucBottomPanel(o)}`
${
mutedAnonymousMessage
? html`<span class="muc-bottom-panel muc-bottom-panel--muted">${mutedAnonymousMessage}</span>`
: tplMucBottomPanel(o)
}`
}
4 changes: 3 additions & 1 deletion conversejs/lib/plugins/livechat-specific.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ export const livechatSpecificsPlugin = {

_converse.api.settings.extend({
// if user is authenticated with an external account (to add a logout button)
livechat_specific_external_authent: false
livechat_specific_external_authent: false,
// if user is anonymous
livechat_specific_is_anonymous: false
})

_converse.api.listen.on('getHeadingButtons', (view: any, buttons: any[]) => {
Expand Down
3 changes: 2 additions & 1 deletion conversejs/loc.keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const locKeys = [
'task_delete_confirm',
'task_list_pick_title',
'task_list_pick_empty',
'task_list_pick_message'
'task_list_pick_message',
'muted_anonymous_message'
]

module.exports = locKeys
2 changes: 2 additions & 0 deletions languages/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -546,3 +546,5 @@ livechat_token_disabled_description: |
These tokens can for example be used to include the chat in OBS web docks.
Check <a href="https://livingston.frama.io/peertube-plugin-livechat/documentation/user/obs" target="_blank">the documentation</a> for more information.
You can disable this feature by checking this setting.
muted_anonymous_message: Only registered users can send messages.
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ local function set_peertubelivechat_mute_anonymous(room, mute_anonymous)
end

module:hook("muc-disco#info", function(event)
if not get_peertubelivechat_mute_anonymous(event.room) then
return;
if get_peertubelivechat_mute_anonymous(event.room) then
event.reply:tag("feature", {var = "x_peertubelivechat_mute_anonymous"}):up();
end
-- TODO: is this feature really necessary? (if not, we should also remove the status_code 104 when saving roomconfig)
event.reply:tag("feature", {var = "x_peertubelivechat_mute_anonymous"}):up();
end);

module:hook("muc-config-form", function(event)
Expand Down

0 comments on commit 92cdf9b

Please sign in to comment.