userinfo: refactor GetUserInfo and remote profile handling

This commit is contained in:
Tulir Asokan
2025-12-04 15:39:46 +02:00
parent 2cac8f8b4a
commit 526903cb7c
5 changed files with 201 additions and 152 deletions
+35 -100
View File
@@ -450,31 +450,57 @@ func (t *TelegramClient) onPing() {
}
}
func userToRemoteProfile(self *tg.User) (profile status.RemoteProfile, name string) {
func userToRemoteProfile(
self *tg.User,
ghost *bridgev2.Ghost,
prevState *status.RemoteProfile,
) (profile status.RemoteProfile, name string) {
profile.Name = util.FormatFullName(self.FirstName, self.LastName, self.Deleted, self.ID)
profile.Phone = "+" + strings.TrimPrefix(self.Phone, "+")
if self.Phone != "" {
profile.Phone = "+" + strings.TrimPrefix(self.Phone, "+")
} else if prevState != nil {
profile.Phone = prevState.Phone
}
profile.Username = self.Username
if self.Username == "" && len(self.Usernames) > 0 {
profile.Username = self.Usernames[0].Username
}
if ghost != nil {
profile.Avatar = ghost.AvatarMXC
} else if prevState != nil {
profile.Avatar = prevState.Avatar
}
name = cmp.Or(profile.Username, profile.Phone, profile.Name)
return
}
func (t *TelegramClient) onConnected(self *tg.User) {
// TODO update ghost info?
newProfile, newName := userToRemoteProfile(self)
// TODO fill avatar from ghost or something?
newProfile.Avatar = t.userLogin.RemoteProfile.Avatar
newProfile.AvatarFile = t.userLogin.RemoteProfile.AvatarFile
func (t *TelegramClient) updateRemoteProfile(ctx context.Context, self *tg.User, ghost *bridgev2.Ghost) bool {
newProfile, newName := userToRemoteProfile(self, ghost, &t.userLogin.RemoteProfile)
if t.userLogin.RemoteProfile != newProfile || t.userLogin.RemoteName != newName {
t.userLogin.RemoteProfile = newProfile
t.userLogin.RemoteName = newName
err := t.userLogin.Save(t.main.Bridge.BackgroundCtx)
err := t.userLogin.Save(ctx)
if err != nil {
t.userLogin.Log.Err(err).Msg("Failed to save user login after profile update")
}
return true
}
return false
}
func (t *TelegramClient) onConnected(self *tg.User) {
log := t.userLogin.Log
ctx := log.WithContext(t.main.Bridge.BackgroundCtx)
ghost, err := t.main.Bridge.GetGhostByID(ctx, t.userID)
if err != nil {
log.Err(err).Msg("Failed to get own ghost")
} else if wrapped, err := t.wrapUserInfo(ctx, self); err != nil {
log.Err(err).Msg("Failed to wrap own user info")
} else {
ghost.UpdateInfo(ctx, wrapped)
}
t.updateRemoteProfile(ctx, self, ghost)
t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
}
@@ -592,97 +618,6 @@ func (t *TelegramClient) getSingleChannel(ctx context.Context, id int64) (*tg.Ch
}
}
func (t *TelegramClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.UserInfo, error) {
peerType, id, err := ids.ParseUserID(ghost.ID)
if err != nil {
return nil, err
}
switch peerType {
case ids.PeerTypeUser:
if user, err := t.getSingleUser(ctx, id); err != nil {
return nil, fmt.Errorf("failed to get user %d: %w", id, err)
} else if user.TypeID() != tg.UserTypeID {
return nil, err
} else {
return t.updateGhost(ctx, id, user.(*tg.User))
}
case ids.PeerTypeChannel:
if channel, err := t.getSingleChannel(ctx, id); err != nil {
return nil, fmt.Errorf("failed to get channel %d: %w", id, err)
} else if channel.TypeID() != tg.ChannelTypeID {
return nil, err
} else {
return t.updateChannel(ctx, channel)
}
default:
return nil, fmt.Errorf("unexpected peer type: %s", peerType)
}
}
func (t *TelegramClient) getUserInfoFromTelegramUser(ctx context.Context, u tg.UserClass) (*bridgev2.UserInfo, error) {
user, ok := u.(*tg.User)
if !ok {
return nil, fmt.Errorf("user is %T not *tg.User", user)
}
var identifiers []string
if !user.Min {
if accessHash, ok := user.GetAccessHash(); ok {
if err := t.ScopedStore.SetAccessHash(ctx, ids.PeerTypeUser, user.ID, accessHash); err != nil {
return nil, err
}
}
if err := t.ScopedStore.SetUsername(ctx, ids.PeerTypeUser, user.ID, user.Username); err != nil {
return nil, err
}
if user.Username != "" {
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", user.Username))
}
for _, username := range user.Usernames {
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", username.Username))
}
if phone, ok := user.GetPhone(); ok {
normalized := strings.TrimPrefix(phone, "+")
identifiers = append(identifiers, fmt.Sprintf("tel:+%s", normalized))
if err := t.ScopedStore.SetPhoneNumber(ctx, user.ID, normalized); err != nil {
return nil, err
}
}
}
slices.Sort(identifiers)
identifiers = slices.Compact(identifiers)
var avatar *bridgev2.Avatar
if p, ok := user.GetPhoto(); ok && p.TypeID() == tg.UserProfilePhotoTypeID {
photo := p.(*tg.UserProfilePhoto)
var err error
avatar, err = t.convertUserProfilePhoto(ctx, user.ID, photo)
if err != nil {
return nil, err
}
}
name := util.FormatFullName(user.FirstName, user.LastName, user.Deleted, user.ID)
return &bridgev2.UserInfo{
IsBot: &user.Bot,
Name: &name,
Avatar: avatar,
Identifiers: identifiers,
ExtraUpdates: func(ctx context.Context, ghost *bridgev2.Ghost) (changed bool) {
meta := ghost.Metadata.(*GhostMetadata)
if !user.Min {
changed = changed || meta.IsPremium != user.Premium || meta.IsBot != user.Bot || meta.IsContact != user.Contact
meta.IsPremium = user.Premium
meta.IsBot = user.Bot
meta.IsContact = user.Contact
meta.Deleted = user.Deleted
}
return changed
},
}, nil
}
func (t *TelegramClient) IsLoggedIn() bool {
if t == nil || t.clientCtx == nil {
return false
+23 -49
View File
@@ -659,52 +659,6 @@ func (t *TelegramClient) getPeerSender(peer tg.PeerClass) bridgev2.EventSender {
}
}
func (t *TelegramClient) maybeUpdateRemoteProfile(ctx context.Context, ghost *bridgev2.Ghost, user *tg.User) error {
if ghost.ID != t.userID {
return nil
}
var changed bool
if user != nil {
fullName := util.FormatFullName(user.FirstName, user.LastName, user.Deleted, user.ID)
username := user.Username
if username == "" && len(user.Usernames) > 0 {
username = user.Usernames[0].Username
}
normalizedPhone := "+" + strings.TrimPrefix(user.Phone, "+")
remoteName := username
if remoteName == "" {
remoteName = normalizedPhone
}
if remoteName == "" {
remoteName = fullName
}
changed = t.userLogin.RemoteName != remoteName ||
t.userLogin.RemoteProfile.Phone != normalizedPhone ||
t.userLogin.RemoteProfile.Username != username ||
t.userLogin.RemoteProfile.Name != fullName
t.userLogin.RemoteName = remoteName
t.userLogin.RemoteProfile.Phone = normalizedPhone
t.userLogin.RemoteProfile.Username = username
t.userLogin.RemoteProfile.Name = fullName
} else {
changed = t.userLogin.RemoteName != ghost.Name
t.userLogin.RemoteProfile.Name = ghost.Name
}
changed = changed || t.userLogin.RemoteProfile.Avatar != ghost.AvatarMXC
t.userLogin.RemoteProfile.Avatar = ghost.AvatarMXC
if changed {
if err := t.userLogin.Save(ctx); err != nil {
return err
}
t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
}
return nil
}
func (t *TelegramClient) onUserName(ctx context.Context, e tg.Entities, update *tg.UpdateUserName) error {
ghost, err := t.main.Bridge.GetGhostByID(ctx, ids.MakeUserID(update.UserID))
if err != nil {
@@ -734,7 +688,22 @@ func (t *TelegramClient) onUserName(ctx context.Context, e tg.Entities, update *
}
ghost.UpdateInfo(ctx, &userInfo)
return t.maybeUpdateRemoteProfile(ctx, ghost, nil)
if ghost.ID == t.userID {
var firstUsername string
if len(update.Usernames) > 0 {
firstUsername = update.Usernames[0].Username
}
t.updateRemoteProfile(ctx, &tg.User{
Self: true,
ID: update.UserID,
FirstName: update.FirstName,
LastName: update.LastName,
Username: firstUsername,
Usernames: update.Usernames,
}, ghost)
}
return nil
}
func (t *TelegramClient) onDeleteMessages(ctx context.Context, channelID int64, update IGetMessages) error {
@@ -783,12 +752,17 @@ func (t *TelegramClient) updateGhost(ctx context.Context, userID int64, user *tg
if err != nil {
return nil, err
}
userInfo, err := t.getUserInfoFromTelegramUser(ctx, user)
userInfo, err := t.wrapUserInfo(ctx, user)
if err != nil {
return nil, err
}
ghost.UpdateInfo(ctx, userInfo)
return userInfo, t.maybeUpdateRemoteProfile(ctx, ghost, user)
if ghost.ID == t.userID && t.updateRemoteProfile(ctx, user, ghost) {
t.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
}
return userInfo, nil
}
func (t *TelegramClient) updateChannel(ctx context.Context, channel *tg.Channel) (*bridgev2.UserInfo, error) {
+1 -1
View File
@@ -128,7 +128,7 @@ func finalizeLogin(ctx context.Context, user *bridgev2.User, authorization *tg.A
}
}()
ul.RemoteProfile, ul.RemoteName = userToRemoteProfile(me)
ul.RemoteProfile, ul.RemoteName = userToRemoteProfile(me, nil, nil)
err = ul.Save(ctx)
if err != nil {
return nil, fmt.Errorf("failed to save login: %w", err)
+2 -2
View File
@@ -41,7 +41,7 @@ var (
func (t *TelegramClient) getResolveIdentifierResponseForUser(ctx context.Context, user tg.UserClass) (*bridgev2.ResolveIdentifierResponse, error) {
networkUserID := ids.MakeUserID(user.GetID())
if userInfo, err := t.getUserInfoFromTelegramUser(ctx, user); err != nil {
if userInfo, err := t.wrapUserInfo(ctx, user); err != nil {
return nil, fmt.Errorf("failed to get user info: %w", err)
} else if ghost, err := t.main.Bridge.GetGhostByID(ctx, networkUserID); err != nil {
return nil, fmt.Errorf("failed to get ghost: %w", err)
@@ -88,7 +88,7 @@ func (t *TelegramClient) getResolveIdentifierResponseForUserID(ctx context.Conte
// - Usernames must start with a letter
// - Usernames must contain only letters, numbers, and underscores
// - Usernames cannot end with an underscore
var usernameRe = regexp.MustCompile(`^@?([a-zA-Z](?:\w{3,30})[a-zA-Z\d])$`)
var usernameRe = regexp.MustCompile(`^@?([a-zA-Z]\w{3,30}[a-zA-Z\d])$`)
func (t *TelegramClient) ResolveIdentifier(ctx context.Context, identifier string, createChat bool) (*bridgev2.ResolveIdentifierResponse, error) {
log := zerolog.Ctx(ctx).With().Str("identifier", identifier).Logger()
+140
View File
@@ -0,0 +1,140 @@
package connector
import (
"context"
"fmt"
"slices"
"strings"
"maunium.net/go/mautrix/bridgev2"
"go.mau.fi/mautrix-telegram/pkg/connector/ids"
"go.mau.fi/mautrix-telegram/pkg/connector/util"
"go.mau.fi/mautrix-telegram/pkg/gotd/tg"
)
func (t *TelegramClient) GetUserInfo(ctx context.Context, ghost *bridgev2.Ghost) (*bridgev2.UserInfo, error) {
peerType, id, err := ids.ParseUserID(ghost.ID)
if err != nil {
return nil, err
}
switch peerType {
case ids.PeerTypeUser:
if user, err := t.getSingleUser(ctx, id); err != nil {
return nil, fmt.Errorf("failed to get user %d: %w", id, err)
} else {
return t.wrapUserInfo(ctx, user)
}
case ids.PeerTypeChannel:
if channel, err := t.getSingleChannel(ctx, id); err != nil {
return nil, fmt.Errorf("failed to get channel %d: %w", id, err)
} else {
return t.wrapChannelGhostInfo(ctx, channel)
}
default:
return nil, fmt.Errorf("unexpected peer type: %s", peerType)
}
}
func (t *TelegramClient) wrapChannelGhostInfo(ctx context.Context, channel *tg.Channel) (*bridgev2.UserInfo, error) {
var err error
if accessHash, ok := channel.GetAccessHash(); ok && !channel.Min {
if err = t.ScopedStore.SetAccessHash(ctx, ids.PeerTypeChannel, channel.ID, accessHash); err != nil {
return nil, err
}
}
if !channel.Broadcast {
return nil, nil
}
var avatar *bridgev2.Avatar
avatar, err = t.convertChatPhoto(channel.AsInputPeer(), channel.GetPhoto())
if err != nil {
return nil, err
}
var identifiers []string
if username, set := channel.GetUsername(); set {
err = t.ScopedStore.SetUsername(ctx, ids.PeerTypeChannel, channel.ID, username)
if err != nil {
return nil, err
}
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", username))
}
return &bridgev2.UserInfo{
Name: &channel.Title,
Avatar: avatar,
Identifiers: identifiers,
ExtraUpdates: func(ctx context.Context, g *bridgev2.Ghost) bool {
updated := !g.Metadata.(*GhostMetadata).IsChannel
g.Metadata.(*GhostMetadata).IsChannel = true
return updated
},
}, nil
}
func (t *TelegramClient) wrapUserInfo(ctx context.Context, u tg.UserClass) (*bridgev2.UserInfo, error) {
user, ok := u.(*tg.User)
if !ok {
return nil, fmt.Errorf("user is %T not *tg.User", user)
}
var identifiers []string
if !user.Min {
if accessHash, ok := user.GetAccessHash(); ok {
if err := t.ScopedStore.SetAccessHash(ctx, ids.PeerTypeUser, user.ID, accessHash); err != nil {
return nil, err
}
}
if err := t.ScopedStore.SetUsername(ctx, ids.PeerTypeUser, user.ID, user.Username); err != nil {
return nil, err
}
if user.Username != "" {
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", user.Username))
}
for _, username := range user.Usernames {
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", username.Username))
}
if phone, ok := user.GetPhone(); ok {
normalized := strings.TrimPrefix(phone, "+")
identifiers = append(identifiers, fmt.Sprintf("tel:+%s", normalized))
if err := t.ScopedStore.SetPhoneNumber(ctx, user.ID, normalized); err != nil {
return nil, err
}
}
}
slices.Sort(identifiers)
identifiers = slices.Compact(identifiers)
var avatar *bridgev2.Avatar
if p, ok := user.GetPhoto(); ok && p.TypeID() == tg.UserProfilePhotoTypeID {
photo := p.(*tg.UserProfilePhoto)
var err error
avatar, err = t.convertUserProfilePhoto(ctx, user.ID, photo)
if err != nil {
return nil, err
}
}
name := util.FormatFullName(user.FirstName, user.LastName, user.Deleted, user.ID)
return &bridgev2.UserInfo{
IsBot: &user.Bot,
Name: &name,
Avatar: avatar,
Identifiers: identifiers,
ExtraUpdates: func(ctx context.Context, ghost *bridgev2.Ghost) (changed bool) {
meta := ghost.Metadata.(*GhostMetadata)
if !user.Min {
changed = changed || meta.IsPremium != user.Premium || meta.IsBot != user.Bot || meta.IsContact != user.Contact
meta.IsPremium = user.Premium
meta.IsBot = user.Bot
meta.IsContact = user.Contact
meta.Deleted = user.Deleted
}
return changed
},
}, nil
}