forked from cadence/out-of-your-element
Add new WHERE feature to my funny orm
This commit is contained in:
parent
28abdac5b6
commit
475cd5b724
30 changed files with 149 additions and 105 deletions
|
@ -21,7 +21,7 @@ async function addReaction(data) {
|
|||
const user = data.member?.user
|
||||
assert.ok(user && user.username)
|
||||
|
||||
const parentID = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id) // 0 = primary
|
||||
const parentID = select("event_message", "event_id", {message_id: data.message_id, part: 0}).pluck().get() // 0 = primary
|
||||
if (!parentID) return // Nothing can be done if the parent message was never bridged.
|
||||
assert.equal(typeof parentID, "string")
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ const api = sync.require("../../matrix/api")
|
|||
* @param {import("discord-api-types/v10").APIThreadChannel} thread
|
||||
*/
|
||||
async function announceThread(parentRoomID, threadRoomID, thread) {
|
||||
const creatorMxid = select("sim", "mxid", "WHERE user_id = ?").pluck().get(thread.owner_id)
|
||||
const creatorMxid = select("sim", "mxid", {user_id: thread.owner_id}).pluck().get()
|
||||
|
||||
const content = await threadToAnnouncement.threadToAnnouncement(parentRoomID, threadRoomID, creatorMxid, thread, {api})
|
||||
|
||||
|
|
|
@ -71,7 +71,7 @@ async function channelToKState(channel, guild) {
|
|||
const spaceID = await createSpace.ensureSpace(guild)
|
||||
assert.ok(typeof spaceID === "string")
|
||||
|
||||
const row = select("channel_room", ["nick", "custom_avatar"], "WHERE channel_id = ?").get(channel.id)
|
||||
const row = select("channel_room", ["nick", "custom_avatar"], {channel_id: channel.id}).get()
|
||||
const customName = row?.nick
|
||||
const customAvatar = row?.custom_avatar
|
||||
const [convertedName, convertedTopic] = convertNameAndTopic(channel, guild, customName)
|
||||
|
@ -248,7 +248,7 @@ async function _syncRoom(channelID, shouldActuallySync) {
|
|||
await inflightRoomCreate.get(channelID) // just waiting, and then doing a new db query afterwards, is the simplest way of doing it
|
||||
}
|
||||
|
||||
const existing = select("channel_room", ["room_id", "thread_parent"], "WHERE channel_id = ?").get(channelID)
|
||||
const existing = select("channel_room", ["room_id", "thread_parent"], {channel_id: channelID}).get()
|
||||
|
||||
if (!existing) {
|
||||
const creation = (async () => {
|
||||
|
@ -308,9 +308,9 @@ async function _unbridgeRoom(channelID) {
|
|||
}
|
||||
|
||||
async function unbridgeDeletedChannel(channelID, guildID) {
|
||||
const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(channelID)
|
||||
const roomID = select("channel_room", "room_id", {channel_id: channelID}).pluck().get()
|
||||
assert.ok(roomID)
|
||||
const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guildID)
|
||||
const spaceID = select("guild_space", "space_id", {guild_id: guildID}).pluck().get()
|
||||
assert.ok(spaceID)
|
||||
|
||||
// remove room from being a space member
|
||||
|
|
|
@ -86,7 +86,7 @@ async function _syncSpace(guild, shouldActuallySync) {
|
|||
await inflightSpaceCreate.get(guild.id) // just waiting, and then doing a new db query afterwards, is the simplest way of doing it
|
||||
}
|
||||
|
||||
const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guild.id)
|
||||
const spaceID = select("guild_space", "space_id", {guild_id: guild.id}).pluck().get()
|
||||
|
||||
if (!spaceID) {
|
||||
const creation = (async () => {
|
||||
|
@ -117,7 +117,7 @@ async function _syncSpace(guild, shouldActuallySync) {
|
|||
const newAvatarState = spaceDiff["m.room.avatar/"]
|
||||
if (guild.icon && newAvatarState?.url) {
|
||||
// don't try to update rooms with custom avatars though
|
||||
const roomsWithCustomAvatars = select("channel_room", "room_id", "WHERE custom_avatar IS NOT NULL").pluck().all()
|
||||
const roomsWithCustomAvatars = select("channel_room", "room_id", {}, "WHERE custom_avatar IS NOT NULL").pluck().all()
|
||||
|
||||
const childRooms = ks.kstateToState(spaceKState).filter(({type, state_key, content}) => {
|
||||
return type === "m.space.child" && "via" in content && !roomsWithCustomAvatars.includes(state_key)
|
||||
|
@ -159,7 +159,7 @@ async function syncSpaceFully(guildID) {
|
|||
const guild = discord.guilds.get(guildID)
|
||||
assert.ok(guild)
|
||||
|
||||
const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(guildID)
|
||||
const spaceID = select("guild_space", "space_id", {guild_id: guildID}).pluck().get()
|
||||
|
||||
const guildKState = await guildToKState(guild)
|
||||
|
||||
|
@ -180,7 +180,7 @@ async function syncSpaceFully(guildID) {
|
|||
}).map(({state_key}) => state_key)
|
||||
|
||||
for (const roomID of childRooms) {
|
||||
const channelID = select("channel_room", "channel_id", "WHERE room_id = ?").pluck().get(roomID)
|
||||
const channelID = select("channel_room", "channel_id", {room_id: roomID}).pluck().get()
|
||||
if (!channelID) continue
|
||||
if (discord.channels.has(channelID)) {
|
||||
await createRoom.syncRoom(channelID)
|
||||
|
@ -198,7 +198,7 @@ async function syncSpaceFully(guildID) {
|
|||
async function syncSpaceExpressions(data) {
|
||||
// No need for kstate here. Each of these maps to a single state event, which will always overwrite what was there before. I can just send the state event.
|
||||
|
||||
const spaceID = select("guild_space", "space_id", "WHERE guild_id = ?").pluck().get(data.guild_id)
|
||||
const spaceID = select("guild_space", "space_id", {guild_id: data.guild_id}).pluck().get()
|
||||
if (!spaceID) return
|
||||
|
||||
if ("emojis" in data && data.emojis.length) {
|
||||
|
|
|
@ -9,10 +9,10 @@ const api = sync.require("../../matrix/api")
|
|||
* @param {import("discord-api-types/v10").GatewayMessageDeleteDispatchData} data
|
||||
*/
|
||||
async function deleteMessage(data) {
|
||||
const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
|
||||
const roomID = select("channel_room", "room_id", {channel_id: data.channel_id}).pluck().get()
|
||||
if (!roomID) return
|
||||
|
||||
const eventsToRedact = select("event_message", "event_id", "WHERE message_id = ?").pluck().all(data.id)
|
||||
const eventsToRedact = select("event_message", "event_id", {message_id: data.id}).pluck().all()
|
||||
for (const eventID of eventsToRedact) {
|
||||
// Unfortunately, we can't specify a sender to do the redaction as, unless we find out that info via the audit logs
|
||||
await api.redactEvent(roomID, eventID)
|
||||
|
|
|
@ -51,7 +51,7 @@ async function createSim(user) {
|
|||
*/
|
||||
async function ensureSim(user) {
|
||||
let mxid = null
|
||||
const existing = select("sim", "mxid", "WHERE user_id = ?").pluck().get(user.id)
|
||||
const existing = select("sim", "mxid", {user_id: user.id}).pluck().get()
|
||||
if (existing) {
|
||||
mxid = existing
|
||||
} else {
|
||||
|
@ -74,7 +74,7 @@ async function ensureSimJoined(user, roomID) {
|
|||
const mxid = await ensureSim(user)
|
||||
|
||||
// Ensure joined
|
||||
const existing = select("sim_member", "mxid", "WHERE room_id = ? AND mxid = ?").pluck().get(roomID, mxid)
|
||||
const existing = select("sim_member", "mxid", {room_id: roomID, mxid}).pluck().get()
|
||||
if (!existing) {
|
||||
try {
|
||||
await api.inviteToRoom(roomID, mxid)
|
||||
|
@ -143,7 +143,7 @@ async function syncUser(user, member, guildID, roomID) {
|
|||
const mxid = await ensureSimJoined(user, roomID)
|
||||
const content = await memberToStateContent(user, member, guildID)
|
||||
const currentHash = hashProfileContent(content)
|
||||
const existingHash = select("sim_member", "hashed_profile_content", "WHERE room_id = ? AND mxid = ?").safeIntegers().pluck().get(roomID, mxid)
|
||||
const existingHash = select("sim_member", "hashed_profile_content", {room_id: roomID, mxid}).safeIntegers().pluck().get()
|
||||
// only do the actual sync if the hash has changed since we last looked
|
||||
if (existingHash !== currentHash) {
|
||||
await api.sendState(roomID, "m.room.member", mxid, content, mxid)
|
||||
|
@ -153,9 +153,9 @@ async function syncUser(user, member, guildID, roomID) {
|
|||
}
|
||||
|
||||
async function syncAllUsersInRoom(roomID) {
|
||||
const mxids = select("sim_member", "mxid", "WHERE room_id = ?").pluck().all(roomID)
|
||||
const mxids = select("sim_member", "mxid", {room_id: roomID}).pluck().all()
|
||||
|
||||
const channelID = select("channel_room", "channel_id", "WHERE room_id = ?").pluck().get(roomID)
|
||||
const channelID = select("channel_room", "channel_id", {room_id: roomID}).pluck().get()
|
||||
assert.ok(typeof channelID === "string")
|
||||
|
||||
/** @ts-ignore @type {import("discord-api-types/v10").APIGuildChannel} */
|
||||
|
@ -164,7 +164,7 @@ async function syncAllUsersInRoom(roomID) {
|
|||
assert.ok(typeof guildID === "string")
|
||||
|
||||
for (const mxid of mxids) {
|
||||
const userID = select("sim", "user_id", "WHERE mxid = ?").pluck().get(mxid)
|
||||
const userID = select("sim", "user_id", {mxid}).pluck().get()
|
||||
assert.ok(typeof userID === "string")
|
||||
|
||||
/** @ts-ignore @type {Required<import("discord-api-types/v10").APIGuildMember>} */
|
||||
|
|
|
@ -18,9 +18,9 @@ const emoji = sync.require("../../m2d/converters/emoji")
|
|||
* @param {import("discord-api-types/v10").GatewayMessageReactionRemoveDispatchData} data
|
||||
*/
|
||||
async function removeReaction(data) {
|
||||
const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
|
||||
const roomID = select("channel_room", "room_id", {channel_id: data.channel_id}).pluck().get()
|
||||
if (!roomID) return
|
||||
const eventIDForMessage = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id)
|
||||
const eventIDForMessage = select("event_message", "event_id", {message_id: data.message_id, part: 0}).pluck().get()
|
||||
if (!eventIDForMessage) return
|
||||
|
||||
/** @type {Ty.Pagination<Ty.Event.Outer<Ty.Event.M_Reaction>>} */
|
||||
|
@ -42,7 +42,7 @@ async function removeReaction(data) {
|
|||
}
|
||||
if (!lookingAtMatrixReaction && !wantToRemoveMatrixReaction) {
|
||||
// We are removing a Discord user's reaction, so we just make the sim user remove it.
|
||||
const mxid = select("sim", "mxid", "WHERE user_id = ?").pluck().get(data.user_id)
|
||||
const mxid = select("sim", "mxid", {user_id: data.user_id}).pluck().get()
|
||||
if (mxid === event.sender) {
|
||||
await api.redactEvent(roomID, event.event_id, mxid)
|
||||
}
|
||||
|
@ -55,9 +55,9 @@ async function removeReaction(data) {
|
|||
* @param {import("discord-api-types/v10").GatewayMessageReactionRemoveEmojiDispatchData} data
|
||||
*/
|
||||
async function removeEmojiReaction(data) {
|
||||
const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
|
||||
const roomID = select("channel_room", "room_id", {channel_id: data.channel_id}).pluck().get()
|
||||
if (!roomID) return
|
||||
const eventIDForMessage = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id)
|
||||
const eventIDForMessage = select("event_message", "event_id", {message_id: data.message_id, part: 0}).pluck().get()
|
||||
if (!eventIDForMessage) return
|
||||
|
||||
/** @type {Ty.Pagination<Ty.Event.Outer<Ty.Event.M_Reaction>>} */
|
||||
|
@ -79,9 +79,9 @@ async function removeEmojiReaction(data) {
|
|||
* @param {import("discord-api-types/v10").GatewayMessageReactionRemoveAllDispatchData} data
|
||||
*/
|
||||
async function removeAllReactions(data) {
|
||||
const roomID = select("channel_room", "room_id", "WHERE channel_id = ?").pluck().get(data.channel_id)
|
||||
const roomID = select("channel_room", "room_id", {channel_id: data.channel_id}).pluck().get()
|
||||
if (!roomID) return
|
||||
const eventIDForMessage = select("event_message", "event_id", "WHERE message_id = ? AND part = 0").pluck().get(data.message_id)
|
||||
const eventIDForMessage = select("event_message", "event_id", {message_id: data.message_id, part: 0}).pluck().get()
|
||||
if (!eventIDForMessage) return
|
||||
|
||||
/** @type {Ty.Pagination<Ty.Event.Outer<Ty.Event.M_Reaction>>} */
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue