2018-08-16 23:11:28 +02:00
|
|
|
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
|
2021-10-22 19:14:34 +02:00
|
|
|
// Copyright (C) 2021 Tulir Asokan
|
2018-08-16 23:11:28 +02: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/>.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-11 21:27:10 +01:00
|
|
|
"context"
|
2018-08-16 23:11:28 +02:00
|
|
|
"fmt"
|
|
|
|
"regexp"
|
2021-03-19 19:55:08 +01:00
|
|
|
"sync"
|
2021-10-27 18:30:34 +02:00
|
|
|
"time"
|
2018-08-24 18:46:14 +02:00
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
"github.com/rs/zerolog"
|
2021-10-22 19:14:34 +02:00
|
|
|
"go.mau.fi/whatsmeow/types"
|
2021-02-09 22:41:14 +01:00
|
|
|
|
2023-06-19 13:48:26 +02:00
|
|
|
"maunium.net/go/mautrix"
|
2020-05-09 13:31:06 +02:00
|
|
|
"maunium.net/go/mautrix/appservice"
|
2022-05-22 15:15:54 +02:00
|
|
|
"maunium.net/go/mautrix/bridge"
|
2020-05-08 21:32:22 +02:00
|
|
|
"maunium.net/go/mautrix/id"
|
2019-01-11 20:17:31 +01:00
|
|
|
|
2022-05-13 10:34:45 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/config"
|
2018-08-24 18:46:14 +02:00
|
|
|
"maunium.net/go/mautrix-whatsapp/database"
|
2018-08-16 23:11:28 +02:00
|
|
|
)
|
|
|
|
|
2020-06-25 22:33:11 +02:00
|
|
|
var userIDRegex *regexp.Regexp
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) ParsePuppetMXID(mxid id.UserID) (jid types.JID, ok bool) {
|
2020-06-25 22:33:11 +02:00
|
|
|
if userIDRegex == nil {
|
2022-11-05 10:29:11 +01:00
|
|
|
userIDRegex = br.Config.MakeUserIDRegex("([0-9]+)")
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
match := userIDRegex.FindStringSubmatch(string(mxid))
|
2021-10-22 19:14:34 +02:00
|
|
|
if len(match) == 2 {
|
|
|
|
jid = types.NewJID(match[1], types.DefaultUserServer)
|
|
|
|
ok = true
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
2021-10-22 19:14:34 +02:00
|
|
|
return
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) GetPuppetByMXID(mxid id.UserID) *Puppet {
|
|
|
|
jid, ok := br.ParsePuppetMXID(mxid)
|
2018-08-16 23:11:28 +02:00
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
return br.GetPuppetByJID(jid)
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) GetPuppetByJID(jid types.JID) *Puppet {
|
2024-03-11 21:27:10 +01:00
|
|
|
ctx := context.TODO()
|
2021-10-22 19:14:34 +02:00
|
|
|
jid = jid.ToNonAD()
|
|
|
|
if jid.Server == types.LegacyUserServer {
|
|
|
|
jid.Server = types.DefaultUserServer
|
|
|
|
} else if jid.Server != types.DefaultUserServer {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-22 00:06:30 +02:00
|
|
|
br.puppetsLock.Lock()
|
|
|
|
defer br.puppetsLock.Unlock()
|
|
|
|
puppet, ok := br.puppets[jid]
|
2018-08-16 23:11:28 +02:00
|
|
|
if !ok {
|
2024-03-11 21:27:10 +01:00
|
|
|
dbPuppet, err := br.DB.Puppet.Get(ctx, jid)
|
|
|
|
if err != nil {
|
|
|
|
br.ZLog.Err(err).Stringer("jid", jid).Msg("Failed to get puppet from database")
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-16 23:11:28 +02:00
|
|
|
if dbPuppet == nil {
|
2022-05-22 00:06:30 +02:00
|
|
|
dbPuppet = br.DB.Puppet.New()
|
2018-08-18 21:57:08 +02:00
|
|
|
dbPuppet.JID = jid
|
2024-03-11 21:27:10 +01:00
|
|
|
err = dbPuppet.Insert(ctx)
|
|
|
|
if err != nil {
|
|
|
|
br.ZLog.Err(err).Stringer("jid", jid).Msg("Failed to insert new puppet to database")
|
|
|
|
return nil
|
|
|
|
}
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
2022-05-22 00:06:30 +02:00
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppets[puppet.JID] = puppet
|
2019-05-24 01:33:26 +02:00
|
|
|
if len(puppet.CustomMXID) > 0 {
|
2022-05-22 00:06:30 +02:00
|
|
|
br.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
2019-05-24 01:33:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) GetPuppetByCustomMXID(mxid id.UserID) *Puppet {
|
|
|
|
br.puppetsLock.Lock()
|
|
|
|
defer br.puppetsLock.Unlock()
|
|
|
|
puppet, ok := br.puppetsByCustomMXID[mxid]
|
2019-05-24 01:33:26 +02:00
|
|
|
if !ok {
|
2024-03-11 21:27:10 +01:00
|
|
|
dbPuppet, err := br.DB.Puppet.GetByCustomMXID(context.TODO(), mxid)
|
|
|
|
if err != nil {
|
|
|
|
br.ZLog.Err(err).Stringer("mxid", mxid).Msg("Failed to get puppet by custom mxid from database")
|
|
|
|
}
|
2019-05-24 01:33:26 +02:00
|
|
|
if dbPuppet == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-22 00:06:30 +02:00
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppets[puppet.JID] = puppet
|
|
|
|
br.puppetsByCustomMXID[puppet.CustomMXID] = puppet
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
return puppet
|
|
|
|
}
|
|
|
|
|
2022-05-22 15:15:54 +02:00
|
|
|
func (user *User) GetIDoublePuppet() bridge.DoublePuppet {
|
|
|
|
p := user.bridge.GetPuppetByCustomMXID(user.MXID)
|
2022-05-22 18:11:08 +02:00
|
|
|
if p == nil || p.CustomIntent() == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (user *User) GetIGhost() bridge.Ghost {
|
|
|
|
if user.JID.IsEmpty() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
p := user.bridge.GetPuppetByJID(user.JID)
|
2022-05-22 15:15:54 +02:00
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *WABridge) IsGhost(id id.UserID) bool {
|
|
|
|
_, ok := br.ParsePuppetMXID(id)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
|
|
|
func (br *WABridge) GetIGhost(id id.UserID) bridge.Ghost {
|
|
|
|
p := br.GetPuppetByMXID(id)
|
|
|
|
if p == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2022-05-24 13:02:06 +02:00
|
|
|
func (puppet *Puppet) GetMXID() id.UserID {
|
|
|
|
return puppet.MXID
|
2022-05-22 15:15:54 +02:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) GetAllPuppetsWithCustomMXID() []*Puppet {
|
2024-03-11 21:27:10 +01:00
|
|
|
return br.dbPuppetsToPuppets(br.DB.Puppet.GetAllWithCustomMXID(context.TODO()))
|
2019-05-24 01:33:26 +02:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) GetAllPuppets() []*Puppet {
|
2024-03-11 21:27:10 +01:00
|
|
|
return br.dbPuppetsToPuppets(br.DB.Puppet.GetAll(context.TODO()))
|
2019-05-24 01:33:26 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (br *WABridge) dbPuppetsToPuppets(dbPuppets []*database.Puppet, err error) []*Puppet {
|
|
|
|
if err != nil {
|
|
|
|
br.ZLog.Err(err).Msg("Error getting puppets from database")
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-22 00:06:30 +02:00
|
|
|
br.puppetsLock.Lock()
|
|
|
|
defer br.puppetsLock.Unlock()
|
2018-08-16 23:11:28 +02:00
|
|
|
output := make([]*Puppet, len(dbPuppets))
|
|
|
|
for index, dbPuppet := range dbPuppets {
|
2019-06-13 20:30:38 +02:00
|
|
|
if dbPuppet == nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-05-22 00:06:30 +02:00
|
|
|
puppet, ok := br.puppets[dbPuppet.JID]
|
2018-08-16 23:11:28 +02:00
|
|
|
if !ok {
|
2022-05-22 00:06:30 +02:00
|
|
|
puppet = br.NewPuppet(dbPuppet)
|
|
|
|
br.puppets[dbPuppet.JID] = puppet
|
2019-05-30 16:22:03 +02:00
|
|
|
if len(dbPuppet.CustomMXID) > 0 {
|
2022-05-22 00:06:30 +02:00
|
|
|
br.puppetsByCustomMXID[dbPuppet.CustomMXID] = puppet
|
2019-05-24 01:33:26 +02:00
|
|
|
}
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
output[index] = puppet
|
|
|
|
}
|
|
|
|
return output
|
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) FormatPuppetMXID(jid types.JID) id.UserID {
|
2020-08-22 12:07:55 +02:00
|
|
|
return id.NewUserID(
|
2022-05-22 00:06:30 +02:00
|
|
|
br.Config.Bridge.FormatUsername(jid.User),
|
|
|
|
br.Config.Homeserver.Domain)
|
2020-08-22 12:07:55 +02:00
|
|
|
}
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
func (br *WABridge) NewPuppet(dbPuppet *database.Puppet) *Puppet {
|
2018-08-16 23:11:28 +02:00
|
|
|
return &Puppet{
|
|
|
|
Puppet: dbPuppet,
|
2022-05-22 00:06:30 +02:00
|
|
|
bridge: br,
|
2024-03-11 21:27:10 +01:00
|
|
|
zlog: br.ZLog.With().Stringer("puppet_jid", dbPuppet.JID).Logger(),
|
2018-08-18 21:57:08 +02:00
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
MXID: br.FormatPuppetMXID(dbPuppet.JID),
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Puppet struct {
|
|
|
|
*database.Puppet
|
|
|
|
|
2022-05-22 00:06:30 +02:00
|
|
|
bridge *WABridge
|
2024-03-11 21:27:10 +01:00
|
|
|
zlog zerolog.Logger
|
2018-08-18 21:57:08 +02:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
typingIn id.RoomID
|
2021-10-27 18:30:34 +02:00
|
|
|
typingAt time.Time
|
2018-08-24 19:02:38 +02:00
|
|
|
|
2020-05-08 21:32:22 +02:00
|
|
|
MXID id.UserID
|
2019-05-24 01:33:26 +02:00
|
|
|
|
2021-12-07 15:02:51 +01:00
|
|
|
customIntent *appservice.IntentAPI
|
|
|
|
customUser *User
|
2021-03-19 19:55:08 +01:00
|
|
|
|
|
|
|
syncLock sync.Mutex
|
2018-08-18 21:57:08 +02:00
|
|
|
}
|
|
|
|
|
2022-06-30 19:56:25 +02:00
|
|
|
var _ bridge.GhostWithProfile = (*Puppet)(nil)
|
|
|
|
|
|
|
|
func (puppet *Puppet) GetDisplayname() string {
|
|
|
|
return puppet.Displayname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) GetAvatarURL() id.ContentURI {
|
|
|
|
return puppet.AvatarURL
|
|
|
|
}
|
|
|
|
|
2019-05-24 01:33:26 +02:00
|
|
|
func (puppet *Puppet) IntentFor(portal *Portal) *appservice.IntentAPI {
|
2022-05-29 18:17:29 +02:00
|
|
|
if puppet.customIntent == nil || portal.Key.JID == puppet.JID || (portal.Key.JID.Server == types.BroadcastServer && portal.Key.Receiver != puppet.JID) {
|
2019-05-24 01:33:26 +02:00
|
|
|
return puppet.DefaultIntent()
|
|
|
|
}
|
|
|
|
return puppet.customIntent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) CustomIntent() *appservice.IntentAPI {
|
|
|
|
return puppet.customIntent
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI {
|
2018-08-28 23:40:54 +02:00
|
|
|
return puppet.bridge.AS.Intent(puppet.MXID)
|
2018-08-16 23:11:28 +02:00
|
|
|
}
|
2018-08-19 17:21:38 +02:00
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (puppet *Puppet) UpdateAvatar(ctx context.Context, source *User, forcePortalSync bool) bool {
|
|
|
|
changed := source.updateAvatar(ctx, puppet.JID, false, &puppet.Avatar, &puppet.AvatarURL, &puppet.AvatarSet, puppet.DefaultIntent())
|
2022-06-28 15:22:10 +02:00
|
|
|
if !changed || puppet.Avatar == "unauthorized" {
|
2022-07-05 10:11:43 +02:00
|
|
|
if forcePortalSync {
|
2024-03-11 21:27:10 +01:00
|
|
|
go puppet.updatePortalAvatar(ctx)
|
2022-07-05 10:11:43 +02:00
|
|
|
}
|
2022-06-28 15:22:10 +02:00
|
|
|
return changed
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
err := puppet.DefaultIntent().SetAvatarURL(ctx, puppet.AvatarURL)
|
2019-01-21 22:55:16 +01:00
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
zerolog.Ctx(ctx).Err(err).Msg("Failed to set avatar from puppet")
|
2022-06-28 13:37:49 +02:00
|
|
|
} else {
|
|
|
|
puppet.AvatarSet = true
|
2019-01-21 22:55:16 +01:00
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
go puppet.updatePortalAvatar(ctx)
|
2018-08-23 00:12:26 +02:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (puppet *Puppet) UpdateName(ctx context.Context, contact types.ContactInfo, forcePortalSync bool) bool {
|
2021-10-22 19:14:34 +02:00
|
|
|
newName, quality := puppet.bridge.Config.Bridge.FormatDisplayname(puppet.JID, contact)
|
2022-06-28 13:37:49 +02:00
|
|
|
if (puppet.Displayname != newName || !puppet.NameSet) && quality >= puppet.NameQuality {
|
2022-09-22 15:20:13 +02:00
|
|
|
oldName := puppet.Displayname
|
2022-06-28 13:37:49 +02:00
|
|
|
puppet.Displayname = newName
|
|
|
|
puppet.NameQuality = quality
|
|
|
|
puppet.NameSet = false
|
2024-03-11 21:27:10 +01:00
|
|
|
err := puppet.DefaultIntent().SetDisplayName(ctx, newName)
|
2018-08-23 00:12:26 +02:00
|
|
|
if err == nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
puppet.zlog.Debug().Str("old_name", oldName).Str("new_name", newName).Msg("Updated name")
|
2022-06-28 13:37:49 +02:00
|
|
|
puppet.NameSet = true
|
2024-03-11 21:27:10 +01:00
|
|
|
go puppet.updatePortalName(ctx)
|
2018-08-23 00:12:26 +02:00
|
|
|
} else {
|
2024-03-11 21:27:10 +01:00
|
|
|
puppet.zlog.Err(err).Msg("Failed to set displayname")
|
2018-08-23 00:12:26 +02:00
|
|
|
}
|
2019-06-01 19:03:29 +02:00
|
|
|
return true
|
2022-07-05 10:11:43 +02:00
|
|
|
} else if forcePortalSync {
|
2024-03-11 21:27:10 +01:00
|
|
|
go puppet.updatePortalName(ctx)
|
2019-06-01 19:03:29 +02:00
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (puppet *Puppet) UpdateContactInfo(ctx context.Context) bool {
|
2023-06-19 13:48:26 +02:00
|
|
|
if !puppet.bridge.SpecVersions.Supports(mautrix.BeeperFeatureArbitraryProfileMeta) {
|
2023-04-10 17:39:05 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if puppet.ContactInfoSet {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
contactInfo := map[string]any{
|
|
|
|
"com.beeper.bridge.identifiers": []string{
|
|
|
|
fmt.Sprintf("tel:+%s", puppet.JID.User),
|
|
|
|
fmt.Sprintf("whatsapp:%s", puppet.JID.String()),
|
|
|
|
},
|
2023-04-18 18:17:55 +02:00
|
|
|
"com.beeper.bridge.remote_id": puppet.JID.String(),
|
|
|
|
"com.beeper.bridge.service": "whatsapp",
|
|
|
|
"com.beeper.bridge.network": "whatsapp",
|
2023-04-10 17:39:05 +02:00
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
err := puppet.DefaultIntent().BeeperUpdateProfile(ctx, contactInfo)
|
2023-04-10 17:39:05 +02:00
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
puppet.zlog.Err(err).Msg("Failed to store custom contact info in profile")
|
2023-04-10 17:39:05 +02:00
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
puppet.ContactInfoSet = true
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-01 19:03:29 +02:00
|
|
|
func (puppet *Puppet) updatePortalMeta(meta func(portal *Portal)) {
|
2023-04-14 12:40:34 +02:00
|
|
|
for _, portal := range puppet.bridge.GetAllPortalsByJID(puppet.JID) {
|
|
|
|
// Get room create lock to prevent races between receiving contact info and room creation.
|
|
|
|
portal.roomCreateLock.Lock()
|
|
|
|
meta(portal)
|
|
|
|
portal.roomCreateLock.Unlock()
|
2019-06-01 19:03:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (puppet *Puppet) updatePortalAvatar(ctx context.Context) {
|
2019-06-01 19:03:29 +02:00
|
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
2023-04-14 12:40:34 +02:00
|
|
|
if portal.Avatar == puppet.Avatar && portal.AvatarURL == puppet.AvatarURL && (portal.AvatarSet || !portal.shouldSetDMRoomMetadata()) {
|
2022-07-05 10:02:54 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
portal.AvatarURL = puppet.AvatarURL
|
|
|
|
portal.Avatar = puppet.Avatar
|
|
|
|
portal.AvatarSet = false
|
2023-04-14 12:40:34 +02:00
|
|
|
if len(portal.MXID) > 0 && !portal.shouldSetDMRoomMetadata() {
|
2024-03-11 21:27:10 +01:00
|
|
|
portal.UpdateBridgeInfo(ctx)
|
2023-04-14 12:40:34 +02:00
|
|
|
} else if len(portal.MXID) > 0 {
|
2024-03-11 21:27:10 +01:00
|
|
|
_, err := portal.MainIntent().SetRoomAvatar(ctx, portal.MXID, puppet.AvatarURL)
|
2019-06-01 19:03:29 +02:00
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
portal.zlog.Err(err).Msg("Failed to set avatar from puppet")
|
2022-07-05 10:02:54 +02:00
|
|
|
} else {
|
|
|
|
portal.AvatarSet = true
|
2024-03-11 21:27:10 +01:00
|
|
|
portal.UpdateBridgeInfo(ctx)
|
2019-06-01 19:03:29 +02:00
|
|
|
}
|
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
err := portal.Update(ctx)
|
|
|
|
if err != nil {
|
|
|
|
portal.zlog.Err(err).Msg("Failed to save portal after updating avatar from puppet")
|
|
|
|
}
|
2019-06-01 19:03:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (puppet *Puppet) updatePortalName(ctx context.Context) {
|
2019-06-01 19:03:29 +02:00
|
|
|
puppet.updatePortalMeta(func(portal *Portal) {
|
2024-03-11 21:27:10 +01:00
|
|
|
portal.UpdateName(ctx, puppet.Displayname, types.EmptyJID, true)
|
2019-06-01 19:03:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (puppet *Puppet) SyncContact(ctx context.Context, source *User, onlyIfNoName, shouldHavePushName bool, reason string) {
|
2023-08-08 14:11:30 +02:00
|
|
|
if puppet == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-05-13 10:34:45 +02:00
|
|
|
if onlyIfNoName && len(puppet.Displayname) > 0 && (!shouldHavePushName || puppet.NameQuality > config.NameQualityPhone) {
|
2022-06-28 13:37:49 +02:00
|
|
|
source.EnqueuePuppetResync(puppet)
|
2021-03-19 19:55:08 +01:00
|
|
|
return
|
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
log := zerolog.Ctx(ctx).With().
|
|
|
|
Str("method", "Puppet.SyncContact").
|
|
|
|
Stringer("puppet_jid", puppet.JID).
|
|
|
|
Stringer("source_user_jid", source.JID).
|
|
|
|
Stringer("source_user_mxid", source.MXID).
|
|
|
|
Logger()
|
|
|
|
ctx = log.WithContext(ctx)
|
2021-03-19 19:55:08 +01:00
|
|
|
|
2021-10-22 19:14:34 +02:00
|
|
|
contact, err := source.Client.Store.Contacts.GetContact(puppet.JID)
|
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
log.Err(err).
|
|
|
|
Stringer("source_mxid", source.MXID).
|
|
|
|
Str("sync_reason", reason).
|
|
|
|
Msg("Failed to get contact info through user in SyncContact")
|
2021-10-22 19:14:34 +02:00
|
|
|
} else if !contact.Found {
|
2024-03-11 21:27:10 +01:00
|
|
|
log.Warn().
|
|
|
|
Stringer("source_mxid", source.MXID).
|
|
|
|
Str("sync_reason", reason).
|
|
|
|
Msg("No contact info found through user in SyncContact")
|
2021-03-19 19:55:08 +01:00
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
puppet.syncInternal(ctx, source, &contact, false, false)
|
2021-03-19 19:55:08 +01:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
func (puppet *Puppet) Sync(ctx context.Context, source *User, contact *types.ContactInfo, forceAvatarSync, forcePortalSync bool) {
|
|
|
|
log := zerolog.Ctx(ctx).With().
|
|
|
|
Str("method", "Puppet.Sync").
|
|
|
|
Stringer("puppet_jid", puppet.JID).
|
|
|
|
Stringer("source_user_jid", source.JID).
|
|
|
|
Stringer("source_user_mxid", source.MXID).
|
|
|
|
Logger()
|
|
|
|
ctx = log.WithContext(ctx)
|
|
|
|
puppet.syncInternal(ctx, source, contact, forceAvatarSync, forcePortalSync)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (puppet *Puppet) syncInternal(ctx context.Context, source *User, contact *types.ContactInfo, forceAvatarSync, forcePortalSync bool) {
|
|
|
|
log := zerolog.Ctx(ctx)
|
2021-03-19 19:55:08 +01:00
|
|
|
puppet.syncLock.Lock()
|
|
|
|
defer puppet.syncLock.Unlock()
|
2024-03-11 21:27:10 +01:00
|
|
|
err := puppet.DefaultIntent().EnsureRegistered(ctx)
|
2019-06-01 19:03:29 +02:00
|
|
|
if err != nil {
|
2024-03-11 21:27:10 +01:00
|
|
|
log.Err(err).Msg("Failed to ensure registered")
|
2019-06-01 19:03:29 +02:00
|
|
|
}
|
|
|
|
|
2024-03-11 21:27:10 +01:00
|
|
|
log.Debug().Stringer("source_jid", source.JID).Msg("Syncing info through user")
|
2018-08-23 00:12:26 +02:00
|
|
|
|
2019-06-01 19:03:29 +02:00
|
|
|
update := false
|
2022-06-28 13:37:49 +02:00
|
|
|
if contact != nil {
|
|
|
|
if puppet.JID.User == source.JID.User {
|
|
|
|
contact.PushName = source.Client.Store.PushName
|
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
update = puppet.UpdateName(ctx, *contact, forcePortalSync) || update
|
2022-06-28 13:37:49 +02:00
|
|
|
}
|
|
|
|
if len(puppet.Avatar) == 0 || forceAvatarSync || puppet.bridge.Config.Bridge.UserAvatarSync {
|
2024-03-11 21:27:10 +01:00
|
|
|
update = puppet.UpdateAvatar(ctx, source, forcePortalSync) || update
|
2021-02-09 22:41:14 +01:00
|
|
|
}
|
2024-03-11 21:27:10 +01:00
|
|
|
update = puppet.UpdateContactInfo(ctx) || update
|
2022-06-28 13:37:49 +02:00
|
|
|
if update || puppet.LastSync.Add(24*time.Hour).Before(time.Now()) {
|
|
|
|
puppet.LastSync = time.Now()
|
2024-03-11 21:27:10 +01:00
|
|
|
err = puppet.Update(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("Failed to save puppet after sync")
|
|
|
|
}
|
2018-08-19 17:21:38 +02:00
|
|
|
}
|
|
|
|
}
|