diff --git a/src/manageMsg.go b/src/manageMsg.go index 3352a74..468bbcd 100644 --- a/src/manageMsg.go +++ b/src/manageMsg.go @@ -10,11 +10,11 @@ import ( ) type groupMsg struct { - SenderID int `sql:"sender_id" json:"sender_id"` - Group userGroup `sql:"group" json:"group"` - Msg string `sql:"msg" json:"msg"` - Date time.Time `sql:"date" json:"date"` - Sent bool `sql:"sent" json:"sent"` + SenderID int `sql:"sender_id" json:"sender_id"` + Group []userGroup `sql:"group" json:"group"` + Msg string `sql:"msg" json:"msg"` + Date time.Time `sql:"date" json:"date"` + Sent bool `sql:"sent" json:"sent"` } func modifyPrevMsg(userID int, storedMsg *tb.StoredMessage, newMsg string, newOptions *tb.SendOptions) error { @@ -146,7 +146,7 @@ func sendMsgWithSpecificMenu(user *tb.User, msg string, menu [][]tb.InlineButton return nil } -func addNewGroupMsg(sender *tb.User, group userGroup, msg string) (int64, error) { +func addNewGroupMsg(sender *tb.User, group []userGroup, msg string) (int64, error) { newGroupMsg := groupMsg{sender.ID, group, msg, time.Now(), false} jsonMsg, err := json.Marshal(newGroupMsg) if err != nil { @@ -162,13 +162,13 @@ func addNewGroupMsg(sender *tb.User, group userGroup, msg string) (int64, error) return msgID - 1, nil } -func setGroupMsg(msg *groupMsg, index int64) error { +func setGroupMsg(msg *groupMsg, msgID int64) error { jsonMsg, err := json.Marshal(msg) if err != nil { log.Printf("Error in marshalling groupMsg to json: %v", err) return ErrJSONMarshall } - err = redisClient.LSet(groupMsgs, index, jsonMsg).Err() + err = redisClient.LSet(groupMsgs, msgID, jsonMsg).Err() if err != nil { log.Printf("Error modifying a groupMsg: %v", err) return ErrRedisSetList @@ -176,6 +176,24 @@ func setGroupMsg(msg *groupMsg, index int64) error { return nil } +func addUGToGroupMsg(msgID int64, group userGroup) error { + msg, err := redisClient.LIndex(groupMsgs, msgID).Result() + if err != nil { + log.Printf("Error retriving group message from hash: %v", err) + return ErrRedisRetrieveHash + } + jsonMsg := &groupMsg{} + err = json.Unmarshal([]byte(msg), jsonMsg) + if err != nil { + log.Printf("Error unmarshalling groupMsg: %v", err) + return ErrJSONUnmarshall + } + jsonMsg.Group = append(jsonMsg.Group, group) + setGroupMsg(jsonMsg, msgID) + + return nil +} + func sendMsgToGroup(msgID string) error { ID, err := strconv.ParseInt(msgID, 10, 64) if err != nil { @@ -193,34 +211,47 @@ func sendMsgToGroup(msgID string) error { log.Printf("Error unmarshalling groupMsg: %v", err) return ErrJSONUnmarshall } - if jsonMsg.Sent { - return ErrSendMsg - } sender, err := getUserInfo(jsonMsg.SenderID) if err != nil { log.Printf("Error retrieving sender info: %v", err) return ErrGetUser } - users, err := getUsersInGroup(jsonMsg.Group) - if err != nil { - log.Printf("Error retrieving users in sendTo group: %v", err) - return ErrGroupInvalid + if jsonMsg.Sent { + err = sendMsgWithMenu(sender, "Il messaggio é giá stato inviato!", false) + if err != nil { + log.Printf("Error sending msg to sender: %v", err) + return ErrSendMsg + } + + } else { + err = sendMsg(sender, "Inizio invio massivo", false) + if err != nil { + log.Printf("Error sending msg to sender: %v", err) + return ErrSendMsg + } } - for _, userID := range users { - user, err := getUserInfo(userID) + for _, group := range jsonMsg.Group { + users, err := getUsersInGroup(group) if err != nil { - log.Printf("Error retrieving user info from id: %v", err) - continue + log.Printf("Error retrieving users in sendTo group: %v", err) + return ErrGroupInvalid } - groupName, _ := getGroupName(jsonMsg.Group) - msg = "*Messaggio inviato da " + sender.FirstName + " a tutta la sezione " + groupName + "*\n" + jsonMsg.Msg - err = sendMsg(user, msg, true) - if err != nil { - log.Printf("Error sending msg to user: %v", err) - } - err = sendMsgWithMenu(user, msgReceivedMsg, true) - if err != nil { - log.Printf("Error sending msg to user: %v", err) + for _, userID := range users { + user, err := getUserInfo(userID) + if err != nil { + log.Printf("Error retrieving user info from id: %v", err) + continue + } + groupName, _ := getGroupName(group) + msg = "*Messaggio inviato da " + sender.FirstName + " a tutta la sezione " + groupName + "*\n" + jsonMsg.Msg + err = sendMsg(user, msg, true) + if err != nil { + log.Printf("Error sending msg to user: %v", err) + } + err = sendMsgWithMenu(user, msgReceivedMsg, true) + if err != nil { + log.Printf("Error sending msg to user: %v", err) + } } } @@ -231,7 +262,7 @@ func sendMsgToGroup(msgID string) error { log.Printf("Error updating groupMsg after send: %v", err) } - err = sendMsgWithMenu(sender, "Messaggio inviato a tutti i componenti della sezione", false) + err = sendMsgWithMenu(sender, "Messaggio inviato a tutti i componenti delle sezioni indicate", false) if err != nil { log.Printf("Error sending msg to sender: %v", err) } diff --git a/src/telegramCommands.go b/src/telegramCommands.go index 49e9a8b..69e1b3b 100644 --- a/src/telegramCommands.go +++ b/src/telegramCommands.go @@ -3,7 +3,6 @@ package main import ( "log" "strconv" - "strings" tb "gopkg.in/tucnak/telebot.v2" ) @@ -116,14 +115,14 @@ func authUserCmd(sender *tb.User, payload string, newMsg bool) { log.Printf("Error retriving user groups: %v", err) } - menu := getAuthUserMenu() - menu[0][0].Data = strconv.Itoa(user.ID) - menu[0][1].Data = strconv.Itoa(user.ID) - menu[1][0].Data = strconv.Itoa(user.ID) - menu[1][1].Data = strconv.Itoa(user.ID) - menu[2][0].Data = strconv.Itoa(user.ID) - menu[2][1].Data = strconv.Itoa(user.ID) - menu[2][2].Data = strconv.Itoa(user.ID) + menu := getUserGroupMenu() + menu[0][0].Data = strconv.Itoa(user.ID) + "+auth" + menu[0][1].Data = strconv.Itoa(user.ID) + "+auth" + menu[1][0].Data = strconv.Itoa(user.ID) + "+auth" + menu[1][1].Data = strconv.Itoa(user.ID) + "+auth" + menu[2][0].Data = strconv.Itoa(user.ID) + "+auth" + menu[2][1].Data = strconv.Itoa(user.ID) + "+auth" + menu[2][2].Data = strconv.Itoa(user.ID) + "+auth" for _, group := range userGroups { switch group { @@ -176,14 +175,14 @@ func deAuthUserCmd(sender *tb.User, payload string, newMsg bool) { log.Printf("Error retriving user description: %v", err) } - menu := getAuthUserMenu() - menu[0][0].Data = strconv.Itoa(user.ID) + "+remove" - menu[0][1].Data = strconv.Itoa(user.ID) + "+remove" - menu[1][0].Data = strconv.Itoa(user.ID) + "+remove" - menu[1][1].Data = strconv.Itoa(user.ID) + "+remove" - menu[2][0].Data = strconv.Itoa(user.ID) + "+remove" - menu[2][1].Data = strconv.Itoa(user.ID) + "+remove" - menu[2][2].Data = strconv.Itoa(user.ID) + "+remove" + menu := getUserGroupMenu() + menu[0][0].Data = strconv.Itoa(user.ID) + "+deAuth" + menu[0][1].Data = strconv.Itoa(user.ID) + "+deAuth" + menu[1][0].Data = strconv.Itoa(user.ID) + "+deAuth" + menu[1][1].Data = strconv.Itoa(user.ID) + "+deAuth" + menu[2][0].Data = strconv.Itoa(user.ID) + "+deAuth" + menu[2][1].Data = strconv.Itoa(user.ID) + "+deAuth" + menu[2][2].Data = strconv.Itoa(user.ID) + "+deAuth" if is, _ := isUserInGroup(user.ID, ugSoprano); !is { menu[0][0].Text = "" @@ -291,40 +290,50 @@ func helpCmd(user *tb.User, newMsg bool) { } func sendMsgCmd(sender *tb.User, payload string, newMsg bool) { - arg := strings.SplitN(payload, " ", 2) - if payload == "" || len(arg) != 2 || arg[1] == "" || arg[1] == " " { + if payload == "" { err := sendMsgWithMenu(sender, sendMsgHowToMsg, newMsg) if err != nil { log.Printf("Error in sending message: %v", err) } } else { - group, err := getUserGroupFromStr(arg[0]) + msgID, err := addNewGroupMsg(sender, []userGroup{}, payload) if err != nil { - log.Printf("Error in parsing the userGroup: %v", err) + log.Printf("Error adding new groupMsg in db: %v", err) return } + menu := getUserGroupMenu() + menu[3] = append(menu[3], confirmSendBtn) + menu[0][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg" + menu[0][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg" + menu[1][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg" + menu[1][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg" + menu[2][0].Data = strconv.FormatInt(msgID, 10) + "+sendUg" + menu[2][1].Data = strconv.FormatInt(msgID, 10) + "+sendUg" + menu[2][2].Data = strconv.FormatInt(msgID, 10) + "+sendUg" + menu[3][1].Data = strconv.FormatInt(msgID, 10) + if is, _ := isUserInGroup(sender.ID, ugSoprano); !is { + menu[0][0].Text = "" + } + if is, _ := isUserInGroup(sender.ID, ugContralto); !is { + menu[0][1].Text = "" + } + if is, _ := isUserInGroup(sender.ID, ugTenore); !is { + menu[1][0].Text = "" + } + if is, _ := isUserInGroup(sender.ID, ugBasso); !is { + menu[1][1].Text = "" + } + if is, _ := isUserInGroup(sender.ID, ugCommissario); !is { + menu[2][0].Text = "" + } + if is, _ := isUserInGroup(sender.ID, ugReferente); !is { + menu[2][1].Text = "" + } + if is, _ := isUserInGroup(sender.ID, ugPreparatore); !is { + menu[2][2].Text = "" + } - is, err := isUserInGroup(sender.ID, group) - if err != nil { - log.Printf("Error checking if sender is in sendTo group: %v", err) - return - } - if is { - ugName, _ := getGroupName(group) - //sendMsgToGroup(sender, group, arg[1]) - msgID, err := addNewGroupMsg(sender, group, arg[1]) - if err != nil { - log.Printf("Error adding new groupMsg in db: %v", err) - return - } - msg := "*Sei sicuro di voler inviare il seguente messaggio al gruppo " + ugName + "?*\n" + arg[1] - sendMsgMenu[0][0].Data = strconv.FormatInt(msgID, 10) - sendMsgWithSpecificMenu(sender, msg, sendMsgMenu, true) - } else { - err = sendMsgWithMenu(sender, sendMsgErrMsg, true) - if err != nil { - log.Printf("Error sending msg to user: %v", err) - } - } + msg := "*Il messaggio che stai per inviare é *\n" + payload + "\n*Seleziona i gruppi a cui vuoi inviarlo*" + sendMsgWithSpecificMenu(sender, msg, menu, true) } } diff --git a/src/telegramMenus.go b/src/telegramMenus.go index a5b7b10..f26daed 100644 --- a/src/telegramMenus.go +++ b/src/telegramMenus.go @@ -69,32 +69,32 @@ var ( Unique: "confirm_send_btn", Text: "\xE2\x9C\x85 Conferma", } - authUGSopranoBtn = tb.InlineButton{ - Unique: "auth_ugSoprano_btn", + ugSopranoBtn = tb.InlineButton{ + Unique: "ugSoprano_btn", Text: "\xF0\x9F\x91\xA7 Soprani", } - authUGContraltoBtn = tb.InlineButton{ - Unique: "auth_ugContralto_btn", + ugContraltoBtn = tb.InlineButton{ + Unique: "ugContralto_btn", Text: "\xF0\x9F\x91\xA9 Contralti", } - authUGTenoreBtn = tb.InlineButton{ - Unique: "auth_ugTenore_btn", + ugTenoreBtn = tb.InlineButton{ + Unique: "ugTenore_btn", Text: "\xF0\x9F\x91\xA6 Tenori", } - authUGBassoBtn = tb.InlineButton{ - Unique: "auth_ugBasso_btn", + ugBassoBtn = tb.InlineButton{ + Unique: "ugBasso_btn", Text: "\xF0\x9F\x91\xA8 Bassi", } - authUGCommissarioBtn = tb.InlineButton{ - Unique: "auth_ugCommissario_btn", + ugCommissarioBtn = tb.InlineButton{ + Unique: "ugCommissario_btn", Text: "\xF0\x9F\x93\x9D Commissari", } - authUGReferenteBtn = tb.InlineButton{ - Unique: "auth_ugReferente_btn", + ugReferenteBtn = tb.InlineButton{ + Unique: "ugReferente_btn", Text: "\xF0\x9F\x93\x8B Referenti", } - authUGPreparatoreBtn = tb.InlineButton{ - Unique: "auth_ugPreparatori_btn", + ugPreparatoreBtn = tb.InlineButton{ + Unique: "ugPreparatori_btn", Text: "\xF0\x9F\x8E\xB9 Preparatori", } ) @@ -118,64 +118,99 @@ func setBotMenus() error { startMenu = append(startMenu, []tb.InlineButton{startBtn}) backMenu = append(backMenu, []tb.InlineButton{backBtn}) botInfoMenu = append(botInfoMenu, []tb.InlineButton{helpBtn, stopBtn}, []tb.InlineButton{backBtn}) - authUserMenu = append(authUserMenu, - []tb.InlineButton{authUGSopranoBtn, authUGContraltoBtn}, - []tb.InlineButton{authUGTenoreBtn, authUGBassoBtn}, - []tb.InlineButton{authUGCommissarioBtn, authUGReferenteBtn, authUGPreparatoreBtn}, + /* authUserMenu = append(authUserMenu, + []tb.InlineButton{ugSopranoBtn, ugContraltoBtn}, + []tb.InlineButton{ugTenoreBtn, ugBassoBtn}, + []tb.InlineButton{ugCommissarioBtn, ugReferenteBtn, ugPreparatoreBtn}, []tb.InlineButton{backBtn}, - ) + ) */ sendMsgMenu = append(sendMsgMenu, []tb.InlineButton{confirmSendBtn, backBtn}) return nil } -func getAuthUserMenu() [][]tb.InlineButton { - var authUserMenu [][]tb.InlineButton - authUserMenu = append(authUserMenu, - []tb.InlineButton{authUGSopranoBtn, authUGContraltoBtn}, - []tb.InlineButton{authUGTenoreBtn, authUGBassoBtn}, - []tb.InlineButton{authUGCommissarioBtn, authUGReferenteBtn, authUGPreparatoreBtn}, +func getUserGroupMenu() [][]tb.InlineButton { + var ugMenu [][]tb.InlineButton + ugMenu = append(ugMenu, + []tb.InlineButton{ugSopranoBtn, ugContraltoBtn}, + []tb.InlineButton{ugTenoreBtn, ugBassoBtn}, + []tb.InlineButton{ugCommissarioBtn, ugReferenteBtn, ugPreparatoreBtn}, []tb.InlineButton{backBtn}, ) - return authUserMenu + return ugMenu } -func groupCallback(c *tb.Callback, group userGroup) { +func ugBtnCallback(c *tb.Callback, group userGroup) { dataContent := strings.Split(c.Data, "+") - userID, err := strconv.Atoi(dataContent[0]) - if err != nil { - log.Printf("Error converting string to int: %v", err) + if len(dataContent) <= 1 { + log.Printf("Error: too few arguments") return } - var errAlert, authAlert string - var add bool + var userID int + var msgID int64 + var errAlert, successAlert string + auth, deAuth, sendUg := false, false, false groupName, err := getGroupName(group) - if len(dataContent) > 1 && dataContent[1] == "remove" { - add = false + if dataContent[1] == "deAuth" { + deAuth = true errAlert = "Impossibile deautorizzare l'utente per il gruppo " + groupName - authAlert = "Utente " + dataContent[0] + " rimosso dal gruppo " + groupName - } else { - add = true + successAlert = "Utente " + dataContent[0] + " rimosso dal gruppo " + groupName + userID, err = strconv.Atoi(dataContent[0]) + if err != nil { + log.Printf("Error converting string to int: %v", err) + return + } + } else if dataContent[1] == "auth" { + auth = true errAlert = "Impossibile aggiungere l'utente al gruppo " + groupName - authAlert = "Utente " + dataContent[0] + " aggiunto al gruppo " + groupName + successAlert = "Utente " + dataContent[0] + " aggiunto al gruppo " + groupName + userID, err = strconv.Atoi(dataContent[0]) + if err != nil { + log.Printf("Error converting string to int: %v", err) + return + } + } else if dataContent[1] == "sendUg" { + sendUg = true + errAlert = "Impossibile aggiungere il gruppo " + groupName + " alla lista dei destinatari" + successAlert = "Gruppo " + groupName + " aggiunto alla lista dei destinatari" + msgID, err = strconv.ParseInt(dataContent[0], 10, 64) + if err != nil { + log.Printf("Error converting msgID to int64: %v", err) + return + } } - err = addUserGroupCmd(userID, group, add) - if err != nil { - bot.Respond(c, &tb.CallbackResponse{ - Text: errAlert, - ShowAlert: true, - }) - } else { - bot.Respond(c, &tb.CallbackResponse{ - Text: authAlert, - ShowAlert: true, - }) - if add { - authUserCmd(c.Sender, dataContent[0], false) + if auth || deAuth { + err = addUserGroupCmd(userID, group, auth) + if err != nil { + bot.Respond(c, &tb.CallbackResponse{ + Text: errAlert, + ShowAlert: true, + }) } else { - deAuthUserCmd(c.Sender, dataContent[0], false) + bot.Respond(c, &tb.CallbackResponse{ + Text: successAlert, + ShowAlert: true, + }) + if auth { + authUserCmd(c.Sender, dataContent[0], false) + } else if deAuth { + deAuthUserCmd(c.Sender, dataContent[0], false) + } + } + } else if sendUg { + err = addUGToGroupMsg(msgID, group) + if err != nil { + bot.Respond(c, &tb.CallbackResponse{ + Text: errAlert, + ShowAlert: true, + }) + } else { + bot.Respond(c, &tb.CallbackResponse{ + Text: successAlert, + ShowAlert: true, + }) } } } @@ -216,7 +251,6 @@ func setBotCallbacks() error { sendMsgWithMenu(c.Sender, menuMsg, false) }) - //TODO bot.Handle(&confirmSendBtn, func(c *tb.Callback) { bot.Respond(c, &tb.CallbackResponse{ Text: sentStartedMsg, @@ -240,32 +274,32 @@ func setBotCallbacks() error { sendMsgWithMenu(c.Sender, sendMsgHowToMsg, false) }) - bot.Handle(&authUGSopranoBtn, func(c *tb.Callback) { - groupCallback(c, ugSoprano) + bot.Handle(&ugSopranoBtn, func(c *tb.Callback) { + ugBtnCallback(c, ugSoprano) }) - bot.Handle(&authUGContraltoBtn, func(c *tb.Callback) { - groupCallback(c, ugContralto) + bot.Handle(&ugContraltoBtn, func(c *tb.Callback) { + ugBtnCallback(c, ugContralto) }) - bot.Handle(&authUGTenoreBtn, func(c *tb.Callback) { - groupCallback(c, ugTenore) + bot.Handle(&ugTenoreBtn, func(c *tb.Callback) { + ugBtnCallback(c, ugTenore) }) - bot.Handle(&authUGBassoBtn, func(c *tb.Callback) { - groupCallback(c, ugBasso) + bot.Handle(&ugBassoBtn, func(c *tb.Callback) { + ugBtnCallback(c, ugBasso) }) - bot.Handle(&authUGCommissarioBtn, func(c *tb.Callback) { - groupCallback(c, ugCommissario) + bot.Handle(&ugCommissarioBtn, func(c *tb.Callback) { + ugBtnCallback(c, ugCommissario) }) - bot.Handle(&authUGReferenteBtn, func(c *tb.Callback) { - groupCallback(c, ugReferente) + bot.Handle(&ugReferenteBtn, func(c *tb.Callback) { + ugBtnCallback(c, ugReferente) }) - bot.Handle(&authUGPreparatoreBtn, func(c *tb.Callback) { - groupCallback(c, ugPreparatore) + bot.Handle(&ugPreparatoreBtn, func(c *tb.Callback) { + ugBtnCallback(c, ugPreparatore) }) return nil