Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MM-58483: handle empty notification messages better #690

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions server/bot_messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,13 @@ func (p *Plugin) makeWelcomeMessageWithNotificationActionPost() *model.Post {
}
}

// notifyMessage sends the given receipient a notification of a chat received on Teams.
func (p *Plugin) notifyChat(recipientUserID string, actorDisplayName string, chatTopic string, chatSize int, chatLink string, message string, attachmentCount int) {
// formatNotificationMessage formats the message about a notification of a chat received on Teams.
func formatNotificationMessage(actorDisplayName string, chatTopic string, chatSize int, chatLink string, message string, attachmentCount int) string {
message = strings.TrimSpace(message)
if message == "" && attachmentCount == 0 {
return ""
}

var preamble string

var chatTopicDesc string
Expand All @@ -145,31 +150,46 @@ func (p *Plugin) notifyChat(recipientUserID string, actorDisplayName string, cha
}

if chatSize <= 1 {
return
return ""
} else if chatSize == 2 {
preamble = fmt.Sprintf("**%s** messaged you in an [MS Teams chat%s](%s):", actorDisplayName, chatTopicDesc, chatLink)
} else if chatSize == 3 {
preamble = fmt.Sprintf("**%s** messaged you and 1 other user in an [MS Teams group chat%s](%s):", actorDisplayName, chatTopicDesc, chatLink)
} else {
preamble = fmt.Sprintf("**%s** messaged you and %d other users in an [MS Teams group chat%s](%s):", actorDisplayName, chatSize-2, chatTopicDesc, chatLink)
}
preamble += "\n"

message = "> " + strings.ReplaceAll(message, "\n", "\n>")
if message != "" {
message = "> " + strings.ReplaceAll(message, "\n", "\n> ")
}

attachmentsNotice := ""
if attachmentCount > 0 {
if len(message) > 0 {
attachmentsNotice += "\n"
}
attachmentsNotice += "\n*"
if attachmentCount == 1 {
attachmentsNotice += "This message was originally sent with one attachment."
} else {
attachmentsNotice += fmt.Sprintf("This message was originally sent with %d attachments.", attachmentCount)
}
attachmentsNotice += "*\n"
attachmentsNotice += "*"
}

formattedMessage := fmt.Sprintf(`%s
%s
%s`, preamble, message, attachmentsNotice)
formattedMessage := fmt.Sprintf(`%s%s%s`, preamble, message, attachmentsNotice)

return formattedMessage
}

// notifyMessage sends the given receipient a notification of a chat received on Teams.
func (p *Plugin) notifyChat(recipientUserID string, actorDisplayName string, chatTopic string, chatSize int, chatLink string, message string, attachmentCount int) {
formattedMessage := formatNotificationMessage(actorDisplayName, chatTopic, chatSize, chatLink, message, attachmentCount)

if formattedMessage == "" {
return
}

if err := p.botSendDirectMessage(recipientUserID, formattedMessage); err != nil {
p.GetAPI().LogWarn("Failed to send notification message", "user_id", recipientUserID, "error", err)
Expand Down
Loading
Loading