Skip to content

Commit

Permalink
handler service messages
Browse files Browse the repository at this point in the history
  • Loading branch information
celestix committed Mar 15, 2024
1 parent c24da3b commit 9047b14
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 39 deletions.
2 changes: 1 addition & 1 deletion authConversation.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ func (b *basicConservator) AskCode() (string, error) {
func (b *basicConservator) RetryPassword(trialsLeft int) (string, error) {
fmt.Println("The 2FA Code you just entered seems to be incorrect,")
fmt.Println("Attempts Left:", trialsLeft)
fmt.Println("Please try again.... ")
fmt.Println("Please try again....")
return b.AskCode()
}
12 changes: 4 additions & 8 deletions dispatcher/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,20 +106,16 @@ func (dp *NativeDispatcher) Handle(ctx context.Context, updates tg.UpdatesClass)
chats := u.MapChats()
e.Chats = chats.ChatToMap()
e.Channels = chats.ChannelToMap()
go func() {
saveUsersPeers(u.Users, dp.pStorage)
saveChatsPeers(u.Chats, dp.pStorage)
}()
go saveUsersPeers(u.Users, dp.pStorage)
go saveChatsPeers(u.Chats, dp.pStorage)
case *tg.UpdatesCombined:
upds = u.Updates
e.Users = u.MapUsers().NotEmptyToMap()
chats := u.MapChats()
e.Chats = chats.ChatToMap()
e.Channels = chats.ChannelToMap()
go func() {
saveUsersPeers(u.Users, dp.pStorage)
saveChatsPeers(u.Chats, dp.pStorage)
}()
go saveUsersPeers(u.Users, dp.pStorage)
go saveChatsPeers(u.Chats, dp.pStorage)
case *tg.UpdateShort:
upds = []tg.UpdateClass{u.Update}
e.short()
Expand Down
61 changes: 31 additions & 30 deletions ext/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ type Update struct {
UpdateClass tg.UpdateClass
// Entities of an update, i.e. mapped users, chats and channels.
Entities *tg.Entities
// User id of the user responsible for the update.
userId int64
}

// GetNewUpdate creates a new Update with provided parameters.
Expand All @@ -38,10 +40,8 @@ func GetNewUpdate(ctx context.Context, client *tg.Client, p *storage.PeerStorage
}
switch update := update.(type) {
case *tg.UpdateNewMessage:
m, ok := update.GetMessage().(*tg.Message)
if ok {
u.EffectiveMessage = types.ConstructMessage(m)
}
m := update.GetMessage()
u.EffectiveMessage = types.ConstructMessage(m)
diff, err := client.UpdatesGetDifference(ctx, &tg.UpdatesGetDifferenceRequest{
Pts: update.Pts - 1,
Date: int(time.Now().Unix()),
Expand Down Expand Up @@ -69,21 +69,25 @@ func GetNewUpdate(ctx context.Context, client *tg.Client, p *storage.PeerStorage
}
}
}
u.fillUserIdFromMessage(m)
case message.AnswerableMessageUpdate:
m, ok := update.GetMessage().(*tg.Message)
if ok {
u.EffectiveMessage = types.ConstructMessage(m)
}
m := update.GetMessage()
u.EffectiveMessage = types.ConstructMessage(m)
u.fillUserIdFromMessage(m)
case *tg.UpdateBotCallbackQuery:
u.CallbackQuery = update
u.userId = update.UserID
case *tg.UpdateBotInlineQuery:
u.InlineQuery = update
u.userId = update.UserID
case *tg.UpdatePendingJoinRequests:
u.ChatJoinRequest = update
case *tg.UpdateChatParticipant:
u.ChatParticipant = update
u.userId = update.UserID
case *tg.UpdateChannelParticipant:
u.ChannelParticipant = update
u.userId = update.UserID
}
u.Entities = e
return u
Expand All @@ -107,29 +111,10 @@ func (u *Update) EffectiveUser() *tg.User {
if u.Entities == nil {
return nil
}
var userId int64
switch {
case u.EffectiveMessage != nil:
uId, ok := u.EffectiveMessage.FromID.(*tg.PeerUser)
if !ok {
for _, user := range u.Entities.Users {
if user.Self && user.Bot {
return nil
}
return user
}
}
userId = uId.UserID
case u.CallbackQuery != nil:
userId = u.CallbackQuery.UserID
case u.InlineQuery != nil:
userId = u.InlineQuery.UserID
case u.ChatParticipant != nil:
userId = u.ChannelParticipant.UserID
case u.ChannelParticipant != nil:
userId = u.ChannelParticipant.UserID
if u.userId != 0 {
return nil
}
return u.Entities.Users[userId]
return u.Entities.Users[u.userId]
}

// GetChat returns the responsible tg.Chat for the current update.
Expand Down Expand Up @@ -230,3 +215,19 @@ func (u *Update) EffectiveChat() types.EffectiveChat {
}
return &types.EmptyUC{}
}

func (u *Update) fillUserIdFromMessage(m tg.MessageClass) {
var userPeer tg.PeerClass
switch _m := m.(type) {
case *tg.Message:
userPeer = _m.FromID
case *tg.MessageService:
userPeer = _m.FromID
}
uId, ok := userPeer.(*tg.PeerUser)
if !ok {
u.userId = u.Entities.Users[0].ID
} else {
u.userId = uId.UserID
}
}

0 comments on commit 9047b14

Please sign in to comment.