Simplify user chat subscription with a helper
the build was successful Details

This commit is contained in:
Antoine Bartuccio 2019-01-02 15:46:15 +01:00
parent 0acdcb6351
commit f6f17e7a7b
Signed by: klmp200
GPG Key ID: E7245548C53F904B
1 changed files with 76 additions and 44 deletions

View File

@ -19,6 +19,33 @@ import (
tb "gopkg.in/tucnak/telebot.v2"
)
func getUserSubscribedChats(username string) []int64 {
serializedSubscriptions, exists := shared.Users.Get(username, "subscribed_chats")
if !exists {
return []int64{}
}
subscriptions := []int64{}
for _, chatID := range strings.Split(serializedSubscriptions, ":") {
if id, err := strconv.ParseInt(chatID, 10, 64); err == nil {
subscriptions = append(subscriptions, id)
}
}
return subscriptions
}
func setUserSubscribedChats(username string, subscriptions []int64) {
subs := make([]string, len(subscriptions))
for i, sub := range subscriptions {
subs[i] = strconv.FormatInt(sub, 10)
}
shared.Users.Set(username, "subscribed_chats", strings.Join(subs, ":"))
}
func getPublishedMessages(chatID int64) []tb.Message {
messages := []tb.Message{}
@ -50,6 +77,7 @@ func setPublishedMessages(chatID int64, messages []tb.Message) {
// 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 autorisée pour ce type de chat")
return
@ -60,30 +88,31 @@ func Subscribe(m *tb.Message) {
return
}
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists {
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10))
return
}
splittedChats := strings.Split(userSubscribedChats, ":")
for _, chatID := range splittedChats {
if chatID == strconv.FormatInt(m.Chat.ID, 10) {
shared.Bot.Send(m.Chat, "Vous êtes déjà abonné à ce chat : "+m.Chat.Title)
return
subscriptions := getUserSubscribedChats(m.Sender.Username)
alreadySuscribed := false
for _, sub := range subscriptions {
if sub == m.Chat.ID {
alreadySuscribed = true
break
}
}
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
if len(userSubscribedChats) != 0 {
shared.Users.Set(m.Sender.Username, "subscribed_chats", userSubscribedChats+":"+strconv.FormatInt(m.Chat.ID, 10))
} else {
shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10))
if alreadySuscribed {
shared.Bot.Send(m.Chat, "Vous êtes déjà abonné à ce chat : "+m.Chat.Title)
return
}
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
subscriptions = append(subscriptions, m.Chat.ID)
setUserSubscribedChats(m.Sender.Username, subscriptions)
}
// Unsubscribe user sending the command from the current chat
// 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 autorisée pour ce type de chat")
return
@ -94,20 +123,32 @@ func Unsubscribe(m *tb.Message) {
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")
subscriptions := getUserSubscribedChats(m.Sender.Username)
if len(subscriptions) == 0 {
shared.Bot.Send(m.Chat, "Vous n'êtes abonnés à aucun chat")
return
}
splittedChats := strings.Split(userSubscribedChats, ":")
for i, chatID := range splittedChats {
if chatID == strconv.FormatInt(m.Chat.ID, 10) {
shared.Bot.Send(m.Chat, "désabonnement du chat : "+m.Chat.Title)
splittedChats = append(splittedChats[:i], splittedChats[i+1:]...)
break
filteredSubscriptions := []int64{}
wasSuscribed := false
for _, sub := range subscriptions {
if sub == m.Chat.ID {
wasSuscribed = true
continue
}
filteredSubscriptions = append(filteredSubscriptions, sub)
}
shared.Users.Set(m.Sender.Username, "subscribed_chats", strings.Join(splittedChats, ":"))
if !wasSuscribed {
shared.Bot.Send(m.Chat, "Vous n'êtes pas abonné au chat : "+m.Chat.Title)
return
}
shared.Bot.Send(m.Chat, "désabonnement du chat : "+m.Chat.Title)
setUserSubscribedChats(m.Sender.Username, filteredSubscriptions)
}
// ListSubscribers List all subscribers of the current chat
@ -204,20 +245,13 @@ func Retrieve(m *tb.Message) {
}
if m.Chat.Type == tb.ChatPrivate {
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
if !exists || len(userSubscribedChats) == 0 {
shared.Bot.Send(m.Chat, "Aucun abonnements")
if m.Sender.Username == "" {
shared.Bot.Send(m.Chat, "Il faut avoir enregistré un username pour pouvoir utiliser cette fonction")
return
}
for _, chat := range strings.Split(userSubscribedChats, ":") {
chatID, err := strconv.ParseInt(chat, 10, 64)
if err != nil {
shared.Bot.Send(m.Chat, "Erreur lors de la récupération de vos évènements")
return
}
chatList = append(chatList, chatID)
}
chatList = getUserSubscribedChats(m.Sender.Username)
}
if m.Chat.Type == tb.ChatGroup || m.Chat.Type == tb.ChatSuperGroup {
@ -247,13 +281,11 @@ func Retrieve(m *tb.Message) {
func getSubscribers(chatID int64) []string {
var subscribers []string
for _, username := range shared.Users.GetUsernames() {
userSubscribedChats, exists := shared.Users.Get(username, "subscribed_chats")
if exists {
splittedChats := strings.Split(userSubscribedChats, ":")
for _, splittedChatID := range splittedChats {
if splittedChatID == strconv.FormatInt(chatID, 10) {
subscribers = append(subscribers, username)
}
subscriptions := getUserSubscribedChats(username)
for _, sub := range subscriptions {
if sub == chatID {
subscribers = append(subscribers, username)
break
}
}
}