💄 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 {
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

View File

@ -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

View File

@ -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,