mirror of
https://git.klmp200.net/ALFRED/ALFRED.git
synced 2025-01-18 18:46:44 +01:00
Simplify user chat subscription with a helper
This commit is contained in:
parent
0acdcb6351
commit
f6f17e7a7b
@ -19,6 +19,33 @@ import (
|
|||||||
tb "gopkg.in/tucnak/telebot.v2"
|
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 {
|
func getPublishedMessages(chatID int64) []tb.Message {
|
||||||
messages := []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
|
// 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 autorisé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
|
||||||
@ -60,30 +88,31 @@ func Subscribe(m *tb.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
|
subscriptions := getUserSubscribedChats(m.Sender.Username)
|
||||||
if !exists {
|
alreadySuscribed := false
|
||||||
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))
|
for _, sub := range subscriptions {
|
||||||
return
|
if sub == m.Chat.ID {
|
||||||
}
|
alreadySuscribed = true
|
||||||
splittedChats := strings.Split(userSubscribedChats, ":")
|
break
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
shared.Bot.Send(m.Chat, "Abonnement au chat : "+m.Chat.Title)
|
|
||||||
if len(userSubscribedChats) != 0 {
|
if alreadySuscribed {
|
||||||
shared.Users.Set(m.Sender.Username, "subscribed_chats", userSubscribedChats+":"+strconv.FormatInt(m.Chat.ID, 10))
|
shared.Bot.Send(m.Chat, "Vous êtes déjà abonné à ce chat : "+m.Chat.Title)
|
||||||
} else {
|
return
|
||||||
shared.Users.Set(m.Sender.Username, "subscribed_chats", strconv.FormatInt(m.Chat.ID, 10))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
// Unsubscribe user sending the command from the current chat
|
||||||
// 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 autorisé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
|
||||||
@ -94,20 +123,32 @@ func Unsubscribe(m *tb.Message) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
|
subscriptions := getUserSubscribedChats(m.Sender.Username)
|
||||||
if !exists || len(userSubscribedChats) == 0 {
|
|
||||||
shared.Bot.Send(m.Chat, "Vous n'êtes abonné à aucun chat")
|
if len(subscriptions) == 0 {
|
||||||
|
shared.Bot.Send(m.Chat, "Vous n'êtes abonnés à aucun chat")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
splittedChats := strings.Split(userSubscribedChats, ":")
|
|
||||||
for i, chatID := range splittedChats {
|
filteredSubscriptions := []int64{}
|
||||||
if chatID == strconv.FormatInt(m.Chat.ID, 10) {
|
wasSuscribed := false
|
||||||
shared.Bot.Send(m.Chat, "désabonnement du chat : "+m.Chat.Title)
|
|
||||||
splittedChats = append(splittedChats[:i], splittedChats[i+1:]...)
|
for _, sub := range subscriptions {
|
||||||
break
|
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
|
// ListSubscribers List all subscribers of the current chat
|
||||||
@ -204,20 +245,13 @@ func Retrieve(m *tb.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if m.Chat.Type == tb.ChatPrivate {
|
if m.Chat.Type == tb.ChatPrivate {
|
||||||
userSubscribedChats, exists := shared.Users.Get(m.Sender.Username, "subscribed_chats")
|
|
||||||
if !exists || len(userSubscribedChats) == 0 {
|
if m.Sender.Username == "" {
|
||||||
shared.Bot.Send(m.Chat, "Aucun abonnements")
|
shared.Bot.Send(m.Chat, "Il faut avoir enregistré un username pour pouvoir utiliser cette fonction")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, chat := range strings.Split(userSubscribedChats, ":") {
|
chatList = getUserSubscribedChats(m.Sender.Username)
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.Chat.Type == tb.ChatGroup || m.Chat.Type == tb.ChatSuperGroup {
|
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 {
|
func getSubscribers(chatID int64) []string {
|
||||||
var subscribers []string
|
var subscribers []string
|
||||||
for _, username := range shared.Users.GetUsernames() {
|
for _, username := range shared.Users.GetUsernames() {
|
||||||
userSubscribedChats, exists := shared.Users.Get(username, "subscribed_chats")
|
subscriptions := getUserSubscribedChats(username)
|
||||||
if exists {
|
for _, sub := range subscriptions {
|
||||||
splittedChats := strings.Split(userSubscribedChats, ":")
|
if sub == chatID {
|
||||||
for _, splittedChatID := range splittedChats {
|
subscribers = append(subscribers, username)
|
||||||
if splittedChatID == strconv.FormatInt(chatID, 10) {
|
break
|
||||||
subscribers = append(subscribers, username)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user