2022-05-22 22:16:42 +03:00
|
|
|
// mautrix-discord - A Matrix-Discord puppeting bridge.
|
2026-01-08 16:55:34 -08:00
|
|
|
// Copyright (C) 2026 Tulir Asokan
|
2022-05-22 22:16:42 +03:00
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2024-08-15 16:43:13 +03:00
|
|
|
package connector
|
2022-05-22 22:16:42 +03:00
|
|
|
|
|
|
|
|
import (
|
2024-08-15 16:43:13 +03:00
|
|
|
"context"
|
2026-02-13 18:16:03 -08:00
|
|
|
"fmt"
|
2022-05-22 22:16:42 +03:00
|
|
|
|
2026-02-13 18:16:03 -08:00
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
"go.mau.fi/util/ptr"
|
2024-08-15 16:43:13 +03:00
|
|
|
"maunium.net/go/mautrix/bridgev2"
|
2026-02-13 18:16:03 -08:00
|
|
|
"maunium.net/go/mautrix/bridgev2/database"
|
|
|
|
|
"maunium.net/go/mautrix/bridgev2/networkid"
|
|
|
|
|
|
|
|
|
|
"go.mau.fi/mautrix-discord/pkg/discordid"
|
2022-05-22 22:16:42 +03:00
|
|
|
)
|
|
|
|
|
|
2026-02-13 18:16:03 -08:00
|
|
|
// getGuildSpaceInfo computes the [bridgev2.ChatInfo] for a guild space.
|
|
|
|
|
func (d *DiscordClient) getGuildSpaceInfo(_ctx context.Context, guild *discordgo.Guild) (*bridgev2.ChatInfo, error) {
|
|
|
|
|
selfEvtSender := d.selfEventSender()
|
|
|
|
|
|
|
|
|
|
return &bridgev2.ChatInfo{
|
|
|
|
|
Name: &guild.Name,
|
|
|
|
|
Topic: nil,
|
|
|
|
|
Members: &bridgev2.ChatMemberList{
|
|
|
|
|
MemberMap: map[networkid.UserID]bridgev2.ChatMember{
|
|
|
|
|
selfEvtSender.Sender: {EventSender: selfEvtSender},
|
|
|
|
|
},
|
|
|
|
|
// As recommended by the spec, prohibit normal events by setting
|
|
|
|
|
// events_default to a suitably high number.
|
|
|
|
|
PowerLevels: &bridgev2.PowerLevelOverrides{EventsDefault: ptr.Ptr(100)},
|
|
|
|
|
},
|
|
|
|
|
Avatar: d.makeAvatarForGuild(guild),
|
|
|
|
|
Type: ptr.Ptr(database.RoomTypeSpace),
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-11 18:13:00 -07:00
|
|
|
func portalIsPrivate(p *bridgev2.Portal) bool {
|
|
|
|
|
return p.RoomType == database.RoomTypeDM || p.RoomType == database.RoomTypeGroupDM
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 18:16:03 -08:00
|
|
|
func channelIsPrivate(ch *discordgo.Channel) bool {
|
|
|
|
|
return ch.Type == discordgo.ChannelTypeDM || ch.Type == discordgo.ChannelTypeGroupDM
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-30 21:42:52 -07:00
|
|
|
func readableChannelType(typ discordgo.ChannelType) (desc string) {
|
|
|
|
|
desc = "other"
|
|
|
|
|
|
|
|
|
|
switch typ {
|
|
|
|
|
case discordgo.ChannelTypeGuildText:
|
|
|
|
|
desc = "guild text"
|
|
|
|
|
case discordgo.ChannelTypeDM:
|
|
|
|
|
desc = "dm"
|
|
|
|
|
case discordgo.ChannelTypeGroupDM:
|
|
|
|
|
desc = "group dm"
|
|
|
|
|
case discordgo.ChannelTypeGuildPublicThread:
|
|
|
|
|
desc = "public thread"
|
|
|
|
|
case discordgo.ChannelTypeGuildPrivateThread:
|
|
|
|
|
desc = "private thread"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 18:16:03 -08:00
|
|
|
func (d *DiscordClient) makeAvatarForChannel(ctx context.Context, ch *discordgo.Channel) *bridgev2.Avatar {
|
2026-02-20 17:37:39 -08:00
|
|
|
if channelIsPrivate(ch) {
|
|
|
|
|
return &bridgev2.Avatar{
|
|
|
|
|
ID: discordid.MakeAvatarID(ch.Icon),
|
|
|
|
|
Get: func(ctx context.Context) ([]byte, error) {
|
|
|
|
|
url := discordgo.EndpointGroupIcon(ch.ID, ch.Icon)
|
|
|
|
|
return httpGet(ctx, d.httpClient, url, "channel/gdm icon")
|
|
|
|
|
},
|
|
|
|
|
Remove: ch.Icon == "",
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
if !d.connector.Config.GuildAvatarsInRoomsEnabled() {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 18:16:03 -08:00
|
|
|
guild, err := d.Session.State.Guild(ch.GuildID)
|
|
|
|
|
|
|
|
|
|
if err != nil || guild == nil {
|
|
|
|
|
zerolog.Ctx(ctx).Err(err).Msg("Couldn't look up guild in cache in order to create room avatar")
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.makeAvatarForGuild(guild)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DiscordClient) getPrivateChannelMemberList(ch *discordgo.Channel) bridgev2.ChatMemberList {
|
|
|
|
|
var members bridgev2.ChatMemberList
|
|
|
|
|
members.IsFull = true
|
|
|
|
|
members.MemberMap = make(bridgev2.ChatMemberMap, len(ch.Recipients))
|
|
|
|
|
|
|
|
|
|
if len(ch.Recipients) > 0 {
|
|
|
|
|
selfEventSender := d.selfEventSender()
|
|
|
|
|
|
|
|
|
|
// Private channels' array of participants doesn't include ourselves,
|
|
|
|
|
// so inject ourselves as a member.
|
|
|
|
|
members.MemberMap[selfEventSender.Sender] = bridgev2.ChatMember{EventSender: selfEventSender}
|
|
|
|
|
|
|
|
|
|
for _, recipient := range ch.Recipients {
|
|
|
|
|
sender := d.makeEventSender(recipient)
|
|
|
|
|
members.MemberMap[sender.Sender] = bridgev2.ChatMember{EventSender: sender}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
members.TotalMemberCount = len(ch.Recipients)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return members
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-18 22:32:37 -07:00
|
|
|
func (d *DiscordClient) getChannelNameParams(ch *discordgo.Channel) *ChannelNameParams {
|
|
|
|
|
params := &ChannelNameParams{
|
|
|
|
|
Name: ch.Name,
|
|
|
|
|
Type: ch.Type,
|
|
|
|
|
NSFW: ch.NSFW,
|
|
|
|
|
IsDM: ch.Type == discordgo.ChannelTypeDM,
|
|
|
|
|
IsGroupDM: ch.Type == discordgo.ChannelTypeGroupDM,
|
|
|
|
|
IsCategory: ch.Type == discordgo.ChannelTypeGuildCategory,
|
|
|
|
|
IsGuildChannel: ch.GuildID != "",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ch.ParentID != "" {
|
|
|
|
|
parent, err := d.Session.State.Channel(ch.ParentID)
|
|
|
|
|
if err == nil && parent != nil {
|
|
|
|
|
params.ParentName = parent.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ch.GuildID != "" {
|
|
|
|
|
guild, err := d.Session.State.Guild(ch.GuildID)
|
|
|
|
|
if err == nil && guild != nil {
|
|
|
|
|
params.GuildName = guild.Name
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return params
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (d *DiscordClient) getChannelName(ch *discordgo.Channel) *string {
|
|
|
|
|
if ch.Type == discordgo.ChannelTypeDM {
|
2026-04-08 23:31:59 -07:00
|
|
|
// Respect friend nicknames.
|
2026-04-20 20:33:58 -07:00
|
|
|
if len(ch.Recipients) > 0 {
|
|
|
|
|
if rel := d.relationshipWithUserID(ch.Recipients[0].ID); rel != nil && rel.Nickname != "" {
|
|
|
|
|
return &rel.Nickname
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// Impossible?
|
2026-04-08 23:31:59 -07:00
|
|
|
}
|
|
|
|
|
|
2026-03-18 22:32:37 -07:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
name := d.connector.Config.FormatChannelName(d.getChannelNameParams(ch))
|
|
|
|
|
return &name
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 18:16:03 -08:00
|
|
|
// getChannelChatInfo computes [bridgev2.ChatInfo] for a guild channel or private (DM or group DM) channel.
|
|
|
|
|
func (d *DiscordClient) getChannelChatInfo(ctx context.Context, ch *discordgo.Channel) (*bridgev2.ChatInfo, error) {
|
|
|
|
|
var roomType database.RoomType
|
|
|
|
|
switch ch.Type {
|
|
|
|
|
case discordgo.ChannelTypeGuildCategory:
|
|
|
|
|
roomType = database.RoomTypeSpace
|
|
|
|
|
case discordgo.ChannelTypeDM:
|
|
|
|
|
roomType = database.RoomTypeDM
|
|
|
|
|
case discordgo.ChannelTypeGroupDM:
|
|
|
|
|
roomType = database.RoomTypeGroupDM
|
|
|
|
|
default:
|
|
|
|
|
roomType = database.RoomTypeDefault
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parentPortalID *networkid.PortalID
|
|
|
|
|
if ch.Type == discordgo.ChannelTypeGuildCategory || (ch.ParentID == "" && ch.GuildID != "") {
|
|
|
|
|
// Categories and uncategorized guild channels always have the guild as their parent.
|
2026-02-13 21:06:37 -08:00
|
|
|
parentPortalID = ptr.Ptr(discordid.MakeGuildPortalIDWithID(ch.GuildID))
|
2026-02-13 18:16:03 -08:00
|
|
|
} else if ch.ParentID != "" {
|
|
|
|
|
// Categorized guild channels.
|
2026-02-13 21:06:37 -08:00
|
|
|
parentPortalID = ptr.Ptr(discordid.MakeChannelPortalIDWithID(ch.ParentID))
|
2026-02-13 18:16:03 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var memberList bridgev2.ChatMemberList
|
|
|
|
|
if channelIsPrivate(ch) {
|
|
|
|
|
memberList = d.getPrivateChannelMemberList(ch)
|
|
|
|
|
} else {
|
|
|
|
|
// TODO we're _always_ sending partial member lists for guilds; we can probably
|
|
|
|
|
// do better than that
|
|
|
|
|
selfEventSender := d.selfEventSender()
|
|
|
|
|
|
|
|
|
|
memberList = bridgev2.ChatMemberList{
|
|
|
|
|
IsFull: false,
|
|
|
|
|
MemberMap: map[networkid.UserID]bridgev2.ChatMember{
|
|
|
|
|
selfEventSender.Sender: {EventSender: selfEventSender},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &bridgev2.ChatInfo{
|
2026-03-18 22:32:37 -07:00
|
|
|
Name: d.getChannelName(ch),
|
2026-02-13 18:16:03 -08:00
|
|
|
Topic: &ch.Topic,
|
|
|
|
|
Avatar: d.makeAvatarForChannel(ctx, ch),
|
|
|
|
|
|
|
|
|
|
Members: &memberList,
|
|
|
|
|
|
|
|
|
|
Type: &roomType,
|
|
|
|
|
ParentID: parentPortalID,
|
|
|
|
|
|
2026-03-05 14:05:35 -08:00
|
|
|
UserLocal: &bridgev2.UserLocalPortalInfo{
|
|
|
|
|
MutedUntil: ptr.Ptr(d.channelMutedUntil(ch.GuildID, ch.ID)),
|
|
|
|
|
},
|
2026-02-13 18:16:03 -08:00
|
|
|
CanBackfill: true,
|
|
|
|
|
|
|
|
|
|
ExtraUpdates: func(ctx context.Context, portal *bridgev2.Portal) (changed bool) {
|
|
|
|
|
meta := portal.Metadata.(*discordid.PortalMetadata)
|
|
|
|
|
if meta.GuildID != ch.GuildID {
|
|
|
|
|
meta.GuildID = ch.GuildID
|
|
|
|
|
changed = true
|
|
|
|
|
}
|
2026-03-04 20:36:17 -08:00
|
|
|
if meta.ChannelType == nil || *meta.ChannelType != ch.Type {
|
|
|
|
|
meta.ChannelType = ptr.Ptr(ch.Type)
|
|
|
|
|
changed = true
|
|
|
|
|
}
|
2026-02-13 18:16:03 -08:00
|
|
|
|
|
|
|
|
return
|
|
|
|
|
},
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-15 16:43:13 +03:00
|
|
|
func (d *DiscordClient) GetChatInfo(ctx context.Context, portal *bridgev2.Portal) (*bridgev2.ChatInfo, error) {
|
2026-03-10 22:31:16 -07:00
|
|
|
if !d.IsLoggedIn() {
|
|
|
|
|
return nil, bridgev2.ErrNotLoggedIn
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-13 18:16:03 -08:00
|
|
|
guildID := discordid.ParseGuildPortalID(portal.ID)
|
|
|
|
|
if guildID != "" {
|
|
|
|
|
// Portal is a space representing a Discord guild.
|
|
|
|
|
|
|
|
|
|
guild, err := d.Session.State.Guild(guildID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't get guild: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.getGuildSpaceInfo(ctx, guild)
|
|
|
|
|
} else {
|
|
|
|
|
// Portal is to a channel of some kind (private or guild).
|
2026-02-13 21:06:37 -08:00
|
|
|
channelID := discordid.ParseChannelPortalID(portal.ID)
|
2026-02-13 18:16:03 -08:00
|
|
|
|
|
|
|
|
ch, err := d.Session.State.Channel(channelID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("couldn't get channel: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return d.getChannelChatInfo(ctx, ch)
|
|
|
|
|
}
|
2022-05-22 22:16:42 +03:00
|
|
|
}
|