add tests, implement kstate and state diffing
This commit is contained in:
parent
c7868e9dbb
commit
f09eeccef3
10 changed files with 2656 additions and 64 deletions
|
@ -1,6 +1,8 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
const assert = require("assert").strict
|
const assert = require("assert").strict
|
||||||
|
const {test} = require("supertape")
|
||||||
|
const testData = require("../../test/data")
|
||||||
const DiscordTypes = require("discord-api-types/v10")
|
const DiscordTypes = require("discord-api-types/v10")
|
||||||
|
|
||||||
const passthrough = require("../../passthrough")
|
const passthrough = require("../../passthrough")
|
||||||
|
@ -10,22 +12,134 @@ const mreq = sync.require("../../matrix/mreq")
|
||||||
/** @type {import("../../matrix/file")} */
|
/** @type {import("../../matrix/file")} */
|
||||||
const file = sync.require("../../matrix/file")
|
const file = sync.require("../../matrix/file")
|
||||||
|
|
||||||
|
function kstateToState(kstate) {
|
||||||
|
return Object.entries(kstate).map(([k, content]) => {
|
||||||
|
console.log(k)
|
||||||
|
const [type, state_key] = k.split("/")
|
||||||
|
assert.ok(typeof type === "string")
|
||||||
|
assert.ok(typeof state_key === "string")
|
||||||
|
return {type, state_key, content}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
test("kstate2state: general", t => {
|
||||||
|
t.deepEqual(kstateToState({
|
||||||
|
"m.room.name/": {name: "test name"},
|
||||||
|
"m.room.member/@cadence:cadence.moe": {membership: "join"}
|
||||||
|
}), [
|
||||||
|
{
|
||||||
|
type: "m.room.name",
|
||||||
|
state_key: "",
|
||||||
|
content: {
|
||||||
|
name: "test name"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
type: "m.room.member",
|
||||||
|
state_key: "@cadence:cadence.moe",
|
||||||
|
content: {
|
||||||
|
membership: "join"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
])
|
||||||
|
})
|
||||||
|
|
||||||
|
function diffKState(actual, target) {
|
||||||
|
const diff = {}
|
||||||
|
// go through each key that it should have
|
||||||
|
for (const key of Object.keys(target)) {
|
||||||
|
if (key in actual) {
|
||||||
|
// diff
|
||||||
|
try {
|
||||||
|
assert.deepEqual(actual[key], target[key])
|
||||||
|
} catch (e) {
|
||||||
|
// they differ. reassign the target
|
||||||
|
diff[key] = target[key]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// not present, needs to be added
|
||||||
|
diff[key] = target[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return diff
|
||||||
|
}
|
||||||
|
|
||||||
|
test("diffKState: detects edits", t => {
|
||||||
|
t.deepEqual(
|
||||||
|
diffKState({
|
||||||
|
"m.room.name/": {name: "test name"},
|
||||||
|
"same/": {a: 2}
|
||||||
|
}, {
|
||||||
|
"m.room.name/": {name: "edited name"},
|
||||||
|
"same/": {a: 2}
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
"m.room.name/": {name: "edited name"}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("diffKState: detects new properties", t => {
|
||||||
|
t.deepEqual(
|
||||||
|
diffKState({
|
||||||
|
"m.room.name/": {name: "test name"},
|
||||||
|
}, {
|
||||||
|
"m.room.name/": {name: "test name"},
|
||||||
|
"new/": {a: 2}
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
"new/": {a: 2}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
||||||
|
* @param {import("discord-api-types/v10").APIGuild} guild
|
||||||
*/
|
*/
|
||||||
async function createRoom(channel) {
|
async function channelToKState(channel, guild) {
|
||||||
const guildID = channel.guild_id
|
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guild.id)
|
||||||
assert.ok(guildID)
|
|
||||||
const guild = discord.guilds.get(guildID)
|
|
||||||
assert.ok(guild)
|
|
||||||
const spaceID = db.prepare("SELECT space_id FROM guild_space WHERE guild_id = ?").pluck().get(guildID)
|
|
||||||
assert.ok(typeof spaceID === "string")
|
assert.ok(typeof spaceID === "string")
|
||||||
|
|
||||||
const avatarEventContent = {}
|
const avatarEventContent = {}
|
||||||
if (guild.icon) {
|
if (guild.icon) {
|
||||||
avatarEventContent.url = await file.uploadDiscordFileToMxc(file.guildIcon(guild))
|
avatarEventContent.discord_path = file.guildIcon(guild)
|
||||||
|
avatarEventContent.url = await file.uploadDiscordFileToMxc(avatarEventContent.discord_path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const kstate = {
|
||||||
|
"m.room.name/": {name: channel.name},
|
||||||
|
"m.room.topic/": {topic: channel.topic || undefined},
|
||||||
|
"m.room.avatar/": avatarEventContent,
|
||||||
|
"m.room.guest_access/": {guest_access: "can_join"},
|
||||||
|
"m.room.history_visibility/": {history_visibility: "invited"},
|
||||||
|
[`m.space.parent/${spaceID}`]: { // TODO: put the proper server here
|
||||||
|
via: ["cadence.moe"],
|
||||||
|
canonical: true
|
||||||
|
},
|
||||||
|
"m.room.join_rules/": {
|
||||||
|
join_rule: "restricted",
|
||||||
|
allow: [{
|
||||||
|
type: "m.room.membership",
|
||||||
|
room_id: spaceID
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {spaceID, kstate}
|
||||||
|
}
|
||||||
|
|
||||||
|
test("channel2room: general", async t => {
|
||||||
|
t.deepEqual(await channelToKState(testData.channel.general, testData.guild.general).then(x => x.kstate), {expected: true, ...testData.room.general})
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
||||||
|
* @param guild
|
||||||
|
* @param {string} spaceID
|
||||||
|
* @param {any} kstate
|
||||||
|
*/
|
||||||
|
async function createRoom(channel, guild, spaceID, kstate) {
|
||||||
/** @type {import("../../types").R_RoomCreated} */
|
/** @type {import("../../types").R_RoomCreated} */
|
||||||
const root = await mreq.mreq("POST", "/client/v3/createRoom", {
|
const root = await mreq.mreq("POST", "/client/v3/createRoom", {
|
||||||
name: channel.name,
|
name: channel.name,
|
||||||
|
@ -33,45 +147,7 @@ async function createRoom(channel) {
|
||||||
preset: "private_chat",
|
preset: "private_chat",
|
||||||
visibility: "private",
|
visibility: "private",
|
||||||
invite: ["@cadence:cadence.moe"], // TODO
|
invite: ["@cadence:cadence.moe"], // TODO
|
||||||
initial_state: [
|
initial_state: kstateToState(kstate)
|
||||||
{
|
|
||||||
type: "m.room.avatar",
|
|
||||||
state_key: "",
|
|
||||||
content: avatarEventContent
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "m.room.guest_access",
|
|
||||||
state_key: "",
|
|
||||||
content: {
|
|
||||||
guest_access: "can_join"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "m.room.history_visibility",
|
|
||||||
state_key: "",
|
|
||||||
content: {
|
|
||||||
history_visibility: "invited"
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "m.space.parent",
|
|
||||||
state_key: spaceID,
|
|
||||||
content: {
|
|
||||||
via: ["cadence.moe"], // TODO: put the proper server here
|
|
||||||
canonical: true
|
|
||||||
}
|
|
||||||
},
|
|
||||||
{
|
|
||||||
type: "m.room.join_rules",
|
|
||||||
content: {
|
|
||||||
join_rule: "restricted",
|
|
||||||
allow: [{
|
|
||||||
type: "m.room.membership",
|
|
||||||
room_id: spaceID
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
]
|
|
||||||
})
|
})
|
||||||
|
|
||||||
db.prepare("INSERT INTO channel_room (channel_id, room_id) VALUES (?, ?)").run(channel.id, root.room_id)
|
db.prepare("INSERT INTO channel_room (channel_id, room_id) VALUES (?, ?)").run(channel.id, root.room_id)
|
||||||
|
@ -82,6 +158,24 @@ async function createRoom(channel) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
||||||
|
*/
|
||||||
|
async function syncRoom(channel) {
|
||||||
|
const guildID = channel.guild_id
|
||||||
|
assert(guildID)
|
||||||
|
const guild = discord.guilds.get(guildID)
|
||||||
|
assert(guild)
|
||||||
|
|
||||||
|
const {spaceID, kstate} = await channelToKState(channel, guild)
|
||||||
|
|
||||||
|
/** @type {string?} */
|
||||||
|
const existing = db.prepare("SELECT room_id from channel_room WHERE channel_id = ?").pluck().get(channel.id)
|
||||||
|
if (!existing) {
|
||||||
|
createRoom(channel, guild, spaceID, kstate)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function createAllForGuild(guildID) {
|
async function createAllForGuild(guildID) {
|
||||||
const channelIDs = discord.guildChannelMap.get(guildID)
|
const channelIDs = discord.guildChannelMap.get(guildID)
|
||||||
assert.ok(channelIDs)
|
assert.ok(channelIDs)
|
||||||
|
|
|
@ -2,14 +2,14 @@
|
||||||
|
|
||||||
const reg = require("../../matrix/read-registration.js")
|
const reg = require("../../matrix/read-registration.js")
|
||||||
const makeTxnId = require("../../matrix/txnid.js")
|
const makeTxnId = require("../../matrix/txnid.js")
|
||||||
const fetch = require("node-fetch")
|
const fetch = require("node-fetch").default
|
||||||
const messageToEvent = require("../converters/message-to-event.js")
|
const messageToEvent = require("../converters/message-to-event.js")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
|
* @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
|
||||||
*/
|
*/
|
||||||
function sendMessage(message) {
|
function sendMessage(message) {
|
||||||
const event = messageToEvent(message)
|
const event = messageToEvent.messageToEvent(message)
|
||||||
return fetch(`https://matrix.cadence.moe/_matrix/client/v3/rooms/!VwVlIAjOjejUpDhlbA:cadence.moe/send/m.room.message/${makeTxnId()}?user_id=@_ooye_example:cadence.moe`, {
|
return fetch(`https://matrix.cadence.moe/_matrix/client/v3/rooms/!VwVlIAjOjejUpDhlbA:cadence.moe/send/m.room.message/${makeTxnId()}?user_id=@_ooye_example:cadence.moe`, {
|
||||||
method: "PUT",
|
method: "PUT",
|
||||||
body: JSON.stringify(event),
|
body: JSON.stringify(event),
|
||||||
|
|
|
@ -6,7 +6,7 @@ const markdown = require("discord-markdown")
|
||||||
* @param {import("discord-api-types/v10").APIMessage} message
|
* @param {import("discord-api-types/v10").APIMessage} message
|
||||||
* @returns {import("../../types").M_Room_Message_content}
|
* @returns {import("../../types").M_Room_Message_content}
|
||||||
*/
|
*/
|
||||||
module.exports = function messageToEvent(message) {
|
function messageToEvent(message) {
|
||||||
const body = message.content
|
const body = message.content
|
||||||
const html = markdown.toHTML(body, {
|
const html = markdown.toHTML(body, {
|
||||||
/* discordCallback: {
|
/* discordCallback: {
|
||||||
|
@ -24,3 +24,5 @@ module.exports = function messageToEvent(message) {
|
||||||
formatted_body: html
|
formatted_body: html
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
module.exports.messageToEvent = messageToEvent
|
|
@ -1,6 +1,6 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
const fetch = require("node-fetch")
|
const fetch = require("node-fetch").default
|
||||||
|
|
||||||
const passthrough = require("../passthrough")
|
const passthrough = require("../passthrough")
|
||||||
const { sync, db } = passthrough
|
const { sync, db } = passthrough
|
||||||
|
@ -33,7 +33,6 @@ async function uploadDiscordFileToMxc(path) {
|
||||||
|
|
||||||
// Download from Discord
|
// Download from Discord
|
||||||
const promise = fetch(url, {}).then(/** @param {import("node-fetch").Response} res */ async res => {
|
const promise = fetch(url, {}).then(/** @param {import("node-fetch").Response} res */ async res => {
|
||||||
/** @ts-ignore @type {import("stream").Readable} body */
|
|
||||||
const body = res.body
|
const body = res.body
|
||||||
|
|
||||||
// Upload to Matrix
|
// Upload to Matrix
|
||||||
|
@ -56,7 +55,7 @@ async function uploadDiscordFileToMxc(path) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function guildIcon(guild) {
|
function guildIcon(guild) {
|
||||||
return `/icons/${guild.id}/${guild.icon}?size=${IMAGE_SIZE}`
|
return `/icons/${guild.id}/${guild.icon}.png?size=${IMAGE_SIZE}`
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.guildIcon = guildIcon
|
module.exports.guildIcon = guildIcon
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// @ts-check
|
// @ts-check
|
||||||
|
|
||||||
const fetch = require("node-fetch")
|
const fetch = require("node-fetch").default
|
||||||
const mixin = require("mixin-deep")
|
const mixin = require("mixin-deep")
|
||||||
|
|
||||||
const passthrough = require("../passthrough")
|
const passthrough = require("../passthrough")
|
||||||
|
@ -26,7 +26,7 @@ class MatrixServerError {
|
||||||
* @param {any} [body]
|
* @param {any} [body]
|
||||||
* @param {any} [extra]
|
* @param {any} [extra]
|
||||||
*/
|
*/
|
||||||
function mreq(method, url, body, extra = {}) {
|
async function mreq(method, url, body, extra = {}) {
|
||||||
const opts = mixin({
|
const opts = mixin({
|
||||||
method,
|
method,
|
||||||
body: (body == undefined || Object.is(body.constructor, Object)) ? JSON.stringify(body) : body,
|
body: (body == undefined || Object.is(body.constructor, Object)) ? JSON.stringify(body) : body,
|
||||||
|
@ -34,13 +34,13 @@ function mreq(method, url, body, extra = {}) {
|
||||||
Authorization: `Bearer ${reg.as_token}`
|
Authorization: `Bearer ${reg.as_token}`
|
||||||
}
|
}
|
||||||
}, extra)
|
}, extra)
|
||||||
|
|
||||||
console.log(baseUrl + url, opts)
|
console.log(baseUrl + url, opts)
|
||||||
return fetch(baseUrl + url, opts).then(res => {
|
const res = await fetch(baseUrl + url, opts)
|
||||||
return res.json().then(root => {
|
const root = await res.json()
|
||||||
if (!res.ok || root.errcode) throw new MatrixServerError(root)
|
|
||||||
return root
|
if (!res.ok || root.errcode) throw new MatrixServerError(root)
|
||||||
})
|
return root
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports.MatrixServerError = MatrixServerError
|
module.exports.MatrixServerError = MatrixServerError
|
||||||
|
|
|
@ -3,5 +3,6 @@
|
||||||
const fs = require("fs")
|
const fs = require("fs")
|
||||||
const yaml = require("js-yaml")
|
const yaml = require("js-yaml")
|
||||||
|
|
||||||
/** @type {import("../types").AppServiceRegistrationConfig} */
|
/** @ts-ignore @type {import("../types").AppServiceRegistrationConfig} */
|
||||||
module.exports = yaml.load(fs.readFileSync("registration.yaml", "utf8"))
|
const reg = yaml.load(fs.readFileSync("registration.yaml", "utf8"))
|
||||||
|
module.exports = reg
|
2397
package-lock.json
generated
2397
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
@ -29,5 +29,8 @@
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@types/node": "^18.16.0"
|
"@types/node": "^18.16.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "supertape --format short test/test.js"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
85
test/data.js
Normal file
85
test/data.js
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
// @ts-check
|
||||||
|
|
||||||
|
const DiscordTypes = require("discord-api-types/v10")
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
channel: {
|
||||||
|
general: {
|
||||||
|
type: 0,
|
||||||
|
topic: 'https://docs.google.com/document/d/blah/edit | I spread, pipe, and whip because it is my will. :headstone:',
|
||||||
|
rate_limit_per_user: 0,
|
||||||
|
position: 0,
|
||||||
|
permission_overwrites: [],
|
||||||
|
parent_id: null,
|
||||||
|
nsfw: false,
|
||||||
|
name: 'collective-unconscious' ,
|
||||||
|
last_pin_timestamp: '2023-04-06T09:51:57+00:00',
|
||||||
|
last_message_id: '1103832925784514580',
|
||||||
|
id: '112760669178241024',
|
||||||
|
default_thread_rate_limit_per_user: 0,
|
||||||
|
guild_id: '112760669178241024'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
room: {
|
||||||
|
general: {
|
||||||
|
"m.room.name/": {name: "collective-unconscious"},
|
||||||
|
"m.room.topic/": {topic: "https://docs.google.com/document/d/blah/edit | I spread, pipe, and whip because it is my will. :headstone:"},
|
||||||
|
"m.room.guest_access/": {guest_access: "can_join"},
|
||||||
|
"m.room.history_visibility/": {history_visibility: "invited"},
|
||||||
|
"m.space.parent/!jjWAGMeQdNrVZSSfvz:cadence.moe": {
|
||||||
|
via: ["cadence.moe"], // TODO: put the proper server here
|
||||||
|
canonical: true
|
||||||
|
},
|
||||||
|
"m.room.join_rules/": {
|
||||||
|
join_rule: "restricted",
|
||||||
|
allow: [{
|
||||||
|
type: "m.room.membership",
|
||||||
|
room_id: "!jjWAGMeQdNrVZSSfvz:cadence.moe"
|
||||||
|
}]
|
||||||
|
},
|
||||||
|
"m.room.avatar/": {
|
||||||
|
discord_path: "/icons/112760669178241024/a_f83622e09ead74f0c5c527fe241f8f8c.png?size=1024",
|
||||||
|
url: "mxc://cadence.moe/sZtPwbfOIsvfSoWCWPrGnzql"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
guild: {
|
||||||
|
general: {
|
||||||
|
owner_id: '112760500130975744',
|
||||||
|
premium_tier: 3,
|
||||||
|
stickers: [],
|
||||||
|
max_members: 500000,
|
||||||
|
splash: '86a34ed02524b972918bef810087f8e7',
|
||||||
|
explicit_content_filter: 0,
|
||||||
|
afk_channel_id: null,
|
||||||
|
nsfw_level: 0,
|
||||||
|
description: null,
|
||||||
|
preferred_locale: 'en-US',
|
||||||
|
system_channel_id: '112760669178241024',
|
||||||
|
mfa_level: 0,
|
||||||
|
/** @type {300} */
|
||||||
|
afk_timeout: 300,
|
||||||
|
id: '112760669178241024',
|
||||||
|
icon: 'a_f83622e09ead74f0c5c527fe241f8f8c',
|
||||||
|
emojis: [],
|
||||||
|
premium_subscription_count: 14,
|
||||||
|
roles: [],
|
||||||
|
discovery_splash: null,
|
||||||
|
default_message_notifications: 1,
|
||||||
|
region: 'deprecated',
|
||||||
|
max_video_channel_users: 25,
|
||||||
|
verification_level: 0,
|
||||||
|
application_id: null,
|
||||||
|
premium_progress_bar_enabled: false,
|
||||||
|
banner: 'a_a666ae551605a2d8cda0afd591c0af3a',
|
||||||
|
features: [],
|
||||||
|
vanity_url_code: null,
|
||||||
|
hub_type: null,
|
||||||
|
public_updates_channel_id: null,
|
||||||
|
rules_channel_id: null,
|
||||||
|
name: 'Psychonauts 3',
|
||||||
|
max_stage_video_channel_users: 300,
|
||||||
|
system_channel_flags: 0|0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
15
test/test.js
Normal file
15
test/test.js
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
// @ts-check
|
||||||
|
|
||||||
|
const sqlite = require("better-sqlite3")
|
||||||
|
const HeatSync = require("heatsync")
|
||||||
|
|
||||||
|
const config = require("../config")
|
||||||
|
const passthrough = require("../passthrough")
|
||||||
|
const db = new sqlite("db/ooye.db")
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
const sync = new HeatSync({persistent: false})
|
||||||
|
|
||||||
|
Object.assign(passthrough, { config, sync, db })
|
||||||
|
|
||||||
|
require("../d2m/actions/create-room")
|
Loading…
Reference in a new issue