Notifications with new Flutter app [Kinda work!]
All checks were successful
Build Docker and Deploy / Build Docker Container (push) Successful in 1m56s
Build Docker and Deploy / Deploy to Server (push) Successful in 5s

This commit is contained in:
2024-06-01 15:37:59 +02:00
parent 0560330f68
commit e397f009b9
19 changed files with 198 additions and 551 deletions

View File

@@ -12,6 +12,6 @@ func NewDummy() NotificationClient {
return &DummyConnector{}
}
func (d DummyConnector) SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string, compatMsgIDOverride *string) (string, error) {
func (d DummyConnector) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error) {
return "%DUMMY%", nil
}

View File

@@ -11,10 +11,8 @@ import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"io"
"net/http"
"strconv"
"strings"
"time"
)
@@ -53,32 +51,36 @@ type Notification struct {
Priority int
}
func (fb FirebaseConnector) SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string, compatMsgIDOverride *string) (string, error) {
func (fb FirebaseConnector) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error) {
uri := "https://fcm.googleapis.com/v1/projects/" + fb.fbProject + "/messages:send"
jsonBody := gin.H{
"data": gin.H{
"scn_msg_id": langext.Coalesce(compatMsgIDOverride, msg.MessageID.String()),
"usr_msg_id": langext.Coalesce(msg.UserMessageID, ""),
"client_id": client.ClientID.String(),
"timestamp": strconv.FormatInt(msg.Timestamp().Unix(), 10),
"priority": strconv.Itoa(msg.Priority),
"trimmed": langext.Conditional(msg.NeedsTrim(), "true", "false"),
"title": langext.Coalesce(compatTitleOverride, msg.Title),
"body": langext.Coalesce(msg.TrimmedContent(), ""),
},
"token": client.FCMToken,
"android": gin.H{
"priority": "high",
},
"apns": gin.H{},
}
if client.Type == models.ClientTypeIOS {
jsonBody["notification"] = gin.H{
"title": msg.Title,
"body": msg.ShortContent(),
}
jsonBody["apns"] = gin.H{}
} else if client.Type == models.ClientTypeAndroid {
jsonBody["android"] = gin.H{
"priority": "high",
"collapse_key": msg.ChannelID.String(),
"notification": gin.H{
"event_time": msg.Timestamp().Format(time.RFC3339),
"title": msg.FormatNotificationTitle(user, channel),
"body": msg.ShortContent(),
},
"fcm_options": gin.H{},
}
} else {
jsonBody["notification"] = gin.H{
"title": msg.FormatNotificationTitle(user, channel),
"body": msg.ShortContent(),
}
}
bytesBody, err := json.Marshal(gin.H{"message": jsonBody})

View File

@@ -6,5 +6,5 @@ import (
)
type NotificationClient interface {
SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string, compatMsgIDOverride *string) (string, error)
SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error)
}

View File

@@ -8,10 +8,8 @@ import (
)
type SinkData struct {
Message models.Message
Client models.Client
CompatTitleOverride *string
CompatMsgIDOverride *string
Message models.Message
Client models.Client
}
type TestSink struct {
@@ -26,7 +24,7 @@ func (d *TestSink) Last() SinkData {
return d.Data[len(d.Data)-1]
}
func (d *TestSink) SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string, compatMsgIDOverride *string) (string, error) {
func (d *TestSink) SendNotification(ctx context.Context, user models.User, client models.Client, channel models.Channel, msg models.Message) (string, error) {
id, err := langext.NewHexUUID()
if err != nil {
return "", err
@@ -35,10 +33,8 @@ func (d *TestSink) SendNotification(ctx context.Context, client models.Client, m
key := "TestSink[" + id + "]"
d.Data = append(d.Data, SinkData{
Message: msg,
Client: client,
CompatTitleOverride: compatTitleOverride,
CompatMsgIDOverride: compatMsgIDOverride,
Message: msg,
Client: client,
})
return key, nil