💄 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,15 +115,20 @@ 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
} }
if lo.Contains(body.RelatedUsers, member.AccountID) {
metionedUsers = append(metionedUsers, member.Account)
} else {
pendingUsers = append(pendingUsers, member.Account) pendingUsers = append(pendingUsers, member.Account)
} }
} }
}
var displayText string var displayText string
var displaySubtitle *string var displaySubtitle *string
@ -141,9 +147,40 @@ 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 if len(body.Attachments) > 0 {
displayText += fmt.Sprintf("with %d file(s)", len(body.Attachments))
}
if len(pendingUsers) > 0 {
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.")
}
}
if len(metionedUsers) > 0 {
if displaySubtitle != nil && len(*displaySubtitle) > 0 {
*displaySubtitle += ", and metioned you"
} else { } else {
displayText += fmt.Sprintf("w/ %d attachment(s)", len(body.Attachments)) displaySubtitle = lo.ToPtr("Metioned you")
} }
err := NotifyAccountMessagerBatch( err := NotifyAccountMessagerBatch(
@ -168,6 +205,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
log.Warn().Err(err).Msg("An error occurred when trying notify user.") log.Warn().Err(err).Msg("An error occurred when trying notify user.")
} }
} }
}
func EditEvent(event models.Event) (models.Event, error) { func EditEvent(event models.Event) (models.Event, error) {
if err := database.C.Save(&event).Error; err != nil { if err := database.C.Save(&event).Error; err != nil {