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
+4 -8
View File
@@ -370,23 +370,19 @@ func (t *TelegramClient) inputPeerForPortalID(ctx context.Context, portalID netw
}
switch peerType {
case ids.PeerTypeUser:
if accessHash, found, err := t.ScopedStore.GetUserAccessHash(ctx, id); err != nil {
if accessHash, err := t.ScopedStore.GetAccessHash(ctx, id); err != nil {
return nil, fmt.Errorf("failed to get user access hash for %d: %w", id, err)
} else if !found {
return nil, fmt.Errorf("user access hash not found for %d", id)
} else {
return &tg.InputPeerUser{UserID: id, AccessHash: accessHash}, nil
}
case ids.PeerTypeChat:
return &tg.InputPeerChat{ChatID: id}, nil
case ids.PeerTypeChannel:
accessHash, found, err := t.ScopedStore.GetChannelAccessHash(ctx, t.telegramUserID, id)
if err != nil {
if accessHash, err := t.ScopedStore.GetAccessHash(ctx, id); err != nil {
return nil, err
} else if !found {
return nil, fmt.Errorf("channel access hash not found for %d", id)
} else {
return &tg.InputPeerChannel{ChannelID: id, AccessHash: accessHash}, nil
}
return &tg.InputPeerChannel{ChannelID: id, AccessHash: accessHash}, nil
default:
panic("invalid peer type")
}