mautrix-discord/puppet.go

372 lines
9.1 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"regexp"
"strings"
"sync"
2022-05-28 20:03:24 +00:00
"github.com/bwmarrin/discordgo"
"github.com/rs/zerolog"
2022-05-28 20:03:24 +00:00
"maunium.net/go/mautrix/appservice"
"maunium.net/go/mautrix/bridge"
"maunium.net/go/mautrix/bridge/bridgeconfig"
"maunium.net/go/mautrix/id"
"go.mau.fi/mautrix-discord/database"
)
type Puppet struct {
*database.Puppet
bridge *DiscordBridge
log zerolog.Logger
MXID id.UserID
customIntent *appservice.IntentAPI
customUser *User
syncLock sync.Mutex
}
var _ bridge.Ghost = (*Puppet)(nil)
2022-06-30 19:00:12 +00:00
var _ bridge.GhostWithProfile = (*Puppet)(nil)
func (puppet *Puppet) GetMXID() id.UserID {
return puppet.MXID
}
var userIDRegex *regexp.Regexp
func (br *DiscordBridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
return &Puppet{
Puppet: dbPuppet,
bridge: br,
log: br.ZLog.With().Str("discord_user_id", dbPuppet.ID).Logger(),
MXID: br.FormatPuppetMXID(dbPuppet.ID),
}
}
func (br *DiscordBridge) ParsePuppetMXID(mxid id.UserID) (string, bool) {
if userIDRegex == nil {
pattern := fmt.Sprintf(
"^@%s:%s$",
br.Config.Bridge.FormatUsername("([0-9]+)"),
br.Config.Homeserver.Domain,
)
userIDRegex = regexp.MustCompile(pattern)
}
match := userIDRegex.FindStringSubmatch(string(mxid))
if len(match) == 2 {
return match[1], true
}
return "", false
}
func (br *DiscordBridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
2022-05-28 20:03:24 +00:00
discordID, ok := br.ParsePuppetMXID(mxid)
if !ok {
return nil
}
2022-05-28 20:03:24 +00:00
return br.GetPuppetByID(discordID)
}
func (br *DiscordBridge) GetPuppetByID(id string) *Puppet {
br.puppetsLock.Lock()
defer br.puppetsLock.Unlock()
puppet, ok := br.puppets[id]
if !ok {
dbPuppet := br.DB.Puppet.Get(id)
if dbPuppet == nil {
dbPuppet = br.DB.Puppet.New()
dbPuppet.ID = id
dbPuppet.Insert()
}
puppet = br.NewPuppet(dbPuppet)
br.puppets[puppet.ID] = puppet
}
return puppet
}
func (br *DiscordBridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
br.puppetsLock.Lock()
defer br.puppetsLock.Unlock()
puppet, ok := br.puppetsByCustomMXID[mxid]
if !ok {
dbPuppet := br.DB.Puppet.GetByCustomMXID(mxid)
if dbPuppet == nil {
return nil
}
puppet = br.NewPuppet(dbPuppet)
br.puppets[puppet.ID] = puppet
br.puppetsByCustomMXID[puppet.CustomMXID] = puppet
}
return puppet
}
func (br *DiscordBridge) GetAllPuppetsWithCustomMXID() []*Puppet {
return br.dbPuppetsToPuppets(br.DB.Puppet.GetAllWithCustomMXID())
}
func (br *DiscordBridge) GetAllPuppets() []*Puppet {
return br.dbPuppetsToPuppets(br.DB.Puppet.GetAll())
}
func (br *DiscordBridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet) []*Puppet {
br.puppetsLock.Lock()
defer br.puppetsLock.Unlock()
output := make([]*Puppet, len(dbPuppets))
for index, dbPuppet := range dbPuppets {
if dbPuppet == nil {
continue
}
puppet, ok := br.puppets[dbPuppet.ID]
if !ok {
puppet = br.NewPuppet(dbPuppet)
br.puppets[dbPuppet.ID] = puppet
if dbPuppet.CustomMXID != "" {
br.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
}
}
output[index] = puppet
}
return output
}
func (br *DiscordBridge) FormatPuppetMXID(did string) id.UserID {
return id.NewUserID(
br.Config.Bridge.FormatUsername(did),
br.Config.Homeserver.Domain,
)
}
func (br *DiscordBridge) updatePuppetsContactInfo() {
if br.Config.Homeserver.Software != bridgeconfig.SoftwareHungry {
return
}
for _, puppet := range br.GetAllPuppets() {
if !puppet.ContactInfoSet && puppet.NameSet {
puppet.ResendContactInfo()
puppet.Update()
}
}
}
2022-06-30 19:00:12 +00:00
func (puppet *Puppet) GetDisplayname() string {
return puppet.Name
}
func (puppet *Puppet) GetAvatarURL() id.ContentURI {
return puppet.AvatarURL
}
func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
return puppet.bridge.AS.Intent(puppet.MXID)
}
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
2022-05-28 20:03:24 +00:00
if puppet.customIntent == nil || (portal.Key.Receiver != "" && portal.Key.Receiver != puppet.ID) {
return puppet.DefaultIntent()
}
return puppet.customIntent
}
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
if puppet == nil {
return nil
}
return puppet.customIntent
}
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
for _, portal := range puppet.bridge.GetDMPortalsWith(puppet.ID) {
// Get room create lock to prevent races between receiving contact info and room creation.
portal.roomCreateLock.Lock()
meta(portal)
portal.roomCreateLock.Unlock()
}
}
2022-05-28 20:03:24 +00:00
func (puppet *Puppet) UpdateName(info *discordgo.User) bool {
newName := puppet.bridge.Config.Bridge.FormatDisplayname(info, puppet.IsWebhook)
2022-05-28 20:03:24 +00:00
if puppet.Name == newName && puppet.NameSet {
return false
}
2022-05-28 20:03:24 +00:00
puppet.Name = newName
puppet.NameSet = false
err := puppet.DefaultIntent().SetDisplayName(newName)
if err != nil {
puppet.log.Warn().Err(err).Msg("Failed to update displayname")
2022-05-28 20:03:24 +00:00
} else {
go puppet.updatePortalMeta(func(portal *Portal) {
2023-04-21 23:50:14 +00:00
if portal.UpdateNameDirect(puppet.Name, false) {
2022-05-28 20:03:24 +00:00
portal.Update()
portal.UpdateBridgeInfo()
2022-05-28 20:03:24 +00:00
}
})
puppet.NameSet = true
}
2022-05-28 20:03:24 +00:00
return true
}
func (br *DiscordBridge) reuploadUserAvatar(intent *appservice.IntentAPI, userID, avatarID string) (id.ContentURI, error) {
downloadURL := discordgo.EndpointUserAvatar(userID, avatarID)
ext := "png"
if strings.HasPrefix(avatarID, "a_") {
downloadURL = discordgo.EndpointUserAvatarAnimated(userID, avatarID)
ext = "gif"
}
url := br.Config.Bridge.MediaPatterns.Avatar(userID, avatarID, ext)
if !url.IsEmpty() {
return url, nil
}
copied, err := br.copyAttachmentToMatrix(intent, downloadURL, false, AttachmentMeta{
AttachmentID: fmt.Sprintf("avatar/%s/%s", userID, avatarID),
})
if err != nil {
return url, err
}
return copied.MXC, nil
}
2022-05-28 20:03:24 +00:00
func (puppet *Puppet) UpdateAvatar(info *discordgo.User) bool {
avatarID := info.Avatar
if puppet.IsWebhook && !puppet.bridge.Config.Bridge.EnableWebhookAvatars {
avatarID = ""
}
if puppet.Avatar == avatarID && puppet.AvatarSet {
return false
}
avatarChanged := avatarID != puppet.Avatar
puppet.Avatar = avatarID
2022-05-28 20:03:24 +00:00
puppet.AvatarSet = false
2022-05-30 21:34:21 +00:00
puppet.AvatarURL = id.ContentURI{}
2022-05-30 21:34:21 +00:00
if puppet.Avatar != "" && (puppet.AvatarURL.IsEmpty() || avatarChanged) {
url, err := puppet.bridge.reuploadUserAvatar(puppet.DefaultIntent(), info.ID, puppet.Avatar)
if err != nil {
puppet.log.Warn().Err(err).Str("avatar_id", puppet.Avatar).Msg("Failed to reupload user avatar")
return true
2022-05-28 20:03:24 +00:00
}
puppet.AvatarURL = url
}
2022-05-28 20:03:24 +00:00
err := puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
if err != nil {
puppet.log.Warn().Err(err).Msg("Failed to update avatar")
2022-05-28 20:03:24 +00:00
} else {
go puppet.updatePortalMeta(func(portal *Portal) {
if portal.UpdateAvatarFromPuppet(puppet) {
portal.Update()
portal.UpdateBridgeInfo()
2022-05-28 20:03:24 +00:00
}
})
puppet.AvatarSet = true
}
return true
}
func (puppet *Puppet) UpdateInfo(source *User, info *discordgo.User, webhookID string) {
puppet.syncLock.Lock()
defer puppet.syncLock.Unlock()
if info == nil || len(info.Username) == 0 || len(info.Discriminator) == 0 {
if puppet.Name != "" || source == nil {
return
}
var err error
puppet.log.Debug().Str("source_user", source.DiscordID).Msg("Fetching info through user to update puppet")
info, err = source.Session.User(puppet.ID)
if err != nil {
puppet.log.Error().Err(err).Str("source_user", source.DiscordID).Msg("Failed to fetch info through user")
return
}
2022-05-28 20:03:24 +00:00
}
err := puppet.DefaultIntent().EnsureRegistered()
if err != nil {
puppet.log.Error().Err(err).Msg("Failed to ensure registered")
}
changed := false
if webhookID != "" && webhookID == info.ID && !puppet.IsWebhook {
puppet.IsWebhook = true
changed = true
}
changed = puppet.UpdateContactInfo(info) || changed
2022-05-28 20:03:24 +00:00
changed = puppet.UpdateName(info) || changed
changed = puppet.UpdateAvatar(info) || changed
if changed {
puppet.Update()
}
}
func (puppet *Puppet) UpdateContactInfo(info *discordgo.User) bool {
changed := false
if puppet.Username != info.Username {
puppet.Username = info.Username
changed = true
}
if puppet.GlobalName != info.GlobalName {
puppet.GlobalName = info.GlobalName
changed = true
}
if puppet.Discriminator != info.Discriminator {
puppet.Discriminator = info.Discriminator
changed = true
}
if puppet.IsBot != info.Bot {
puppet.IsBot = info.Bot
changed = true
}
if (changed && !puppet.IsWebhook) || !puppet.ContactInfoSet {
puppet.ContactInfoSet = false
puppet.ResendContactInfo()
return true
}
return false
}
func (puppet *Puppet) ResendContactInfo() {
if puppet.bridge.Config.Homeserver.Software != bridgeconfig.SoftwareHungry || puppet.ContactInfoSet {
return
}
contactInfo := map[string]any{
"com.beeper.bridge.identifiers": []string{
fmt.Sprintf("discord:%s#%s", puppet.Username, puppet.Discriminator),
},
"com.beeper.bridge.remote_id": puppet.ID,
"com.beeper.bridge.service": puppet.bridge.BeeperServiceName,
"com.beeper.bridge.network": puppet.bridge.BeeperNetworkName,
"com.beeper.bridge.is_network_bot": puppet.IsBot,
}
if puppet.IsWebhook {
contactInfo["com.beeper.bridge.identifiers"] = []string{}
}
err := puppet.DefaultIntent().BeeperUpdateProfile(contactInfo)
if err != nil {
puppet.log.Warn().Err(err).Msg("Failed to store custom contact info in profile")
} else {
puppet.ContactInfoSet = true
}
}