💄 Optimized message notification

This commit is contained in:
LittleSheep 2024-08-01 12:03:34 +08:00
parent 262bac2a15
commit 4bd7f4dc8b
3 changed files with 34 additions and 26 deletions

View File

@ -31,7 +31,10 @@ func (v Channel) DisplayText() string {
if v.Type == ChannelTypeDirect { if v.Type == ChannelTypeDirect {
return "DM" 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 type NotifyLevel = int8

View File

@ -29,7 +29,7 @@ func newMessageEvent(c *fiber.Ctx) error {
} }
if len(data.Body.Text) == 0 && len(data.Body.Attachments) == 0 { 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 var err error
@ -37,18 +37,13 @@ func newMessageEvent(c *fiber.Ctx) error {
var member models.ChannelMember var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(models.Realm); ok {
channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID) 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 { } else {
channel, member, err = services.GetAvailableChannelWithAlias(alias, user) channel, member, err = services.GetAvailableChannelWithAlias(alias, user)
}
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} else if member.PowerLevel < 0 { } else if member.PowerLevel < 0 {
return fiber.NewError(fiber.StatusForbidden, "you have not enough permission to send message") return fiber.NewError(fiber.StatusForbidden, "unable to send message, access denied")
}
} }
var parsed map[string]any var parsed map[string]any
@ -99,15 +94,12 @@ func editMessageEvent(c *fiber.Ctx) error {
var member models.ChannelMember var member models.ChannelMember
if val, ok := c.Locals("realm").(models.Realm); ok { if val, ok := c.Locals("realm").(models.Realm); ok {
channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID) channel, member, err = services.GetAvailableChannelWithAlias(alias, user, val.ID)
if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error())
}
} else { } else {
channel, member, err = services.GetAvailableChannelWithAlias(alias, user) channel, member, err = services.GetAvailableChannelWithAlias(alias, user)
}
if err != nil { if err != nil {
return fiber.NewError(fiber.StatusNotFound, err.Error()) return fiber.NewError(fiber.StatusNotFound, err.Error())
} }
}
var event models.Event var event models.Event
if event, err = services.GetEventWithSender(channel, member, uint(messageId)); err != nil { if event, err = services.GetEventWithSender(channel, member, uint(messageId)); err != nil {

View File

@ -83,7 +83,6 @@ func NewEvent(event models.Event) (models.Event, error) {
return event, nil return event, nil
} }
channel := event.Channel
event, _ = GetEvent(event.Channel, event.ID) event, _ = GetEvent(event.Channel, event.ID)
idxList := lo.Map(members, func(item models.ChannelMember, index int) uint64 { idxList := lo.Map(members, func(item models.ChannelMember, index int) uint64 {
return uint64(item.AccountID) return uint64(item.AccountID)
@ -94,7 +93,7 @@ func NewEvent(event models.Event) (models.Event, error) {
}) })
if strings.HasPrefix(event.Type, "messages") { if strings.HasPrefix(event.Type, "messages") {
event.Channel = channel event.Channel, _ = GetChannel(event.ChannelID)
NotifyMessageEvent(members, event) NotifyMessageEvent(members, event)
} }
@ -126,19 +125,33 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
} }
var displayText string var displayText string
var displaySubtitle *string
switch event.Type {
case models.EventMessageNew:
if body.Algorithm == "plain" { if body.Algorithm == "plain" {
displayText = body.Text 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 { if len(displayText) == 0 {
displayText = fmt.Sprintf("%d attachment(s)", len(body.Attachments)) displayText = fmt.Sprintf("%d attachment(s)", len(body.Attachments))
} else {
displayText += fmt.Sprintf("w/ %d attachment(s)", len(body.Attachments))
} }
err := NotifyAccountMessagerBatch( err := NotifyAccountMessagerBatch(
pendingUsers, pendingUsers,
&proto.NotifyRequest{ &proto.NotifyRequest{
Topic: "messaging.message", Topic: "messaging.message",
Title: fmt.Sprintf("%s in %s", event.Sender.Account.Nick, event.Channel.DisplayText()), Title: fmt.Sprintf("%s (%s)", event.Sender.Account.Nick, event.Channel.DisplayText()),
Subtitle: displaySubtitle,
Body: displayText, Body: displayText,
Avatar: &event.Sender.Account.Avatar, Avatar: &event.Sender.Account.Avatar,
Metadata: EncodeJSONBody(map[string]any{ Metadata: EncodeJSONBody(map[string]any{