mirror of
https://github.com/mautrix/discord.git
synced 2025-03-14 14:15:37 +00:00
299 lines
6.2 KiB
Go
299 lines
6.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"regexp"
|
|
"sync"
|
|
|
|
log "maunium.net/go/maulogger/v2"
|
|
|
|
"maunium.net/go/mautrix/appservice"
|
|
"maunium.net/go/mautrix/bridge"
|
|
"maunium.net/go/mautrix/id"
|
|
|
|
"go.mau.fi/mautrix-discord/database"
|
|
)
|
|
|
|
type Puppet struct {
|
|
*database.Puppet
|
|
|
|
bridge *DiscordBridge
|
|
log log.Logger
|
|
|
|
MXID id.UserID
|
|
|
|
customIntent *appservice.IntentAPI
|
|
customUser *User
|
|
|
|
syncLock sync.Mutex
|
|
}
|
|
|
|
var _ bridge.Ghost = (*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.Log.Sub(fmt.Sprintf("Puppet/%s", dbPuppet.ID)),
|
|
|
|
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 {
|
|
id, ok := br.ParsePuppetMXID(mxid)
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
return br.GetPuppetByID(id)
|
|
}
|
|
|
|
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 (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
|
|
return puppet.bridge.AS.Intent(puppet.MXID)
|
|
}
|
|
|
|
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
|
|
if puppet.customIntent == nil {
|
|
return puppet.DefaultIntent()
|
|
}
|
|
|
|
return puppet.customIntent
|
|
}
|
|
|
|
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
|
|
return puppet.customIntent
|
|
}
|
|
|
|
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
|
|
for _, portal := range puppet.bridge.GetAllPortalsByID(puppet.ID) {
|
|
// Get room create lock to prevent races between receiving contact info and room creation.
|
|
portal.roomCreateLock.Lock()
|
|
meta(portal)
|
|
portal.roomCreateLock.Unlock()
|
|
}
|
|
}
|
|
|
|
func (puppet *Puppet) updateName(source *User) bool {
|
|
user, err := source.Session.User(puppet.ID)
|
|
if err != nil {
|
|
puppet.log.Warnln("failed to get user from id:", err)
|
|
return false
|
|
}
|
|
|
|
newName := puppet.bridge.Config.Bridge.FormatDisplayname(user)
|
|
|
|
if puppet.DisplayName != newName {
|
|
err := puppet.DefaultIntent().SetDisplayName(newName)
|
|
if err == nil {
|
|
puppet.DisplayName = newName
|
|
go puppet.updatePortalName()
|
|
puppet.Update()
|
|
} else {
|
|
puppet.log.Warnln("failed to set display name:", err)
|
|
}
|
|
|
|
return true
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func (puppet *Puppet) updatePortalName() {
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
|
if portal.MXID != "" {
|
|
_, err := portal.MainIntent().SetRoomName(portal.MXID, puppet.DisplayName)
|
|
if err != nil {
|
|
portal.log.Warnln("Failed to set name:", err)
|
|
}
|
|
}
|
|
|
|
portal.Name = puppet.DisplayName
|
|
portal.Update()
|
|
})
|
|
}
|
|
|
|
func (puppet *Puppet) updateAvatar(source *User) bool {
|
|
user, err := source.Session.User(puppet.ID)
|
|
if err != nil {
|
|
puppet.log.Warnln("Failed to get user:", err)
|
|
|
|
return false
|
|
}
|
|
|
|
if puppet.Avatar == user.Avatar {
|
|
return false
|
|
}
|
|
|
|
if user.Avatar == "" {
|
|
puppet.log.Warnln("User does not have an avatar")
|
|
|
|
return false
|
|
}
|
|
|
|
url, err := uploadAvatar(puppet.DefaultIntent(), user.AvatarURL(""))
|
|
if err != nil {
|
|
puppet.log.Warnln("Failed to upload user avatar:", err)
|
|
|
|
return false
|
|
}
|
|
|
|
puppet.AvatarURL = url
|
|
|
|
err = puppet.DefaultIntent().SetAvatarURL(puppet.AvatarURL)
|
|
if err != nil {
|
|
puppet.log.Warnln("Failed to set avatar:", err)
|
|
}
|
|
|
|
puppet.log.Debugln("Updated avatar", puppet.Avatar, "->", user.Avatar)
|
|
puppet.Avatar = user.Avatar
|
|
go puppet.updatePortalAvatar()
|
|
|
|
return true
|
|
}
|
|
|
|
func (puppet *Puppet) updatePortalAvatar() {
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
|
if portal.MXID != "" {
|
|
_, err := portal.MainIntent().SetRoomAvatar(portal.MXID, puppet.AvatarURL)
|
|
if err != nil {
|
|
portal.log.Warnln("Failed to set avatar:", err)
|
|
}
|
|
}
|
|
|
|
portal.AvatarURL = puppet.AvatarURL
|
|
portal.Avatar = puppet.Avatar
|
|
portal.Update()
|
|
})
|
|
|
|
}
|
|
|
|
func (puppet *Puppet) SyncContact(source *User) {
|
|
puppet.syncLock.Lock()
|
|
defer puppet.syncLock.Unlock()
|
|
|
|
puppet.log.Debugln("syncing contact", puppet.DisplayName)
|
|
|
|
err := puppet.DefaultIntent().EnsureRegistered()
|
|
if err != nil {
|
|
puppet.log.Errorln("Failed to ensure registered:", err)
|
|
}
|
|
|
|
update := false
|
|
|
|
update = puppet.updateName(source) || update
|
|
|
|
if puppet.Avatar == "" {
|
|
update = puppet.updateAvatar(source) || update
|
|
puppet.log.Debugln("update avatar returned", update)
|
|
}
|
|
|
|
if update {
|
|
puppet.Update()
|
|
}
|
|
}
|