store: refactor access hash and session tables

* Move sessions to user_login metadata, as that data rarely changes after login.
* Merge user and channel access hashes. Those IDs don't conflict.
* Split usernames into a new table to allow better `ON CONFLICT` updates
  (when a username moves to another entity, we want the old row to be replaced).
  Usernames also don't need to be scoped to a login.
This commit is contained in:
Tulir Asokan
2024-08-22 16:21:24 +03:00
parent e611c87342
commit b25c09fc53
14 changed files with 129 additions and 195 deletions
+2 -6
View File
@@ -22,11 +22,9 @@ func (t *TelegramClient) getDMChatInfo(ctx context.Context, userID int64) (*brid
Members: &bridgev2.ChatMemberList{IsFull: true},
CanBackfill: true,
}
accessHash, found, err := t.ScopedStore.GetUserAccessHash(ctx, userID)
accessHash, err := t.ScopedStore.GetAccessHash(ctx, userID)
if err != nil {
return nil, fmt.Errorf("failed to get access hash for user %d: %w", userID, err)
} else if !found {
return nil, fmt.Errorf("access hash not found for user %d", userID)
}
users, err := t.client.API().UsersGetUsers(ctx, []tg.InputUserClass{&tg.InputUser{
UserID: userID,
@@ -198,11 +196,9 @@ func (t *TelegramClient) GetChatInfo(ctx context.Context, portal *bridgev2.Porta
}
return chatInfo, nil
case ids.PeerTypeChannel:
accessHash, found, err := t.ScopedStore.GetChannelAccessHash(ctx, t.telegramUserID, id)
accessHash, err := t.ScopedStore.GetAccessHash(ctx, id)
if err != nil {
return nil, fmt.Errorf("failed to get channel access hash: %w", err)
} else if !found {
return nil, fmt.Errorf("channel access hash not found for %d", id)
}
inputChannel := &tg.InputChannel{ChannelID: id, AccessHash: accessHash}
fullChat, err := APICallWithUpdates(ctx, t, func() (*tg.MessagesChatFull, error) {