💄 Extra info when you got metioned in a message in notification

This commit is contained in:
LittleSheep 2024-08-01 12:33:22 +08:00
parent 4bd7f4dc8b
commit 583bec1619

View File

@ -106,6 +106,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
_ = jsoniter.Unmarshal(raw, &body) _ = jsoniter.Unmarshal(raw, &body)
var pendingUsers []models.Account var pendingUsers []models.Account
var metionedUsers []models.Account
for _, member := range members { for _, member := range members {
if member.ID != event.SenderID { if member.ID != event.SenderID {
@ -114,13 +115,18 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
continue continue
case models.NotifyLevelMentioned: case models.NotifyLevelMentioned:
if len(body.RelatedUsers) == 0 || !lo.Contains(body.RelatedUsers, member.AccountID) { if len(body.RelatedUsers) == 0 || !lo.Contains(body.RelatedUsers, member.AccountID) {
continue metionedUsers = append(metionedUsers, member.Account)
break
} }
default: default:
break break
} }
pendingUsers = append(pendingUsers, member.Account) if lo.Contains(body.RelatedUsers, member.AccountID) {
metionedUsers = append(metionedUsers, member.Account)
} else {
pendingUsers = append(pendingUsers, member.Account)
}
} }
} }
@ -141,31 +147,63 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
} }
if len(displayText) == 0 { if len(displayText) == 0 {
displayText = fmt.Sprintf("%d attachment(s)", len(body.Attachments)) displayText = fmt.Sprintf("%d file(s)", len(body.Attachments))
} else { } else if len(body.Attachments) > 0 {
displayText += fmt.Sprintf("w/ %d attachment(s)", len(body.Attachments)) displayText += fmt.Sprintf("with %d file(s)", len(body.Attachments))
} }
err := NotifyAccountMessagerBatch( if len(pendingUsers) > 0 {
pendingUsers, err := NotifyAccountMessagerBatch(
&proto.NotifyRequest{ pendingUsers,
Topic: "messaging.message", &proto.NotifyRequest{
Title: fmt.Sprintf("%s (%s)", event.Sender.Account.Nick, event.Channel.DisplayText()), Topic: "messaging.message",
Subtitle: displaySubtitle, Title: fmt.Sprintf("%s (%s)", event.Sender.Account.Nick, event.Channel.DisplayText()),
Body: displayText, Subtitle: displaySubtitle,
Avatar: &event.Sender.Account.Avatar, Body: displayText,
Metadata: EncodeJSONBody(map[string]any{ Avatar: &event.Sender.Account.Avatar,
"user_id": event.Sender.Account.ExternalID, Metadata: EncodeJSONBody(map[string]any{
"user_name": event.Sender.Account.Name, "user_id": event.Sender.Account.ExternalID,
"user_nick": event.Sender.Account.Nick, "user_name": event.Sender.Account.Name,
"channel_id": event.ChannelID, "user_nick": event.Sender.Account.Nick,
}), "channel_id": event.ChannelID,
IsRealtime: true, }),
IsForcePush: false, IsRealtime: true,
}, IsForcePush: false,
) },
if err != nil { )
log.Warn().Err(err).Msg("An error occurred when trying notify user.") if err != nil {
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
}
}
if len(metionedUsers) > 0 {
if displaySubtitle != nil && len(*displaySubtitle) > 0 {
*displaySubtitle += ", and metioned you"
} else {
displaySubtitle = lo.ToPtr("Metioned you")
}
err := NotifyAccountMessagerBatch(
pendingUsers,
&proto.NotifyRequest{
Topic: "messaging.message",
Title: fmt.Sprintf("%s (%s)", event.Sender.Account.Nick, event.Channel.DisplayText()),
Subtitle: displaySubtitle,
Body: displayText,
Avatar: &event.Sender.Account.Avatar,
Metadata: EncodeJSONBody(map[string]any{
"user_id": event.Sender.Account.ExternalID,
"user_name": event.Sender.Account.Name,
"user_nick": event.Sender.Account.Nick,
"channel_id": event.ChannelID,
}),
IsRealtime: true,
IsForcePush: false,
},
)
if err != nil {
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
}
} }
} }