More detailed notification

This commit is contained in:
LittleSheep 2024-07-20 16:02:16 +08:00
parent 09e670b096
commit fda9b7517c
7 changed files with 72 additions and 48 deletions

2
go.mod
View File

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

2
go.sum
View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,8 @@
package services
import jsoniter "github.com/json-iterator/go"
func EncodeJSONBody(in any) []byte {
out, _ := jsoniter.Marshal(in)
return out
}

View File

@ -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.")