mirror of
https://github.com/mautrix/telegram.git
synced 2026-05-16 23:15:45 +03:00
userinfo,handletelegram: adjust processing usernames
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", 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
|
||||
}
|
||||
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,7 +239,9 @@ 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 {
|
||||
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", username.Username))
|
||||
if username.Active {
|
||||
identifiers = append(identifiers, fmt.Sprintf("telegram:%s", username.Username))
|
||||
}
|
||||
}
|
||||
if phone, ok := user.GetPhone(); ok {
|
||||
normalized := strings.TrimPrefix(phone, "+")
|
||||
|
||||
Reference in New Issue
Block a user