Compare commits
2 commits
109b3d2560
...
ed9ea57f12
Author | SHA1 | Date | |
---|---|---|---|
ed9ea57f12 | |||
cd4d777090 |
8 changed files with 149 additions and 0 deletions
|
@ -154,6 +154,7 @@ async function syncAllUsersInRoom(roomID) {
|
|||
}
|
||||
}
|
||||
|
||||
module.exports._memberToStateContent = memberToStateContent
|
||||
module.exports.ensureSim = ensureSim
|
||||
module.exports.ensureSimJoined = ensureSimJoined
|
||||
module.exports.syncUser = syncUser
|
||||
|
|
24
d2m/actions/register-user.test.js
Normal file
24
d2m/actions/register-user.test.js
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
BIN
db/ooye.db
BIN
db/ooye.db
Binary file not shown.
50
m2d/actions/register-webhook.js
Normal file
50
m2d/actions/register-webhook.js
Normal file
|
@ -0,0 +1,50 @@
|
|||
// @ts-check
|
||||
|
||||
const assert = require("assert").strict
|
||||
const passthrough = require("../../passthrough")
|
||||
const {discord, db} = passthrough
|
||||
|
||||
/**
|
||||
* Look in the database to find webhook credentials for a channel.
|
||||
* (Note that the credentials may be invalid and need to be re-created if the webhook was interfered with from outside.)
|
||||
* @param {string} channelID
|
||||
* @param {boolean} forceCreate create a new webhook no matter what the database says about the state
|
||||
* @returns id and token for a webhook for that channel
|
||||
*/
|
||||
async function ensureWebhook(channelID, forceCreate = false) {
|
||||
if (!forceCreate) {
|
||||
/** @type {{id: string, token: string} | null} */
|
||||
const row = db.prepare("SELECT webhook_id as id, webhook_token as token FROM webhook WHERE channel_id = ?").get(channelID)
|
||||
if (row) {
|
||||
return {created: false, ...row}
|
||||
}
|
||||
}
|
||||
|
||||
// If we got here, we need to create a new webhook.
|
||||
const webhook = await discord.snow.webhook.createWebhook(channelID, {name: "Out Of Your Element: Matrix Bridge"})
|
||||
assert(webhook.token)
|
||||
db.prepare("REPLACE INTO webhook (channel_id, webhook_id, webhook_token) VALUES (?, ?, ?)").run(channelID, webhook.id, webhook.token)
|
||||
return {
|
||||
id: webhook.id,
|
||||
token: webhook.token,
|
||||
created: true
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} channelID
|
||||
* @param {(webhook: import("../../types").WebhookCreds) => Promise<T>} callback
|
||||
* @returns Promise<T>
|
||||
* @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
|
23
m2d/actions/send-message.js
Normal file
23
m2d/actions/send-message.js
Normal file
|
@ -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
|
45
test/data.js
45
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: {
|
||||
|
|
|
@ -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")
|
||||
|
|
5
types.d.ts
vendored
5
types.d.ts
vendored
|
@ -9,6 +9,11 @@ export type AppServiceRegistrationConfig = {
|
|||
rate_limited: boolean
|
||||
}
|
||||
|
||||
export type WebhookCreds = {
|
||||
id: string
|
||||
token: string
|
||||
}
|
||||
|
||||
namespace Event {
|
||||
export type BaseStateEvent = {
|
||||
type: string
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue