client: add shortcut field for user login metadata
This commit is contained in:
+12
-12
@@ -47,7 +47,7 @@ func (t *TelegramClient) getTakeoutID(ctx context.Context) (takeoutID int64, err
|
|||||||
}
|
}
|
||||||
log := zerolog.Ctx(ctx).With().Str("function", "getTakeoutID").Logger()
|
log := zerolog.Ctx(ctx).With().Str("function", "getTakeoutID").Logger()
|
||||||
|
|
||||||
if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID != 0 {
|
if t.metadata.TakeoutID != 0 {
|
||||||
// Resume fetching dialogs using takeout and enqueueing them for
|
// Resume fetching dialogs using takeout and enqueueing them for
|
||||||
// backfill.
|
// backfill.
|
||||||
go t.takeoutDialogsOnce.Do(func() {
|
go t.takeoutDialogsOnce.Do(func() {
|
||||||
@@ -55,7 +55,7 @@ func (t *TelegramClient) getTakeoutID(ctx context.Context) (takeoutID int64, err
|
|||||||
log.Err(err).Msg("Failed to takeout dialogs")
|
log.Err(err).Msg("Failed to takeout dialogs")
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
return t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID, nil
|
return t.metadata.TakeoutID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
t.stopTakeoutTimer = time.AfterFunc(max(time.Hour, time.Duration(t.main.Bridge.Config.Backfill.Queue.BatchDelay*2)), sync.OnceFunc(func() { t.stopTakeout(ctx) }))
|
t.stopTakeoutTimer = time.AfterFunc(max(time.Hour, time.Duration(t.main.Bridge.Config.Backfill.Queue.BatchDelay*2)), sync.OnceFunc(func() { t.stopTakeout(ctx) }))
|
||||||
@@ -89,14 +89,14 @@ func (t *TelegramClient) getTakeoutID(ctx context.Context) (takeoutID int64, err
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID = accountTakeout.ID
|
t.metadata.TakeoutID = accountTakeout.ID
|
||||||
return accountTakeout.ID, t.userLogin.Save(ctx)
|
return accountTakeout.ID, t.userLogin.Save(ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) error {
|
func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) error {
|
||||||
log := zerolog.Ctx(ctx).With().Str("loop", "chat_fetch").Logger()
|
log := zerolog.Ctx(ctx).With().Str("loop", "chat_fetch").Logger()
|
||||||
if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlDone {
|
if t.metadata.TakeoutDialogCrawlDone {
|
||||||
log.Debug().Msg("Dialogs already crawled")
|
log.Debug().Msg("Dialogs already crawled")
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -105,9 +105,9 @@ func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) er
|
|||||||
Limit: 100,
|
Limit: 100,
|
||||||
OffsetPeer: &tg.InputPeerEmpty{},
|
OffsetPeer: &tg.InputPeerEmpty{},
|
||||||
}
|
}
|
||||||
if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor != "" {
|
if t.metadata.TakeoutDialogCrawlCursor != "" {
|
||||||
var err error
|
var err error
|
||||||
req.OffsetPeer, _, err = t.inputPeerForPortalID(ctx, t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor)
|
req.OffsetPeer, _, err = t.inputPeerForPortalID(ctx, t.metadata.TakeoutDialogCrawlCursor)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get input peer for pagination: %w", err)
|
return fmt.Errorf("failed to get input peer for pagination: %w", err)
|
||||||
}
|
}
|
||||||
@@ -130,7 +130,7 @@ func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) er
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to get dialogs: %w", err)
|
return fmt.Errorf("failed to get dialogs: %w", err)
|
||||||
} else if len(dialogs.GetDialogs()) == 0 {
|
} else if len(dialogs.GetDialogs()) == 0 {
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlDone = true
|
t.metadata.TakeoutDialogCrawlDone = true
|
||||||
if err = t.userLogin.Save(ctx); err != nil {
|
if err = t.userLogin.Save(ctx); err != nil {
|
||||||
return fmt.Errorf("failed to save user login: %w", err)
|
return fmt.Errorf("failed to save user login: %w", err)
|
||||||
}
|
}
|
||||||
@@ -153,13 +153,13 @@ func (t *TelegramClient) takeoutDialogs(ctx context.Context, takeoutID int64) er
|
|||||||
|
|
||||||
portalKey := t.makePortalKeyFromPeer(dialogs.GetDialogs()[len(dialogs.GetDialogs())-1].GetPeer(), 0)
|
portalKey := t.makePortalKeyFromPeer(dialogs.GetDialogs()[len(dialogs.GetDialogs())-1].GetPeer(), 0)
|
||||||
|
|
||||||
if t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor == portalKey.ID {
|
if t.metadata.TakeoutDialogCrawlCursor == portalKey.ID {
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlDone = true
|
t.metadata.TakeoutDialogCrawlDone = true
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor = ""
|
t.metadata.TakeoutDialogCrawlCursor = ""
|
||||||
log.Debug().Msg("No more dialogs found")
|
log.Debug().Msg("No more dialogs found")
|
||||||
return nil
|
return nil
|
||||||
} else {
|
} else {
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).TakeoutDialogCrawlCursor = portalKey.ID
|
t.metadata.TakeoutDialogCrawlCursor = portalKey.ID
|
||||||
}
|
}
|
||||||
if err = t.userLogin.Save(ctx); err != nil {
|
if err = t.userLogin.Save(ctx); err != nil {
|
||||||
return fmt.Errorf("failed to save user login: %w", err)
|
return fmt.Errorf("failed to save user login: %w", err)
|
||||||
@@ -180,7 +180,7 @@ func (t *TelegramClient) stopTakeout(ctx context.Context) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).TakeoutID = 0
|
t.metadata.TakeoutID = 0
|
||||||
return t.userLogin.Save(ctx)
|
return t.userLogin.Save(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -76,6 +76,7 @@ type TelegramClient struct {
|
|||||||
loginID networkid.UserLoginID
|
loginID networkid.UserLoginID
|
||||||
userID networkid.UserID
|
userID networkid.UserID
|
||||||
userLogin *bridgev2.UserLogin
|
userLogin *bridgev2.UserLogin
|
||||||
|
metadata *UserLoginMetadata
|
||||||
client *telegram.Client
|
client *telegram.Client
|
||||||
updatesManager *updates.Manager
|
updatesManager *updates.Manager
|
||||||
clientCtx context.Context
|
clientCtx context.Context
|
||||||
@@ -184,6 +185,7 @@ func NewTelegramClient(ctx context.Context, tc *TelegramConnector, login *bridge
|
|||||||
loginID: login.ID,
|
loginID: login.ID,
|
||||||
userID: networkid.UserID(login.ID),
|
userID: networkid.UserID(login.ID),
|
||||||
userLogin: login,
|
userLogin: login,
|
||||||
|
metadata: login.Metadata.(*UserLoginMetadata),
|
||||||
|
|
||||||
takeoutAccepted: exsync.NewEvent(),
|
takeoutAccepted: exsync.NewEvent(),
|
||||||
|
|
||||||
@@ -526,7 +528,7 @@ func (t *TelegramClient) onSession() {
|
|||||||
|
|
||||||
func (t *TelegramClient) onAuthError(err error) {
|
func (t *TelegramClient) onAuthError(err error) {
|
||||||
t.sendBadCredentialsOrUnknownError(err)
|
t.sendBadCredentialsOrUnknownError(err)
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).ResetOnLogout()
|
t.metadata.ResetOnLogout()
|
||||||
go func() {
|
go func() {
|
||||||
if err := t.userLogin.Save(context.Background()); err != nil {
|
if err := t.userLogin.Save(context.Background()); err != nil {
|
||||||
t.main.Bridge.Log.Err(err).Msg("failed to save user login")
|
t.main.Bridge.Log.Err(err).Msg("failed to save user login")
|
||||||
@@ -543,7 +545,7 @@ func (t *TelegramClient) Connect(_ context.Context) {
|
|||||||
log := zerolog.Ctx(context.Background()).With().Int64("user_id", t.telegramUserID).Logger()
|
log := zerolog.Ctx(context.Background()).With().Int64("user_id", t.telegramUserID).Logger()
|
||||||
ctx = log.WithContext(ctx)
|
ctx = log.WithContext(ctx)
|
||||||
|
|
||||||
if !t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() {
|
if !t.metadata.Session.HasAuthKey() {
|
||||||
log.Warn().Msg("user does not have an auth key, sending bad credentials state")
|
log.Warn().Msg("user does not have an auth key, sending bad credentials state")
|
||||||
t.sendBadCredentialsOrUnknownError(ErrNoAuthKey)
|
t.sendBadCredentialsOrUnknownError(ErrNoAuthKey)
|
||||||
return
|
return
|
||||||
@@ -637,7 +639,7 @@ func (t *TelegramClient) getSingleChannel(ctx context.Context, id int64) (*tg.Ch
|
|||||||
func (t *TelegramClient) IsLoggedIn() bool {
|
func (t *TelegramClient) IsLoggedIn() bool {
|
||||||
// TODO use less hacky check than context cancellation
|
// TODO use less hacky check than context cancellation
|
||||||
return t != nil && t.clientCtx != nil && t.client != nil && t.clientCtx.Err() == nil &&
|
return t != nil && t.clientCtx != nil && t.client != nil && t.clientCtx.Err() == nil &&
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey()
|
t.metadata.Session.HasAuthKey()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) LogoutRemote(ctx context.Context) {
|
func (t *TelegramClient) LogoutRemote(ctx context.Context) {
|
||||||
@@ -648,7 +650,7 @@ func (t *TelegramClient) LogoutRemote(ctx context.Context) {
|
|||||||
|
|
||||||
log.Info().Msg("Logging out and disconnecting")
|
log.Info().Msg("Logging out and disconnecting")
|
||||||
|
|
||||||
if t.userLogin.Metadata.(*UserLoginMetadata).Session.HasAuthKey() {
|
if t.metadata.Session.HasAuthKey() {
|
||||||
log.Info().Msg("User has an auth key, logging out")
|
log.Info().Msg("User has an auth key, logging out")
|
||||||
|
|
||||||
// logging out is best effort, we want to logout even if we can't call the endpoint
|
// logging out is best effort, we want to logout even if we can't call the endpoint
|
||||||
|
|||||||
@@ -1172,14 +1172,14 @@ func (t *TelegramClient) onNotifySettings(ctx context.Context, e tg.Entities, up
|
|||||||
|
|
||||||
func (t *TelegramClient) onPinnedDialogs(ctx context.Context, e tg.Entities, msg *tg.UpdatePinnedDialogs) error {
|
func (t *TelegramClient) onPinnedDialogs(ctx context.Context, e tg.Entities, msg *tg.UpdatePinnedDialogs) error {
|
||||||
needsUnpinning := map[networkid.PortalKey]struct{}{}
|
needsUnpinning := map[networkid.PortalKey]struct{}{}
|
||||||
for _, portalID := range t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs {
|
for _, portalID := range t.metadata.PinnedDialogs {
|
||||||
pt, id, _, err := ids.ParsePortalID(portalID)
|
pt, id, _, err := ids.ParsePortalID(portalID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
needsUnpinning[t.makePortalKeyFromID(pt, id, 0)] = struct{}{}
|
needsUnpinning[t.makePortalKeyFromID(pt, id, 0)] = struct{}{}
|
||||||
}
|
}
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = nil
|
t.metadata.PinnedDialogs = nil
|
||||||
|
|
||||||
for _, d := range msg.Order {
|
for _, d := range msg.Order {
|
||||||
dialog, ok := d.(*tg.DialogPeer)
|
dialog, ok := d.(*tg.DialogPeer)
|
||||||
@@ -1188,7 +1188,7 @@ func (t *TelegramClient) onPinnedDialogs(ctx context.Context, e tg.Entities, msg
|
|||||||
}
|
}
|
||||||
portalKey := t.makePortalKeyFromPeer(dialog.Peer, 0)
|
portalKey := t.makePortalKeyFromPeer(dialog.Peer, 0)
|
||||||
delete(needsUnpinning, portalKey)
|
delete(needsUnpinning, portalKey)
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = append(t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs, portalKey.ID)
|
t.metadata.PinnedDialogs = append(t.metadata.PinnedDialogs, portalKey.ID)
|
||||||
|
|
||||||
res := t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.ChatInfoChange{
|
res := t.main.Bridge.QueueRemoteEvent(t.userLogin, &simplevent.ChatInfoChange{
|
||||||
ChatInfoChange: &bridgev2.ChatInfoChange{
|
ChatInfoChange: &bridgev2.ChatInfoChange{
|
||||||
|
|||||||
@@ -402,7 +402,7 @@ UserLoop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error {
|
func (t *TelegramClient) RegisterPushNotifications(ctx context.Context, pushType bridgev2.PushType, token string) error {
|
||||||
meta := t.userLogin.Metadata.(*UserLoginMetadata)
|
meta := t.metadata
|
||||||
if meta.PushEncryptionKey == nil {
|
if meta.PushEncryptionKey == nil {
|
||||||
meta.PushEncryptionKey = random.Bytes(256)
|
meta.PushEncryptionKey = random.Bytes(256)
|
||||||
err := t.userLogin.Save(ctx)
|
err := t.userLogin.Save(ctx)
|
||||||
|
|||||||
@@ -66,11 +66,11 @@ func (t *TelegramClient) SyncChats(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TelegramClient) resetPinnedDialogs(ctx context.Context, dialogs []tg.DialogClass) error {
|
func (t *TelegramClient) resetPinnedDialogs(ctx context.Context, dialogs []tg.DialogClass) error {
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = nil
|
t.metadata.PinnedDialogs = nil
|
||||||
for _, dialog := range dialogs {
|
for _, dialog := range dialogs {
|
||||||
if dialog.GetPinned() {
|
if dialog.GetPinned() {
|
||||||
portalKey := t.makePortalKeyFromPeer(dialog.GetPeer(), 0)
|
portalKey := t.makePortalKeyFromPeer(dialog.GetPeer(), 0)
|
||||||
t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs = append(t.userLogin.Metadata.(*UserLoginMetadata).PinnedDialogs, portalKey.ID)
|
t.metadata.PinnedDialogs = append(t.metadata.PinnedDialogs, portalKey.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return t.userLogin.Save(ctx)
|
return t.userLogin.Save(ctx)
|
||||||
|
|||||||
Reference in New Issue
Block a user