1
0
Fork 0

Allow Matrixers to @room if Discorders can too

This commit is contained in:
Cadence Ember 2024-03-07 10:17:39 +13:00
parent 0f1cf7a20c
commit 12d85c982e
4 changed files with 35 additions and 7 deletions

View file

@ -12,6 +12,8 @@ const file = sync.require("../../matrix/file")
const api = sync.require("../../matrix/api") const api = sync.require("../../matrix/api")
/** @type {import("../../matrix/kstate")} */ /** @type {import("../../matrix/kstate")} */
const ks = sync.require("../../matrix/kstate") const ks = sync.require("../../matrix/kstate")
/** @type {import("../../discord/utils")} */
const utils = sync.require("../../discord/utils")
/** @type {import("./create-space")}) */ /** @type {import("./create-space")}) */
const createSpace = sync.require("./create-space") // watch out for the require loop 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]} 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 = { const channelKState = {
"m.room.name/": {name: convertedName}, "m.room.name/": {name: convertedName},
"m.room.topic/": {topic: convertedTopic}, "m.room.topic/": {topic: convertedTopic},
@ -136,7 +141,7 @@ async function channelToKState(channel, guild) {
"m.room.avatar": 0 "m.room.avatar": 0
}, },
notifications: { 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), {}) users: reg.ooye.invite.reduce((a, c) => (a[c] = 100, a), {})
}, },

View file

@ -1,5 +1,6 @@
// @ts-check // @ts-check
const mixin = require("mixin-deep")
const {channelToKState, _convertNameAndTopic} = require("./create-room") const {channelToKState, _convertNameAndTopic} = require("./create-room")
const {kstateStripConditionals} = require("../../matrix/kstate") const {kstateStripConditionals} = require("../../matrix/kstate")
const {test} = require("supertape") 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 => { test("convertNameAndTopic: custom name and topic", t => {
t.deepEqual( t.deepEqual(
_convertNameAndTopic({id: "123", name: "the-twilight-zone", topic: "Spooky stuff here. :ghost:", type: 0}, {id: "456"}, "hauntings"), _convertNameAndTopic({id: "123", name: "the-twilight-zone", topic: "Spooky stuff here. :ghost:", type: 0}, {id: "456"}, "hauntings"),

View file

@ -26,7 +26,7 @@ function getPermissions(userRoles, guildRoles, userID, channelOverwrites) {
} }
if (channelOverwrites) { if (channelOverwrites) {
/** @type {((overwrite: Required<DiscordTypes.APIGuildChannel>["permission_overwrites"][0]) => any)[]} */ /** @type {((overwrite: Required<DiscordTypes.APIOverwrite>) => any)[]} */
const actions = [ const actions = [
// Channel @everyone deny // Channel @everyone deny
overwrite => overwrite.id === everyoneID && (allowed &= ~BigInt(overwrite.deny)), overwrite => overwrite.id === everyoneID && (allowed &= ~BigInt(overwrite.deny)),

View file

@ -47,6 +47,9 @@ module.exports = {
}, },
users: { users: {
"@test_auto_invite:example.org": 100 "@test_auto_invite:example.org": 100
},
notifications: {
room: 0
} }
}, },
"chat.schildi.hide_ui/read_receipts": {hidden: true}, "chat.schildi.hide_ui/read_receipts": {hidden: true},
@ -98,7 +101,6 @@ module.exports = {
icon: "a_f83622e09ead74f0c5c527fe241f8f8c", icon: "a_f83622e09ead74f0c5c527fe241f8f8c",
emojis: [ emojis: [
{ {
version: 0,
roles: [], roles: [],
require_colons: true, require_colons: true,
name: "hippo", name: "hippo",
@ -108,7 +110,6 @@ module.exports = {
animated: false animated: false
}, },
{ {
version: 0,
roles: [], roles: [],
require_colons: true, require_colons: true,
name: "hipposcope", name: "hipposcope",
@ -121,7 +122,20 @@ module.exports = {
premium_subscription_count: 14, premium_subscription_count: 14,
roles: [ 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, unicode_emoji: null,
tags: {}, tags: {},
position: 22, position: 22,
@ -135,7 +149,6 @@ module.exports = {
flags: 0, flags: 0,
color: 0 color: 0
}, { }, {
version: 1696964862776,
unicode_emoji: null, unicode_emoji: null,
tags: {}, tags: {},
position: 131, position: 131,
@ -149,7 +162,6 @@ module.exports = {
flags: 0, flags: 0,
color: 11076095 color: 11076095
}, { }, {
version: 1696964862698,
unicode_emoji: '🍂', unicode_emoji: '🍂',
tags: {}, tags: {},
position: 102, position: 102,