From 659a69e7b750656852e69d2fc94f75d167714239 Mon Sep 17 00:00:00 2001 From: JC Brand Date: Thu, 3 Nov 2022 09:55:20 +0100 Subject: [PATCH] Make sure a moderator can also add contacts... since they can see real JIDs, even if the room is semi-anonymous. Also made changes regarding programming style and conventions: - Pass in `this` to the template Recently I've started simplifying the `render` method by just passing `this` into the template and letting the template calculate what it needs from that. - Don't use `bind()` It's slow and arrow functions can be used instead. - Don't use an i18n string inside a tagged template literal Due to limitations with `xgettext` which cannot parse properly inside tagged template literals - Use snake-case for variables --- dev.html | 1 + src/plugins/muc-views/modals/occupant.js | 18 +------- .../muc-views/modals/templates/occupant.js | 41 ++++++++++++++----- src/plugins/omemo/mixins/converse.js | 6 +-- 4 files changed, 36 insertions(+), 30 deletions(-) diff --git a/dev.html b/dev.html index 1dfbf517b..d1a312a5f 100644 --- a/dev.html +++ b/dev.html @@ -28,6 +28,7 @@ }); converse.initialize({ + theme: 'dracula', auto_away: 300, enable_smacks: true, loglevel: 'debug', diff --git a/src/plugins/muc-views/modals/occupant.js b/src/plugins/muc-views/modals/occupant.js index 0bee536f8..f623fe1a8 100644 --- a/src/plugins/muc-views/modals/occupant.js +++ b/src/plugins/muc-views/modals/occupant.js @@ -28,24 +28,10 @@ export default class OccupantModal extends BaseModal { } renderModal () { - const model = this.model ?? this.message; - const jid = model?.get('jid'); - const vcard = this.getVcard(); - const nick = model.get('nick'); - const occupant_id = model.get('occupant_id'); - const role = this.model?.get('role'); - const affiliation = this.model?.get('affiliation'); - const hats = this.model?.get('hats')?.length ? this.model.get('hats') : null; - const muc = this.model.collection.chatroom; - const addToContacts = api.contacts.get(jid).then(contact => { - if (!contact && muc.features.get('nonanonymous') && jid && jid != _converse.bare_jid) { - return this.addToContacts.bind(this); - } - }); - return tpl_occupant_modal({ jid, vcard, nick, occupant_id, role, affiliation, hats, addToContacts }); + return tpl_occupant_modal(this); } - getModalTitle () { // eslint-disable-line class-methods-use-this + getModalTitle () { const model = this.model ?? this.message; return model?.getDisplayName(); } diff --git a/src/plugins/muc-views/modals/templates/occupant.js b/src/plugins/muc-views/modals/templates/occupant.js index c5de45c68..2c248811e 100644 --- a/src/plugins/muc-views/modals/templates/occupant.js +++ b/src/plugins/muc-views/modals/templates/occupant.js @@ -2,40 +2,59 @@ import 'shared/avatar/avatar.js'; import { __ } from 'i18n'; import { html } from "lit"; import { until } from 'lit/directives/until.js'; +import { _converse, api } from "@converse/headless/core"; -export default (o) => { - const addToContacts = o.addToContacts.then(add => add ? html`
  • ` : ''); +export default (el) => { + const model = el.model ?? el.message; + const jid = model?.get('jid'); + const vcard = el.getVcard(); + const nick = model.get('nick'); + const occupant_id = model.get('occupant_id'); + const role = el.model?.get('role'); + const affiliation = el.model?.get('affiliation'); + const hats = el.model?.get('hats')?.length ? el.model.get('hats') : null; + const muc = el.model.collection.chatroom; + + const i18n_add_to_contacts = __('Add to Contacts'); + + const can_see_real_jids = muc.features.get('nonanonymous') || muc.getOwnRole() === 'moderator'; + const not_me = jid != _converse.bare_jid; + + const add_to_contacts = api.contacts.get(jid) + .then(contact => !contact && not_me && can_see_real_jids) + .then(add => add ? html`
  • ` : ''); + return html`
    • - ${ o.nick ? html`
      ${__('Nickname')}:
      ${o.nick}
      ` : '' } + ${ nick ? html`
      ${__('Nickname')}:
      ${nick}
      ` : '' }
    • - ${ o.jid ? html`
      ${__('XMPP Address')}:
      ${o.jid}
      ` : '' } + ${ jid ? html`
      ${__('XMPP Address')}:
      ${jid}
      ` : '' }
    • - ${ o.affiliation ? html`
      ${__('Affiliation')}:
      ${o.affiliation}
      ` : '' } + ${ affiliation ? html`
      ${__('Affiliation')}:
      ${affiliation}
      ` : '' }
    • - ${ o.role ? html`
      ${__('Roles')}:
      ${o.role}
      ` : '' } + ${ role ? html`
      ${__('Roles')}:
      ${role}
      ` : '' }
    • - ${ o.hats ? html`
      ${__('Hats')}:
      ${o.hats}
      ` : '' } + ${ hats ? html`
      ${__('Hats')}:
      ${hats}
      ` : '' }
    • - ${ o.occupant_id ? html`
      ${__('Occupant Id')}:
      ${o.occupant_id}
      ` : '' } + ${ occupant_id ? html`
      ${__('Occupant Id')}:
      ${occupant_id}
      ` : '' }
    • - ${ until(addToContacts, '') } + ${ until(add_to_contacts, '') }
    diff --git a/src/plugins/omemo/mixins/converse.js b/src/plugins/omemo/mixins/converse.js index 4badf874c..575f13ef2 100644 --- a/src/plugins/omemo/mixins/converse.js +++ b/src/plugins/omemo/mixins/converse.js @@ -3,16 +3,16 @@ import { generateFingerprint, getDevicesForContact, } from '../utils.js'; const ConverseMixins = { - generateFingerprints: async function (jid) { + async generateFingerprints (jid) { const devices = await getDevicesForContact(jid); return Promise.all(devices.map(d => generateFingerprint(d))); }, - getDeviceForContact: function (jid, device_id) { + getDeviceForContact (jid, device_id) { return getDevicesForContact(jid).then(devices => devices.get(device_id)); }, - contactHasOMEMOSupport: async function (jid) { + async contactHasOMEMOSupport (jid) { /* Checks whether the contact advertises any OMEMO-compatible devices. */ const devices = await getDevicesForContact(jid); return devices.length > 0;