✨ More detailed notification
This commit is contained in:
parent
09e670b096
commit
fda9b7517c
2
go.mod
2
go.mod
@ -5,7 +5,7 @@ go 1.22
|
|||||||
toolchain go1.22.1
|
toolchain go1.22.1
|
||||||
|
|
||||||
require (
|
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
|
git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745
|
||||||
github.com/go-playground/validator/v10 v10.17.0
|
github.com/go-playground/validator/v10 v10.17.0
|
||||||
github.com/gofiber/fiber/v2 v2.52.4
|
github.com/gofiber/fiber/v2 v2.52.4
|
||||||
|
2
go.sum
2
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-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 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-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 h1:40BUsQMNXjqHyytkyF9py1HjTAWlRgO6R57YXUrHNy4=
|
||||||
git.solsynth.dev/hydrogen/paperclip v0.0.0-20240622051057-0f56dba45745/go.mod h1:FsQGSLTl0gvo+9Jmbot02S72suyF9tFTrzDj70Xhifo=
|
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=
|
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
type ChannelType = uint8
|
type ChannelType = uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -25,6 +27,13 @@ type Channel struct {
|
|||||||
RealmID *uint `json:"realm_id"`
|
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
|
type NotifyLevel = int8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
||||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/gap"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
|
"github.com/samber/lo"
|
||||||
|
|
||||||
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
||||||
@ -49,7 +50,7 @@ func CheckUserPerm(userId, otherId uint, key string, val any) error {
|
|||||||
return nil
|
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)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@ -59,20 +60,13 @@ func NotifyAccountMessager(user models.Account, title, body string, subtitle *st
|
|||||||
}
|
}
|
||||||
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
_, err = proto.NewNotifierClient(pc).NotifyUser(ctx, &proto.NotifyUserRequest{
|
||||||
UserId: uint64(user.ExternalID),
|
UserId: uint64(user.ExternalID),
|
||||||
Notify: &proto.NotifyRequest{
|
Notify: notification,
|
||||||
Topic: "messaging.message",
|
|
||||||
Title: title,
|
|
||||||
Subtitle: subtitle,
|
|
||||||
Body: body,
|
|
||||||
IsRealtime: realtime,
|
|
||||||
IsForcePush: forcePush,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return err
|
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)
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
@ -81,15 +75,10 @@ func NotifyAccountMessagerBatch(users []uint64, title, body string, subtitle *st
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
_, err = proto.NewNotifierClient(pc).NotifyUserBatch(ctx, &proto.NotifyUserBatchRequest{
|
||||||
UserId: users,
|
UserId: lo.Map(users, func(item models.Account, idx int) uint64 {
|
||||||
Notify: &proto.NotifyRequest{
|
return uint64(item.ExternalID)
|
||||||
Topic: "messaging.message",
|
}),
|
||||||
Title: title,
|
Notify: notification,
|
||||||
Subtitle: subtitle,
|
|
||||||
Body: body,
|
|
||||||
IsRealtime: realtime,
|
|
||||||
IsForcePush: forcePush,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
|
|
||||||
return err
|
return err
|
||||||
|
@ -4,6 +4,9 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
||||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
@ -13,7 +16,6 @@ import (
|
|||||||
"github.com/samber/lo"
|
"github.com/samber/lo"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func ListCall(channel models.Channel, take, offset int) ([]models.Call, error) {
|
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 {
|
}).Preload("Account").Find(&members).Error; err == nil {
|
||||||
channel = call.Channel
|
channel = call.Channel
|
||||||
call, _ = GetCall(call.Channel, call.ID)
|
call, _ = GetCall(call.Channel, call.ID)
|
||||||
|
var pendingUsers []models.Account
|
||||||
for _, member := range members {
|
for _, member := range members {
|
||||||
if member.ID != call.Founder.ID {
|
if member.ID != call.Founder.ID {
|
||||||
err = NotifyAccountMessager(member.Account,
|
pendingUsers = append(pendingUsers, 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.")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
PushCommand(member.AccountID, models.UnifiedCommand{
|
PushCommand(member.AccountID, models.UnifiedCommand{
|
||||||
Action: "calls.new",
|
Action: "calls.new",
|
||||||
Payload: call,
|
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
|
return call, nil
|
||||||
|
8
pkg/internal/services/encoder.go
Normal file
8
pkg/internal/services/encoder.go
Normal 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
|
||||||
|
}
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"git.solsynth.dev/hydrogen/dealer/pkg/proto"
|
||||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/database"
|
||||||
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
"git.solsynth.dev/hydrogen/messaging/pkg/internal/models"
|
||||||
jsoniter "github.com/json-iterator/go"
|
jsoniter "github.com/json-iterator/go"
|
||||||
@ -105,7 +106,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
|
|||||||
raw, _ := jsoniter.Marshal(event.Body)
|
raw, _ := jsoniter.Marshal(event.Body)
|
||||||
_ = jsoniter.Unmarshal(raw, &body)
|
_ = jsoniter.Unmarshal(raw, &body)
|
||||||
|
|
||||||
var pendingIdx []uint64
|
var pendingUsers []models.Account
|
||||||
|
|
||||||
for _, member := range members {
|
for _, member := range members {
|
||||||
if member.ID != event.SenderID {
|
if member.ID != event.SenderID {
|
||||||
@ -120,7 +121,7 @@ func NotifyMessageEvent(members []models.ChannelMember, event models.Event) {
|
|||||||
break
|
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))
|
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(
|
err := NotifyAccountMessagerBatch(
|
||||||
pendingIdx,
|
pendingUsers,
|
||||||
fmt.Sprintf("%s in %s", event.Sender.Account.Nick, channelDisplay),
|
&proto.NotifyRequest{
|
||||||
displayText,
|
Topic: "messaging.message",
|
||||||
nil,
|
Title: fmt.Sprint("%s in %s", event.Sender.Account.Nick, event.Channel.DisplayText()),
|
||||||
true,
|
Body: displayText,
|
||||||
false,
|
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 {
|
if err != nil {
|
||||||
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
log.Warn().Err(err).Msg("An error occurred when trying notify user.")
|
||||||
|
Loading…
Reference in New Issue
Block a user