discordid: remove all ID-related casts

In the same vein as mautrix-whatsapp, -slack and others, do not make
assumptions about how the ID is represented in the connector code. Let
the discordid package be entirely responsible.
This commit is contained in:
Skip R
2026-02-01 21:03:17 -08:00
parent e7554b212f
commit 92352ce603
12 changed files with 134 additions and 76 deletions

View File

@@ -21,8 +21,85 @@ import (
"maunium.net/go/mautrix/bridgev2/networkid"
)
func MakeUserID(userID string) networkid.UserID {
return networkid.UserID(userID)
}
func ParseUserID(userID networkid.UserID) string {
return string(userID)
}
func MakeUserLoginID(userID string) networkid.UserLoginID {
return networkid.UserLoginID(userID)
}
func ParseUserLoginID(id networkid.UserLoginID) string {
return string(id)
}
// UserLoginIDToUserID converts a UserLoginID to a UserID. In Discord, both
// are the same underlying snowflake.
func UserLoginIDToUserID(id networkid.UserLoginID) networkid.UserID {
return networkid.UserID(id)
}
func MakePortalID(channelID string) networkid.PortalID {
return networkid.PortalID(channelID)
}
func ParsePortalID(portalID networkid.PortalID) string {
return string(portalID)
}
func MakeMessageID(messageID string) networkid.MessageID {
return networkid.MessageID(messageID)
}
func ParseMessageID(messageID networkid.MessageID) string {
return string(messageID)
}
func MakeEmojiID(emojiName string) networkid.EmojiID {
return networkid.EmojiID(emojiName)
}
func ParseEmojiID(emojiID networkid.EmojiID) string {
return string(emojiID)
}
func MakeAvatarID(avatar string) networkid.AvatarID {
return networkid.AvatarID(avatar)
}
// The string prepended to [networkid.PortalKey]s identifying spaces that
// bridge Discord guilds.
//
// Every Discord guild created before August 2017 contained a channel
// having _the same ID as the guild itself_. This channel also functioned as
// the "default channel" in that incoming members would view this channel by
// default. It was also impossible to delete.
//
// After this date, these "default channels" became deletable, and fresh guilds
// were no longer created with a channel that exactly corresponded to the guild
// ID.
//
// To accommodate Discord guilds created before this API change that have also
// never deleted the default channel, we need a way to distinguish between the
// guild and the default channel, as we wouldn't be able to bridge the guild
// as a space otherwise.
//
// "*" was chosen as the asterisk character is used to filter by guilds in
// the quick switcher (in Discord's first-party clients).
//
// For more information, see: https://discord.com/developers/docs/change-log#breaking-change-default-channels:~:text=New%20guilds%20will%20no%20longer.
const GuildPortalKeySigil = "*"
func MakeGuildPortalID(guildID string) networkid.PortalID {
return networkid.PortalID(GuildPortalKeySigil + guildID)
}
func MakePortalKey(ch *discordgo.Channel, userLoginID networkid.UserLoginID, wantReceiver bool) (key networkid.PortalKey) {
key.ID = networkid.PortalID(ch.ID)
key.ID = MakePortalID(ch.ID)
if wantReceiver {
key.Receiver = userLoginID
}
@@ -30,6 +107,6 @@ func MakePortalKey(ch *discordgo.Channel, userLoginID networkid.UserLoginID, wan
}
func MakePortalKeyWithID(channelID string) (key networkid.PortalKey) {
key.ID = networkid.PortalID(channelID)
key.ID = MakePortalID(channelID)
return
}