Compare commits

..
1 Commits
Author SHA1 Message Date
N0VA 34936fd621 Add customisable @room mention roles 2026-10-13 11:51:27 +02:00
8 changed files with 16 additions and 138 deletions
+1 -1
View File
@@ -916,7 +916,7 @@ async function messageToEvent(message, guild, options = {}, di) {
// Add time // Add time
if (event.scheduled_end_time) { if (event.scheduled_end_time) {
const formatter = new Intl.DateTimeFormat("en-NZ", {weekday: "long", month: "long", day: "numeric", hour: "numeric", minute: "2-digit", timeZone: "UTC"}) // 9 June at 3:00 pm NZT const formatter = new Intl.DateTimeFormat("en-NZ", {weekday: "long", month: "long", day: "numeric", hour: "numeric", minute: "2-digit", timeZone: "UTC"}) // 9 June at 3:00 pm NZT
var displayTime = (formatter.formatRange(new Date(event.scheduled_start_time), new Date(event.scheduled_end_time)) + " (UTC)").replaceAll(/\p{Zs}/gv, " ") var displayTime = formatter.formatRange(new Date(event.scheduled_start_time), new Date(event.scheduled_end_time)) + " (UTC)"
} else { } else {
var displayTime = discordTime.convertDiscordTime(unixSeconds, "F") var displayTime = discordTime.convertDiscordTime(unixSeconds, "F")
} }
+2 -2
View File
@@ -1589,12 +1589,12 @@ test("message2event: irl invite event renders embed", async t => {
{ {
$type: "m.room.message", $type: "m.room.message",
msgtype: "m.notice", msgtype: "m.notice",
body: `| Scheduled Event - Sunday, 8 June, 10:00 am – 12:00 pm (UTC)` body: `| Scheduled Event - Sunday, 8 June, 10:00 am – 12:00 pm (UTC)`
+ `\n| ## forest exploration` + `\n| ## forest exploration`
+ `\n| ` + `\n| `
+ `\n| 📍 the dark forest`, + `\n| 📍 the dark forest`,
format: "org.matrix.custom.html", format: "org.matrix.custom.html",
formatted_body: `<blockquote><p>Scheduled Event - <a href=\"https://bridge.example.org/time/1749376800/F\">Sunday, 8 June, 10:00 am – 12:00 pm (UTC)</a></p>` formatted_body: `<blockquote><p>Scheduled Event - <a href=\"https://bridge.example.org/time/1749376800/F\">Sunday, 8 June, 10:00 am – 12:00 pm (UTC)</a></p>`
+ `<strong>forest exploration</strong>` + `<strong>forest exploration</strong>`
+ `<p>📍 the dark forest</p></blockquote>`, + `<p>📍 the dark forest</p></blockquote>`,
"m.mentions": {} "m.mentions": {}
+1 -1
View File
@@ -44,7 +44,7 @@ function getFormatOptions(style) {
function convertDiscordTime(timestamp, style) { function convertDiscordTime(timestamp, style) {
const formatter = new Intl.DateTimeFormat("en-NZ", getFormatOptions(style)) const formatter = new Intl.DateTimeFormat("en-NZ", getFormatOptions(style))
const d = new Date(timestamp * 1000) const d = new Date(timestamp * 1000)
return (formatter.format(d) + " (UTC)").replaceAll(/\p{Zs}/gv, " ") return formatter.format(d) + " (UTC)"
} }
module.exports.getFormatOptions = getFormatOptions module.exports.getFormatOptions = getFormatOptions
+7 -7
View File
@@ -452,18 +452,18 @@ async function handleRoomOrMessageLinks(input, di) {
/** /**
* @param {string} content * @param {string} content
* @param {{content: any, room_id: string, sender: string}} event * @param {string} senderMxid
* @param {string} roomID
* @param {DiscordTypes.APIGuild} guild * @param {DiscordTypes.APIGuild} guild
* @param {{api: import("../../matrix/api"), snow: import("snowtransfer").SnowTransfer}} di * @param {{api: import("../../matrix/api"), snow: import("snowtransfer").SnowTransfer}} di
*/ */
async function checkWrittenMentions(content, event, guild, di) { async function checkWrittenMentions(content, senderMxid, roomID, guild, di) {
let writtenMentionMatch = content.match(/(?:^|[^"[<>/A-Za-z0-9])@([A-Za-z][A-Za-z0-9._\[\]\(\)-]+):?/d) // /d flag for indices requires node.js 16+ let writtenMentionMatch = content.match(/(?:^|[^"[<>/A-Za-z0-9])@([A-Za-z][A-Za-z0-9._\[\]\(\)-]+):?/d) // /d flag for indices requires node.js 16+
if (writtenMentionMatch) { if (writtenMentionMatch) {
if (writtenMentionMatch[1] === "room") { // convert @room to discord if (writtenMentionMatch[1] === "room") { // convert @room to discord
if (("m.mentions" in event.content) && !event.content["m.mentions"].room) return // check it's allowed by m.mentions if present const {powers: {[senderMxid]: userPower}, powerLevels} = await mxUtils.getEffectivePower(roomID, [senderMxid], di.api)
const {powers: {[event.sender]: userPower}, powerLevels} = await mxUtils.getEffectivePower(event.room_id, [event.sender], di.api)
if (userPower >= (powerLevels.notifications?.room ?? 50)) { if (userPower >= (powerLevels.notifications?.room ?? 50)) {
const roleID = select("channel_room", "room_mention_role", {room_id: event.room_id}).pluck().get() const roleID = select("channel_room", "room_mention_role", {room_id: roomID}).pluck().get()
return { return {
// @ts-ignore - typescript doesn't know about indices yet // @ts-ignore - typescript doesn't know about indices yet
content: content.slice(0, writtenMentionMatch.indices[1][0]-1) + (roleID ? `<@&${roleID}>` : "@everyone") + content.slice(writtenMentionMatch.indices[1][1]), content: content.slice(0, writtenMentionMatch.indices[1][0]-1) + (roleID ? `<@&${roleID}>` : "@everyone") + content.slice(writtenMentionMatch.indices[1][1]),
@@ -914,7 +914,7 @@ async function eventToMessage(event, guild, channel, di) {
// Check written mentions // Check written mentions
if (node.nodeType === 3 && node.nodeValue.includes("@") && !nodeIsChildOf(node, ["A", "CODE", "PRE"])) { if (node.nodeType === 3 && node.nodeValue.includes("@") && !nodeIsChildOf(node, ["A", "CODE", "PRE"])) {
const result = await checkWrittenMentions(node.nodeValue, event, guild, di) const result = await checkWrittenMentions(node.nodeValue, event.sender, event.room_id, guild, di)
if (result) { if (result) {
node.nodeValue = result.content node.nodeValue = result.content
ensureJoined.push(...result.ensureJoined) ensureJoined.push(...result.ensureJoined)
@@ -1032,7 +1032,7 @@ async function eventToMessage(event, guild, channel, di) {
} }
} }
const result = await checkWrittenMentions(content, event, guild, di) const result = await checkWrittenMentions(content, event.sender, event.room_id, guild, di)
if (result) { if (result) {
content = result.content content = result.content
ensureJoined.push(...result.ensureJoined) ensureJoined.push(...result.ensureJoined)
+1 -121
View File
@@ -5521,9 +5521,6 @@ test("event2message: @room converts to @everyone and is allowed when the room do
type: "m.room.message", type: "m.room.message",
sender: "@cadence:cadence.moe", sender: "@cadence:cadence.moe",
content: { content: {
"m.mentions": {
room: true
},
msgtype: "m.text", msgtype: "m.text",
body: "@room dinner's ready", body: "@room dinner's ready",
}, },
@@ -5566,124 +5563,7 @@ test("event2message: @room converts to @everyone and is allowed when the room do
content: "@everyone dinner's ready", content: "@everyone dinner's ready",
avatar_url: "https://bridge.example.org/download/letter-avatar?letter=C&hue=90", avatar_url: "https://bridge.example.org/download/letter-avatar?letter=C&hue=90",
allowed_mentions: { allowed_mentions: {
users: [], parse: ["users", "roles", "everyone"]
parse: ["roles", "everyone"]
}
}],
ensureJoined: []
}
)
})
test("event2message: @room is converted to a role in configured rooms", async t => {
let called = 0
t.deepEqual(
await eventToMessage({
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
msgtype: "m.text",
body: "@room dinner's ready",
},
room_id: "!fGgIymcYWOqjbSRUdV:cadence.moe",
event_id: "$SiXetU9h9Dg-M9Frcw_C6ahnoXZ3QPZe3MVJR5tcB9A"
}, data.guild.general, data.channel.general, {
api: {
async getStateEvent(roomID, type, key) {
called++
t.equal(roomID, "!fGgIymcYWOqjbSRUdV:cadence.moe")
t.equal(type, "m.room.power_levels")
t.equal(key, "")
return {
users: {},
notifications: {
room: 0
}
}
},
async getStateEventOuter(roomID, type, key) {
t.equal(roomID, "!fGgIymcYWOqjbSRUdV:cadence.moe")
t.equal(type, "m.room.create")
t.equal(key, "")
return {
type: "m.room.create",
state_key: "",
sender: "@_ooye_bot:cadence.moe",
content: {
room_version: "11"
}
}
}
}
}),
{
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "<@&123456> dinner's ready",
avatar_url: "https://bridge.example.org/download/matrix/cadence.moe/azCAhThKTojXSZJRoWwZmhvU?preset=avatar",
allowed_mentions: {
parse: ["users", "roles"]
}
}],
ensureJoined: []
}
)
})
test("event2message: @room is not allowed with mismatched m.mentions", async t => {
let called = 0
t.deepEqual(
await eventToMessage({
type: "m.room.message",
sender: "@cadence:cadence.moe",
content: {
"m.mentions": {},
msgtype: "m.text",
body: "@room dinner's ready",
},
room_id: "!kLRqKKUQXcibIMtOpl:cadence.moe",
event_id: "$SiXetU9h9Dg-M9Frcw_C6ahnoXZ3QPZe3MVJR5tcB9A"
}, data.guild.general, data.channel.general, {
api: {
async getStateEvent(roomID, type, key) {
called++
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
t.equal(type, "m.room.power_levels")
t.equal(key, "")
return {
users: {},
notifications: {
room: 0
}
}
},
async getStateEventOuter(roomID, type, key) {
t.equal(roomID, "!kLRqKKUQXcibIMtOpl:cadence.moe")
t.equal(type, "m.room.create")
t.equal(key, "")
return {
type: "m.room.create",
state_key: "",
sender: "@_ooye_bot:cadence.moe",
content: {
room_version: "11"
}
}
}
}
}),
{
messagesToDelete: [],
messagesToEdit: [],
messagesToSend: [{
username: "cadence [they]",
content: "@room dinner's ready",
avatar_url: "https://bridge.example.org/download/letter-avatar?letter=C&hue=90",
allowed_mentions: {
users: [],
parse: ["roles"]
} }
}], }],
ensureJoined: [] ensureJoined: []
+4 -2
View File
@@ -1,8 +1,10 @@
#!/usr/bin/env node #!/usr/bin/env node
// @ts-check // @ts-check
// Ensure consistent output from the Intl and Temporal API across Node.js versions // Temporal API stabilised in Node.js 26. Previous versions can use `--harmony-temporal`, but that's jank, so have a polyfill.
require("temporal-polyfill-lite/global") if (!("Temporal" in globalThis)) {
require("temporal-polyfill-lite/global")
}
const fs = require("fs") const fs = require("fs")
const sqlite = require("better-sqlite3") const sqlite = require("better-sqlite3")
-1
View File
@@ -25,7 +25,6 @@ INSERT INTO channel_room (channel_id, room_id, name, nick, thread_parent, custom
('1161864271370666075', '!mHmhQQPwXNananMUqq:cadence.moe', 'updates', NULL, NULL, NULL, '112760669178241024'), ('1161864271370666075', '!mHmhQQPwXNananMUqq:cadence.moe', 'updates', NULL, NULL, NULL, '112760669178241024'),
('1438284564815548418', '!MHxNpwtgVqWOrmyoTn:cadence.moe', 'sin-cave', NULL, NULL, NULL, '665289423482519565'), ('1438284564815548418', '!MHxNpwtgVqWOrmyoTn:cadence.moe', 'sin-cave', NULL, NULL, NULL, '665289423482519565'),
('598707048112193536', '!JBxeGYnzQwLnaooOLD:cadence.moe', 'winners', NULL, NULL, NULL, '1345641201902288987'); ('598707048112193536', '!JBxeGYnzQwLnaooOLD:cadence.moe', 'winners', NULL, NULL, NULL, '1345641201902288987');
UPDATE channel_room SET room_mention_role = '123456' WHERE room_id = '!fGgIymcYWOqjbSRUdV:cadence.moe';
INSERT INTO historical_channel_room (reference_channel_id, room_id, upgraded_timestamp) SELECT channel_id, room_id, 0 FROM channel_room; INSERT INTO historical_channel_room (reference_channel_id, room_id, upgraded_timestamp) SELECT channel_id, room_id, 0 FROM channel_room;
-3
View File
@@ -1,8 +1,5 @@
// @ts-check // @ts-check
// Ensure consistent output from the Intl and Temporal API across Node.js versions
require("temporal-polyfill-lite/global")
const fs = require("fs") const fs = require("fs")
const {join} = require("path") const {join} = require("path")
const sqlite = require("better-sqlite3") const sqlite = require("better-sqlite3")