2023-10-10 04:41:53 +00:00
|
|
|
// @ts-check
|
|
|
|
|
|
|
|
const passthrough = require("../../passthrough")
|
2024-01-17 11:30:55 +00:00
|
|
|
const {discord, sync, db} = passthrough
|
2023-10-10 04:41:53 +00:00
|
|
|
/** @type {import("../converters/pins-to-list")} */
|
|
|
|
const pinsToList = sync.require("../converters/pins-to-list")
|
|
|
|
/** @type {import("../../matrix/api")} */
|
|
|
|
const api = sync.require("../../matrix/api")
|
|
|
|
|
2024-01-17 11:30:55 +00:00
|
|
|
/**
|
|
|
|
* @template {string | null | undefined} T
|
|
|
|
* @param {T} timestamp
|
|
|
|
* @returns {T extends string ? number : null}
|
|
|
|
*/
|
|
|
|
function convertTimestamp(timestamp) {
|
|
|
|
// @ts-ignore
|
|
|
|
return typeof timestamp === "string" ? Math.floor(new Date(timestamp).getTime() / 1000) : null
|
|
|
|
}
|
|
|
|
|
2023-10-10 04:41:53 +00:00
|
|
|
/**
|
|
|
|
* @param {string} channelID
|
|
|
|
* @param {string} roomID
|
2024-01-17 11:30:55 +00:00
|
|
|
* @param {number?} convertedTimestamp
|
2023-10-10 04:41:53 +00:00
|
|
|
*/
|
2024-01-17 11:30:55 +00:00
|
|
|
async function updatePins(channelID, roomID, convertedTimestamp) {
|
2023-10-10 04:41:53 +00:00
|
|
|
const pins = await discord.snow.channel.getChannelPinnedMessages(channelID)
|
|
|
|
const eventIDs = pinsToList.pinsToList(pins)
|
2024-01-17 11:30:55 +00:00
|
|
|
if (pins.length === eventIDs.length || eventIDs.length) {
|
|
|
|
await api.sendState(roomID, "m.room.pinned_events", "", {
|
|
|
|
pinned: eventIDs
|
|
|
|
})
|
|
|
|
}
|
|
|
|
db.prepare("UPDATE channel_room SET last_bridged_pin_timestamp = ? WHERE channel_id = ?").run(convertedTimestamp || 0, channelID)
|
2023-10-10 04:41:53 +00:00
|
|
|
}
|
|
|
|
|
2024-01-17 11:30:55 +00:00
|
|
|
module.exports.convertTimestamp = convertTimestamp
|
2023-10-10 04:41:53 +00:00
|
|
|
module.exports.updatePins = updatePins
|