From cd4d7770901b039d5d62cc9070150e69d3774da3 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Fri, 30 Jun 2023 15:15:23 +1200 Subject: [PATCH 1/2] add test for member state content --- d2m/actions/register-user.js | 1 + d2m/actions/register-user.test.js | 24 +++++++++++++++++ test/data.js | 45 +++++++++++++++++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 d2m/actions/register-user.test.js diff --git a/d2m/actions/register-user.js b/d2m/actions/register-user.js index cc5f515..1d1eb3d 100644 --- a/d2m/actions/register-user.js +++ b/d2m/actions/register-user.js @@ -154,6 +154,7 @@ async function syncAllUsersInRoom(roomID) { } } +module.exports._memberToStateContent = memberToStateContent module.exports.ensureSim = ensureSim module.exports.ensureSimJoined = ensureSimJoined module.exports.syncUser = syncUser diff --git a/d2m/actions/register-user.test.js b/d2m/actions/register-user.test.js new file mode 100644 index 0000000..7e23450 --- /dev/null +++ b/d2m/actions/register-user.test.js @@ -0,0 +1,24 @@ +const {channelToKState} = require("./create-room") +const {_memberToStateContent} = require("./register-user") +const {test} = require("supertape") +const testData = require("../../test/data") + +test("member2state: general", async t => { + t.deepEqual( + await _memberToStateContent(testData.member.sheep.user, testData.member.sheep, testData.guild.general.id), + { + avatar_url: "mxc://cadence.moe/rfemHmAtcprjLEiPiEuzPhpl", + displayname: "The Expert's Submarine | aprilsong", + membership: "join", + "moe.cadence.ooye.member": { + avatar: "/guilds/112760669178241024/users/134826546694193153/avatars/38dd359aa12bcd52dd3164126c587f8c.png?size=1024" + }, + "uk.half-shot.discord.member": { + bot: false, + displayColor: null, + id: "134826546694193153", + username: "@aprilsong" + } + } + ) +}) diff --git a/test/data.js b/test/data.js index bb70cbe..85b3cd4 100644 --- a/test/data.js +++ b/test/data.js @@ -93,6 +93,51 @@ module.exports = { system_channel_flags: 0|0 } }, + member: { + sheep: { + avatar: "38dd359aa12bcd52dd3164126c587f8c", + communication_disabled_until: null, + flags: 0, + joined_at: "2020-10-14T22:08:37.804000+00:00", + nick: "The Expert's Submarine", + pending: false, + premium_since: "2022-05-04T00:28:44.326000+00:00", + roles: [ + "112767366235959296", "118924814567211009", + "118923488755974146", "199995902742626304", + "204427286542417920", "217013981053845504", + "222168467627835392", "260993819204386816", + "265239342648131584", "271173313575780353", + "225744901915148298", "287733611912757249", + "318243902521868288", "348651574924541953", + "352291384021090304", "378402925128712193", + "392141548932038658", "393912152173576203", + "1123460940935991296", "872274377150980116", + "373336013109461013", "530220455085473813", + "768280323829137430", "842343433452257310", + "454567553738473472", "920107226528612383", + "1123528381514911745", "1040735082610167858", + "585531096071012409", "849737964090556488", + "660272211449479249" + ], + user: { + id: "134826546694193153", + username: "aprilsong", + avatar: "c754c120bce07ae3b3130e2b0e61d9dd", + discriminator: "0", + public_flags: 640, + flags: 640, + banner: "a3ad0693213f9dbf793b4159dbae0717", + accent_color: null, + global_name: "sheep", + avatar_decoration: null, + display_name: "sheep", + banner_color: null + }, + mute: false, + deaf: false + } + }, message: { // Display order is text content, attachments, then stickers sticker: { From ed9ea57f12548e5889e77af579a7b14e5fab69f0 Mon Sep 17 00:00:00 2001 From: Cadence Ember Date: Fri, 30 Jun 2023 15:15:34 +1200 Subject: [PATCH 2/2] add webhook creation utility --- db/ooye.db | Bin 131072 -> 143360 bytes m2d/actions/register-webhook.js | 50 ++++++++++++++++++++++++++++++++ m2d/actions/send-message.js | 23 +++++++++++++++ test/test.js | 1 + types.d.ts | 5 ++++ 5 files changed, 79 insertions(+) create mode 100644 m2d/actions/register-webhook.js create mode 100644 m2d/actions/send-message.js diff --git a/db/ooye.db b/db/ooye.db index 9c7d480b489cab00620de78d776874b511700f41..be5650515d1dd449ecdae32f120a2e170559af4d 100644 GIT binary patch delta 623 zcmZo@;AnWjF+p1J00RSqG7!Un)eHcVJJSS$WvT=Cw@$%l=ENJkJ zZE_F$UgJg!ZFX^KX~rh^lEkE()biA%jQsp;D5dBe( zV;|s<664~8NyV4sXQ$>VafY}?giP+{lHB}_ql$@-O`8quCUY$|ac|YhAGjqZpXcV? zyqMd7Nsv!mRh6-c8)UhzE}OWcG)P(;YI?j8l%cGv1CtYk$r(Z!N}Fx?P82e$F+82j z-r&dtq&ymCvo>J~W*eEkOyIaxI*u=uf%+$op%+kcr(%8_{SU<5Wu_UpmSl`$p zCB@j(GBMH6C@DF`)CkBkG&3a!fXxDJEHzmYVC4>sXRpP?Y84ni-JkT3Qv5QIMlHS#g$Fv(L=! zJ~J72m`QPS9%SIJ;QPRr!TW&Mn`asiA9owqKdv&)gTOdh%*obhqy}=hVlmj^R^&T; sa$=c+G%iOqvZ_vJyv^vo{lG0o2URv+Q1l3F7Ie7DKXHR18y7w<0F)fLnE(I) delta 297 zcmZp8z|qjaF+o~z9|Hq}0uaN1=0qK1o_!2@Sr>S@eHer|Ste$yvT?_N;ATOCZ)}r$ z*!NEU%fYSzl-(k(s>;};T9TNQlbW27n3tED6JL~{pNq_9nw-Ly#HcX&1fS++7S0@| z$^W=H*vz%q#JyD~f8dswe4d+k^I~oTCe<9UVcY}_v*K#B(O?(X*Jo@}-)zHoqL5jY zv0yTLgX3hchS?K0^lmO_oWjSf%4s&4eS+g=uL*lOCT=jC%sx}B*=Oc Promise} callback + * @returns Promise + * @template T + */ +async function withWebhook(channelID, callback) { + const webhook = await ensureWebhook(channelID, false) + return callback(webhook).catch(e => { + console.error(e) + // TODO: check if the error was webhook-related and if webhook.created === false, then: const webhook = ensureWebhook(channelID, true); return callback(webhook) + throw new Error(e) + }) +} + +module.exports.ensureWebhook = ensureWebhook +module.exports.withWebhook = withWebhook diff --git a/m2d/actions/send-message.js b/m2d/actions/send-message.js new file mode 100644 index 0000000..9c61107 --- /dev/null +++ b/m2d/actions/send-message.js @@ -0,0 +1,23 @@ +// @ts-check + +const assert = require("assert").strict +const DiscordTypes = require("discord-api-types/v10") +const passthrough = require("../../passthrough") +const {sync, discord, db} = passthrough + +/** @type {import("./register-webhook")} */ +const registerWebhook = sync.require("./register-webhook") + +/** + * @param {string} channelID + * @param {DiscordTypes.RESTPostAPIWebhookWithTokenJSONBody & {name: string, file: Buffer}[]} data + */ +// param {DiscordTypes.RESTPostAPIWebhookWithTokenQuery & {wait: true, disableEveryone?: boolean}} options +async function sendMessage(channelID, data) { + const result = await registerWebhook.withWebhook(channelID, async webhook => { + return discord.snow.webhook.executeWebhook(webhook.id, webhook.token, data, {wait: true, disableEveryone: true}) + }) + return result +} + +module.exports.sendMessage = sendMessage diff --git a/test/test.js b/test/test.js index ae6aea6..5f06ae4 100644 --- a/test/test.js +++ b/test/test.js @@ -17,3 +17,4 @@ require("../matrix/read-registration.test") require("../d2m/converters/message-to-event.test") require("../d2m/actions/create-room.test") require("../d2m/converters/user-to-mxid.test") +require("../d2m/actions/register-user.test") diff --git a/types.d.ts b/types.d.ts index bc24329..aaa8db1 100644 --- a/types.d.ts +++ b/types.d.ts @@ -9,6 +9,11 @@ export type AppServiceRegistrationConfig = { rate_limited: boolean } +export type WebhookCreds = { + id: string + token: string +} + namespace Event { export type BaseStateEvent = { type: string