From 4bd7f4dc8b3751b577a441b5ab76da8ac7c9d8ce Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Thu, 1 Aug 2024 12:03:34 +0800 Subject: [PATCH] :lipstick: Optimized message notification --- pkg/internal/models/channels.go | 5 +++- pkg/internal/server/api/events_message_api.go | 26 ++++++----------- pkg/internal/services/events.go | 29 ++++++++++++++----- 3 files changed, 34 insertions(+), 26 deletions(-) diff --git a/pkg/internal/models/channels.go b/pkg/internal/models/channels.go index 8e99bb2..9d12e9b 100644 --- a/pkg/internal/models/channels.go +++ b/pkg/internal/models/channels.go @@ -31,7 +31,10 @@ func (v Channel) DisplayText() string { if v.Type == ChannelTypeDirect { return "DM" } - return fmt.Sprintf("#%s", v.Alias) + if v.RealmID != nil { + return fmt.Sprintf("%s, %s", v.Alias, v.Realm.Alias) + } + return fmt.Sprintf("%s", v.Alias) } type NotifyLevel = int8 diff --git a/pkg/internal/server/api/events_message_api.go b/pkg/internal/server/api/events_message_api.go index 166abba..e3fc165 100644 --- a/pkg/internal/server/api/events_message_api.go +++ b/pkg/internal/server/api/events_message_api.go @@ -29,7 +29,7 @@ func newMessageEvent(c *fiber.Ctx) error { } if len(data.Body.Text) == 0 && len(data.Body.Attachments) == 0 { - return fiber.NewError(fiber.StatusBadRequest, "you cannot send an empty message") + return fiber.NewError(fiber.StatusBadRequest, "cannot send an empty message") } var err error @@ -37,18 +37,13 @@ func newMessageEvent(c *fiber.Ctx) error { var member models.ChannelMember if val, ok := c.Locals("realm").(models.Realm); ok { channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID) - if err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } else if member.PowerLevel < 0 { - return fiber.NewError(fiber.StatusForbidden, "you have not enough permission to send message") - } } else { channel, member, err = services.GetAvailableChannelWithAlias(alias, user) - if err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } else if member.PowerLevel < 0 { - return fiber.NewError(fiber.StatusForbidden, "you have not enough permission to send message") - } + } + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) + } else if member.PowerLevel < 0 { + return fiber.NewError(fiber.StatusForbidden, "unable to send message, access denied") } var parsed map[string]any @@ -99,14 +94,11 @@ func editMessageEvent(c *fiber.Ctx) error { var member models.ChannelMember if val, ok := c.Locals("realm").(models.Realm); ok { channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID) - if err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } } else { channel, member, err = services.GetAvailableChannelWithAlias(alias, user) - if err != nil { - return fiber.NewError(fiber.StatusNotFound, err.Error()) - } + } + if err != nil { + return fiber.NewError(fiber.StatusNotFound, err.Error()) } var event models.Event diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go index 305a9f6..32c957c 100644 --- a/pkg/internal/services/events.go +++ b/pkg/internal/services/events.go @@ -83,7 +83,6 @@ func NewEvent(event models.Event) (models.Event, error) { return event, nil } - channel := event.Channel event, _ = GetEvent(event.Channel, event.ID) idxList := lo.Map(members, func(item models.ChannelMember, index int) uint64 { return uint64(item.AccountID) @@ -94,7 +93,7 @@ func NewEvent(event models.Event) (models.Event, error) { }) if strings.HasPrefix(event.Type, "messages") { - event.Channel = channel + event.Channel, _ = GetChannel(event.ChannelID) NotifyMessageEvent(members, event) } @@ -126,21 +125,35 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) { } var displayText string - if body.Algorithm == "plain" { - displayText = body.Text + var displaySubtitle *string + switch event.Type { + case models.EventMessageNew: + if body.Algorithm == "plain" { + displayText = body.Text + } + case models.EventMessageEdit: + displaySubtitle = lo.ToPtr("Edited a message") + if body.Algorithm == "plain" { + displayText = body.Text + } + case models.EventMessageDelete: + displayText = "Recalled a message" } if len(displayText) == 0 { displayText = fmt.Sprintf("%d attachment(s)", len(body.Attachments)) + } else { + displayText += fmt.Sprintf("w/ %d attachment(s)", len(body.Attachments)) } err := NotifyAccountMessagerBatch( pendingUsers, &proto.NotifyRequest{ - Topic: "messaging.message", - Title: fmt.Sprintf("%s in %s", event.Sender.Account.Nick, event.Channel.DisplayText()), - Body: displayText, - Avatar: &event.Sender.Account.Avatar, + 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,