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:
@@ -24,7 +24,8 @@ import (
|
||||
"github.com/bwmarrin/discordgo"
|
||||
"github.com/rs/zerolog"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
|
||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -36,7 +37,7 @@ func (dc *DiscordClient) FetchMessages(ctx context.Context, fetchParams bridgev2
|
||||
return nil, bridgev2.ErrNotLoggedIn
|
||||
}
|
||||
|
||||
channelID := string(fetchParams.Portal.ID)
|
||||
channelID := discordid.ParsePortalID(fetchParams.Portal.ID)
|
||||
log := zerolog.Ctx(ctx).With().
|
||||
Str("channel_id", channelID).
|
||||
Int("desired_count", fetchParams.Count).
|
||||
@@ -46,7 +47,7 @@ func (dc *DiscordClient) FetchMessages(ctx context.Context, fetchParams bridgev2
|
||||
var afterID string
|
||||
|
||||
if fetchParams.AnchorMessage != nil {
|
||||
anchorID := string(fetchParams.AnchorMessage.ID)
|
||||
anchorID := discordid.ParseMessageID(fetchParams.AnchorMessage.ID)
|
||||
|
||||
if fetchParams.Forward {
|
||||
afterID = anchorID
|
||||
@@ -99,7 +100,7 @@ func (dc *DiscordClient) FetchMessages(ctx context.Context, fetchParams bridgev2
|
||||
}
|
||||
|
||||
converted = append(converted, &bridgev2.BackfillMessage{
|
||||
ID: networkid.MessageID(msg.ID),
|
||||
ID: discordid.MakeMessageID(msg.ID),
|
||||
ConvertedMessage: dc.connector.MsgConv.ToMatrix(ctx, fetchParams.Portal, intent, dc.UserLogin, dc.Session, msg),
|
||||
Sender: sender,
|
||||
Timestamp: ts,
|
||||
|
||||
@@ -275,40 +275,17 @@ func (d *DiscordClient) canSeeGuildChannel(ctx context.Context, ch *discordgo.Ch
|
||||
return canView
|
||||
}
|
||||
|
||||
// The string prepended to [networkid.PortalKey]s identifying spaces that
|
||||
// bridge Discord guilds.
|
||||
//
|
||||
// Every Discord guild created before August 2017 contained an 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 (d *DiscordClient) guildPortalKeyFromID(guildID string) networkid.PortalKey {
|
||||
// TODO: Support configuring `split_portals`.
|
||||
return networkid.PortalKey{
|
||||
ID: networkid.PortalID(guildPortalKeySigil + guildID),
|
||||
ID: discordid.MakeGuildPortalID(guildID),
|
||||
Receiver: d.UserLogin.ID,
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DiscordClient) makeAvatarForGuild(guild *discordgo.Guild) *bridgev2.Avatar {
|
||||
return &bridgev2.Avatar{
|
||||
ID: networkid.AvatarID(guild.Icon),
|
||||
ID: discordid.MakeAvatarID(guild.Icon),
|
||||
Get: func(ctx context.Context) ([]byte, error) {
|
||||
url := discordgo.EndpointGuildIcon(guild.ID, guild.Icon)
|
||||
return simpleDownload(ctx, url, "guild icon")
|
||||
@@ -440,8 +417,8 @@ func simpleDownload(ctx context.Context, url, thing string) ([]byte, error) {
|
||||
func (d *DiscordClient) makeEventSenderWithID(userID string) bridgev2.EventSender {
|
||||
return bridgev2.EventSender{
|
||||
IsFromMe: userID == d.Session.State.User.ID,
|
||||
SenderLogin: networkid.UserLoginID(userID),
|
||||
Sender: networkid.UserID(userID),
|
||||
SenderLogin: discordid.MakeUserLoginID(userID),
|
||||
Sender: discordid.MakeUserID(userID),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -74,7 +74,7 @@ func (d *DiscordChatResync) avatar(ctx context.Context) *bridgev2.Avatar {
|
||||
}
|
||||
|
||||
return &bridgev2.Avatar{
|
||||
ID: networkid.AvatarID(ch.Icon),
|
||||
ID: discordid.MakeAvatarID(ch.Icon),
|
||||
Get: func(ctx context.Context) ([]byte, error) {
|
||||
url := discordgo.EndpointGroupIcon(ch.ID, ch.Icon)
|
||||
return simpleDownload(ctx, url, "group dm icon")
|
||||
@@ -175,5 +175,5 @@ func (d *DiscordChatResync) CheckNeedsBackfill(ctx context.Context, latestBridge
|
||||
zerolog.Ctx(ctx).Debug().Str("channel_id", d.channel.ID).Msg("Haven't bridged any messages at all, not forward backfilling")
|
||||
return false, nil
|
||||
}
|
||||
return latestBridged.ID < networkid.MessageID(d.channel.LastMessageID), nil
|
||||
return latestBridged.ID < discordid.MakeMessageID(d.channel.LastMessageID), nil
|
||||
}
|
||||
|
||||
@@ -26,6 +26,8 @@ import (
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
"maunium.net/go/mautrix/bridgev2/status"
|
||||
|
||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||
)
|
||||
|
||||
type DiscordEventMeta struct {
|
||||
@@ -67,7 +69,7 @@ func (m *DiscordMessage) ConvertMessage(ctx context.Context, portal *bridgev2.Po
|
||||
}
|
||||
|
||||
func (m *DiscordMessage) GetID() networkid.MessageID {
|
||||
return networkid.MessageID(m.Data.ID)
|
||||
return discordid.MakeMessageID(m.Data.ID)
|
||||
}
|
||||
|
||||
func (m *DiscordMessage) GetSender() bridgev2.EventSender {
|
||||
@@ -79,7 +81,7 @@ func (d *DiscordClient) wrapDiscordMessage(evt *discordgo.MessageCreate) Discord
|
||||
DiscordEventMeta: &DiscordEventMeta{
|
||||
Type: bridgev2.RemoteEventMessage,
|
||||
PortalKey: networkid.PortalKey{
|
||||
ID: networkid.PortalID(evt.ChannelID),
|
||||
ID: discordid.MakePortalID(evt.ChannelID),
|
||||
Receiver: d.UserLogin.ID,
|
||||
},
|
||||
},
|
||||
@@ -99,11 +101,11 @@ func (r *DiscordReaction) GetSender() bridgev2.EventSender {
|
||||
}
|
||||
|
||||
func (r *DiscordReaction) GetTargetMessage() networkid.MessageID {
|
||||
return networkid.MessageID(r.Reaction.MessageID)
|
||||
return discordid.MakeMessageID(r.Reaction.MessageID)
|
||||
}
|
||||
|
||||
func (r *DiscordReaction) GetRemovedEmojiID() networkid.EmojiID {
|
||||
return networkid.EmojiID(r.Reaction.Emoji.Name)
|
||||
return discordid.MakeEmojiID(r.Reaction.Emoji.Name)
|
||||
}
|
||||
|
||||
var (
|
||||
@@ -116,7 +118,7 @@ func (r *DiscordReaction) GetReactionEmoji() (string, networkid.EmojiID) {
|
||||
// name is either a grapheme cluster consisting of a Unicode emoji, or the
|
||||
// name of a custom emoji.
|
||||
name := r.Reaction.Emoji.Name
|
||||
return name, networkid.EmojiID(name)
|
||||
return name, discordid.MakeEmojiID(name)
|
||||
}
|
||||
|
||||
func (r *DiscordReaction) GetReactionExtraContent() map[string]any {
|
||||
@@ -152,7 +154,7 @@ func (d *DiscordClient) wrapDiscordReaction(reaction *discordgo.MessageReaction,
|
||||
DiscordEventMeta: &DiscordEventMeta{
|
||||
Type: evtType,
|
||||
PortalKey: networkid.PortalKey{
|
||||
ID: networkid.PortalID(reaction.ChannelID),
|
||||
ID: discordid.MakePortalID(reaction.ChannelID),
|
||||
Receiver: d.UserLogin.ID,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/database"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
|
||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||
)
|
||||
@@ -44,7 +43,7 @@ func (d *DiscordClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M
|
||||
|
||||
portal := msg.Portal
|
||||
guildID := portal.Metadata.(*discordid.PortalMetadata).GuildID
|
||||
channelID := string(portal.ID)
|
||||
channelID := discordid.ParsePortalID(portal.ID)
|
||||
|
||||
sendReq, err := d.connector.MsgConv.ToDiscord(ctx, d.Session, msg)
|
||||
if err != nil {
|
||||
@@ -55,7 +54,7 @@ func (d *DiscordClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M
|
||||
// TODO: When supporting threads (and not a bot user), send a thread referer.
|
||||
options = append(options, discordgo.WithChannelReferer(guildID, channelID))
|
||||
|
||||
sentMsg, err := d.Session.ChannelMessageSendComplex(string(msg.Portal.ID), sendReq, options...)
|
||||
sentMsg, err := d.Session.ChannelMessageSendComplex(discordid.ParsePortalID(msg.Portal.ID), sendReq, options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -63,8 +62,8 @@ func (d *DiscordClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M
|
||||
|
||||
return &bridgev2.MatrixMessageResponse{
|
||||
DB: &database.Message{
|
||||
ID: networkid.MessageID(sentMsg.ID),
|
||||
SenderID: networkid.UserID(sentMsg.Author.ID),
|
||||
ID: discordid.MakeMessageID(sentMsg.ID),
|
||||
SenderID: discordid.MakeUserID(sentMsg.Author.ID),
|
||||
Timestamp: sentMsgTimestamp,
|
||||
},
|
||||
}, nil
|
||||
@@ -80,8 +79,8 @@ func (d *DiscordClient) PreHandleMatrixReaction(ctx context.Context, reaction *b
|
||||
// TODO: Handle custom emoji.
|
||||
|
||||
return bridgev2.MatrixReactionPreResponse{
|
||||
SenderID: networkid.UserID(d.UserLogin.ID),
|
||||
EmojiID: networkid.EmojiID(key),
|
||||
SenderID: discordid.UserLoginIDToUserID(d.UserLogin.ID),
|
||||
EmojiID: discordid.MakeEmojiID(key),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -90,23 +89,23 @@ func (d *DiscordClient) HandleMatrixReaction(ctx context.Context, reaction *brid
|
||||
portal := reaction.Portal
|
||||
meta := portal.Metadata.(*discordid.PortalMetadata)
|
||||
|
||||
err := d.Session.MessageReactionAddUser(meta.GuildID, string(portal.ID), string(reaction.TargetMessage.ID), relatesToKey)
|
||||
err := d.Session.MessageReactionAddUser(meta.GuildID, discordid.ParsePortalID(portal.ID), discordid.ParseMessageID(reaction.TargetMessage.ID), relatesToKey)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func (d *DiscordClient) HandleMatrixReactionRemove(ctx context.Context, removal *bridgev2.MatrixReactionRemove) error {
|
||||
removing := removal.TargetReaction
|
||||
emojiID := removing.EmojiID
|
||||
channelID := string(removing.Room.ID)
|
||||
channelID := discordid.ParsePortalID(removing.Room.ID)
|
||||
guildID := removal.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
||||
|
||||
err := d.Session.MessageReactionRemoveUser(guildID, channelID, string(removing.MessageID), string(emojiID), string(d.UserLogin.ID))
|
||||
err := d.Session.MessageReactionRemoveUser(guildID, channelID, discordid.ParseMessageID(removing.MessageID), discordid.ParseEmojiID(emojiID), discordid.ParseUserLoginID(d.UserLogin.ID))
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *DiscordClient) HandleMatrixMessageRemove(ctx context.Context, removal *bridgev2.MatrixMessageRemove) error {
|
||||
channelID := string(removal.Portal.ID)
|
||||
messageID := string(removal.TargetMessage.ID)
|
||||
channelID := discordid.ParsePortalID(removal.Portal.ID)
|
||||
messageID := discordid.ParseMessageID(removal.TargetMessage.ID)
|
||||
return d.Session.ChannelMessageDelete(channelID, messageID)
|
||||
}
|
||||
|
||||
@@ -122,7 +121,7 @@ func (d *DiscordClient) HandleMatrixReadReceipt(ctx context.Context, msg *bridge
|
||||
// receipt didn't exactly correspond with a message, try finding one close
|
||||
// by to use as the target.
|
||||
if msg.ExactMessage != nil {
|
||||
targetMessageID = string(msg.ExactMessage.ID)
|
||||
targetMessageID = discordid.ParseMessageID(msg.ExactMessage.ID)
|
||||
log = log.With().
|
||||
Str("message_id", targetMessageID).
|
||||
Logger()
|
||||
@@ -136,7 +135,7 @@ func (d *DiscordClient) HandleMatrixReadReceipt(ctx context.Context, msg *bridge
|
||||
// The read receipt didn't specify an exact message but we were able to
|
||||
// find one close by.
|
||||
|
||||
targetMessageID = string(closestMessage.ID)
|
||||
targetMessageID = discordid.ParseMessageID(closestMessage.ID)
|
||||
log = log.With().
|
||||
Str("closest_message_id", targetMessageID).
|
||||
Str("closest_event_id", closestMessage.MXID.String()).
|
||||
@@ -151,7 +150,7 @@ func (d *DiscordClient) HandleMatrixReadReceipt(ctx context.Context, msg *bridge
|
||||
|
||||
// TODO: Support threads.
|
||||
guildID := msg.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
||||
channelID := string(msg.Portal.ID)
|
||||
channelID := discordid.ParsePortalID(msg.Portal.ID)
|
||||
resp, err := d.Session.ChannelMessageAckNoToken(channelID, targetMessageID, discordgo.WithChannelReferer(guildID, channelID))
|
||||
if err != nil {
|
||||
log.Err(err).Msg("Failed to send read receipt to Discord")
|
||||
@@ -176,7 +175,7 @@ func (d *DiscordClient) viewingChannel(ctx context.Context, portal *bridgev2.Por
|
||||
d.markedOpenedLock.Lock()
|
||||
defer d.markedOpenedLock.Unlock()
|
||||
|
||||
channelID := string(portal.ID)
|
||||
channelID := discordid.ParsePortalID(portal.ID)
|
||||
log := zerolog.Ctx(ctx).With().
|
||||
Str("channel_id", channelID).Logger()
|
||||
|
||||
@@ -207,7 +206,7 @@ func (d *DiscordClient) HandleMatrixTyping(ctx context.Context, msg *bridgev2.Ma
|
||||
_ = d.viewingChannel(ctx, msg.Portal)
|
||||
|
||||
guildID := msg.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
||||
channelID := string(msg.Portal.ID)
|
||||
channelID := discordid.ParsePortalID(msg.Portal.ID)
|
||||
// TODO: Support threads properly when sending the referer.
|
||||
err := d.Session.ChannelTyping(channelID, discordgo.WithChannelReferer(guildID, channelID))
|
||||
|
||||
|
||||
@@ -24,7 +24,6 @@ import (
|
||||
"github.com/rs/zerolog"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/database"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
|
||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||
)
|
||||
@@ -71,7 +70,7 @@ func (dl *DiscordGenericLogin) FinalizeCreatingLogin(ctx context.Context, token
|
||||
|
||||
dl.Session = session
|
||||
ul, err := dl.User.NewLogin(ctx, &database.UserLogin{
|
||||
ID: networkid.UserLoginID(user.ID),
|
||||
ID: discordid.MakeUserLoginID(user.ID),
|
||||
Metadata: &discordid.UserLoginMetadata{
|
||||
Token: token,
|
||||
HeartbeatSession: session.HeartbeatSession,
|
||||
|
||||
@@ -26,6 +26,8 @@ import (
|
||||
"go.mau.fi/util/exhttp"
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
|
||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -115,7 +117,7 @@ func (p *ProvisioningAPI) makeHandler(handler func(http.ResponseWriter, *http.Re
|
||||
}
|
||||
|
||||
func (p *ProvisioningAPI) guildsList(w http.ResponseWriter, r *http.Request, login *bridgev2.UserLogin, client *DiscordClient) {
|
||||
p.log.Info().Str("login_id", string(login.ID)).Msg("guilds list requested via provisioning api")
|
||||
p.log.Info().Str("login_id", discordid.ParseUserLoginID(login.ID)).Msg("guilds list requested via provisioning api")
|
||||
|
||||
var resp respGuildsList
|
||||
resp.Guilds = []guildEntry{}
|
||||
@@ -142,7 +144,7 @@ func (p *ProvisioningAPI) bridgeGuild(w http.ResponseWriter, r *http.Request, lo
|
||||
}
|
||||
|
||||
p.log.Info().
|
||||
Str("login_id", string(login.ID)).
|
||||
Str("login_id", discordid.ParseUserLoginID(login.ID)).
|
||||
Str("guild_id", guildID).
|
||||
Msg("requested to bridge guild via provisioning api")
|
||||
|
||||
@@ -160,7 +162,7 @@ func (p *ProvisioningAPI) unbridgeGuild(w http.ResponseWriter, r *http.Request,
|
||||
}
|
||||
|
||||
p.log.Info().
|
||||
Str("login_id", string(login.ID)).
|
||||
Str("login_id", discordid.ParseUserLoginID(login.ID)).
|
||||
Str("guild_id", guildID).
|
||||
Msg("requested to unbridge guild via provisioning api")
|
||||
|
||||
|
||||
@@ -25,19 +25,21 @@ import (
|
||||
"go.mau.fi/util/ptr"
|
||||
"maunium.net/go/mautrix/bridgev2"
|
||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||
|
||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||
)
|
||||
|
||||
func (d *DiscordClient) IsThisUser(ctx context.Context, userID networkid.UserID) bool {
|
||||
// We define `UserID`s and `UserLoginID`s to be interchangeable, i.e. they map
|
||||
// directly to Discord user IDs ("snowflakes"), so we can perform a direct comparison.
|
||||
return userID == networkid.UserID(d.UserLogin.ID)
|
||||
return userID == discordid.UserLoginIDToUserID(d.UserLogin.ID)
|
||||
}
|
||||
|
||||
func makeUserAvatar(u *discordgo.User) *bridgev2.Avatar {
|
||||
url := u.AvatarURL("256")
|
||||
|
||||
return &bridgev2.Avatar{
|
||||
ID: networkid.AvatarID(url),
|
||||
ID: discordid.MakeAvatarID(url),
|
||||
Get: func(ctx context.Context) ([]byte, error) {
|
||||
return simpleDownload(ctx, url, "user avatar")
|
||||
},
|
||||
@@ -54,9 +56,9 @@ func (d *DiscordClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost)
|
||||
|
||||
// FIXME(skip): This won't work for users in guilds.
|
||||
|
||||
user, ok := d.usersFromReady[string(ghost.ID)]
|
||||
user, ok := d.usersFromReady[discordid.ParseUserID(ghost.ID)]
|
||||
if !ok {
|
||||
log.Error().Str("ghost_id", string(ghost.ID)).Msg("Couldn't find corresponding user from READY for ghost")
|
||||
log.Error().Str("ghost_id", discordid.ParseUserID(ghost.ID)).Msg("Couldn't find corresponding user from READY for ghost")
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user