|
|
|
|
@ -218,48 +218,17 @@ func (h *Handler) HandleCommentTextInput(bot *tgbotapi.BotAPI, msg *tgbotapi.Mes
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user, err := h.ipc.Database.User.GetUserByTelegramIDDB(ctx, telegramID)
|
|
|
|
|
if err != nil || user == nil || user.ID == nil {
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
commentState.Stage = "await_visibility"
|
|
|
|
|
commentState.Text = msg.Text
|
|
|
|
|
commentState.Targets = make(map[uint64]bool)
|
|
|
|
|
h.States.SetCommentFlow(ctx, telegramID, commentState) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
approval, err := h.ipc.Database.Approval.GetApprovalByIDDB(ctx, commentState.ApprovalID)
|
|
|
|
|
if err != nil || approval == nil {
|
|
|
|
|
sendText(bot, chatID, "⚠️ Заявка не найдена.")
|
|
|
|
|
h.States.ClearCommentFlow(ctx, telegramID) //nolint:errcheck
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
bot.Request(tgbotapi.NewDeleteMessage(chatID, msg.MessageID)) // Удаляем сообщение с текстом для чистоты
|
|
|
|
|
|
|
|
|
|
comment := &typescore.ApprovalComment{
|
|
|
|
|
ApprovalID: &commentState.ApprovalID,
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Comment: &msg.Text,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, _, err = h.ipc.Database.ApprovalComment.CreateApprovalCommentDB(ctx, nil, comment)
|
|
|
|
|
if err != nil {
|
|
|
|
|
logrus.Errorf("HandleCommentTextInput: CreateApprovalCommentDB err=%v", err)
|
|
|
|
|
sendText(bot, chatID, "Ошибка при сохранении комментария.")
|
|
|
|
|
h.States.ClearCommentFlow(ctx, telegramID) //nolint:errcheck
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.States.ClearCommentFlow(ctx, telegramID) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
userName := "—"
|
|
|
|
|
if user.FullName != nil {
|
|
|
|
|
userName = *user.FullName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendText(bot, chatID, fmt.Sprintf(
|
|
|
|
|
"✅ <b>Комментарий добавлен</b>\n\n"+
|
|
|
|
|
"📋 Заявка: #%d\n"+
|
|
|
|
|
"👤 Автор: %s\n"+
|
|
|
|
|
"💬 Текст: %s",
|
|
|
|
|
commentState.ApprovalID, userName, msg.Text,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
h.notifyAllParticipantsAboutComment(bot, ctx, approval, user, comment, telegramID)
|
|
|
|
|
replyMsg := tgbotapi.NewMessage(chatID, "💬 <b>Комментарий сохранен.</b>\n\nВыберите, кому отправить уведомление:")
|
|
|
|
|
replyMsg.ParseMode = "HTML"
|
|
|
|
|
replyMsg.ReplyMarkup = keyboard.BuildCommentVisibilityMenu()
|
|
|
|
|
bot.Send(replyMsg) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
@ -488,3 +457,240 @@ func (h *Handler) GetCommentsCount(ctx context.Context, approvalID uint64) int {
|
|
|
|
|
}
|
|
|
|
|
return len(comments)
|
|
|
|
|
}
|
|
|
|
|
func (h *Handler) HandleCommentVisibilityCallback(bot *tgbotapi.BotAPI, cq *tgbotapi.CallbackQuery, action string) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
telegramID := cq.From.ID
|
|
|
|
|
chatID := cq.Message.Chat.ID
|
|
|
|
|
|
|
|
|
|
bot.Request(tgbotapi.NewCallback(cq.ID, "")) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
if action == "cancel" {
|
|
|
|
|
bot.Request(tgbotapi.NewDeleteMessage(chatID, cq.Message.MessageID)) //nolint:errcheck
|
|
|
|
|
h.States.ClearCommentFlow(ctx, telegramID) //nolint:errcheck
|
|
|
|
|
sendText(bot, chatID, "❌ Добавление комментария отменено.")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
state, err := h.States.GetCommentFlow(ctx, telegramID)
|
|
|
|
|
if err != nil || state == nil || state.Stage != "await_visibility" {
|
|
|
|
|
bot.Request(tgbotapi.NewDeleteMessage(chatID, cq.Message.MessageID)) //nolint:errcheck
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
user, _ := h.ipc.Database.User.GetUserByTelegramIDDB(ctx, telegramID)
|
|
|
|
|
approval, _ := h.ipc.Database.Approval.GetApprovalByIDDB(ctx, state.ApprovalID)
|
|
|
|
|
|
|
|
|
|
// Если выбрали "Конкретным" -> показываем меню мультиселекта
|
|
|
|
|
if action == "specific" {
|
|
|
|
|
state.Stage = "await_targets"
|
|
|
|
|
h.States.SetCommentFlow(ctx, telegramID, state) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
participants := h.getFilteredParticipants(ctx, approval, user)
|
|
|
|
|
editMsg := tgbotapi.NewEditMessageTextAndMarkup(chatID, cq.Message.MessageID,
|
|
|
|
|
"👤 <b>Выберите, кого нужно уведомить:</b>",
|
|
|
|
|
keyboard.BuildCommentTargetsMenu(participants, state.Targets),
|
|
|
|
|
)
|
|
|
|
|
editMsg.ParseMode = "HTML"
|
|
|
|
|
bot.Request(editMsg) //nolint:errcheck
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ─── Если "Всем" или "Без пушей" ───
|
|
|
|
|
bot.Request(tgbotapi.NewDeleteMessage(chatID, cq.Message.MessageID)) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
// Создаем коммент в базе
|
|
|
|
|
comment := &typescore.ApprovalComment{
|
|
|
|
|
ApprovalID: &state.ApprovalID,
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Comment: &state.Text,
|
|
|
|
|
}
|
|
|
|
|
h.ipc.Database.ApprovalComment.CreateApprovalCommentDB(ctx, nil, comment) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
userName := "—"
|
|
|
|
|
if user.FullName != nil {
|
|
|
|
|
userName = *user.FullName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
statusText := "оставлен как дополнение"
|
|
|
|
|
if action == "all" {
|
|
|
|
|
statusText = "отправлен всем участникам"
|
|
|
|
|
h.notifyAllParticipantsAboutComment(bot, ctx, approval, user, comment, telegramID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendText(bot, chatID, fmt.Sprintf(
|
|
|
|
|
"✅ <b>Комментарий %s</b>\n\n"+
|
|
|
|
|
"📋 Заявка: #%d\n"+
|
|
|
|
|
"👤 Автор: %s\n"+
|
|
|
|
|
"💬 Текст: %s",
|
|
|
|
|
statusText, state.ApprovalID, userName, state.Text,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
h.States.ClearCommentFlow(ctx, telegramID) //nolint:errcheck
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) HandleCommentTargetToggle(bot *tgbotapi.BotAPI, cq *tgbotapi.CallbackQuery, targetIDStr string) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
telegramID := cq.From.ID
|
|
|
|
|
|
|
|
|
|
bot.Request(tgbotapi.NewCallback(cq.ID, "")) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
state, err := h.States.GetCommentFlow(ctx, telegramID)
|
|
|
|
|
if err != nil || state == nil || state.Stage != "await_targets" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
targetID := parseID("id:"+targetIDStr, "id:") // Используем вашу функцию parseID
|
|
|
|
|
if state.Targets == nil {
|
|
|
|
|
state.Targets = make(map[uint64]bool)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if state.Targets[targetID] {
|
|
|
|
|
delete(state.Targets, targetID)
|
|
|
|
|
} else {
|
|
|
|
|
state.Targets[targetID] = true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
h.States.SetCommentFlow(ctx, telegramID, state) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
user, _ := h.ipc.Database.User.GetUserByTelegramIDDB(ctx, telegramID)
|
|
|
|
|
approval, _ := h.ipc.Database.Approval.GetApprovalByIDDB(ctx, state.ApprovalID)
|
|
|
|
|
participants := h.getFilteredParticipants(ctx, approval, user)
|
|
|
|
|
|
|
|
|
|
edit := tgbotapi.NewEditMessageReplyMarkup(cq.Message.Chat.ID, cq.Message.MessageID,
|
|
|
|
|
keyboard.BuildCommentTargetsMenu(participants, state.Targets),
|
|
|
|
|
)
|
|
|
|
|
bot.Request(edit) //nolint:errcheck
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) HandleCommentTargetDone(bot *tgbotapi.BotAPI, cq *tgbotapi.CallbackQuery) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
telegramID := cq.From.ID
|
|
|
|
|
chatID := cq.Message.Chat.ID
|
|
|
|
|
|
|
|
|
|
bot.Request(tgbotapi.NewCallback(cq.ID, "")) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
state, err := h.States.GetCommentFlow(ctx, telegramID)
|
|
|
|
|
if err != nil || state == nil || state.Stage != "await_targets" {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(state.Targets) == 0 {
|
|
|
|
|
sendText(bot, chatID, "⚠️ Вы не выбрали ни одного участника. Выберите хотя бы одного или нажмите «Назад».")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bot.Request(tgbotapi.NewDeleteMessage(chatID, cq.Message.MessageID)) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
user, _ := h.ipc.Database.User.GetUserByTelegramIDDB(ctx, telegramID)
|
|
|
|
|
approval, _ := h.ipc.Database.Approval.GetApprovalByIDDB(ctx, state.ApprovalID)
|
|
|
|
|
|
|
|
|
|
// Записываем комментарий
|
|
|
|
|
comment := &typescore.ApprovalComment{
|
|
|
|
|
ApprovalID: &state.ApprovalID,
|
|
|
|
|
UserID: user.ID,
|
|
|
|
|
Comment: &state.Text,
|
|
|
|
|
}
|
|
|
|
|
h.ipc.Database.ApprovalComment.CreateApprovalCommentDB(ctx, nil, comment) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
userName := "—"
|
|
|
|
|
if user.FullName != nil {
|
|
|
|
|
userName = *user.FullName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sendText(bot, chatID, fmt.Sprintf(
|
|
|
|
|
"✅ <b>Комментарий отправлен выбранным участникам</b>\n\n"+
|
|
|
|
|
"📋 Заявка: #%d\n"+
|
|
|
|
|
"👤 Автор: %s\n"+
|
|
|
|
|
"💬 Текст: %s",
|
|
|
|
|
state.ApprovalID, userName, state.Text,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
h.notifySpecificParticipantsAboutComment(bot, ctx, approval, user, comment, state.Targets)
|
|
|
|
|
|
|
|
|
|
h.States.ClearCommentFlow(ctx, telegramID) //nolint:errcheck
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) HandleCommentTargetBack(bot *tgbotapi.BotAPI, cq *tgbotapi.CallbackQuery) {
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
telegramID := cq.From.ID
|
|
|
|
|
bot.Request(tgbotapi.NewCallback(cq.ID, "")) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
state, err := h.States.GetCommentFlow(ctx, telegramID)
|
|
|
|
|
if err == nil && state != nil {
|
|
|
|
|
state.Stage = "await_visibility"
|
|
|
|
|
h.States.SetCommentFlow(ctx, telegramID, state) //nolint:errcheck
|
|
|
|
|
|
|
|
|
|
msg := tgbotapi.NewEditMessageTextAndMarkup(cq.Message.Chat.ID, cq.Message.MessageID,
|
|
|
|
|
"💬 <b>Комментарий сохранен.</b>\n\nВыберите, кому отправить уведомление:",
|
|
|
|
|
keyboard.BuildCommentVisibilityMenu())
|
|
|
|
|
msg.ParseMode = "HTML"
|
|
|
|
|
bot.Request(msg) //nolint:errcheck
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ── Вспомогательные функции ──
|
|
|
|
|
|
|
|
|
|
// getFilteredParticipants возвращает участников чата заявки, исключая самого комментатора
|
|
|
|
|
func (h *Handler) getFilteredParticipants(ctx context.Context, approval *typescore.Approval, excludeUser *typescore.User) []*typescore.User {
|
|
|
|
|
all := h.getChatParticipants(ctx, approval)
|
|
|
|
|
var filtered []*typescore.User
|
|
|
|
|
for _, u := range all {
|
|
|
|
|
if excludeUser != nil && excludeUser.ID != nil && u.ID != nil && *u.ID == *excludeUser.ID {
|
|
|
|
|
continue // Пропускаем автора
|
|
|
|
|
}
|
|
|
|
|
filtered = append(filtered, u)
|
|
|
|
|
}
|
|
|
|
|
return filtered
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// notifySpecificParticipantsAboutComment рассылает комментарий только тем, чьи ID есть в мапе targets
|
|
|
|
|
func (h *Handler) notifySpecificParticipantsAboutComment(
|
|
|
|
|
bot *tgbotapi.BotAPI,
|
|
|
|
|
ctx context.Context,
|
|
|
|
|
approval *typescore.Approval,
|
|
|
|
|
commenter *typescore.User,
|
|
|
|
|
comment *typescore.ApprovalComment,
|
|
|
|
|
targets map[uint64]bool,
|
|
|
|
|
) {
|
|
|
|
|
if approval == nil || approval.ID == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
commenterName := "—"
|
|
|
|
|
if commenter != nil && commenter.FullName != nil {
|
|
|
|
|
commenterName = *commenter.FullName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
topicName := h.getTopicNameByApproval(ctx, approval)
|
|
|
|
|
content := "—"
|
|
|
|
|
if approval.Content != nil {
|
|
|
|
|
content = *approval.Content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text := fmt.Sprintf(
|
|
|
|
|
"🔔 <b>Вам отправлен персональный комментарий к заявке #%d</b>\n\n"+
|
|
|
|
|
"📋 <b>Тема:</b> %s\n"+
|
|
|
|
|
"📝 <b>Текст заявки:</b> %s\n\n"+
|
|
|
|
|
"👤 <b>Автор комментария:</b> %s\n"+
|
|
|
|
|
"💬 <b>Комментарий:</b>\n%s",
|
|
|
|
|
*approval.ID, topicName, content, commenterName, *comment.Comment,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
commentsCount := h.GetCommentsCount(ctx, *approval.ID)
|
|
|
|
|
participants := h.getChatParticipants(ctx, approval)
|
|
|
|
|
|
|
|
|
|
for _, u := range participants {
|
|
|
|
|
if u.ID == nil || u.TelegramID == nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if !targets[*u.ID] { // Проверяем, отмечен ли участник
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
msg := tgbotapi.NewMessage(*u.TelegramID, text)
|
|
|
|
|
msg.ParseMode = "HTML"
|
|
|
|
|
|
|
|
|
|
// Используем клавиатуру для возможности быстро ответить, как и в общей рассылке
|
|
|
|
|
msg.ReplyMarkup = keyboard.BuildCommentReplyKeyboard(*approval.ID, commentsCount)
|
|
|
|
|
bot.Send(msg) //nolint:errcheck
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|