diff --git a/commands/subscribe.go b/commands/subscribe.go index 48100b6..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. @@ -11,7 +11,7 @@ package commands import ( - "fmt" + "encoding/json" "strconv" "strings" @@ -19,13 +19,47 @@ import ( tb "gopkg.in/tucnak/telebot.v2" ) +func getPublishedMessages(chatID int64) []tb.Message { + messages := []tb.Message{} + + serializedMessages, exists := shared.ChatData.Get(chatID, "published_messages") + if !exists { + return messages + } + + if _, ok := serializedMessages.(string); !ok { + return messages + } + + if json.Unmarshal([]byte(serializedMessages.(string)), &messages) != nil { + return []tb.Message{} + } + + return messages +} + +func setPublishedMessages(chatID int64, messages []tb.Message) { + data, err := json.Marshal(messages) + if err != nil { + return + } + + shared.ChatData.Set(chatID, "published_messages", string(data)) +} + // Subscribe user sending the command to the current chat // Command syntax : /subscribe func Subscribe(m *tb.Message) { if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { - shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + 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) @@ -51,9 +85,15 @@ func Subscribe(m *tb.Message) { // Command syntax : /unsubscribe func Unsubscribe(m *tb.Message) { if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { - shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + 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") @@ -74,7 +114,7 @@ func Unsubscribe(m *tb.Message) { // Command syntax : /listsubscribers func ListSubscribers(m *tb.Message) { if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { - shared.Bot.Send(m.Chat, "Cette commande n'est pas authorisée pour ce type de chat") + shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") return } subscribers := m.Chat.Title + " subscribers : \n\n" @@ -85,52 +125,69 @@ func ListSubscribers(m *tb.Message) { } // Publish a message from the current chat -// Command syntax (while repying to a message) : /publish +// Command syntax (while replying to a message) : /publish func Publish(m *tb.Message) { if m.ReplyTo == nil { shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le publier") return } - defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) - savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages") - if !exists { - messageList := []string{m.ReplyTo.Text} - shared.ChatData.Set(m.Chat.ID, "published_messages", messageList) + 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 } - shared.ChatData.Set( - m.Chat.ID, - "published_messages", - append(savedMessages.([]interface{}), m.ReplyTo.Text), - ) + + defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) + savedMessages := getPublishedMessages(m.Chat.ID) + setPublishedMessages(m.Chat.ID, append(savedMessages, *m.ReplyTo)) } // Unpublish remove a message from published messages in the current chat -// Command syntax : /unpublish [publication ID] +// Command syntax (while replying to a message) : /unpublish func Unpublish(m *tb.Message) { - parsedCommand := strings.Split(m.Text, " ") - if len(parsedCommand) < 2 { - shared.Bot.Send(m.Chat, "syntaxe : /unpublish [publication ID]") + + 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 } - index, err := strconv.Atoi(parsedCommand[1]) - if err != nil { - shared.Bot.Send(m.Chat, "ID de publication invalide") + + if m.ReplyTo == nil { + shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le dépublier") return } - savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages") - if !exists || len(savedMessages.([]interface{})) == 0 { - shared.Bot.Send(m.Chat, "Aucun message publié") + + if !m.ReplyTo.IsForwarded() { + shared.Bot.Send(m.Chat, "Ce message ne peut pas avoir été publié") return } - if len(savedMessages.([]interface{})) <= index || index < 0 { - shared.Bot.Send(m.Chat, "Aucun message avec cet ID de publication n'existe") + + publishedMessages := getPublishedMessages(m.Chat.ID) + filteredPublishedMessages := []tb.Message{} + found := false + for _, message := range publishedMessages { + // 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 + } + filteredPublishedMessages = append(filteredPublishedMessages, message) + } + + if !found { + shared.Bot.Send(m.Chat, "Ce message n'a jamais été publié") return } - savedMessages = append(savedMessages.([]interface{})[:index], savedMessages.([]interface{})[index+1:]...) - shared.ChatData.Set(m.Chat.ID, "published_messages", savedMessages) + + setPublishedMessages(m.Chat.ID, filteredPublishedMessages) shared.Bot.Send(m.Chat, "Message supprimé des publication") } @@ -139,7 +196,7 @@ func Unpublish(m *tb.Message) { // Command syntax : /retrieve func Retrieve(m *tb.Message) { chatList := []int64{} - messageList := []string{} + hasMessage := false if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup && m.Chat.Type != tb.ChatPrivate { shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat") @@ -167,26 +224,23 @@ func Retrieve(m *tb.Message) { chatList = append(chatList, m.Chat.ID) } + shared.Bot.Send(m.Chat, "--- Messages publiés ---") + for _, chatID := range chatList { - if savedMessages, exists := shared.ChatData.Get(chatID, "published_messages"); exists { - for _, message := range savedMessages.([]interface{}) { - if message, ok := message.(string); ok { - messageList = append(messageList, message) - } - } + messages := getPublishedMessages(chatID) + if len(messages) > 0 { + hasMessage = true + } + for _, message := range messages { + shared.Bot.Forward(m.Chat, &message) } } - if len(messageList) == 0 { + if !hasMessage { shared.Bot.Send(m.Chat, "Aucun message publié") - return } shared.Bot.Send(m.Chat, "--- Messages publiés ---") - for index, message := range messageList { - shared.Bot.Send(m.Chat, fmt.Sprintf("%d : %s", index, message)) - } - shared.Bot.Send(m.Chat, "--- Messages publiés ---") } // Get all users subscribed to the provided channel 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