Compare commits

..

No commits in common. "cd0b8bff2b358568fb826003193d3122751fab31" and "33eef25cf15fe03bc674a7026bba78e2413cc5ab" have entirely different histories.

22 changed files with 65 additions and 134 deletions

View file

@ -2,7 +2,6 @@
"compilerOptions": {
"target": "es2024",
"module": "nodenext",
"lib": ["ESNext"],
"strict": true,
"noImplicitAny": false,
"useUnknownInCatchVariables": false

View file

@ -1,17 +0,0 @@
// @ts-check
const {reg, writeRegistration, registrationFilePath} = require("../src/matrix/read-registration")
const {prompt} = require("enquirer")
;(async () => {
/** @type {{web_password: string}} */
const passwordResponse = await prompt({
type: "text",
name: "web_password",
message: "Choose a simple password (optional)"
})
reg.ooye.web_password = passwordResponse.web_password
writeRegistration(reg)
console.log("Saved. Restart Out Of Your Element to apply this change.")
})()

View file

@ -70,14 +70,13 @@ async function sendVotes(userOrID, channelID, pollMessageID, pollEventID) {
return
}
let userID, senderMxid
if (typeof userOrID === "string") { // just a string when double-checking a vote removal - good thing the unvoter is already here from having voted
userID = userOrID
senderMxid = from("sim").join("sim_member", "mxid").where({user_id: userOrID, room_id: matchingRoomID}).pluck("mxid").get()
var userID = userOrID
var senderMxid = from("sim").join("sim_member", "mxid").where({user_id: userOrID, room_id: matchingRoomID}).pluck("mxid").get()
if (!senderMxid) return
} else { // sent in full when double-checking adding a vote, so we can properly ensure joined
userID = userOrID.id
senderMxid = await registerUser.ensureSimJoined(userOrID, matchingRoomID)
var userID = userOrID.id
var senderMxid = await registerUser.ensureSimJoined(userOrID, matchingRoomID)
}
const answersArray = select("poll_vote", "matrix_option", {discord_or_matrix_user_id: userID, message_id: pollMessageID}).pluck().all()

View file

@ -19,7 +19,7 @@ const emitter = new EventEmitter()
* Due to Eventual Consistency(TM) an update/delete may arrive before the original message arrives
* (or before the it has finished being bridged to an event).
* In this case, wait until the original message has finished bridging, then retrigger the passed function.
* @template {(...args: any[]) => any} T
* @template {(...args: any[]) => Promise<any>} T
* @param {string} inputID
* @param {T} fn
* @param {Parameters<T>} rest

View file

@ -54,8 +54,8 @@ async function doSpeedbump(messageID) {
debugSpeedbump(`[speedbump] DELETED ${messageID}`)
return true
}
value = (bumping.get(messageID) ?? 0) - 1
if (value <= 0) {
value = bumping.get(messageID) - 1
if (value === 0) {
debugSpeedbump(`[speedbump] OK ${messageID}-- = ${value}`)
bumping.delete(messageID)
return false

View file

@ -9,7 +9,7 @@ const userRegex = reg.namespaces.users.map(u => new RegExp(u.regex))
* @typedef {{text: string, index: number, end: number}} Token
*/
/** @typedef {{mxids: {localpart: string, mxid: string, displayname?: string | null}[], names: {displaynameTokens: Token[], mxid: string}[]}} ProcessedJoined */
/** @typedef {{mxids: {localpart: string, mxid: string, displayname?: string}[], names: {displaynameTokens: Token[], mxid: string}[]}} ProcessedJoined */
const lengthBonusLengthCap = 50
const lengthBonusValue = 0.5
@ -18,7 +18,7 @@ const lengthBonusValue = 0.5
* 0 = no match
* @param {string} localpart
* @param {string} input
* @param {string | null} [displayname] only for the super tiebreaker
* @param {string} [displayname] only for the super tiebreaker
* @returns {{score: number, matchedInputTokens: Token[]}}
*/
function scoreLocalpart(localpart, input, displayname) {
@ -103,7 +103,7 @@ function tokenise(name) {
}
/**
* @param {{mxid: string, displayname?: string | null}[]} joined
* @param {{mxid: string, displayname?: string}[]} joined
* @returns {ProcessedJoined}
*/
function processJoined(joined) {
@ -120,7 +120,6 @@ function processJoined(joined) {
}),
names: joined.filter(j => j.displayname).map(j => {
return {
// @ts-ignore
displaynameTokens: tokenise(j.displayname),
mxid: j.mxid
}
@ -131,8 +130,6 @@ function processJoined(joined) {
/**
* @param {ProcessedJoined} pjr
* @param {string} maximumWrittenSection lowercase please
* @param {number} baseOffset
* @param {string} prefix
* @param {string} content
*/
function findMention(pjr, maximumWrittenSection, baseOffset, prefix, content) {
@ -145,7 +142,7 @@ function findMention(pjr, maximumWrittenSection, baseOffset, prefix, content) {
if (best.scored.score > 4) { // requires in smallest case perfect match of 2 characters, or in largest case a partial middle match of 5+ characters in a row
// Highlight the relevant part of the message
const start = baseOffset + best.scored.matchedInputTokens[0].index
const end = baseOffset + prefix.length + best.scored.matchedInputTokens.slice(-1)[0].end
const end = baseOffset + prefix.length + best.scored.matchedInputTokens.at(-1).end
const newContent = content.slice(0, start) + "[" + content.slice(start, end) + "](https://matrix.to/#/" + best.mxid + ")" + content.slice(end)
return {
mxid: best.mxid,

View file

@ -113,7 +113,7 @@ test("score name: finds match location", t => {
const message = "evil lillith is an inspiration"
const result = scoreName(tokenise("INX | Evil Lillith (she/her)"), tokenise(message))
const startLocation = result.matchedInputTokens[0].index
const endLocation = result.matchedInputTokens.slice(-1)[0].end
const endLocation = result.matchedInputTokens.at(-1).end
t.equal(message.slice(startLocation, endLocation), "evil lillith")
})
@ -125,5 +125,5 @@ test("find mention: test various tiebreakers", t => {
mxid: "@emma:rory.gay",
displayname: "Emma [it/its]"
}]), "emma ⚡ curious which one this prefers", 0, "@", "@emma ⚡ curious which one this prefers")
t.equal(found?.mxid, "@emma:conduit.rory.gay")
t.equal(found.mxid, "@emma:conduit.rory.gay")
})

View file

@ -427,7 +427,7 @@ async function messageToEvent(message, guild, options = {}, di) {
* @param {string} [timestampChannelID]
*/
async function getHistoricalEventRow(messageID, timestampChannelID) {
/** @type {{room_id: string} | {event_id: string, room_id: string, reference_channel_id: string, source: number} | null | undefined} */
/** @type {{room_id: string} | {event_id: string, room_id: string, reference_channel_id: string, source: number} | null} */
let row = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")
.select("event_id", "room_id", "reference_channel_id", "source").where({message_id: messageID}).and("ORDER BY part ASC").get()
if (!row && timestampChannelID) {
@ -574,7 +574,6 @@ async function messageToEvent(message, guild, options = {}, di) {
if (repliedToEventInDifferentRoom || repliedToUnknownEvent) {
let referenced = message.referenced_message
if (!referenced) { // backend couldn't be bothered to dereference the message, have to do it ourselves
assert(message.message_reference?.message_id)
referenced = await discord.snow.channel.getChannelMessage(message.message_reference.channel_id, message.message_reference.message_id)
}
@ -662,14 +661,14 @@ async function messageToEvent(message, guild, options = {}, di) {
}
// Forwarded content appears first
if (message.message_reference?.type === DiscordTypes.MessageReferenceType.Forward && message.message_reference.message_id && message.message_snapshots?.length) {
if (message.message_reference?.type === DiscordTypes.MessageReferenceType.Forward && message.message_snapshots?.length) {
// Forwarded notice
const row = await getHistoricalEventRow(message.message_reference.message_id, message.message_reference.channel_id)
const room = select("channel_room", ["room_id", "name", "nick"], {channel_id: message.message_reference.channel_id}).get()
const forwardedNotice = new mxUtils.MatrixStringBuilder()
if (room) {
const roomName = room && (room.nick || room.name)
if (row && "event_id" in row) {
if ("event_id" in row) {
const via = await getViaServersMemo(row.room_id)
forwardedNotice.addLine(
`[🔀 Forwarded from #${roomName}]`,
@ -803,23 +802,20 @@ async function messageToEvent(message, guild, options = {}, di) {
// Then components
if (message.components?.length) {
const stack = new mxUtils.MatrixStringBuilderStack()
const stack = [new mxUtils.MatrixStringBuilder()]
/** @param {DiscordTypes.APIMessageComponent} component */
async function processComponent(component) {
// Standalone components
if (component.type === DiscordTypes.ComponentType.TextDisplay) {
const {body, html} = await transformContent(component.content)
stack.msb.addParagraph(body, html)
stack[0].addParagraph(body, html)
}
else if (component.type === DiscordTypes.ComponentType.Separator) {
stack.msb.addParagraph("----", "<hr>")
stack[0].addParagraph("----", "<hr>")
}
else if (component.type === DiscordTypes.ComponentType.File) {
/** @type {{[k in keyof DiscordTypes.APIUnfurledMediaItem]-?: NonNullable<DiscordTypes.APIUnfurledMediaItem[k]>}} */ // @ts-ignore
const file = component.file
assert(component.name && component.size && file.content_type)
const ev = await attachmentToEvent({}, {...file, filename: component.name, size: component.size}, true)
stack.msb.addLine(ev.body, ev.formatted_body)
const ev = await attachmentToEvent({}, {...component.file, filename: component.name, size: component.size}, true)
stack[0].addLine(ev.body, ev.formatted_body)
}
else if (component.type === DiscordTypes.ComponentType.MediaGallery) {
const description = component.items.length === 1 ? component.items[0].description || "Image:" : "Image gallery:"
@ -830,43 +826,43 @@ async function messageToEvent(message, guild, options = {}, di) {
estimatedName: item.media.url.match(/\/([^/?]+)(\?|$)/)?.[1] || publicURL
}
})
stack.msb.addLine(`🖼️ ${description} ${images.map(i => i.url).join(", ")}`, tag`🖼️ ${description} $${images.map(i => tag`<a href="${i.url}">${i.estimatedName}</a>`).join(", ")}`)
stack[0].addLine(`🖼️ ${description} ${images.map(i => i.url).join(", ")}`, tag`🖼️ ${description} $${images.map(i => tag`<a href="${i.url}">${i.estimatedName}</a>`).join(", ")}`)
}
// string select, text input, user select, role select, mentionable select, channel select
// Components that can have things nested
else if (component.type === DiscordTypes.ComponentType.Container) {
// May contain action row, text display, section, media gallery, separator, file
stack.bump()
stack.unshift(new mxUtils.MatrixStringBuilder())
for (const innerComponent of component.components) {
await processComponent(innerComponent)
}
let {body, formatted_body} = stack.shift().get()
body = body.split("\n").map(l => "| " + l).join("\n")
formatted_body = `<blockquote>${formatted_body}</blockquote>`
if (stack.msb.body) stack.msb.body += "\n\n"
stack.msb.add(body, formatted_body)
if (stack[0].body) stack[0].body += "\n\n"
stack[0].add(body, formatted_body)
}
else if (component.type === DiscordTypes.ComponentType.Section) {
// May contain text display, possibly more in the future
// Accessory may be button or thumbnail
stack.bump()
stack.unshift(new mxUtils.MatrixStringBuilder())
for (const innerComponent of component.components) {
await processComponent(innerComponent)
}
if (component.accessory) {
stack.bump()
stack.unshift(new mxUtils.MatrixStringBuilder())
await processComponent(component.accessory)
const {body, formatted_body} = stack.shift().get()
stack.msb.addLine(body, formatted_body)
stack[0].addLine(body, formatted_body)
}
const {body, formatted_body} = stack.shift().get()
stack.msb.addParagraph(body, formatted_body)
stack[0].addParagraph(body, formatted_body)
}
else if (component.type === DiscordTypes.ComponentType.ActionRow) {
const linkButtons = component.components.filter(c => c.type === DiscordTypes.ComponentType.Button && c.style === DiscordTypes.ButtonStyle.Link)
if (linkButtons.length) {
stack.msb.addLine("")
stack[0].addLine("")
for (const linkButton of linkButtons) {
await processComponent(linkButton)
}
@ -875,15 +871,15 @@ async function messageToEvent(message, guild, options = {}, di) {
// Components that can only be inside things
else if (component.type === DiscordTypes.ComponentType.Thumbnail) {
// May only be a section accessory
stack.msb.add(`🖼️ ${component.media.url}`, tag`🖼️ <a href="${component.media.url}">${component.media.url}</a>`)
stack[0].add(`🖼️ ${component.media.url}`, tag`🖼️ <a href="${component.media.url}">${component.media.url}</a>`)
}
else if (component.type === DiscordTypes.ComponentType.Button) {
// May only be a section accessory or in an action row (up to 5)
if (component.style === DiscordTypes.ButtonStyle.Link) {
if (component.label) {
stack.msb.add(`[${component.label} ${component.url}] `, tag`<a href="${component.url}">${component.label}</a> `)
stack[0].add(`[${component.label} ${component.url}] `, tag`<a href="${component.url}">${component.label}</a> `)
} else {
stack.msb.add(component.url)
stack[0].add(component.url)
}
}
}
@ -895,7 +891,7 @@ async function messageToEvent(message, guild, options = {}, di) {
await processComponent(component)
}
const {body, formatted_body} = stack.msb.get()
const {body, formatted_body} = stack[0].get()
if (body.trim().length) {
await addTextEvent(body, formatted_body, "m.text")
}
@ -918,7 +914,7 @@ async function messageToEvent(message, guild, options = {}, di) {
continue // Matrix's own URL previews are fine for images.
}
if (embed.type === "video" && embed.video?.url && !embed.title && message.content.includes(embed.video.url)) {
if (embed.type === "video" && !embed.title && message.content.includes(embed.video?.url)) {
continue // Doesn't add extra information and the direct video URL is already there.
}
@ -941,7 +937,6 @@ async function messageToEvent(message, guild, options = {}, di) {
const rep = new mxUtils.MatrixStringBuilder()
if (isKlipyGIF) {
assert(embed.video?.url)
rep.add("[GIF] ", "➿ ")
if (embed.title) {
rep.add(`${embed.title} ${embed.video.url}`, tag`<a href="${embed.video.url}">${embed.title}</a>`)

View file

@ -308,7 +308,7 @@ module.exports = {
async MESSAGE_REACTION_ADD(client, data) {
if (data.user_id === client.user.id) return // m2d reactions are added by the discord bot user - do not reflect them back to matrix.
if (data.emoji.name === "❓" && select("event_message", "message_id", {message_id: data.message_id, source: 0, part: 0}).get()) { // source 0 = matrix
const guild_id = data.guild_id ?? client.channels.get(data.channel_id)?.["guild_id"]
const guild_id = data.guild_id ?? client.channels.get(data.channel_id)["guild_id"]
await Promise.all([
client.snow.channel.deleteReaction(data.channel_id, data.message_id, data.emoji.name).catch(() => {}),
// @ts-ignore - this is all you need for it to do a matrix-side lookup

View file

@ -54,11 +54,8 @@ async function _interact({guild_id, data}, {api}) {
// from Matrix
const event = await api.getEvent(message.room_id, message.event_id)
const via = await utils.getViaServersQuery(message.room_id, api)
const channelsInGuild = discord.guildChannelMap.get(guild_id)
assert(channelsInGuild)
const inChannels = channelsInGuild
// @ts-ignore
.map(/** @returns {DiscordTypes.APIGuildChannel} */ cid => discord.channels.get(cid))
const inChannels = discord.guildChannelMap.get(guild_id)
.map(cid => discord.channels.get(cid))
.sort((a, b) => webGuild._getPosition(a, discord.channels) - webGuild._getPosition(b, discord.channels))
.filter(channel => from("channel_room").join("member_cache", "room_id").select("mxid").where({channel_id: channel.id, mxid: event.sender}).get())
const matrixMember = select("member_cache", ["displayname", "avatar_url"], {room_id: message.room_id, mxid: event.sender}).get()
@ -70,7 +67,7 @@ async function _interact({guild_id, data}, {api}) {
author: {
name,
url: `https://matrix.to/#/${event.sender}`,
icon_url: utils.getPublicUrlForMxc(matrixMember?.avatar_url)
icon_url: utils.getPublicUrlForMxc(matrixMember.avatar_url)
},
description: `This Matrix message was delivered to Discord by **Out Of Your Element**.\n[View on Matrix →](<https://matrix.to/#/${message.room_id}/${message.event_id}?${via}>)\n\n**User ID**: [${event.sender}](<https://matrix.to/#/${event.sender}>)`,
color: 0x0dbd8b,
@ -99,7 +96,7 @@ async function dm(interaction) {
const channel = await discord.snow.user.createDirectMessageChannel(interaction.member.user.id)
const response = await _interact(interaction, {api})
assert(response.type === DiscordTypes.InteractionResponseType.ChannelMessageWithSource)
response.data.flags = 0 & 0 // not ephemeral
response.data.flags &= 0 // not ephemeral
await discord.snow.channel.createMessage(channel.id, response.data)
}

View file

@ -31,7 +31,7 @@ async function* _interactAutocomplete({data, channel}, {api}) {
}
// Check it was used in a bridged channel
const roomID = select("channel_room", "room_id", {channel_id: channel?.id}).pluck().get()
const roomID = select("channel_room", "room_id", {channel_id: channel.id}).pluck().get()
if (!roomID) return yield exit()
// Check we are in fact autocompleting the first option, the user
@ -58,9 +58,9 @@ async function* _interactAutocomplete({data, channel}, {api}) {
const displaynameMatches = select("member_cache", ["mxid", "displayname"], {room_id: roomID}, "AND displayname IS NOT NULL AND displayname LIKE ? ESCAPE '$' LIMIT 25").all(query)
// prioritise matches closer to the start
displaynameMatches.sort((a, b) => {
let ai = a.displayname?.toLowerCase().indexOf(input.toLowerCase()) ?? -1
let ai = a.displayname.toLowerCase().indexOf(input.toLowerCase())
if (ai === -1) ai = 999
let bi = b.displayname?.toLowerCase().indexOf(input.toLowerCase()) ?? -1
let bi = b.displayname.toLowerCase().indexOf(input.toLowerCase())
if (bi === -1) bi = 999
return ai - bi
})
@ -132,18 +132,14 @@ async function* _interactCommand({data, channel, guild_id}, {api}) {
type: DiscordTypes.InteractionResponseType.DeferredChannelMessageWithSource
}}
let member
try {
/** @type {Ty.Event.M_Room_Member} */
member = await api.getStateEvent(roomID, "m.room.member", mxid)
var member = await api.getStateEvent(roomID, "m.room.member", mxid)
} catch (e) {}
if (!member || member.membership !== "join") {
const channelsInGuild = discord.guildChannelMap.get(guild_id)
assert(channelsInGuild)
const inChannels = channelsInGuild
// @ts-ignore
.map(/** @returns {DiscordTypes.APIGuildChannel} */ cid => discord.channels.get(cid))
const inChannels = discord.guildChannelMap.get(guild_id)
.map(cid => discord.channels.get(cid))
.sort((a, b) => webGuild._getPosition(a, discord.channels) - webGuild._getPosition(b, discord.channels))
.filter(channel => from("channel_room").join("member_cache", "room_id").select("mxid").where({channel_id: channel.id, mxid}).get())
if (inChannels.length) {

View file

@ -17,7 +17,7 @@ const retrigger = sync.require("../../d2m/actions/retrigger")
*/
async function addReaction(event) {
// Wait until the corresponding channel and message have already been bridged
if (retrigger.eventNotFoundThenRetrigger(event.content["m.relates_to"].event_id, () => as.emit("type:m.reaction", event))) return
if (retrigger.eventNotFoundThenRetrigger(event.content["m.relates_to"].event_id, as.emit.bind(as, "type:m.reaction", event))) return
// These will exist because it passed retrigger
const row = from("event_message").join("message_room", "message_id").join("historical_channel_room", "historical_room_index")

View file

@ -58,7 +58,7 @@ async function handle(event) {
await removeReaction(event)
// Or, it might be for removing a message or suppressing embeds. But to do that, the message needs to be bridged first.
if (retrigger.eventNotFoundThenRetrigger(event.redacts, () => as.emit("type:m.room.redaction", event))) return
if (retrigger.eventNotFoundThenRetrigger(event.redacts, as.emit.bind(as, "type:m.room.redaction", event))) return
const row = select("event_message", ["event_type", "event_subtype", "part"], {event_id: event.redacts}).get()
if (row && row.event_type === "m.room.message" && row.event_subtype === "m.notice" && row.part === 1) {

View file

@ -1,5 +1,4 @@
// @ts-check
/// <reference lib="dom" />
const Ty = require("../../types")
const DiscordTypes = require("discord-api-types/v10")
@ -372,7 +371,6 @@ function linkEndOfMessageSpriteSheet(content) {
for (const mxc of endOfMessageEmojis) {
// We can do up to 2000 chars max. (In this maximal case it will get chunked to a separate message.) Ignore additional emojis.
const withoutMxc = mxUtils.makeMxcPublic(mxc)
assert(withoutMxc)
const emojisLength = params.toString().length + encodeURIComponent(withoutMxc).length + 2
if (content.length + emojisLength + afterLink.length > 2000) {
break

View file

@ -196,10 +196,9 @@ async function getInviteState(roomID, event) {
}
// Try calling sliding sync API and extracting from stripped state
let root
try {
/** @type {Ty.R.SSS} */
root = await mreq.mreq("POST", path("/client/unstable/org.matrix.simplified_msc3575/sync", `@${reg.sender_localpart}:${reg.ooye.server_name}`, {timeout: "0"}), {
var root = await mreq.mreq("POST", path("/client/unstable/org.matrix.simplified_msc3575/sync", `@${reg.sender_localpart}:${reg.ooye.server_name}`, {timeout: "0"}), {
lists: {
a: {
ranges: [[0, 999]],
@ -240,7 +239,7 @@ async function getInviteState(roomID, event) {
name: room.name ?? null,
topic: room.topic ?? null,
avatar: room.avatar_url ?? null,
type: room.room_type ?? null
type: room.room_type
}
}
@ -427,7 +426,7 @@ async function profileSetDisplayname(mxid, displayname, inhibitPropagate) {
/**
* @param {string} mxid
* @param {string | null | undefined} avatar_url
* @param {string} avatar_url
* @param {boolean} [inhibitPropagate]
*/
async function profileSetAvatarUrl(mxid, avatar_url, inhibitPropagate) {

View file

@ -124,7 +124,7 @@ const commands = [{
if (matrixOnlyReason) {
// If uploading to Matrix, check if we have permission
const {powerLevels, powers: {[mxUtils.bot]: botPower}} = await mxUtils.getEffectivePower(event.room_id, [mxUtils.bot], api)
const requiredPower = powerLevels.events?.["im.ponies.room_emotes"] ?? powerLevels.state_default ?? 50
const requiredPower = powerLevels.events["im.ponies.room_emotes"] ?? powerLevels.state_default ?? 50
if (botPower < requiredPower) {
return api.sendEvent(event.room_id, "m.room.message", {
...ctx,

View file

@ -57,12 +57,12 @@ async function onBotMembership(event, api, createRoom) {
// Check if an upgrade is pending for this room
const newRoomID = event.room_id
const oldRoomID = select("room_upgrade_pending", "old_room_id", {new_room_id: newRoomID}).pluck().get()
if (!oldRoomID) return false
if (!oldRoomID) return
const channelRow = from("channel_room").join("guild_space", "guild_id").where({room_id: oldRoomID}).select("space_id", "guild_id", "channel_id").get()
assert(channelRow) // this could only fail if the channel was unbridged or something between upgrade and joining
// Check if is join/invite
if (event.content.membership !== "invite" && event.content.membership !== "join") return false
if (event.content.membership !== "invite" && event.content.membership !== "join") return
return await roomUpgradeSema.request(async () => {
// If invited, join

View file

@ -60,26 +60,6 @@ function getEventIDHash(eventID) {
return signedHash
}
class MatrixStringBuilderStack {
constructor() {
this.stack = [new MatrixStringBuilder()]
}
get msb() {
return this.stack[0]
}
bump() {
this.stack.unshift(new MatrixStringBuilder())
}
shift() {
const msb = this.stack.shift()
assert(msb)
return msb
}
}
class MatrixStringBuilder {
constructor() {
this.body = ""
@ -248,7 +228,7 @@ function generatePermittedMediaHash(mxc) {
* @see https://matrix.org/blog/2024/06/26/sunsetting-unauthenticated-media/ background
* @see https://matrix.org/blog/2024/06/20/matrix-v1.11-release/ implementation details
* @see https://www.sqlite.org/fileformat2.html#record_format SQLite integer field size
* @param {string | null | undefined} mxc
* @param {string} mxc
* @returns {string | undefined}
*/
function getPublicUrlForMxc(mxc) {
@ -258,7 +238,7 @@ function getPublicUrlForMxc(mxc) {
}
/**
* @param {string | null | undefined} mxc
* @param {string} mxc
* @returns {string | undefined} mxc URL with protocol stripped, e.g. "cadence.moe/abcdef1234"
*/
function makeMxcPublic(mxc) {
@ -309,7 +289,7 @@ function roomHasAtLeastVersion(roomVersionString, desiredVersion) {
*/
function removeCreatorsFromPowerLevels(roomCreateOuter, powerLevels) {
assert(roomCreateOuter.sender)
if (roomHasAtLeastVersion(roomCreateOuter.content.room_version, 12) && powerLevels.users) {
if (roomHasAtLeastVersion(roomCreateOuter.content.room_version, 12)) {
for (const creator of (roomCreateOuter.content.additional_creators ?? []).concat(roomCreateOuter.sender)) {
delete powerLevels.users[creator]
}
@ -405,7 +385,6 @@ module.exports.makeMxcPublic = makeMxcPublic
module.exports.getPublicUrlForMxc = getPublicUrlForMxc
module.exports.getEventIDHash = getEventIDHash
module.exports.MatrixStringBuilder = MatrixStringBuilder
module.exports.MatrixStringBuilderStack = MatrixStringBuilderStack
module.exports.getViaServers = getViaServers
module.exports.getViaServersQuery = getViaServersQuery
module.exports.roomHasAtLeastVersion = roomHasAtLeastVersion

View file

@ -31,7 +31,7 @@ function getSnow(event) {
/** @type {Map<string, Promise<string>>} */
const cache = new Map()
/** @param {string} url */
/** @param {string | undefined} url */
function timeUntilExpiry(url) {
const params = new URL(url).searchParams
const ex = params.get("ex")

View file

@ -5,7 +5,6 @@ const {convertImageStream} = require("../../m2d/converters/emoji-sheet")
const tryToCatch = require("try-to-catch")
const {test} = require("supertape")
const {router} = require("../../../test/web")
const streamWeb = require("stream/web")
test("web download matrix: access denied if not a known attachment", async t => {
const [error] = await tryToCatch(() =>
@ -28,7 +27,6 @@ test("web download matrix: works if a known attachment", async t => {
},
event,
api: {
// @ts-ignore
async getMedia(mxc, init) {
return new Response("", {status: 200, headers: {"content-type": "image/png"}})
}

View file

@ -54,8 +54,8 @@ function getAPI(event) {
const validNonce = new LRUCache({max: 200})
/**
* @param {{type: number, parent_id?: string | null, position?: number}} channel
* @param {Map<string, {type: number, parent_id?: string | null, position?: number}>} channels
* @param {{type: number, parent_id?: string, position?: number}} channel
* @param {Map<string, {type: number, parent_id?: string, position?: number}>} channels
*/
function getPosition(channel, channels) {
let position = 0
@ -65,11 +65,9 @@ function getPosition(channel, channels) {
// Categories are size 2000.
let foundCategory = channel
while (foundCategory.parent_id) {
const f = channels.get(foundCategory.parent_id)
assert(f)
foundCategory = f
foundCategory = channels.get(foundCategory.parent_id)
}
if (foundCategory.type === DiscordTypes.ChannelType.GuildCategory) position = ((foundCategory.position || 0) + 1) * 2000
if (foundCategory.type === DiscordTypes.ChannelType.GuildCategory) position = (foundCategory.position + 1) * 2000
// Categories always appear above what they contain.
if (channel.type === DiscordTypes.ChannelType.GuildCategory) position -= 0.5
@ -83,7 +81,7 @@ function getPosition(channel, channels) {
// Threads appear below their channel.
if ([DiscordTypes.ChannelType.PublicThread, DiscordTypes.ChannelType.PrivateThread, DiscordTypes.ChannelType.AnnouncementThread].includes(channel.type)) {
position += 0.5
let parent = channels.get(channel.parent_id || "")
let parent = channels.get(channel.parent_id)
if (parent && parent["position"]) position += parent["position"]
}
@ -100,11 +98,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
assert(channelIDs)
let linkedChannels = select("channel_room", ["channel_id", "room_id", "name", "nick"], {channel_id: channelIDs}).all()
let linkedChannelsWithDetails = linkedChannels.map(c => ({
// @ts-ignore
/** @type {DiscordTypes.APIGuildChannel} */ channel: discord.channels.get(c.channel_id),
...c
}))
let linkedChannelsWithDetails = linkedChannels.map(c => ({channel: discord.channels.get(c.channel_id), ...c}))
let removedUncachedChannels = dUtils.filterTo(linkedChannelsWithDetails, c => c.channel)
let linkedChannelIDs = linkedChannelsWithDetails.map(c => c.channel_id)
linkedChannelsWithDetails.sort((a, b) => getPosition(a.channel, discord.channels) - getPosition(b.channel, discord.channels))

View file

@ -1,6 +1,5 @@
// @ts-check
const assert = require("assert").strict
const {z} = require("zod")
const {defineEventHandler, createError, readValidatedBody, setResponseHeader, H3Event} = require("h3")
const Ty = require("../../types")
@ -78,9 +77,7 @@ as.router.post("/api/link-space", defineEventHandler(async event => {
const existing = select("guild_space", "guild_id", {}, "WHERE guild_id = ? OR space_id = ?").get(guildID, spaceID)
if (existing) throw createError({status: 400, message: "Bad Request", data: `Guild ID ${guildID} or space ID ${spaceID} are already bridged and cannot be reused`})
const inviteServer = inviteRow.mxid.match(/:(.*)/)?.[1]
assert(inviteServer)
const via = [inviteServer]
const via = [inviteRow.mxid.match(/:(.*)/)[1]]
// Check space exists and bridge is joined
try {