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/bwmarrin/discordgo"
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
|
||||||
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@@ -36,7 +37,7 @@ func (dc *DiscordClient) FetchMessages(ctx context.Context, fetchParams bridgev2
|
|||||||
return nil, bridgev2.ErrNotLoggedIn
|
return nil, bridgev2.ErrNotLoggedIn
|
||||||
}
|
}
|
||||||
|
|
||||||
channelID := string(fetchParams.Portal.ID)
|
channelID := discordid.ParsePortalID(fetchParams.Portal.ID)
|
||||||
log := zerolog.Ctx(ctx).With().
|
log := zerolog.Ctx(ctx).With().
|
||||||
Str("channel_id", channelID).
|
Str("channel_id", channelID).
|
||||||
Int("desired_count", fetchParams.Count).
|
Int("desired_count", fetchParams.Count).
|
||||||
@@ -46,7 +47,7 @@ func (dc *DiscordClient) FetchMessages(ctx context.Context, fetchParams bridgev2
|
|||||||
var afterID string
|
var afterID string
|
||||||
|
|
||||||
if fetchParams.AnchorMessage != nil {
|
if fetchParams.AnchorMessage != nil {
|
||||||
anchorID := string(fetchParams.AnchorMessage.ID)
|
anchorID := discordid.ParseMessageID(fetchParams.AnchorMessage.ID)
|
||||||
|
|
||||||
if fetchParams.Forward {
|
if fetchParams.Forward {
|
||||||
afterID = anchorID
|
afterID = anchorID
|
||||||
@@ -99,7 +100,7 @@ func (dc *DiscordClient) FetchMessages(ctx context.Context, fetchParams bridgev2
|
|||||||
}
|
}
|
||||||
|
|
||||||
converted = append(converted, &bridgev2.BackfillMessage{
|
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),
|
ConvertedMessage: dc.connector.MsgConv.ToMatrix(ctx, fetchParams.Portal, intent, dc.UserLogin, dc.Session, msg),
|
||||||
Sender: sender,
|
Sender: sender,
|
||||||
Timestamp: ts,
|
Timestamp: ts,
|
||||||
|
|||||||
@@ -275,40 +275,17 @@ func (d *DiscordClient) canSeeGuildChannel(ctx context.Context, ch *discordgo.Ch
|
|||||||
return canView
|
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 {
|
func (d *DiscordClient) guildPortalKeyFromID(guildID string) networkid.PortalKey {
|
||||||
// TODO: Support configuring `split_portals`.
|
// TODO: Support configuring `split_portals`.
|
||||||
return networkid.PortalKey{
|
return networkid.PortalKey{
|
||||||
ID: networkid.PortalID(guildPortalKeySigil + guildID),
|
ID: discordid.MakeGuildPortalID(guildID),
|
||||||
Receiver: d.UserLogin.ID,
|
Receiver: d.UserLogin.ID,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DiscordClient) makeAvatarForGuild(guild *discordgo.Guild) *bridgev2.Avatar {
|
func (d *DiscordClient) makeAvatarForGuild(guild *discordgo.Guild) *bridgev2.Avatar {
|
||||||
return &bridgev2.Avatar{
|
return &bridgev2.Avatar{
|
||||||
ID: networkid.AvatarID(guild.Icon),
|
ID: discordid.MakeAvatarID(guild.Icon),
|
||||||
Get: func(ctx context.Context) ([]byte, error) {
|
Get: func(ctx context.Context) ([]byte, error) {
|
||||||
url := discordgo.EndpointGuildIcon(guild.ID, guild.Icon)
|
url := discordgo.EndpointGuildIcon(guild.ID, guild.Icon)
|
||||||
return simpleDownload(ctx, url, "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 {
|
func (d *DiscordClient) makeEventSenderWithID(userID string) bridgev2.EventSender {
|
||||||
return bridgev2.EventSender{
|
return bridgev2.EventSender{
|
||||||
IsFromMe: userID == d.Session.State.User.ID,
|
IsFromMe: userID == d.Session.State.User.ID,
|
||||||
SenderLogin: networkid.UserLoginID(userID),
|
SenderLogin: discordid.MakeUserLoginID(userID),
|
||||||
Sender: networkid.UserID(userID),
|
Sender: discordid.MakeUserID(userID),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -74,7 +74,7 @@ func (d *DiscordChatResync) avatar(ctx context.Context) *bridgev2.Avatar {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &bridgev2.Avatar{
|
return &bridgev2.Avatar{
|
||||||
ID: networkid.AvatarID(ch.Icon),
|
ID: discordid.MakeAvatarID(ch.Icon),
|
||||||
Get: func(ctx context.Context) ([]byte, error) {
|
Get: func(ctx context.Context) ([]byte, error) {
|
||||||
url := discordgo.EndpointGroupIcon(ch.ID, ch.Icon)
|
url := discordgo.EndpointGroupIcon(ch.ID, ch.Icon)
|
||||||
return simpleDownload(ctx, url, "group dm 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")
|
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 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"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||||
"maunium.net/go/mautrix/bridgev2/status"
|
"maunium.net/go/mautrix/bridgev2/status"
|
||||||
|
|
||||||
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DiscordEventMeta struct {
|
type DiscordEventMeta struct {
|
||||||
@@ -67,7 +69,7 @@ func (m *DiscordMessage) ConvertMessage(ctx context.Context, portal *bridgev2.Po
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *DiscordMessage) GetID() networkid.MessageID {
|
func (m *DiscordMessage) GetID() networkid.MessageID {
|
||||||
return networkid.MessageID(m.Data.ID)
|
return discordid.MakeMessageID(m.Data.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *DiscordMessage) GetSender() bridgev2.EventSender {
|
func (m *DiscordMessage) GetSender() bridgev2.EventSender {
|
||||||
@@ -79,7 +81,7 @@ func (d *DiscordClient) wrapDiscordMessage(evt *discordgo.MessageCreate) Discord
|
|||||||
DiscordEventMeta: &DiscordEventMeta{
|
DiscordEventMeta: &DiscordEventMeta{
|
||||||
Type: bridgev2.RemoteEventMessage,
|
Type: bridgev2.RemoteEventMessage,
|
||||||
PortalKey: networkid.PortalKey{
|
PortalKey: networkid.PortalKey{
|
||||||
ID: networkid.PortalID(evt.ChannelID),
|
ID: discordid.MakePortalID(evt.ChannelID),
|
||||||
Receiver: d.UserLogin.ID,
|
Receiver: d.UserLogin.ID,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -99,11 +101,11 @@ func (r *DiscordReaction) GetSender() bridgev2.EventSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *DiscordReaction) GetTargetMessage() networkid.MessageID {
|
func (r *DiscordReaction) GetTargetMessage() networkid.MessageID {
|
||||||
return networkid.MessageID(r.Reaction.MessageID)
|
return discordid.MakeMessageID(r.Reaction.MessageID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DiscordReaction) GetRemovedEmojiID() networkid.EmojiID {
|
func (r *DiscordReaction) GetRemovedEmojiID() networkid.EmojiID {
|
||||||
return networkid.EmojiID(r.Reaction.Emoji.Name)
|
return discordid.MakeEmojiID(r.Reaction.Emoji.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
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 is either a grapheme cluster consisting of a Unicode emoji, or the
|
||||||
// name of a custom emoji.
|
// name of a custom emoji.
|
||||||
name := r.Reaction.Emoji.Name
|
name := r.Reaction.Emoji.Name
|
||||||
return name, networkid.EmojiID(name)
|
return name, discordid.MakeEmojiID(name)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *DiscordReaction) GetReactionExtraContent() map[string]any {
|
func (r *DiscordReaction) GetReactionExtraContent() map[string]any {
|
||||||
@@ -152,7 +154,7 @@ func (d *DiscordClient) wrapDiscordReaction(reaction *discordgo.MessageReaction,
|
|||||||
DiscordEventMeta: &DiscordEventMeta{
|
DiscordEventMeta: &DiscordEventMeta{
|
||||||
Type: evtType,
|
Type: evtType,
|
||||||
PortalKey: networkid.PortalKey{
|
PortalKey: networkid.PortalKey{
|
||||||
ID: networkid.PortalID(reaction.ChannelID),
|
ID: discordid.MakePortalID(reaction.ChannelID),
|
||||||
Receiver: d.UserLogin.ID,
|
Receiver: d.UserLogin.ID,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import (
|
|||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
"maunium.net/go/mautrix/bridgev2/database"
|
"maunium.net/go/mautrix/bridgev2/database"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
|
||||||
|
|
||||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||||
)
|
)
|
||||||
@@ -44,7 +43,7 @@ func (d *DiscordClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M
|
|||||||
|
|
||||||
portal := msg.Portal
|
portal := msg.Portal
|
||||||
guildID := portal.Metadata.(*discordid.PortalMetadata).GuildID
|
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)
|
sendReq, err := d.connector.MsgConv.ToDiscord(ctx, d.Session, msg)
|
||||||
if err != nil {
|
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.
|
// TODO: When supporting threads (and not a bot user), send a thread referer.
|
||||||
options = append(options, discordgo.WithChannelReferer(guildID, channelID))
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -63,8 +62,8 @@ func (d *DiscordClient) HandleMatrixMessage(ctx context.Context, msg *bridgev2.M
|
|||||||
|
|
||||||
return &bridgev2.MatrixMessageResponse{
|
return &bridgev2.MatrixMessageResponse{
|
||||||
DB: &database.Message{
|
DB: &database.Message{
|
||||||
ID: networkid.MessageID(sentMsg.ID),
|
ID: discordid.MakeMessageID(sentMsg.ID),
|
||||||
SenderID: networkid.UserID(sentMsg.Author.ID),
|
SenderID: discordid.MakeUserID(sentMsg.Author.ID),
|
||||||
Timestamp: sentMsgTimestamp,
|
Timestamp: sentMsgTimestamp,
|
||||||
},
|
},
|
||||||
}, nil
|
}, nil
|
||||||
@@ -80,8 +79,8 @@ func (d *DiscordClient) PreHandleMatrixReaction(ctx context.Context, reaction *b
|
|||||||
// TODO: Handle custom emoji.
|
// TODO: Handle custom emoji.
|
||||||
|
|
||||||
return bridgev2.MatrixReactionPreResponse{
|
return bridgev2.MatrixReactionPreResponse{
|
||||||
SenderID: networkid.UserID(d.UserLogin.ID),
|
SenderID: discordid.UserLoginIDToUserID(d.UserLogin.ID),
|
||||||
EmojiID: networkid.EmojiID(key),
|
EmojiID: discordid.MakeEmojiID(key),
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,23 +89,23 @@ func (d *DiscordClient) HandleMatrixReaction(ctx context.Context, reaction *brid
|
|||||||
portal := reaction.Portal
|
portal := reaction.Portal
|
||||||
meta := portal.Metadata.(*discordid.PortalMetadata)
|
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
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DiscordClient) HandleMatrixReactionRemove(ctx context.Context, removal *bridgev2.MatrixReactionRemove) error {
|
func (d *DiscordClient) HandleMatrixReactionRemove(ctx context.Context, removal *bridgev2.MatrixReactionRemove) error {
|
||||||
removing := removal.TargetReaction
|
removing := removal.TargetReaction
|
||||||
emojiID := removing.EmojiID
|
emojiID := removing.EmojiID
|
||||||
channelID := string(removing.Room.ID)
|
channelID := discordid.ParsePortalID(removing.Room.ID)
|
||||||
guildID := removal.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DiscordClient) HandleMatrixMessageRemove(ctx context.Context, removal *bridgev2.MatrixMessageRemove) error {
|
func (d *DiscordClient) HandleMatrixMessageRemove(ctx context.Context, removal *bridgev2.MatrixMessageRemove) error {
|
||||||
channelID := string(removal.Portal.ID)
|
channelID := discordid.ParsePortalID(removal.Portal.ID)
|
||||||
messageID := string(removal.TargetMessage.ID)
|
messageID := discordid.ParseMessageID(removal.TargetMessage.ID)
|
||||||
return d.Session.ChannelMessageDelete(channelID, messageID)
|
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
|
// receipt didn't exactly correspond with a message, try finding one close
|
||||||
// by to use as the target.
|
// by to use as the target.
|
||||||
if msg.ExactMessage != nil {
|
if msg.ExactMessage != nil {
|
||||||
targetMessageID = string(msg.ExactMessage.ID)
|
targetMessageID = discordid.ParseMessageID(msg.ExactMessage.ID)
|
||||||
log = log.With().
|
log = log.With().
|
||||||
Str("message_id", targetMessageID).
|
Str("message_id", targetMessageID).
|
||||||
Logger()
|
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
|
// The read receipt didn't specify an exact message but we were able to
|
||||||
// find one close by.
|
// find one close by.
|
||||||
|
|
||||||
targetMessageID = string(closestMessage.ID)
|
targetMessageID = discordid.ParseMessageID(closestMessage.ID)
|
||||||
log = log.With().
|
log = log.With().
|
||||||
Str("closest_message_id", targetMessageID).
|
Str("closest_message_id", targetMessageID).
|
||||||
Str("closest_event_id", closestMessage.MXID.String()).
|
Str("closest_event_id", closestMessage.MXID.String()).
|
||||||
@@ -151,7 +150,7 @@ func (d *DiscordClient) HandleMatrixReadReceipt(ctx context.Context, msg *bridge
|
|||||||
|
|
||||||
// TODO: Support threads.
|
// TODO: Support threads.
|
||||||
guildID := msg.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
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))
|
resp, err := d.Session.ChannelMessageAckNoToken(channelID, targetMessageID, discordgo.WithChannelReferer(guildID, channelID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("Failed to send read receipt to Discord")
|
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()
|
d.markedOpenedLock.Lock()
|
||||||
defer d.markedOpenedLock.Unlock()
|
defer d.markedOpenedLock.Unlock()
|
||||||
|
|
||||||
channelID := string(portal.ID)
|
channelID := discordid.ParsePortalID(portal.ID)
|
||||||
log := zerolog.Ctx(ctx).With().
|
log := zerolog.Ctx(ctx).With().
|
||||||
Str("channel_id", channelID).Logger()
|
Str("channel_id", channelID).Logger()
|
||||||
|
|
||||||
@@ -207,7 +206,7 @@ func (d *DiscordClient) HandleMatrixTyping(ctx context.Context, msg *bridgev2.Ma
|
|||||||
_ = d.viewingChannel(ctx, msg.Portal)
|
_ = d.viewingChannel(ctx, msg.Portal)
|
||||||
|
|
||||||
guildID := msg.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
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.
|
// TODO: Support threads properly when sending the referer.
|
||||||
err := d.Session.ChannelTyping(channelID, discordgo.WithChannelReferer(guildID, channelID))
|
err := d.Session.ChannelTyping(channelID, discordgo.WithChannelReferer(guildID, channelID))
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,6 @@ import (
|
|||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
"maunium.net/go/mautrix/bridgev2/database"
|
"maunium.net/go/mautrix/bridgev2/database"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
|
||||||
|
|
||||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||||
)
|
)
|
||||||
@@ -71,7 +70,7 @@ func (dl *DiscordGenericLogin) FinalizeCreatingLogin(ctx context.Context, token
|
|||||||
|
|
||||||
dl.Session = session
|
dl.Session = session
|
||||||
ul, err := dl.User.NewLogin(ctx, &database.UserLogin{
|
ul, err := dl.User.NewLogin(ctx, &database.UserLogin{
|
||||||
ID: networkid.UserLoginID(user.ID),
|
ID: discordid.MakeUserLoginID(user.ID),
|
||||||
Metadata: &discordid.UserLoginMetadata{
|
Metadata: &discordid.UserLoginMetadata{
|
||||||
Token: token,
|
Token: token,
|
||||||
HeartbeatSession: session.HeartbeatSession,
|
HeartbeatSession: session.HeartbeatSession,
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ import (
|
|||||||
"go.mau.fi/util/exhttp"
|
"go.mau.fi/util/exhttp"
|
||||||
"maunium.net/go/mautrix"
|
"maunium.net/go/mautrix"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
|
|
||||||
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
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) {
|
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
|
var resp respGuildsList
|
||||||
resp.Guilds = []guildEntry{}
|
resp.Guilds = []guildEntry{}
|
||||||
@@ -142,7 +144,7 @@ func (p *ProvisioningAPI) bridgeGuild(w http.ResponseWriter, r *http.Request, lo
|
|||||||
}
|
}
|
||||||
|
|
||||||
p.log.Info().
|
p.log.Info().
|
||||||
Str("login_id", string(login.ID)).
|
Str("login_id", discordid.ParseUserLoginID(login.ID)).
|
||||||
Str("guild_id", guildID).
|
Str("guild_id", guildID).
|
||||||
Msg("requested to bridge guild via provisioning api")
|
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().
|
p.log.Info().
|
||||||
Str("login_id", string(login.ID)).
|
Str("login_id", discordid.ParseUserLoginID(login.ID)).
|
||||||
Str("guild_id", guildID).
|
Str("guild_id", guildID).
|
||||||
Msg("requested to unbridge guild via provisioning api")
|
Msg("requested to unbridge guild via provisioning api")
|
||||||
|
|
||||||
|
|||||||
@@ -25,19 +25,21 @@ import (
|
|||||||
"go.mau.fi/util/ptr"
|
"go.mau.fi/util/ptr"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
"maunium.net/go/mautrix/bridgev2/networkid"
|
||||||
|
|
||||||
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (d *DiscordClient) IsThisUser(ctx context.Context, userID networkid.UserID) bool {
|
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
|
// 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.
|
// 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 {
|
func makeUserAvatar(u *discordgo.User) *bridgev2.Avatar {
|
||||||
url := u.AvatarURL("256")
|
url := u.AvatarURL("256")
|
||||||
|
|
||||||
return &bridgev2.Avatar{
|
return &bridgev2.Avatar{
|
||||||
ID: networkid.AvatarID(url),
|
ID: discordid.MakeAvatarID(url),
|
||||||
Get: func(ctx context.Context) ([]byte, error) {
|
Get: func(ctx context.Context) ([]byte, error) {
|
||||||
return simpleDownload(ctx, url, "user avatar")
|
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.
|
// 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 {
|
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
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,8 +21,85 @@ import (
|
|||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
"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) {
|
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 {
|
if wantReceiver {
|
||||||
key.Receiver = userLoginID
|
key.Receiver = userLoginID
|
||||||
}
|
}
|
||||||
@@ -30,6 +107,6 @@ func MakePortalKey(ch *discordgo.Channel, userLoginID networkid.UserLoginID, wan
|
|||||||
}
|
}
|
||||||
|
|
||||||
func MakePortalKeyWithID(channelID string) (key networkid.PortalKey) {
|
func MakePortalKeyWithID(channelID string) (key networkid.PortalKey) {
|
||||||
key.ID = networkid.PortalID(channelID)
|
key.ID = MakePortalID(channelID)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import (
|
|||||||
"github.com/yuin/goldmark/text"
|
"github.com/yuin/goldmark/text"
|
||||||
"github.com/yuin/goldmark/util"
|
"github.com/yuin/goldmark/util"
|
||||||
"maunium.net/go/mautrix/bridgev2"
|
"maunium.net/go/mautrix/bridgev2"
|
||||||
"maunium.net/go/mautrix/bridgev2/networkid"
|
|
||||||
"maunium.net/go/mautrix/id"
|
"maunium.net/go/mautrix/id"
|
||||||
|
|
||||||
"go.mau.fi/mautrix-discord/pkg/discordid"
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
||||||
@@ -271,7 +270,7 @@ func (r *discordTagHTMLRenderer) renderDiscordMention(w util.BufWriter, source [
|
|||||||
case *astDiscordUserMention:
|
case *astDiscordUserMention:
|
||||||
var mxid id.UserID
|
var mxid id.UserID
|
||||||
var name string
|
var name string
|
||||||
if ghost, _ := node.portal.Bridge.GetGhostByID(ctx, networkid.UserID(strconv.FormatInt(node.id, 10))); ghost != nil {
|
if ghost, _ := node.portal.Bridge.GetGhostByID(ctx, discordid.MakeUserID(strconv.FormatInt(node.id, 10))); ghost != nil {
|
||||||
mxid = ghost.Intent.GetMXID()
|
mxid = ghost.Intent.GetMXID()
|
||||||
name = ghost.Name
|
name = ghost.Name
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -179,7 +179,7 @@ func (mc *MessageConverter) tryAddingReplyToConvertedMessage(
|
|||||||
|
|
||||||
// The portal containing the message that was replied to.
|
// The portal containing the message that was replied to.
|
||||||
targetPortal := portal
|
targetPortal := portal
|
||||||
if ref.ChannelID != string(portal.ID) {
|
if ref.ChannelID != discordid.ParsePortalID(portal.ID) {
|
||||||
var err error
|
var err error
|
||||||
targetPortal, err = mc.Bridge.GetPortalByKey(ctx, discordid.MakePortalKeyWithID(ref.ChannelID))
|
targetPortal, err = mc.Bridge.GetPortalByKey(ctx, discordid.MakePortalKeyWithID(ref.ChannelID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -192,7 +192,7 @@ func (mc *MessageConverter) tryAddingReplyToConvertedMessage(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
messageID := networkid.MessageID(ref.MessageID)
|
messageID := discordid.MakeMessageID(ref.MessageID)
|
||||||
repliedToMatrixMsg, err := mc.Bridge.DB.Message.GetFirstPartByID(ctx, source.ID, messageID)
|
repliedToMatrixMsg, err := mc.Bridge.DB.Message.GetFirstPartByID(ctx, source.ID, messageID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Err(err).Msg("Failed to query database for first message part; proceeding")
|
log.Err(err).Msg("Failed to query database for first message part; proceeding")
|
||||||
@@ -229,7 +229,7 @@ func (mc *MessageConverter) renderDiscordTextMessage(ctx context.Context, intent
|
|||||||
var htmlParts []string
|
var htmlParts []string
|
||||||
|
|
||||||
if msg.Interaction != nil {
|
if msg.Interaction != nil {
|
||||||
ghost, err := mc.Bridge.GetGhostByID(ctx, networkid.UserID(msg.Interaction.User.ID))
|
ghost, err := mc.Bridge.GetGhostByID(ctx, discordid.MakeUserID(msg.Interaction.User.ID))
|
||||||
// TODO(skip): Try doing ghost.UpdateInfoIfNecessary.
|
// TODO(skip): Try doing ghost.UpdateInfoIfNecessary.
|
||||||
if err == nil {
|
if err == nil {
|
||||||
htmlParts = append(htmlParts, fmt.Sprintf(msgInteractionTemplateHTML, ghost.Intent.GetMXID(), ghost.Name, msg.Interaction.Name))
|
htmlParts = append(htmlParts, fmt.Sprintf(msgInteractionTemplateHTML, ghost.Intent.GetMXID(), ghost.Name, msg.Interaction.Name))
|
||||||
@@ -302,7 +302,7 @@ func (mc *MessageConverter) forwardedMessageHTMLPart(ctx context.Context, portal
|
|||||||
msgTSText := msg.MessageSnapshots[0].Message.Timestamp.Format("2006-01-02 15:04 MST")
|
msgTSText := msg.MessageSnapshots[0].Message.Timestamp.Format("2006-01-02 15:04 MST")
|
||||||
origLink := fmt.Sprintf("unknown channel • %s", msgTSText)
|
origLink := fmt.Sprintf("unknown channel • %s", msgTSText)
|
||||||
if forwardedFromPortal, err := mc.Bridge.DB.Portal.GetByKey(ctx, discordid.MakePortalKeyWithID(msg.MessageReference.ChannelID)); err == nil && forwardedFromPortal != nil {
|
if forwardedFromPortal, err := mc.Bridge.DB.Portal.GetByKey(ctx, discordid.MakePortalKeyWithID(msg.MessageReference.ChannelID)); err == nil && forwardedFromPortal != nil {
|
||||||
if origMessage, err := mc.Bridge.DB.Message.GetFirstPartByID(ctx, source.ID, networkid.MessageID(msg.MessageReference.MessageID)); err == nil && origMessage != nil {
|
if origMessage, err := mc.Bridge.DB.Message.GetFirstPartByID(ctx, source.ID, discordid.MakeMessageID(msg.MessageReference.MessageID)); err == nil && origMessage != nil {
|
||||||
// We've bridged the message that was forwarded, so we can link to it directly.
|
// We've bridged the message that was forwarded, so we can link to it directly.
|
||||||
origLink = fmt.Sprintf(
|
origLink = fmt.Sprintf(
|
||||||
`<a href="%s">#%s • %s</a>`,
|
`<a href="%s">#%s • %s</a>`,
|
||||||
|
|||||||
@@ -107,14 +107,14 @@ func (mc *MessageConverter) ToDiscord(
|
|||||||
|
|
||||||
if msg.ReplyTo != nil {
|
if msg.ReplyTo != nil {
|
||||||
req.Reference = &discordgo.MessageReference{
|
req.Reference = &discordgo.MessageReference{
|
||||||
ChannelID: string(msg.ReplyTo.Room.ID),
|
ChannelID: discordid.ParsePortalID(msg.ReplyTo.Room.ID),
|
||||||
MessageID: string(msg.ReplyTo.ID),
|
MessageID: discordid.ParseMessageID(msg.ReplyTo.ID),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
portal := msg.Portal
|
portal := msg.Portal
|
||||||
guildID := msg.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
guildID := msg.Portal.Metadata.(*discordid.PortalMetadata).GuildID
|
||||||
channelID := string(portal.ID)
|
channelID := discordid.ParsePortalID(portal.ID)
|
||||||
content := msg.Content
|
content := msg.Content
|
||||||
|
|
||||||
convertMatrix := func() {
|
convertMatrix := func() {
|
||||||
|
|||||||
Reference in New Issue
Block a user