userinfo,handletelegram: adjust processing usernames

This commit is contained in:
Tulir Asokan
2026-05-13 15:26:14 +03:00
parent c7262e8e36
commit 2231c7ad7c
3 changed files with 28 additions and 10 deletions

View File

@@ -763,6 +763,10 @@ func (tc *TelegramClient) onUserName(ctx context.Context, e tg.Entities, update
slices.Sort(userInfo.Identifiers)
userInfo.Identifiers = slices.Compact(userInfo.Identifiers)
}
err = tc.main.Store.Username.Set(ctx, ids.PeerTypeUser, update.UserID, firstUsername)
if err != nil {
return fmt.Errorf("failed to store username: %w", err)
}
name := tc.main.Config.FormatDisplayname(update.FirstName, update.LastName, firstUsername, false, update.UserID)
userInfo.Name = &name

View File

@@ -41,7 +41,7 @@ const (
entity_id=excluded.entity_id
`
getByUsernameQuery = "SELECT entity_type, entity_id FROM telegram_username WHERE LOWER(username)=$1"
clearUsernameQuery = `DELETE FROM telegram_username WHERE entity_type=$1 AND entity_id=$2`
clearUsernameQuery = `DELETE FROM telegram_username WHERE entity_type=$1 AND entity_id=$2 AND LOWER(username)<>$3`
deleteUsernameQuery = `DELETE FROM telegram_username WHERE LOWER(username)=$1`
)
@@ -55,9 +55,12 @@ func (s *UsernameQuery) Get(ctx context.Context, entityType ids.PeerType, userID
func (s *UsernameQuery) Set(ctx context.Context, entityType ids.PeerType, entityID int64, username string) (err error) {
if username == "" {
_, err = s.db.Exec(ctx, clearUsernameQuery, entityType, entityID)
_, err = s.db.Exec(ctx, clearUsernameQuery, entityType, entityID, "")
} else {
_, err = s.db.Exec(ctx, setUsernameQuery, username, entityType, entityID)
if err == nil {
_, err = s.db.Exec(ctx, clearUsernameQuery, entityType, entityID, username)
}
}
return
}

View File

@@ -192,14 +192,22 @@ func (tc *TelegramClient) wrapChannelGhostInfo(ctx context.Context, channel *tg.
return nil, err
}
var identifiers []string
if username, set := channel.GetUsername(); set {
err = tc.main.Store.Username.Set(ctx, ids.PeerTypeChannel, channel.ID, username)
// TODO store alternate usernames in database too
err = tc.main.Store.Username.Set(ctx, ids.PeerTypeChannel, channel.ID, channel.Username)
if err != nil {
return nil, err
}
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", username))
var identifiers []string
if channel.Username != "" {
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", channel.Username))
}
for _, username := range channel.Usernames {
if username.Active {
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", username.Username))
}
}
slices.Sort(identifiers)
identifiers = slices.Compact(identifiers)
return &bridgev2.UserInfo{
Name: &channel.Title,
@@ -222,6 +230,7 @@ func (tc *TelegramClient) wrapUserInfo(ctx context.Context, u tg.UserClass, ghos
}
}
// TODO store alternate usernames in database too
if err := tc.main.Store.Username.Set(ctx, ids.PeerTypeUser, user.ID, user.Username); err != nil {
return nil, err
}
@@ -230,8 +239,10 @@ func (tc *TelegramClient) wrapUserInfo(ctx context.Context, u tg.UserClass, ghos
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", user.Username))
}
for _, username := range user.Usernames {
if username.Active {
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))