Merge branch 'bro-features' of ALFRED/ALFRED into master
the build was successful Details

This commit is contained in:
Antoine Bartuccio 2019-01-02 13:41:05 +00:00 committed by Gitea
commit 4ecc1e2072
2 changed files with 100 additions and 44 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. // This module uses a property named "subscribed_chats" in the shared Users structure.
@ -11,7 +11,7 @@
package commands package commands
import ( import (
"fmt" "encoding/json"
"strconv" "strconv"
"strings" "strings"
@ -19,13 +19,47 @@ import (
tb "gopkg.in/tucnak/telebot.v2" 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 // Subscribe user sending the command to the current chat
// Command syntax : /subscribe // Command syntax : /subscribe
func Subscribe(m *tb.Message) { func Subscribe(m *tb.Message) {
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { 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 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") userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists { if !exists {
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title) shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
@ -51,9 +85,15 @@ func Subscribe(m *tb.Message) {
// Command syntax : /unsubscribe // Command syntax : /unsubscribe
func Unsubscribe(m *tb.Message) { func Unsubscribe(m *tb.Message) {
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { 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 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") userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists || len(userSubscribedChats) == 0 { if !exists || len(userSubscribedChats) == 0 {
shared.Bot.Send(m.Chat, "Vous n'êtes abonné à aucun chat") shared.Bot.Send(m.Chat, "Vous n'êtes abonné à aucun chat")
@ -74,7 +114,7 @@ func Unsubscribe(m *tb.Message) {
// Command syntax : /listsubscribers // Command syntax : /listsubscribers
func ListSubscribers(m *tb.Message) { func ListSubscribers(m *tb.Message) {
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup { 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 return
} }
subscribers := m.Chat.Title + " subscribers : \n\n" subscribers := m.Chat.Title + " subscribers : \n\n"
@ -85,52 +125,69 @@ func ListSubscribers(m *tb.Message) {
} }
// Publish a message from the current chat // 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) { func Publish(m *tb.Message) {
if m.ReplyTo == nil { if m.ReplyTo == nil {
shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le publier") shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le publier")
return return
} }
defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text) if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup {
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages") shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat")
if !exists {
messageList := []string{m.ReplyTo.Text}
shared.ChatData.Set(m.Chat.ID, "published_messages", messageList)
return return
} }
shared.ChatData.Set(
m.Chat.ID, defer shared.Bot.Send(m.Chat, "Message publié : "+m.ReplyTo.Text)
"published_messages", savedMessages := getPublishedMessages(m.Chat.ID)
append(savedMessages.([]interface{}), m.ReplyTo.Text), setPublishedMessages(m.Chat.ID, append(savedMessages, *m.ReplyTo))
)
} }
// Unpublish remove a message from published messages in the current chat // 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) { func Unpublish(m *tb.Message) {
parsedCommand := strings.Split(m.Text, " ")
if len(parsedCommand) < 2 { if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup {
shared.Bot.Send(m.Chat, "syntaxe : /unpublish [publication ID]") shared.Bot.Send(m.Chat, "Cette commande n'est pas autorisée pour ce type de chat")
return return
} }
index, err := strconv.Atoi(parsedCommand[1])
if err != nil { if m.ReplyTo == nil {
shared.Bot.Send(m.Chat, "ID de publication invalide") shared.Bot.Send(m.Chat, "Veuillez répondre à un message pour le dépublier")
return return
} }
savedMessages, exists := shared.ChatData.Get(m.Chat.ID, "published_messages")
if !exists || len(savedMessages.([]interface{})) == 0 { if !m.ReplyTo.IsForwarded() {
shared.Bot.Send(m.Chat, "Aucun message publié") shared.Bot.Send(m.Chat, "Ce message ne peut pas avoir été publié")
return 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 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") shared.Bot.Send(m.Chat, "Message supprimé des publication")
} }
@ -139,7 +196,7 @@ func Unpublish(m *tb.Message) {
// Command syntax : /retrieve // Command syntax : /retrieve
func Retrieve(m *tb.Message) { func Retrieve(m *tb.Message) {
chatList := []int64{} chatList := []int64{}
messageList := []string{} hasMessage := false
if m.Chat.Type != tb.ChatGroup && m.Chat.Type != tb.ChatSuperGroup && m.Chat.Type != tb.ChatPrivate { 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") 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) chatList = append(chatList, m.Chat.ID)
} }
shared.Bot.Send(m.Chat, "--- Messages publiés ---")
for _, chatID := range chatList { for _, chatID := range chatList {
if savedMessages, exists := shared.ChatData.Get(chatID, "published_messages"); exists { messages := getPublishedMessages(chatID)
for _, message := range savedMessages.([]interface{}) { if len(messages) > 0 {
if message, ok := message.(string); ok { hasMessage = true
messageList = append(messageList, message) }
} for _, message := range messages {
} shared.Bot.Forward(m.Chat, &message)
} }
} }
if len(messageList) == 0 { if !hasMessage {
shared.Bot.Send(m.Chat, "Aucun message publié") shared.Bot.Send(m.Chat, "Aucun message publié")
return
} }
shared.Bot.Send(m.Chat, "--- Messages publiés ---") 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 // Get all users subscribed to the provided channel

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 **Usage location** : Group chat only
**Command syntax** : /unpublish [publication ID] **usage conditions** : Reply to the message to unpublish
**Command syntax** : /unpublish
### Retrieve ### Retrieve