Skip to content

Commit

Permalink
Properly query disco whether blocking is supported
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Feb 20, 2023
1 parent a5476b6 commit 663bc5b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
6 changes: 2 additions & 4 deletions src/headless/plugins/blocking/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { _converse, api, converse } from "@converse/headless/core.js";

const { Strophe, $iq } = converse.env;
const { Strophe } = converse.env;

export async function onConnected () {
export function onConnected () {
api.refreshBlocklist();
_converse.connection.addHandler(api.handleBlockingStanza, Strophe.NS.BLOCKING, 'iq', 'set', null, null);
}


14 changes: 10 additions & 4 deletions src/headless/plugins/muc/muc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1336,28 +1336,34 @@ const ChatRoomMixin = {
return true;
},

getAllowedCommands () {
async getAllowedCommands () {
let allowed_commands = ['clear', 'help', 'me', 'nick', 'register'];

// Only allow blocking commands when server supports it and we also support it
if ( _converse.disco_entities.get(_converse.domain, true)?.features?.findWhere({'var': Strophe.NS.BLOCKING}) &&
( _converse.pluggable.plugins['converse-blocking']?.enabled(_converse) )
) {
if (
await api.disco.supports(Strophe.NS.BLOCKING, _converse.domain) &&
_converse.pluggable.plugins['converse-blocking']?.enabled(_converse)
) {
allowed_commands = [...allowed_commands, ...['block', 'unblock']];
}

if (this.config.get('changesubject') || ['owner', 'admin'].includes(this.getOwnAffiliation())) {
allowed_commands = [...allowed_commands, ...['subject', 'topic']];
}

const occupant = this.occupants.findWhere({ 'jid': _converse.bare_jid });
if (this.verifyAffiliations(['owner'], occupant, false)) {
allowed_commands = allowed_commands.concat(OWNER_COMMANDS).concat(ADMIN_COMMANDS);
} else if (this.verifyAffiliations(['admin'], occupant, false)) {
allowed_commands = allowed_commands.concat(ADMIN_COMMANDS);
}

if (this.verifyRoles(['moderator'], occupant, false)) {
allowed_commands = allowed_commands.concat(MODERATOR_COMMANDS).concat(VISITOR_COMMANDS);
} else if (!this.verifyRoles(['visitor', 'participant', 'moderator'], occupant, false)) {
allowed_commands = allowed_commands.concat(VISITOR_COMMANDS);
}

allowed_commands.sort();

if (Array.isArray(api.settings.get('muc_disable_slash_commands'))) {
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/muc-views/chatarea.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ export default class MUCChatArea extends CustomElement {
);
}

getHelpMessages () {
async getHelpMessages () {
const setting = api.settings.get('muc_disable_slash_commands');
const disabled_commands = Array.isArray(setting) ? setting : [];
const allowed_commands = await this.model.getAllowedCommands();

return [
`<strong>/admin</strong>: ${__("Change user's affiliation to admin")}`,
`<strong>/ban</strong>: ${__('Ban user by changing their affiliation to outcast')}`,
Expand All @@ -75,7 +77,7 @@ export default class MUCChatArea extends CustomElement {
`<strong>/voice</strong>: ${__('Allow muted user to post messages')}`
]
.filter(line => disabled_commands.every(c => !line.startsWith(c + '<', 9)))
.filter(line => this.model.getAllowedCommands().some(c => line.startsWith(c + '<', 9)));
.filter(line => allowed_commands.some(c => line.startsWith(c + '<', 9)));
}

onMousedown (ev) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/muc-views/heading.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class MUCHeading extends CustomElement {
* Returns a list of objects which represent buttons for the groupchat header.
* @emits _converse#getHeadingButtons
*/
getHeadingButtons (subject_hidden) {
async getHeadingButtons (subject_hidden) {
const buttons = [];
buttons.push({
'i18n_text': __('Details'),
Expand Down Expand Up @@ -137,7 +137,7 @@ export default class MUCHeading extends CustomElement {

const conn_status = this.model.session.get('connection_status');
if (conn_status === converse.ROOMSTATUS.ENTERED) {
const allowed_commands = this.model.getAllowedCommands();
const allowed_commands = await this.model.getAllowedCommands();
if (allowed_commands.includes('modtools')) {
buttons.push({
'i18n_text': __('Moderate'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { __ } from 'i18n';
import { _converse, api } from "@converse/headless/core";
import { html } from "lit";
import { until } from 'lit/directives/until.js';
import { showOccupantModal, setRole, verifyAndSetAffiliation } from "../../utils.js"


Expand Down Expand Up @@ -32,7 +33,7 @@ export default (el) => {
const handleOwner = async (ev) => {
const confirmed = await _converse.api.confirm(
__(`Are you sure you want to promote %1$s?`, jid),
[ __("Promoting a user to owner may be irreversible."+
[ __("Promoting a user to owner may be irreversible. "+
"Only server administrators may demote an owner of a Multi User Chat.")],
[]).then((x) => x.length === 0);

Expand Down Expand Up @@ -142,7 +143,7 @@ export default (el) => {
}
};

const applicable_buttons = (muc?.getAllowedCommands() ?? []).map(determineApplicable).filter(x => x);
const buttons = muc?.getAllowedCommands()?.then(commands => commands.map(determineApplicable).filter(x => x));

return applicable_buttons ? html`<div class="modal-footer">${applicable_buttons}</div>` : null;
return html`${until(buttons?.then((buttons) => buttons ? html`<div class="modal-footer">${buttons}</div>` : ''), '')}`;
}
4 changes: 2 additions & 2 deletions src/plugins/muc-views/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export function showOccupantModal (ev, occupant) {
}


export function parseMessageForMUCCommands (data, handled) {
export async function parseMessageForMUCCommands (data, handled) {
const model = data.model;
if (handled ||
model.get('type') !== _converse.CHATROOMS_TYPE || (
Expand All @@ -237,7 +237,7 @@ export function parseMessageForMUCCommands (data, handled) {
}

const args = text.slice(('/' + command).length + 1).trim();
const allowed_commands = model.getAllowedCommands() ?? [];
const allowed_commands = await model.getAllowedCommands() ?? [];

if (command === 'admin' && allowed_commands.includes(command)) {
verifyAndSetAffiliation(model, command, args, ['owner']);
Expand Down

0 comments on commit 663bc5b

Please sign in to comment.