The Proper Subscriptions Filter

This commit is contained in:
Bohdan Horbeshko 2022-02-06 08:25:46 -05:00
parent 9d84965e8b
commit 5a48600b7a
3 changed files with 51 additions and 30 deletions

View file

@ -461,12 +461,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return "No error, but chat is nil", true
}
gateway.SendPresence(
c.xmpp,
c.jid,
gateway.SPFrom(strconv.FormatInt(chat.Id, 10)),
gateway.SPType("subscribe"),
)
c.subscribeToID(chat.Id, chat)
// join https://t.me/publichat
case "join":
if len(args) < 1 {

View file

@ -73,6 +73,20 @@ func (c *Client) updateHandler() {
}
c.updateNewChat(typedUpdate)
log.Debugf("%#v", typedUpdate.Chat)
case client.TypeUpdateChatPosition:
typedUpdate, ok := update.(*client.UpdateChatPosition)
if !ok {
uhOh()
}
c.updateChatPosition(typedUpdate)
log.Debugf("%#v", typedUpdate)
case client.TypeUpdateChatLastMessage:
typedUpdate, ok := update.(*client.UpdateChatLastMessage)
if !ok {
uhOh()
}
c.updateChatLastMessage(typedUpdate)
log.Debugf("%#v", typedUpdate)
case client.TypeUpdateNewMessage:
typedUpdate, ok := update.(*client.UpdateNewMessage)
if !ok {
@ -142,30 +156,8 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
c.cache.SetChat(update.Chat.Id, update.Chat)
var isChannel = false
chatType := update.Chat.Type.ChatTypeType()
if chatType == client.TypeChatTypeSupergroup {
typeSupergroup, ok := update.Chat.Type.(*client.ChatTypeSupergroup)
if !ok {
uhOh()
}
isChannel = typeSupergroup.IsChannel
}
// don't subscribe to channel posters
if !((isChannel && update.Chat.LastReadInboxMessageId == 0) ||
// don't subscribe to chats with no conversation
// (manual adding will trigger a subscribe anyway)
(update.Chat.LastReadInboxMessageId == 0 &&
update.Chat.LastReadOutboxMessageId == 0 &&
chatType == client.TypeChatTypePrivate)) {
gateway.SendPresence(
c.xmpp,
c.jid,
gateway.SPFrom(strconv.FormatInt(update.Chat.Id, 10)),
gateway.SPType("subscribe"),
gateway.SPNickname(update.Chat.Title),
)
if update.Chat.Positions != nil && len(update.Chat.Positions) > 0 {
c.subscribeToID(update.Chat.Id, update.Chat)
}
if update.Chat.Id < 0 {
@ -174,6 +166,20 @@ func (c *Client) updateNewChat(update *client.UpdateNewChat) {
}()
}
// chat position is updated
func (c *Client) updateChatPosition(update *client.UpdateChatPosition) {
if update.Position != nil && update.Position.Order != 0 {
go c.subscribeToID(update.ChatId, nil)
}
}
// chat last message is updated
func (c *Client) updateChatLastMessage(update *client.UpdateChatLastMessage) {
if update.Positions != nil && len(update.Positions) > 0 {
go c.subscribeToID(update.ChatId, nil)
}
}
// message received
func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
go func() {

View file

@ -774,3 +774,23 @@ func (c *Client) DownloadFile(id int32, priority int32, synchronous bool) (*clie
Synchronous: synchronous,
})
}
// subscribe to a Telegram ID
func (c *Client) subscribeToID(id int64, chat *client.Chat) {
var args []args.V
args = append(args, gateway.SPFrom(strconv.FormatInt(id, 10)))
args = append(args, gateway.SPType("subscribe"))
if chat == nil {
chat, _, _ = c.GetContactByID(id, nil)
}
if chat != nil {
args = append(args, gateway.SPNickname(chat.Title))
}
gateway.SendPresence(
c.xmpp,
c.jid,
args...,
)
}