d2m channel ordering #109
2 changed files with 87 additions and 3 deletions
|
|
@ -543,6 +543,53 @@ async function unbridgeChannel(channel, guildID, messageBeforeLeave = "This room
|
|||
await api.leaveRoom(roomID)
|
||||
}
|
||||
|
||||
const BASE26ALPHABET = "abcdefghijklmnopqrstuvwxyz"
|
||||
const BASE26PAIRMAX = 26*26-1
|
||||
|
||||
/**
|
||||
* Helper function for _encodeOrder
|
||||
*
|
||||
* @param {number | null | undefined} order
|
||||
* @returns {string}
|
||||
*/
|
||||
function _toBase26Pair(order) {
|
||||
if (order == null || !isFinite(order)) return " "
|
||||
else if (order < 0) order = 0
|
||||
else if (order > BASE26PAIRMAX) order = BASE26PAIRMAX
|
||||
|
||||
const lower = order % 26;
|
||||
const upper = (order - lower) / 26
|
||||
|
||||
return `${BASE26ALPHABET.at(upper)}${BASE26ALPHABET.at(lower)}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts a set of discord numeric channel order parameters to a single matrix space ordering string
|
||||
*
|
||||
* We abuse the lexiographic ordering scheme used by matrix to cheaply bridge the channel ordering,
|
||||
* even with the channel/category structure being flattened on the matrix side:
|
||||
* - The order string is 4 characters for channels and 6 characters for threads.
|
||||
* - Each pair of 2 characters is treated as a single base26 number (alphabet: a-z)
|
||||
* - The first pair is the order number of the category the channel belongs to
|
||||
* - If the channel does not have a category, this pair is set to " ",
|
||||
* which orders these rooms before rooms under categories
|
||||
* - The second pair is the order number of the channel
|
||||
* - The third (optional) pair is the order of the thread
|
||||
* - If not present, it is omitted - this orders threads right after the parent channel
|
||||
*
|
||||
* @param {number | null | undefined} categoryOrder category position
|
||||
* @param {number} channelOrder channel position
|
||||
* @param {number | null | undefined} threadOrder thread position
|
||||
* @returns {string}
|
||||
*/
|
||||
function _encodeOrder(categoryOrder, channelOrder, threadOrder) {
|
||||
const categoryPair = _toBase26Pair(categoryOrder)
|
||||
const channelPair = _toBase26Pair(channelOrder)
|
||||
const threadPair = threadOrder == null ? "" : _toBase26Pair(threadOrder)
|
||||
|
||||
return `${categoryPair}${channelPair}${threadPair}`
|
||||
}
|
||||
|
||||
/**
|
||||
* Async because it gets all space state from the homeserver, then if necessary sends one state event back.
|
||||
* @param {DiscordTypes.APIGuildTextChannel} channel
|
||||
|
|
@ -555,11 +602,15 @@ async function _syncSpaceMember(channel, spaceID, roomID, guild_id) {
|
|||
// If space is self-service then only permit changes to space parenting for threads
|
||||
// (The person setting up self-service has already put it in their preferred space to be able to get this far.)
|
||||
const autocreate = select("guild_active", "autocreate", {guild_id}).pluck().get()
|
||||
if (autocreate === 0 && ![DiscordTypes.ChannelType.PrivateThread, DiscordTypes.ChannelType.PublicThread, DiscordTypes.ChannelType.AnnouncementThread].includes(channel.type)) {
|
||||
const isThread = [DiscordTypes.ChannelType.PrivateThread, DiscordTypes.ChannelType.PublicThread, DiscordTypes.ChannelType.AnnouncementThread].includes(channel.type)
|
||||
if (autocreate === 0 && !isThread) {
|
||||
return []
|
||||
}
|
||||
|
||||
const spaceKState = await ks.roomToKState(spaceID)
|
||||
const childStateKey = `m.space.child/${roomID}`
|
||||
const currentChildEvent = spaceKState[childStateKey]
|
||||
/** @type {{via?: any, suggested?: any, order?: any}} */
|
||||
let spaceEventContent = {}
|
||||
if (
|
||||
channel.type !== DiscordTypes.ChannelType.PrivateThread // private threads do not belong in the space (don't offer people something they can't join)
|
||||
|
|
@ -571,9 +622,42 @@ async function _syncSpaceMember(channel, spaceID, roomID, guild_id) {
|
|||
spaceEventContent = {
|
||||
via: [reg.ooye.server_name]
|
||||
}
|
||||
|
||||
// let admins mark rooms as suggested on the [m] side without us removing the flag
|
||||
const currentSuggested = currentChildEvent?.suggested
|
||||
if (currentSuggested != null) {
|
||||
spaceEventContent.suggested = currentSuggested
|
||||
}
|
||||
|
||||
if (autocreate === 0) {
|
||||
// don't mess with room ordering in self-service spaces
|
||||
const currentOrder = currentChildEvent?.order
|
||||
if (currentOrder != null) {
|
||||
spaceEventContent.order = currentOrder
|
||||
}
|
||||
} else {
|
||||
// bridge the discord channel order
|
||||
/** @ts-ignore @type {DiscordTypes.APIThreadChannel | undefined} */
|
||||
const thread = isThread && channel.parent_id != null ? channel : undefined
|
||||
/** @ts-ignore @type {DiscordTypes.APIGuildTextChannel | undefined} */
|
||||
const channelForReal = isThread && channel.parent_id != null ? discord.channels.get(channel.parent_id) : channel
|
||||
/** @ts-ignore @type {DiscordTypes.APIGuildCategoryChannel | undefined} */
|
||||
const category = discord.channels.get(channelForReal?.parent_id ?? "")
|
||||
|
||||
// we actually don't have the thread's position - put 0 here for now, matrix will sort it automatically by timestamp
|
||||
const threadPosition = thread == null ? null : 0
|
||||
let channelPosition = channelForReal?.position ?? 0
|
||||
const categoryPosition = category?.position ?? null
|
||||
|
||||
// since voice channels are always ordered after all text channels on discord,
|
||||
// offset the order by half of the available range - this should be enough in most cases
|
||||
if (channel.type === DiscordTypes.ChannelType.GuildVoice) channelPosition += 13*26
|
||||
|
||||
spaceEventContent.order = _encodeOrder(categoryPosition, channelPosition, threadPosition)
|
||||
}
|
||||
}
|
||||
const spaceDiff = ks.diffKState(spaceKState, {
|
||||
[`m.space.child/${roomID}`]: spaceEventContent
|
||||
[childStateKey]: spaceEventContent
|
||||
})
|
||||
return ks.applyKStateDiffToRoom(spaceID, spaceDiff)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -238,7 +238,7 @@ module.exports = {
|
|||
if (!spaceID) return
|
||||
|
||||
if (data.role.id === data.guild_id) { // @everyone role changed - find a way to do this more efficiently in the future to handle many role updates
|
||||
await createSpace.syncSpaceFully(guild)
|
||||
await createSpace.syncSpaceFully(data.guild_id)
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue