From 12d85c982e20adba182f73b7576a02d62b0b9ca4 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Thu, 7 Mar 2024 10:17:39 +1300 Subject: [PATCH] Allow Matrixers to @room if Discorders can too --- d2m/actions/create-room.js | 7 ++++++- d2m/actions/create-room.test.js | 11 +++++++++++ discord/utils.js | 2 +- test/data.js | 22 +++++++++++++++++----- 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/d2m/actions/create-room.js b/d2m/actions/create-room.js index 0dea283..a8d2846 100644 --- a/d2m/actions/create-room.js +++ b/d2m/actions/create-room.js @@ -12,6 +12,8 @@ const file = sync.require("../../matrix/file") const api = sync.require("../../matrix/api") /** @type {import("../../matrix/kstate")} */ const ks = sync.require("../../matrix/kstate") +/** @type {import("../../discord/utils")} */ +const utils = sync.require("../../discord/utils") /** @type {import("./create-space")}) */ const createSpace = sync.require("./create-space") // watch out for the require loop @@ -119,6 +121,9 @@ async function channelToKState(channel, guild) { join_rules = {join_rule: PRIVACY_ENUMS.ROOM_JOIN_RULES[privacyLevel]} } + const everyonePermissions = utils.getPermissions([], guild.roles, undefined, channel.permission_overwrites) + const everyoneCanMentionEveryone = utils.hasAllPermissions(everyonePermissions, ["MentionEveryone"]) + const channelKState = { "m.room.name/": {name: convertedName}, "m.room.topic/": {topic: convertedTopic}, @@ -136,7 +141,7 @@ async function channelToKState(channel, guild) { "m.room.avatar": 0 }, notifications: { - room: 20 // TODO: Matrix users should have the same abilities as unprivileged Discord members. So make this automatically configured based on the guild or channel's default mention everyone permissions. That way if unprivileged Discord members can mention everyone, Matrix users can too. + room: everyoneCanMentionEveryone ? 0 : 20 }, users: reg.ooye.invite.reduce((a, c) => (a[c] = 100, a), {}) }, diff --git a/d2m/actions/create-room.test.js b/d2m/actions/create-room.test.js index 93f9203..be0febb 100644 --- a/d2m/actions/create-room.test.js +++ b/d2m/actions/create-room.test.js @@ -1,5 +1,6 @@ // @ts-check +const mixin = require("mixin-deep") const {channelToKState, _convertNameAndTopic} = require("./create-room") const {kstateStripConditionals} = require("../../matrix/kstate") const {test} = require("supertape") @@ -39,6 +40,16 @@ test("channel2room: invite-only privacy room", async t => { ) }) +test("channel2room: room where limited people can mention everyone", async t => { + const limitedGuild = mixin({}, testData.guild.general) + limitedGuild.roles[0].permissions = (BigInt(limitedGuild.roles[0].permissions) - 131072n).toString() + const limitedRoom = mixin({}, testData.room.general, {"m.room.power_levels/": {notifications: {room: 20}}}) + t.deepEqual( + kstateStripConditionals(await channelToKState(testData.channel.general, limitedGuild).then(x => x.channelKState)), + limitedRoom + ) +}) + test("convertNameAndTopic: custom name and topic", t => { t.deepEqual( _convertNameAndTopic({id: "123", name: "the-twilight-zone", topic: "Spooky stuff here. :ghost:", type: 0}, {id: "456"}, "hauntings"), diff --git a/discord/utils.js b/discord/utils.js index 6b6b602..2726a1a 100644 --- a/discord/utils.js +++ b/discord/utils.js @@ -26,7 +26,7 @@ function getPermissions(userRoles, guildRoles, userID, channelOverwrites) { } if (channelOverwrites) { - /** @type {((overwrite: Required["permission_overwrites"][0]) => any)[]} */ + /** @type {((overwrite: Required) => any)[]} */ const actions = [ // Channel @everyone deny overwrite => overwrite.id === everyoneID && (allowed &= ~BigInt(overwrite.deny)), diff --git a/test/data.js b/test/data.js index 61ee5d0..c165322 100644 --- a/test/data.js +++ b/test/data.js @@ -47,6 +47,9 @@ module.exports = { }, users: { "@test_auto_invite:example.org": 100 + }, + notifications: { + room: 0 } }, "chat.schildi.hide_ui/read_receipts": {hidden: true}, @@ -98,7 +101,6 @@ module.exports = { icon: "a_f83622e09ead74f0c5c527fe241f8f8c", emojis: [ { - version: 0, roles: [], require_colons: true, name: "hippo", @@ -108,7 +110,6 @@ module.exports = { animated: false }, { - version: 0, roles: [], require_colons: true, name: "hipposcope", @@ -121,7 +122,20 @@ module.exports = { premium_subscription_count: 14, roles: [ { - version: 1696964862461, + unicode_emoji: null, + tags: {}, + position: 0, + permissions: '559623605575360', + name: '@everyone', + mentionable: false, + managed: false, + id: '112760669178241024', + icon: null, + hoist: false, + flags: 0, + color: 0 + }, + { unicode_emoji: null, tags: {}, position: 22, @@ -135,7 +149,6 @@ module.exports = { flags: 0, color: 0 }, { - version: 1696964862776, unicode_emoji: null, tags: {}, position: 131, @@ -149,7 +162,6 @@ module.exports = { flags: 0, color: 11076095 }, { - version: 1696964862698, unicode_emoji: '🍂', tags: {}, position: 102,