client: save channel usernames in database
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
@@ -234,7 +234,7 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
return userInfo, nil
|
return userInfo, nil
|
||||||
},
|
},
|
||||||
GetUserInfoByUsername: func(ctx context.Context, username string) (telegramfmt.UserInfo, error) {
|
GetUserInfoByUsername: func(ctx context.Context, username string) (telegramfmt.UserInfo, error) {
|
||||||
if peerType, userID, err := client.ScopedStore.GetUserIDByUsername(ctx, username); err != nil {
|
if peerType, userID, err := client.ScopedStore.GetEntityIDByUsername(ctx, username); err != nil {
|
||||||
return telegramfmt.UserInfo{}, err
|
return telegramfmt.UserInfo{}, err
|
||||||
} else if peerType != ids.PeerTypeUser {
|
} else if peerType != ids.PeerTypeUser {
|
||||||
return telegramfmt.UserInfo{}, fmt.Errorf("unexpected peer type: %s", peerType)
|
return telegramfmt.UserInfo{}, fmt.Errorf("unexpected peer type: %s", peerType)
|
||||||
|
|||||||
@@ -81,7 +81,7 @@ func (t *TelegramClient) ResolveIdentifier(ctx context.Context, identifier strin
|
|||||||
return t.getResolveIdentifierResponseForUserID(ctx, userID)
|
return t.getResolveIdentifierResponseForUserID(ctx, userID)
|
||||||
} else if match := usernameRe.FindStringSubmatch(identifier); match != nil && !strings.Contains(identifier, "__") {
|
} else if match := usernameRe.FindStringSubmatch(identifier); match != nil && !strings.Contains(identifier, "__") {
|
||||||
// This is a username
|
// This is a username
|
||||||
entityType, userID, err := t.ScopedStore.GetUserIDByUsername(ctx, match[1])
|
entityType, userID, err := t.ScopedStore.GetEntityIDByUsername(ctx, match[1])
|
||||||
if entityType == ids.PeerTypeUser && (err == nil || userID != 0) {
|
if entityType == ids.PeerTypeUser && (err == nil || userID != 0) {
|
||||||
// We know this username.
|
// We know this username.
|
||||||
return t.getResolveIdentifierResponseForUserID(ctx, userID)
|
return t.getResolveIdentifierResponseForUserID(ctx, userID)
|
||||||
|
|||||||
@@ -192,17 +192,17 @@ func (s *ScopedStore) GetUsername(ctx context.Context, entityType ids.PeerType,
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScopedStore) SetUsername(ctx context.Context, entityType ids.PeerType, userID int64, username string) (err error) {
|
func (s *ScopedStore) SetUsername(ctx context.Context, entityType ids.PeerType, entityID int64, username string) (err error) {
|
||||||
if username == "" {
|
if username == "" {
|
||||||
_, err = s.db.Exec(ctx, clearUsernameQuery, entityType, userID)
|
_, err = s.db.Exec(ctx, clearUsernameQuery, entityType, entityID)
|
||||||
} else {
|
} else {
|
||||||
_, err = s.db.Exec(ctx, setUsernameQuery, username, entityType, userID)
|
_, err = s.db.Exec(ctx, setUsernameQuery, username, entityType, entityID)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *ScopedStore) GetUserIDByUsername(ctx context.Context, username string) (entityType ids.PeerType, userID int64, err error) {
|
func (s *ScopedStore) GetEntityIDByUsername(ctx context.Context, username string) (entityType ids.PeerType, entityID int64, err error) {
|
||||||
err = s.db.QueryRow(ctx, getByUsernameQuery, username).Scan(&entityType, &userID)
|
err = s.db.QueryRow(ctx, getByUsernameQuery, username).Scan(&entityType, &entityID)
|
||||||
if errors.Is(err, sql.ErrNoRows) {
|
if errors.Is(err, sql.ErrNoRows) {
|
||||||
err = nil
|
err = nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -77,6 +77,8 @@ func (t *TelegramClient) onUpdateChannel(ctx context.Context, update *tg.UpdateC
|
|||||||
} else if channel.Left {
|
} else if channel.Left {
|
||||||
log.Error().Msg("Update was for a left channel. Leaving the channel.")
|
log.Error().Msg("Update was for a left channel. Leaving the channel.")
|
||||||
leave()
|
leave()
|
||||||
|
} else {
|
||||||
|
// TODO update the channel info
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -408,6 +410,13 @@ func (t *TelegramClient) updateChannel(ctx context.Context, channel *tg.Channel)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if username, set := channel.GetUsername(); set {
|
||||||
|
err := t.ScopedStore.SetUsername(ctx, ids.PeerTypeChannel, channel.ID, username)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ghost.UpdateInfo(ctx, &bridgev2.UserInfo{
|
ghost.UpdateInfo(ctx, &bridgev2.UserInfo{
|
||||||
Name: &channel.Title,
|
Name: &channel.Title,
|
||||||
Avatar: avatar,
|
Avatar: avatar,
|
||||||
|
|||||||
Reference in New Issue
Block a user