Best effort to provide a unpublish option
the build was successful Details

This commit is contained in:
Antoine Bartuccio 2019-01-02 02:21:15 +01:00
parent da0729d234
commit 0acdcb6351
Signed by: klmp200
GPG Key ID: E7245548C53F904B
2 changed files with 26 additions and 8 deletions

View File

@ -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
}

View File

@ -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