space and room creation
This commit is contained in:
parent
51480e21e5
commit
c7868e9dbb
15 changed files with 328 additions and 36 deletions
|
@ -1,22 +1,99 @@
|
|||
// @ts-check
|
||||
|
||||
const reg = require("../../matrix/read-registration.js")
|
||||
const fetch = require("node-fetch")
|
||||
const assert = require("assert").strict
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
|
||||
fetch("https://matrix.cadence.moe/_matrix/client/v3/createRoom?user_id=@_ooye_example:cadence.moe", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
invite: ["@cadence:cadence.moe"],
|
||||
is_direct: false,
|
||||
name: "New Bot User Room",
|
||||
preset: "trusted_private_chat"
|
||||
}),
|
||||
headers: {
|
||||
Authorization: `Bearer ${reg.as_token}`
|
||||
const passthrough = require("../../passthrough")
|
||||
const { discord, sync, db } = passthrough
|
||||
/** @type {import("../../matrix/mreq")} */
|
||||
const mreq = sync.require("../../matrix/mreq")
|
||||
/** @type {import("../../matrix/file")} */
|
||||
const file = sync.require("../../matrix/file")
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").APIGuildTextChannel} channel
|
||||
*/
|
||||
async function createRoom(channel) {
|
||||
const guildID = channel.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")
|
||||
|
||||
const avatarEventContent = {}
|
||||
if (guild.icon) {
|
||||
avatarEventContent.url = await file.uploadDiscordFileToMxc(file.guildIcon(guild))
|
||||
}
|
||||
}).then(res => res.text()).then(text => {
|
||||
// {"room_id":"!aAVaqeAKwChjWbsywj:cadence.moe"}
|
||||
console.log(text)
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
})
|
||||
|
||||
/** @type {import("../../types").R_RoomCreated} */
|
||||
const root = await mreq.mreq("POST", "/client/v3/createRoom", {
|
||||
name: channel.name,
|
||||
topic: channel.topic || undefined,
|
||||
preset: "private_chat",
|
||||
visibility: "private",
|
||||
invite: ["@cadence:cadence.moe"], // TODO
|
||||
initial_state: [
|
||||
{
|
||||
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)
|
||||
|
||||
// Put the newly created child into the space
|
||||
await mreq.mreq("PUT", `/client/v3/rooms/${spaceID}/state/m.space.child/${root.room_id}`, {
|
||||
via: ["cadence.moe"] // TODO: use the proper server
|
||||
})
|
||||
}
|
||||
|
||||
async function createAllForGuild(guildID) {
|
||||
const channelIDs = discord.guildChannelMap.get(guildID)
|
||||
assert.ok(channelIDs)
|
||||
for (const channelID of channelIDs) {
|
||||
const channel = discord.channels.get(channelID)
|
||||
assert.ok(channel)
|
||||
const existing = db.prepare("SELECT room_id FROM channel_room WHERE channel_id = ?").pluck().get(channel.id)
|
||||
if (channel.type === DiscordTypes.ChannelType.GuildText && !existing) {
|
||||
await createRoom(channel)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports.createRoom = createRoom
|
||||
module.exports.createAllForGuild = createAllForGuild
|
||||
|
|
46
d2m/actions/create-space.js
Normal file
46
d2m/actions/create-space.js
Normal file
|
@ -0,0 +1,46 @@
|
|||
// @ts-check
|
||||
|
||||
const passthrough = require("../../passthrough")
|
||||
const { sync, db } = passthrough
|
||||
/** @type {import("../../matrix/mreq")} */
|
||||
const mreq = sync.require("../../matrix/mreq")
|
||||
|
||||
/**
|
||||
* @param {import("discord-api-types/v10").RESTGetAPIGuildResult} guild
|
||||
*/
|
||||
function createSpace(guild) {
|
||||
return mreq.mreq("POST", "/client/v3/createRoom", {
|
||||
name: guild.name,
|
||||
preset: "private_chat",
|
||||
visibility: "private",
|
||||
power_level_content_override: {
|
||||
events_default: 100,
|
||||
invite: 50
|
||||
},
|
||||
invite: ["@cadence:cadence.moe"], // TODO
|
||||
topic: guild.description || undefined,
|
||||
creation_content: {
|
||||
type: "m.space"
|
||||
},
|
||||
initial_state: [
|
||||
{
|
||||
type: "m.room.guest_access",
|
||||
state_key: "",
|
||||
content: {
|
||||
guest_access: "can_join"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "m.room.history_visibility",
|
||||
content: {
|
||||
history_visibility: "invited"
|
||||
}
|
||||
}
|
||||
]
|
||||
}).then(/** @param {import("../../types").R_RoomCreated} root */ root => {
|
||||
db.prepare("INSERT INTO guild_space (guild_id, space_id) VALUES (?, ?)").run(guild.id, root.room_id)
|
||||
return root
|
||||
})
|
||||
}
|
||||
|
||||
module.exports.createSpace = createSpace
|
|
@ -10,7 +10,7 @@ const messageToEvent = require("../converters/message-to-event.js")
|
|||
*/
|
||||
function sendMessage(message) {
|
||||
const event = messageToEvent(message)
|
||||
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",
|
||||
body: JSON.stringify(event),
|
||||
headers: {
|
||||
|
@ -24,4 +24,4 @@ function sendMessage(message) {
|
|||
})
|
||||
}
|
||||
|
||||
module.exports = sendMessage
|
||||
module.exports.sendMessage = sendMessage
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
// Discord library internals type beat
|
||||
|
||||
const DiscordTypes = require("discord-api-types/v10")
|
||||
const passthrough = require("../passthrough")
|
||||
const { sync } = passthrough
|
||||
|
||||
|
@ -27,6 +28,8 @@ const utils = {
|
|||
const arr = []
|
||||
client.guildChannelMap.set(message.d.id, arr)
|
||||
for (const channel of message.d.channels || []) {
|
||||
// @ts-ignore
|
||||
channel.guild_id = message.d.id
|
||||
arr.push(channel.id)
|
||||
client.channels.set(channel.id, channel)
|
||||
}
|
||||
|
|
|
@ -1,8 +1,14 @@
|
|||
// @ts-check
|
||||
|
||||
// Grab Discord events we care about for the bridge, check them, and pass them on
|
||||
const {sync} = require("../passthrough")
|
||||
|
||||
const sendMessage = require("./actions/send-message")
|
||||
/** @type {import("./actions/create-space")}) */
|
||||
const createSpace = sync.require("./actions/create-space")
|
||||
|
||||
/** @type {import("./actions/send-message")}) */
|
||||
const sendMessage = sync.require("./actions/send-message")
|
||||
|
||||
// Grab Discord events we care about for the bridge, check them, and pass them on
|
||||
|
||||
module.exports = {
|
||||
/**
|
||||
|
@ -10,10 +16,7 @@ module.exports = {
|
|||
* @param {import("discord-api-types/v10").GatewayMessageCreateDispatchData} message
|
||||
*/
|
||||
onMessageCreate(client, message) {
|
||||
console.log(message)
|
||||
console.log(message.guild_id)
|
||||
console.log(message.member)
|
||||
sendMessage(message)
|
||||
sendMessage.sendMessage(message)
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue