From fda9b7517c4d38e62f720702ddb09159c31bceb3 Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sat, 20 Jul 2024 16:02:16 +0800 Subject: [PATCH] :sparkles: More detailed notification --- go.mod | 2 +- go.sum | 2 ++ pkg/internal/models/channels.go | 9 ++++++++ pkg/internal/services/accounts.go | 27 +++++++--------------- pkg/internal/services/calls.go | 37 ++++++++++++++++++++++--------- pkg/internal/services/encoder.go | 8 +++++++ pkg/internal/services/events.go | 35 +++++++++++++++-------------- 7 files changed, 72 insertions(+), 48 deletions(-) create mode 100644 pkg/internal/services/encoder.go diff --git a/go.mod b/go.mod index 5b9bb64..8bdda22 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,7 @@ go 1.22 toolchain go1.22.1 require ( - git.solsynth.dev/hydrogen/dealer v0.0.0-20240717054512-da433c88615a + git.solsynth.dev/hydrogen/dealer v0.0.0-20240719153055-607eba001f65 git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745 github.com/go-playground/validator/v10 v10.17.0 github.com/gofiber/fiber/v2 v2.52.4 diff --git a/go.sum b/go.sum index abffd64..9a74847 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ git.solsynth.dev/hydrogen/dealer v0.0.0-20240717035851-96b96912ed64 h1:1+3LKTvVp git.solsynth.dev/hydrogen/dealer v0.0.0-20240717035851-96b96912ed64/go.mod h1:oPdUxLy6TFeRxiRC/BoNb3YUNcnSiOnJrzFTnCPSoCA= git.solsynth.dev/hydrogen/dealer v0.0.0-20240717054512-da433c88615a h1:08YaBgZtkrPfiw08l4RjtKI1fdt6wlHc3Ea7NDFtLPg= git.solsynth.dev/hydrogen/dealer v0.0.0-20240717054512-da433c88615a/go.mod h1:oPdUxLy6TFeRxiRC/BoNb3YUNcnSiOnJrzFTnCPSoCA= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240719153055-607eba001f65 h1:p9PIsp5RyrLdlOR7MXyeGBx04v3+1IMEEBBV22fJZPo= +git.solsynth.dev/hydrogen/dealer v0.0.0-20240719153055-607eba001f65/go.mod h1:oPdUxLy6TFeRxiRC/BoNb3YUNcnSiOnJrzFTnCPSoCA= git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745 h1:40BUsQMNXjqHyytkyF9py1HjTAWlRgO6R57YXUrHNy4= git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745/go.mod h1:FsQGSLTl0gvo+9Jmbot02S72suyF9tFTrzDj70Xhifo= github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ= diff --git a/pkg/internal/models/channels.go b/pkg/internal/models/channels.go index d754e93..8e99bb2 100644 --- a/pkg/internal/models/channels.go +++ b/pkg/internal/models/channels.go @@ -1,5 +1,7 @@ package models +import "fmt" + type ChannelType = uint8 const ( @@ -25,6 +27,13 @@ type Channel struct { RealmID *uint `json:"realm_id"` } +func (v Channel) DisplayText() string { + if v.Type == ChannelTypeDirect { + return "DM" + } + return fmt.Sprintf("#%s", v.Alias) +} + type NotifyLevel = int8 const ( diff --git a/pkg/internal/services/accounts.go b/pkg/internal/services/accounts.go index 6e81c6f..aba6dfc 100644 --- a/pkg/internal/services/accounts.go +++ b/pkg/internal/services/accounts.go @@ -9,6 +9,7 @@ import ( "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/gap" jsoniter "github.com/json-iterator/go" + "github.com/samber/lo" "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" @@ -49,7 +50,7 @@ func CheckUserPerm(userId, otherId uint, key string, val any) error { return nil } -func NotifyAccountMessager(user models.Account, title, body string, subtitle *string, realtime bool, forcePush bool) error { +func NotifyAccountMessager(user models.Account, notification *proto.NotifyRequest) error { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -59,20 +60,13 @@ func NotifyAccountMessager(user models.Account, title, body string, subtitle *st } _, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{ UserId: uint64(user.ExternalID), - Notify: &proto.NotifyRequest{ - Topic: "messaging.message", - Title: title, - Subtitle: subtitle, - Body: body, - IsRealtime: realtime, - IsForcePush: forcePush, - }, + Notify: notification, }) return err } -func NotifyAccountMessagerBatch(users []uint64, title, body string, subtitle *string, realtime bool, forcePush bool) error { +func NotifyAccountMessagerBatch(users []models.Account, notification *proto.NotifyRequest) error { ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) defer cancel() @@ -81,15 +75,10 @@ func NotifyAccountMessagerBatch(users []uint64, title, body string, subtitle *st return err } _, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{ - UserId: users, - Notify: &proto.NotifyRequest{ - Topic: "messaging.message", - Title: title, - Subtitle: subtitle, - Body: body, - IsRealtime: realtime, - IsForcePush: forcePush, - }, + UserId: lo.Map(users, func(item models.Account, idx int) uint64 { + return uint64(item.ExternalID) + }), + Notify: notification, }) return err diff --git a/pkg/internal/services/calls.go b/pkg/internal/services/calls.go index 61c9700..4f1b20f 100644 --- a/pkg/internal/services/calls.go +++ b/pkg/internal/services/calls.go @@ -4,6 +4,9 @@ import ( "context" "errors" "fmt" + "time" + + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" jsoniter "github.com/json-iterator/go" @@ -13,7 +16,6 @@ import ( "github.com/samber/lo" "github.com/spf13/viper" "gorm.io/gorm" - "time" ) func ListCall(channel models.Channel, take, offset int) ([]models.Call, error) { @@ -106,24 +108,37 @@ func NewCall(channel models.Channel, founder models.ChannelMember) (models.Call, }).Preload("Account").Find(&members).Error; err == nil { channel = call.Channel call, _ = GetCall(call.Channel, call.ID) + var pendingUsers []models.Account for _, member := range members { if member.ID != call.Founder.ID { - err = NotifyAccountMessager(member.Account, - fmt.Sprintf("Call in #%s", channel.Alias), - fmt.Sprintf("%s started a new call", call.Founder.Account.Name), - nil, - false, - true, - ) - if err != nil { - log.Warn().Err(err).Msg("An error occurred when trying notify user.") - } + pendingUsers = append(pendingUsers, member.Account) } PushCommand(member.AccountID, models.UnifiedCommand{ Action: "calls.new", Payload: call, }) } + + err = NotifyAccountMessagerBatch( + pendingUsers, + &proto.NotifyRequest{ + Topic: "messaging.callStart", + Title: fmt.Sprintf("Call in %s", channel.DisplayText()), + Body: fmt.Sprintf("%s is calling", call.Founder.Account.Name), + Avatar: &call.Founder.Account.Avatar, + Metadata: EncodeJSONBody(map[string]any{ + "user_id": call.Founder.Account.ExternalID, + "user_name": call.Founder.Account.Name, + "user_nick": call.Founder.Account.Nick, + "channel_id": call.ChannelID, + }), + IsRealtime: false, + IsForcePush: true, + }, + ) + if err != nil { + log.Warn().Err(err).Msg("An error occurred when trying notify user.") + } } return call, nil diff --git a/pkg/internal/services/encoder.go b/pkg/internal/services/encoder.go new file mode 100644 index 0000000..c203b02 --- /dev/null +++ b/pkg/internal/services/encoder.go @@ -0,0 +1,8 @@ +package services + +import jsoniter "github.com/json-iterator/go" + +func EncodeJSONBody(in any) []byte { + out, _ := jsoniter.Marshal(in) + return out +} diff --git a/pkg/internal/services/events.go b/pkg/internal/services/events.go index 9d104d8..cf813ab 100644 --- a/pkg/internal/services/events.go +++ b/pkg/internal/services/events.go @@ -4,6 +4,7 @@ import ( "fmt" "strings" + "git.solsynth.dev/hydrogen/dealer/pkg/proto" "git.solsynth.dev/hydrogen/messaging/pkg/internal/database" "git.solsynth.dev/hydrogen/messaging/pkg/internal/models" jsoniter "github.com/json-iterator/go" @@ -105,7 +106,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) { raw, _ := jsoniter.Marshal(event.Body) _ = jsoniter.Unmarshal(raw, &body) - var pendingIdx []uint64 + var pendingUsers []models.Account for _, member := range members { if member.ID != event.SenderID { @@ -120,7 +121,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) { break } - pendingIdx = append(pendingIdx, uint64(member.Account.ExternalID)) + pendingUsers = append(pendingUsers, member.Account) } } @@ -133,22 +134,22 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) { displayText = fmt.Sprintf("%d attachment(s)", len(body.Attachments)) } - var channelDisplay string - if event.Channel.Type == models.ChannelTypeDirect { - channelDisplay = "DM" - } - - if len(channelDisplay) == 0 { - channelDisplay = fmt.Sprintf("#%s", event.Channel.Alias) - } - err := NotifyAccountMessagerBatch( - pendingIdx, - fmt.Sprintf("%s in %s", event.Sender.Account.Nick, channelDisplay), - displayText, - nil, - true, - false, + pendingUsers, + &proto.NotifyRequest{ + Topic: "messaging.message", + Title: fmt.Sprint("%s in %s", event.Sender.Account.Nick, event.Channel.DisplayText()), + 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.")