forked from cadence/out-of-your-element
Compare commits
7 commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c68bac5476 | |||
| e275d4c928 | |||
| 780154fd09 | |||
| ea261e825b | |||
| d1aa8f01e7 | |||
|
|
9b3707baa1 | ||
| 411491b405 |
12 changed files with 161 additions and 29 deletions
|
|
@ -38,6 +38,7 @@ For more information about features, [see the user guide.](https://gitdab.com/ca
|
||||||
|
|
||||||
* This bridge is not designed for puppetting.
|
* This bridge is not designed for puppetting.
|
||||||
* Direct Messaging is not supported until I figure out a good way of doing it.
|
* Direct Messaging is not supported until I figure out a good way of doing it.
|
||||||
|
* Encrypted messages are not supported. Decryption is often unreliable on Matrix, and your messages end up in plaintext on Discord anyway, so there's not much advantage.
|
||||||
|
|
||||||
## Get started!
|
## Get started!
|
||||||
|
|
||||||
|
|
|
||||||
65
scripts/estimate-size.js
Normal file
65
scripts/estimate-size.js
Normal file
|
|
@ -0,0 +1,65 @@
|
||||||
|
// @ts-check
|
||||||
|
|
||||||
|
const pb = require("prettier-bytes")
|
||||||
|
const sqlite = require("better-sqlite3")
|
||||||
|
const HeatSync = require("heatsync")
|
||||||
|
|
||||||
|
const {reg} = require("../src/matrix/read-registration")
|
||||||
|
const passthrough = require("../src/passthrough")
|
||||||
|
|
||||||
|
const sync = new HeatSync({watchFS: false})
|
||||||
|
Object.assign(passthrough, {reg, sync})
|
||||||
|
|
||||||
|
const DiscordClient = require("../src/d2m/discord-client")
|
||||||
|
|
||||||
|
const discord = new DiscordClient(reg.ooye.discord_token, "no")
|
||||||
|
passthrough.discord = discord
|
||||||
|
|
||||||
|
const db = new sqlite("ooye.db")
|
||||||
|
passthrough.db = db
|
||||||
|
|
||||||
|
const api = require("../src/matrix/api")
|
||||||
|
|
||||||
|
const {room: roomID} = require("minimist")(process.argv.slice(2), {string: ["room"]})
|
||||||
|
if (!roomID) {
|
||||||
|
console.error("Usage: ./scripts/estimate-size.js --room=<!room id here>")
|
||||||
|
process.exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
const {channel_id, guild_id} = db.prepare("SELECT channel_id, guild_id FROM channel_room WHERE room_id = ?").get(roomID)
|
||||||
|
|
||||||
|
const max = 1000
|
||||||
|
|
||||||
|
;(async () => {
|
||||||
|
let total = 0
|
||||||
|
let size = 0
|
||||||
|
let from
|
||||||
|
|
||||||
|
while (total < max) {
|
||||||
|
const events = await api.getEvents(roomID, "b", {limit: 1000, from})
|
||||||
|
total += events.chunk.length
|
||||||
|
from = events.end
|
||||||
|
console.log(`Fetched ${total} events so far`)
|
||||||
|
|
||||||
|
for (const e of events.chunk) {
|
||||||
|
if (e.content?.info?.size) {
|
||||||
|
size += e.content.info.size
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (events.chunk.length === 0 || !events.end) break
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`Total size of uploads: ${pb(size)}`)
|
||||||
|
|
||||||
|
const searchResults = await discord.snow.requestHandler.request(`/guilds/${guild_id}/messages/search`, {
|
||||||
|
channel_id,
|
||||||
|
offset: "0",
|
||||||
|
limit: "1"
|
||||||
|
}, "get", "json")
|
||||||
|
|
||||||
|
const totalAllTime = searchResults.total_results
|
||||||
|
const fractionCounted = total / totalAllTime
|
||||||
|
console.log(`That counts for ${(fractionCounted*100).toFixed(2)}% of the history on Discord (${totalAllTime.toLocaleString()} messages)`)
|
||||||
|
console.log(`The size of uploads for the whole history would be approx: ${pb(Math.floor(size/total*totalAllTime))}`)
|
||||||
|
})()
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
|
const Ty = require("../src/types")
|
||||||
const assert = require("assert").strict
|
const assert = require("assert").strict
|
||||||
const fs = require("fs")
|
const fs = require("fs")
|
||||||
const sqlite = require("better-sqlite3")
|
const sqlite = require("better-sqlite3")
|
||||||
|
|
@ -285,8 +286,8 @@ function defineEchoHandler() {
|
||||||
console.log()
|
console.log()
|
||||||
|
|
||||||
// Done with user prompts, reg is now guaranteed to be valid
|
// Done with user prompts, reg is now guaranteed to be valid
|
||||||
|
const mreq = require("../src/matrix/mreq")
|
||||||
const api = require("../src/matrix/api")
|
const api = require("../src/matrix/api")
|
||||||
const file = require("../src/matrix/file")
|
|
||||||
const DiscordClient = require("../src/d2m/discord-client")
|
const DiscordClient = require("../src/d2m/discord-client")
|
||||||
const discord = new DiscordClient(reg.ooye.discord_token, "no")
|
const discord = new DiscordClient(reg.ooye.discord_token, "no")
|
||||||
passthrough.discord = discord
|
passthrough.discord = discord
|
||||||
|
|
@ -343,7 +344,13 @@ function defineEchoHandler() {
|
||||||
await api.register(reg.sender_localpart)
|
await api.register(reg.sender_localpart)
|
||||||
|
|
||||||
// upload initial images...
|
// upload initial images...
|
||||||
const avatarUrl = await file.uploadDiscordFileToMxc("https://cadence.moe/friends/out_of_your_element.png")
|
const avatarBuffer = await fs.promises.readFile(join(__dirname, "..", "docs", "img", "icon.png"), null)
|
||||||
|
/** @type {Ty.R.FileUploaded} */
|
||||||
|
const root = await mreq.mreq("POST", "/media/v3/upload", avatarBuffer, {
|
||||||
|
headers: {"Content-Type": "image/png"}
|
||||||
|
})
|
||||||
|
const avatarUrl = root.content_uri
|
||||||
|
assert(avatarUrl)
|
||||||
|
|
||||||
console.log("✅ Matrix appservice login works...")
|
console.log("✅ Matrix appservice login works...")
|
||||||
|
|
||||||
|
|
@ -352,8 +359,7 @@ function defineEchoHandler() {
|
||||||
console.log("✅ Emojis are ready...")
|
console.log("✅ Emojis are ready...")
|
||||||
|
|
||||||
// set profile data on discord...
|
// set profile data on discord...
|
||||||
const avatarImageBuffer = await fetch("https://cadence.moe/friends/out_of_your_element.png").then(res => res.arrayBuffer())
|
await discord.snow.user.updateSelf({avatar: "data:image/png;base64," + avatarBuffer.toString("base64")})
|
||||||
await discord.snow.user.updateSelf({avatar: "data:image/png;base64," + Buffer.from(avatarImageBuffer).toString("base64")})
|
|
||||||
console.log("✅ Discord profile updated...")
|
console.log("✅ Discord profile updated...")
|
||||||
|
|
||||||
// set profile data on homeserver...
|
// set profile data on homeserver...
|
||||||
|
|
|
||||||
|
|
@ -20,10 +20,10 @@ const SPECIAL_USER_MAPPINGS = new Map([
|
||||||
function downcaseUsername(user) {
|
function downcaseUsername(user) {
|
||||||
// First, try to convert the username to the set of allowed characters
|
// First, try to convert the username to the set of allowed characters
|
||||||
let downcased = user.username.toLowerCase()
|
let downcased = user.username.toLowerCase()
|
||||||
// spaces to underscores...
|
// spaces and slashes to underscores...
|
||||||
.replace(/ /g, "_")
|
.replace(/[ /]/g, "_")
|
||||||
// remove disallowed characters...
|
// remove disallowed characters...
|
||||||
.replace(/[^a-z0-9._=/-]*/g, "")
|
.replace(/[^a-z0-9._=-]*/g, "")
|
||||||
// remove leading and trailing dashes and underscores...
|
// remove leading and trailing dashes and underscores...
|
||||||
.replace(/(?:^[_-]*|[_-]*$)/g, "")
|
.replace(/(?:^[_-]*|[_-]*$)/g, "")
|
||||||
// If requested, also make the Discord user ID part of the username
|
// If requested, also make the Discord user ID part of the username
|
||||||
|
|
|
||||||
|
|
@ -21,8 +21,12 @@ test("user2name: works on single emoji at the end", t => {
|
||||||
t.equal(userToSimName({username: "Melody 🎵", discriminator: "2192"}), "melody")
|
t.equal(userToSimName({username: "Melody 🎵", discriminator: "2192"}), "melody")
|
||||||
})
|
})
|
||||||
|
|
||||||
test("user2name: works on crazy name", t => {
|
test("user2name: works on really weird name", t => {
|
||||||
t.equal(userToSimName({username: "*** D3 &W (89) _7//-", discriminator: "0001"}), "d3_w_89__7//")
|
t.equal(userToSimName({username: "*** D3 &W (89) _7//-", discriminator: "0001"}), "d3_w_89__7")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("user2name: treats slashes", t => {
|
||||||
|
t.equal(userToSimName({username: "Evil Lillith (she/her)", discriminator: "5892"}), "evil_lillith_she_her")
|
||||||
})
|
})
|
||||||
|
|
||||||
test("user2name: adds discriminator if name is unavailable (old tag format)", t => {
|
test("user2name: adds discriminator if name is unavailable (old tag format)", t => {
|
||||||
|
|
|
||||||
5
src/db/migrations/0034-slash-not-allowed-in-mxid.sql
Normal file
5
src/db/migrations/0034-slash-not-allowed-in-mxid.sql
Normal file
|
|
@ -0,0 +1,5 @@
|
||||||
|
BEGIN TRANSACTION;
|
||||||
|
|
||||||
|
DELETE FROM sim WHERE sim_name like '%/%';
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
40
src/m2d/actions/sticker.js
Normal file
40
src/m2d/actions/sticker.js
Normal file
|
|
@ -0,0 +1,40 @@
|
||||||
|
// @ts-check
|
||||||
|
|
||||||
|
const {Readable} = require("stream")
|
||||||
|
const {ReadableStream} = require("stream/web")
|
||||||
|
|
||||||
|
const {sync} = require("../../passthrough")
|
||||||
|
const sharp = require("sharp")
|
||||||
|
/** @type {import("../../matrix/api")} */
|
||||||
|
const api = sync.require("../../matrix/api")
|
||||||
|
/** @type {import("../../matrix/mreq")} */
|
||||||
|
const mreq = sync.require("../../matrix/mreq")
|
||||||
|
const streamMimeType = require("stream-mime-type")
|
||||||
|
|
||||||
|
const WIDTH = 160
|
||||||
|
const HEIGHT = 160
|
||||||
|
/**
|
||||||
|
* Downloads the sticker from the web and converts to webp data.
|
||||||
|
* @param {string} mxc a single mxc:// URL
|
||||||
|
* @returns {Promise<ReadableStream>} sticker webp data, or undefined if the downloaded sticker is not valid
|
||||||
|
*/
|
||||||
|
async function getAndResizeSticker(mxc) {
|
||||||
|
const res = await api.getMedia(mxc)
|
||||||
|
if (res.status !== 200) {
|
||||||
|
const root = await res.json()
|
||||||
|
throw new mreq.MatrixServerError(root, {mxc})
|
||||||
|
}
|
||||||
|
|
||||||
|
const streamIn = Readable.fromWeb(res.body)
|
||||||
|
const { stream, mime } = await streamMimeType.getMimeType(streamIn)
|
||||||
|
const animated = ["image/gif", "image/webp"].includes(mime)
|
||||||
|
|
||||||
|
const transformer = sharp({animated: animated})
|
||||||
|
.resize(WIDTH, HEIGHT, {fit: "inside", background: {r: 0, g: 0, b: 0, alpha: 0}})
|
||||||
|
.webp()
|
||||||
|
stream.pipe(transformer)
|
||||||
|
return Readable.toWeb(transformer)
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
module.exports.getAndResizeSticker = getAndResizeSticker
|
||||||
|
|
@ -631,23 +631,10 @@ async function eventToMessage(event, guild, channel, di) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (event.type === "m.sticker") {
|
if (event.type === "m.sticker") {
|
||||||
content = ""
|
const withoutMxc = mxUtils.makeMxcPublic(event.content.url)
|
||||||
let filename = event.content.body
|
assert(withoutMxc)
|
||||||
if (event.type === "m.sticker") {
|
const url = `${reg.ooye.bridge_origin}/download/sticker/${withoutMxc}/_.webp`
|
||||||
let mimetype
|
content = `[${event.content.body || "\u2800"}](${url})`
|
||||||
if (event.content.info?.mimetype?.includes("/")) {
|
|
||||||
mimetype = event.content.info.mimetype
|
|
||||||
} else {
|
|
||||||
const res = await di.api.getMedia(event.content.url, {method: "HEAD"})
|
|
||||||
if (res.status === 200) {
|
|
||||||
mimetype = res.headers.get("content-type")
|
|
||||||
}
|
|
||||||
if (!mimetype) throw new Error(`Server error ${res.status} or missing content-type while detecting sticker mimetype`)
|
|
||||||
}
|
|
||||||
filename += "." + mimetype.split("/")[1]
|
|
||||||
}
|
|
||||||
attachments.push({id: "0", filename})
|
|
||||||
pendingFiles.push({name: filename, mxc: event.content.url})
|
|
||||||
|
|
||||||
} else if (event.type === "org.matrix.msc3381.poll.start") {
|
} else if (event.type === "org.matrix.msc3381.poll.start") {
|
||||||
const pollContent = event.content["org.matrix.msc3381.poll.start"] // just for convenience
|
const pollContent = event.content["org.matrix.msc3381.poll.start"] // just for convenience
|
||||||
|
|
|
||||||
|
|
@ -136,7 +136,7 @@ async function getEventForTimestamp(roomID, ts) {
|
||||||
*/
|
*/
|
||||||
async function getEvents(roomID, dir, pagination = {}, filter) {
|
async function getEvents(roomID, dir, pagination = {}, filter) {
|
||||||
filter = filter && JSON.stringify(filter)
|
filter = filter && JSON.stringify(filter)
|
||||||
/** @type {Ty.Pagination<Ty.Event.Outer<any>>} */
|
/** @type {Ty.MessagesPagination<Ty.Event.Outer<any>>} */
|
||||||
const root = await mreq.mreq("GET", path(`/client/v3/rooms/${roomID}/messages`, null, {...pagination, dir, filter}))
|
const root = await mreq.mreq("GET", path(`/client/v3/rooms/${roomID}/messages`, null, {...pagination, dir, filter}))
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
|
||||||
8
src/types.d.ts
vendored
8
src/types.d.ts
vendored
|
|
@ -498,7 +498,13 @@ export type Membership = "invite" | "knock" | "join" | "leave" | "ban"
|
||||||
export type Pagination<T> = {
|
export type Pagination<T> = {
|
||||||
chunk: T[]
|
chunk: T[]
|
||||||
next_batch?: string
|
next_batch?: string
|
||||||
prev_match?: string
|
prev_batch?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export type MessagesPagination<T> = {
|
||||||
|
chunk: T[]
|
||||||
|
start: string
|
||||||
|
end?: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export type HierarchyPagination<T> = {
|
export type HierarchyPagination<T> = {
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,9 @@ const emojiSheet = sync.require("../../m2d/actions/emoji-sheet")
|
||||||
/** @type {import("../../m2d/converters/emoji-sheet")} */
|
/** @type {import("../../m2d/converters/emoji-sheet")} */
|
||||||
const emojiSheetConverter = sync.require("../../m2d/converters/emoji-sheet")
|
const emojiSheetConverter = sync.require("../../m2d/converters/emoji-sheet")
|
||||||
|
|
||||||
|
/** @type {import("../../m2d/actions/sticker")} */
|
||||||
|
const sticker = sync.require("../../m2d/actions/sticker")
|
||||||
|
|
||||||
const schema = {
|
const schema = {
|
||||||
params: z.object({
|
params: z.object({
|
||||||
server_name: z.string(),
|
server_name: z.string(),
|
||||||
|
|
@ -23,6 +26,10 @@ const schema = {
|
||||||
}),
|
}),
|
||||||
sheet: z.object({
|
sheet: z.object({
|
||||||
e: z.array(z.string()).or(z.string())
|
e: z.array(z.string()).or(z.string())
|
||||||
|
}),
|
||||||
|
sticker: z.object({
|
||||||
|
server_name: z.string().regex(/^[^/]+$/),
|
||||||
|
media_id: z.string().regex(/^[A-Za-z0-9_-]+$/)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -90,3 +97,14 @@ as.router.get(`/download/sheet`, defineEventHandler(async event => {
|
||||||
setResponseHeader(event, "Content-Type", "image/png")
|
setResponseHeader(event, "Content-Type", "image/png")
|
||||||
return buffer
|
return buffer
|
||||||
}))
|
}))
|
||||||
|
|
||||||
|
as.router.get(`/download/sticker/:server_name/:media_id/_.webp`, defineEventHandler(async event => {
|
||||||
|
const {server_name, media_id} = await getValidatedRouterParams(event, schema.sticker.parse)
|
||||||
|
/** remember that this has no mxc:// protocol in the string */
|
||||||
|
const mxc = server_name + "/" + media_id
|
||||||
|
verifyMediaHash(mxc)
|
||||||
|
|
||||||
|
const stream = await sticker.getAndResizeSticker(`mxc://${mxc}`)
|
||||||
|
setResponseHeader(event, "Content-Type", "image/webp")
|
||||||
|
return stream
|
||||||
|
}))
|
||||||
|
|
|
||||||
|
|
@ -115,7 +115,7 @@ function getChannelRoomsLinks(guild, rooms, roles) {
|
||||||
let removedWrongTypeChannels = dUtils.filterTo(unlinkedChannels, c => c && [0, 5].includes(c.type))
|
let removedWrongTypeChannels = dUtils.filterTo(unlinkedChannels, c => c && [0, 5].includes(c.type))
|
||||||
let removedPrivateChannels = dUtils.filterTo(unlinkedChannels, c => {
|
let removedPrivateChannels = dUtils.filterTo(unlinkedChannels, c => {
|
||||||
const permissions = dUtils.getPermissions(guild.id, roles, guild.roles, botID, c["permission_overwrites"])
|
const permissions = dUtils.getPermissions(guild.id, roles, guild.roles, botID, c["permission_overwrites"])
|
||||||
return dUtils.hasPermission(permissions, DiscordTypes.PermissionFlagsBits.ViewChannel)
|
return dUtils.hasSomePermissions(permissions, ["Administrator", "ViewChannel"])
|
||||||
})
|
})
|
||||||
unlinkedChannels.sort((a, b) => getPosition(a, discord.channels) - getPosition(b, discord.channels))
|
unlinkedChannels.sort((a, b) => getPosition(a, discord.channels) - getPosition(b, discord.channels))
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue