Send compat-msgid to compat clients (BF old android client)

This commit is contained in:
Mike Schwörer 2023-06-18 01:55:58 +02:00
parent 67218d8045
commit aefc368cfd
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
10 changed files with 65 additions and 29 deletions

View File

@ -68,7 +68,7 @@ func Wrap(rlacc RequestLogAcceptor, fn WHandlerFunc) gin.HandlerFunc {
if scn.Conf.ReqLogEnabled { if scn.Conf.ReqLogEnabled {
rlacc.InsertRequestLog(createRequestLog(g, t0, ctr, wrap, nil)) rlacc.InsertRequestLog(createRequestLog(g, t0, ctr, wrap, nil))
} }
statuscode := wrap.Statuscode() statuscode := wrap.Statuscode()
if statuscode/100 != 2 { if statuscode/100 != 2 {
log.Warn().Str("url", g.Request.Method+"::"+g.Request.URL.String()).Msg(fmt.Sprintf("Request failed with statuscode %d", statuscode)) log.Warn().Str("url", g.Request.Method+"::"+g.Request.URL.String()).Msg(fmt.Sprintf("Request failed with statuscode %d", statuscode))

View File

@ -9,7 +9,6 @@ import (
"blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/logic"
"blackforestbytes.com/simplecloudnotifier/models" "blackforestbytes.com/simplecloudnotifier/models"
"database/sql" "database/sql"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"gogs.mikescher.com/BlackForestBytes/goext/dataext" "gogs.mikescher.com/BlackForestBytes/goext/dataext"
"gogs.mikescher.com/BlackForestBytes/goext/langext" "gogs.mikescher.com/BlackForestBytes/goext/langext"
@ -524,7 +523,7 @@ func (h CompatHandler) Requery(g *gin.Context) ginresp.HTTPResponse {
} }
compMsgs = append(compMsgs, models.CompatMessage{ compMsgs = append(compMsgs, models.CompatMessage{
Title: compatizeMessageTitle(ctx, h.app, v), Title: h.app.CompatizeMessageTitle(ctx, v),
Body: v.Content, Body: v.Content,
Priority: v.Priority, Priority: v.Priority,
Timestamp: v.Timestamp().Unix(), Timestamp: v.Timestamp().Unix(),
@ -772,7 +771,7 @@ func (h CompatHandler) Expand(g *gin.Context) ginresp.HTTPResponse {
Success: true, Success: true,
Message: "ok", Message: "ok",
Data: models.CompatMessage{ Data: models.CompatMessage{
Title: compatizeMessageTitle(ctx, h.app, msg), Title: h.app.CompatizeMessageTitle(ctx, msg),
Body: msg.Content, Body: msg.Content,
Trimmed: langext.Ptr(false), Trimmed: langext.Ptr(false),
Priority: msg.Priority, Priority: msg.Priority,
@ -919,16 +918,3 @@ func (h CompatHandler) Upgrade(g *gin.Context) ginresp.HTTPResponse {
IsPro: user.IsPro, IsPro: user.IsPro,
})) }))
} }
func compatizeMessageTitle(ctx *logic.AppContext, app *logic.Application, msg models.Message) string {
if msg.ChannelInternalName == "main" {
return msg.Title
}
channel, err := app.Database.Primary.GetChannelByID(ctx, msg.ChannelID)
if err != nil {
return fmt.Sprintf("[%s] %s", "%SCN-ERR%", msg.Title)
}
return fmt.Sprintf("[%s] %s", channel.DisplayName, msg.Title)
}

View File

@ -239,7 +239,7 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create message in db", err)) return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create message in db", err))
} }
cid, err := h.database.CreateCompatID(ctx, "messageid", msg.MessageID.String()) compatMsgID, err := h.database.CreateCompatID(ctx, "messageid", msg.MessageID.String())
if err != nil { if err != nil {
return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create compat-id", err)) return nil, langext.Ptr(ginresp.SendAPIError(g, 500, apierr.DATABASE_ERROR, hl.NONE, "Failed to create compat-id", err))
} }
@ -284,11 +284,13 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
} }
var titleOverride *string = nil var titleOverride *string = nil
var msgidOverride *string = nil
if isCompatClient { if isCompatClient {
titleOverride = langext.Ptr(compatizeMessageTitle(ctx, h.app, msg)) titleOverride = langext.Ptr(h.app.CompatizeMessageTitle(ctx, msg))
msgidOverride = langext.Ptr(fmt.Sprintf("%d", compatMsgID))
} }
fcmDelivID, err := h.app.DeliverMessage(ctx, client, msg, titleOverride) fcmDelivID, err := h.app.DeliverMessage(ctx, client, msg, titleOverride, msgidOverride)
if err != nil { if err != nil {
_, err = h.database.CreateRetryDelivery(ctx, client, msg) _, err = h.database.CreateRetryDelivery(ctx, client, msg)
if err != nil { if err != nil {
@ -308,6 +310,6 @@ func (h MessageHandler) sendMessageInternal(g *gin.Context, ctx *logic.AppContex
User: user, User: user,
Message: msg, Message: msg,
MessageIsOld: false, MessageIsOld: false,
CompatMessageID: cid, CompatMessageID: compatMsgID,
}, nil }, nil
} }

View File

@ -6,6 +6,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
"gogs.mikescher.com/BlackForestBytes/goext/langext"
"gogs.mikescher.com/BlackForestBytes/goext/syncext" "gogs.mikescher.com/BlackForestBytes/goext/syncext"
"time" "time"
) )
@ -156,7 +157,29 @@ func (j *DeliveryRetryJob) redeliver(ctx *logic.SimpleContext, delivery models.D
} }
} else { } else {
fcmDelivID, err := j.app.DeliverMessage(ctx, client, msg, nil) isCompatClient, err := j.app.Database.Primary.IsCompatClient(ctx, client.ClientID)
if err != nil {
log.Err(err).Str("MessageID", delivery.MessageID.String()).Str("ClientID", client.ClientID.String()).Msg("Failed to get <IsCompatClient>")
ctx.RollbackTransaction()
return
}
var titleOverride *string = nil
var msgidOverride *string = nil
if isCompatClient {
messageIdComp, err := j.app.Database.Primary.ConvertToCompatIDOrCreate(ctx, msg.MessageID.String(), "messageid")
if err != nil {
log.Err(err).Str("MessageID", delivery.MessageID.String()).Str("ClientID", client.ClientID.String()).Msg("Failed to query/create messageid")
ctx.RollbackTransaction()
return
}
titleOverride = langext.Ptr(j.app.CompatizeMessageTitle(ctx, msg))
msgidOverride = langext.Ptr(fmt.Sprintf("%d", messageIdComp))
}
fcmDelivID, err := j.app.DeliverMessage(ctx, client, msg, titleOverride, msgidOverride)
if err == nil { if err == nil {
err = j.app.Database.Primary.SetDeliverySuccess(ctx, delivery, fcmDelivID) err = j.app.Database.Primary.SetDeliverySuccess(ctx, delivery, fcmDelivID)
if err != nil { if err != nil {

View File

@ -13,6 +13,15 @@ import (
"time" "time"
) )
type TxContext interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key any) any
GetOrCreateTransaction(db db.DatabaseImpl) (sq.Tx, error)
}
type AppContext struct { type AppContext struct {
app *Application app *Application
inner context.Context inner context.Context

View File

@ -9,6 +9,7 @@ import (
"blackforestbytes.com/simplecloudnotifier/push" "blackforestbytes.com/simplecloudnotifier/push"
"context" "context"
"errors" "errors"
"fmt"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding" "github.com/gin-gonic/gin/binding"
"github.com/rs/zerolog/log" "github.com/rs/zerolog/log"
@ -357,8 +358,8 @@ func (app *Application) NormalizeUsername(v string) string {
return strings.TrimSpace(v) return strings.TrimSpace(v)
} }
func (app *Application) DeliverMessage(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string) (string, error) { func (app *Application) DeliverMessage(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string, compatMsgIDOverride *string) (string, error) {
fcmDelivID, err := app.Pusher.SendNotification(ctx, client, msg, compatTitleOverride) fcmDelivID, err := app.Pusher.SendNotification(ctx, client, msg, compatTitleOverride, compatMsgIDOverride)
if err != nil { if err != nil {
log.Warn().Str("MessageID", msg.MessageID.String()).Str("ClientID", client.ClientID.String()).Err(err).Msg("FCM Delivery failed") log.Warn().Str("MessageID", msg.MessageID.String()).Str("ClientID", client.ClientID.String()).Err(err).Msg("FCM Delivery failed")
return "", err return "", err
@ -372,3 +373,16 @@ func (app *Application) InsertRequestLog(data models.RequestLog) {
log.Error().Msg("failed to insert request-log (queue full)") log.Error().Msg("failed to insert request-log (queue full)")
} }
} }
func (app *Application) CompatizeMessageTitle(ctx TxContext, msg models.Message) string {
if msg.ChannelInternalName == "main" {
return msg.Title
}
channel, err := app.Database.Primary.GetChannelByID(ctx, msg.ChannelID)
if err != nil {
return fmt.Sprintf("[%s] %s", "%SCN-ERR%", msg.Title)
}
return fmt.Sprintf("[%s] %s", channel.DisplayName, msg.Title)
}

View File

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

View File

@ -53,13 +53,13 @@ type Notification struct {
Priority int Priority int
} }
func (fb FirebaseConnector) SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string) (string, error) { func (fb FirebaseConnector) SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string, compatMsgIDOverride *string) (string, error) {
uri := "https://fcm.googleapis.com/v1/projects/" + fb.fbProject + "/messages:send" uri := "https://fcm.googleapis.com/v1/projects/" + fb.fbProject + "/messages:send"
jsonBody := gin.H{ jsonBody := gin.H{
"data": gin.H{ "data": gin.H{
"scn_msg_id": msg.MessageID.String(), "scn_msg_id": langext.Coalesce(compatMsgIDOverride, msg.MessageID.String()),
"usr_msg_id": langext.Coalesce(msg.UserMessageID, ""), "usr_msg_id": langext.Coalesce(msg.UserMessageID, ""),
"client_id": client.ClientID.String(), "client_id": client.ClientID.String(),
"timestamp": strconv.FormatInt(msg.Timestamp().Unix(), 10), "timestamp": strconv.FormatInt(msg.Timestamp().Unix(), 10),

View File

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

View File

@ -11,6 +11,7 @@ type SinkData struct {
Message models.Message Message models.Message
Client models.Client Client models.Client
CompatTitleOverride *string CompatTitleOverride *string
CompatMsgIDOverride *string
} }
type TestSink struct { type TestSink struct {
@ -25,7 +26,7 @@ func (d *TestSink) Last() SinkData {
return d.Data[len(d.Data)-1] return d.Data[len(d.Data)-1]
} }
func (d *TestSink) SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string) (string, error) { func (d *TestSink) SendNotification(ctx context.Context, client models.Client, msg models.Message, compatTitleOverride *string, compatMsgIDOverride *string) (string, error) {
id, err := langext.NewHexUUID() id, err := langext.NewHexUUID()
if err != nil { if err != nil {
return "", err return "", err
@ -37,6 +38,7 @@ func (d *TestSink) SendNotification(ctx context.Context, client models.Client, m
Message: msg, Message: msg,
Client: client, Client: client,
CompatTitleOverride: compatTitleOverride, CompatTitleOverride: compatTitleOverride,
CompatMsgIDOverride: compatMsgIDOverride,
}) })
return key, nil return key, nil