From 0acdcb6351f8fff9f9649dfa7977737849272506 Mon Sep 17 00:00:00 2001 From: Bartuccio Antoine Date: Wed, 2 Jan 2019 02:21:15 +0100 Subject: [PATCH] Best effort to provide a unpublish option --- commands/subscribe.go | 30 +++++++++++++++++++++++------- doc/publish.md | 4 +++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/commands/subscribe.go b/commands/subscribe.go index 7a388df..9bd3600 100644 --- a/commands/subscribe.go +++ b/commands/subscribe.go @@ -1,5 +1,5 @@ /* -* @Author: Amalvy Arthur +* @Authors: Amalvy Arthur, Bartuccio Antoine */ // This module uses a property named "subscribed_chats" in the shared Users structure. @@ -54,6 +54,12 @@ func Subscribe(m *tb.Message) { shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") return } + + if m.Sender.Username == "" { + shared.Bot.Send(m.Chat, "Il faut avoir enregistré un username pour pouvoir utiliser cette fonction") + return + } + userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") if !exists { shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title) @@ -82,6 +88,12 @@ func Unsubscribe(m *tb.Message) { shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") return } + + if m.Sender.Username == "" { + shared.Bot.Send(m.Chat, "Il faut avoir enregistré un username pour pouvoir utiliser cette fonction") + return + } + userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats") if !exists || len(userSubscribedChats) == 0 { shared.Bot.Send(m.Chat, "Vous n'êtes abonné à aucun chat") @@ -135,11 +147,6 @@ func Publish(m *tb.Message) { // Command syntax (while replying to a message) : /unpublish func Unpublish(m *tb.Message) { - // This is not working at the moment - // This can't work because when retrieving, the newly send message - // has a different ID from the one stored so you can't detect that's - // it's in the database except if you find the original message - // which is very unlikely to happen if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") return @@ -159,7 +166,16 @@ func Unpublish(m *tb.Message) { filteredPublishedMessages := []tb.Message{} found := false for _, message := range publishedMessages { - if message.ID == m.ReplyTo.ID { + // You can't just compare messages id because + // when retrieving, the newly send message + // has a different ID from the one stored so you can't detect that's + // it's in the database except if you find the original message + // which is very unlikely to happen + // As a workaround, we check the original sender, original unix time + // and associated text + if message.Sender.ID == m.ReplyTo.OriginalSender.ID && + message.Unixtime == int64(m.ReplyTo.OriginalUnixtime) && + message.Text == m.ReplyTo.Text { found = true continue } diff --git a/doc/publish.md b/doc/publish.md index 84e4e4d..3862db4 100644 --- a/doc/publish.md +++ b/doc/publish.md @@ -52,7 +52,9 @@ The publish module intend to be a "multi-pin" feature for chats, allowing users **Usage location** : Group chat only -**Command syntax** : /unpublish [publication ID] +**usage conditions** : Reply to the message to unpublish + +**Command syntax** : /unpublish ### Retrieve