mirror of
https://github.com/gotify/server.git
synced 2026-09-05 23:39:26 +08:00
Add a final safety net in redirectToChannel.SendMessage that refuses a message when ApplicationID == 0, so it can never be stored as an orphaned message (application_id = 0, not shown, not deletable). Requested in review. Cover the previously untested error branches around internal-application back-fill (create/update failures) so patch coverage no longer regresses. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
45 lines
1.2 KiB
Go
45 lines
1.2 KiB
Go
package plugin
|
|
|
|
import (
|
|
"errors"
|
|
"time"
|
|
|
|
"github.com/gotify/server/v2/model"
|
|
"github.com/gotify/server/v2/plugin/compat"
|
|
)
|
|
|
|
type redirectToChannel struct {
|
|
ApplicationID uint
|
|
UserID uint
|
|
Messages chan MessageWithUserID
|
|
}
|
|
|
|
// MessageWithUserID encapsulates a message with a given user ID.
|
|
type MessageWithUserID struct {
|
|
Message model.MessageExternal
|
|
UserID uint
|
|
}
|
|
|
|
// SendMessage sends a message to the underlying message channel.
|
|
func (c redirectToChannel) SendMessage(msg compat.Message) error {
|
|
if c.ApplicationID == 0 {
|
|
// Final safety net: the internal application should always be set up by
|
|
// Manager.initializeSingleUserPlugin. If it somehow isn't, refuse the
|
|
// message instead of storing it with application_id = 0, where it would
|
|
// be orphaned (not shown in the UI and not deletable).
|
|
return errors.New("plugin messenger has no associated internal application")
|
|
}
|
|
c.Messages <- MessageWithUserID{
|
|
Message: model.MessageExternal{
|
|
ApplicationID: c.ApplicationID,
|
|
Message: msg.Message,
|
|
Title: msg.Title,
|
|
Priority: &msg.Priority,
|
|
Date: time.Now(),
|
|
Extras: msg.Extras,
|
|
},
|
|
UserID: c.UserID,
|
|
}
|
|
return nil
|
|
}
|