diff --git a/pkg/connector/handletelegram.go b/pkg/connector/handletelegram.go index af31829a..7f1c2d1a 100644 --- a/pkg/connector/handletelegram.go +++ b/pkg/connector/handletelegram.go @@ -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 diff --git a/pkg/connector/store/username.go b/pkg/connector/store/username.go index 20f45908..cabd37b4 100644 --- a/pkg/connector/store/username.go +++ b/pkg/connector/store/username.go @@ -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 } diff --git a/pkg/connector/userinfo.go b/pkg/connector/userinfo.go index 35d3dd54..22c7adca 100644 --- a/pkg/connector/userinfo.go +++ b/pkg/connector/userinfo.go @@ -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, "+")