101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package gonexapp
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
)
|
|
|
|
// Notification is one Nextcloud bell for a single Recipient.
|
|
type Notification struct {
|
|
Subject string
|
|
Message string
|
|
Link string
|
|
SubjectParams map[string]any
|
|
MessageParams map[string]any
|
|
}
|
|
|
|
// AppAPINotifications creates Notifications via AppAPI OCS.
|
|
type AppAPINotifications struct {
|
|
Cred Credentials
|
|
Client *http.Client
|
|
OCS OCSClient
|
|
}
|
|
|
|
// NewAppAPINotifications returns a sender using cred for AppAPI auth.
|
|
func NewAppAPINotifications(cred Credentials) AppAPINotifications {
|
|
return AppAPINotifications{
|
|
Cred: cred,
|
|
OCS: OCSClient{Cred: cred},
|
|
}
|
|
}
|
|
|
|
func (n AppAPINotifications) ocsClient() OCSClient {
|
|
c := n.OCS
|
|
if c.Cred.BaseURL == "" {
|
|
c.Cred = n.Cred
|
|
}
|
|
if c.Client == nil {
|
|
c.Client = n.Client
|
|
}
|
|
return c
|
|
}
|
|
|
|
// Send creates a Notification for Cred.UserID.
|
|
// It returns an error when UserID or Subject is empty, or when the OCS call fails.
|
|
// AppAPI notification OCS accepts Subject, Message, Link, and rich-object
|
|
// parameters. It does not accept actions or a custom icon.
|
|
func (n AppAPINotifications) Send(notif Notification) error {
|
|
if n.Cred.UserID == "" {
|
|
return fmt.Errorf("notification recipient user id is required")
|
|
}
|
|
return n.send(n.Cred.UserID, notif)
|
|
}
|
|
|
|
// SendTo creates a Notification for userID.
|
|
// The OCS call is authenticated as that user via WithUser.
|
|
// It returns an error when userID or Subject is empty, or when the OCS call fails.
|
|
func (n AppAPINotifications) SendTo(userID string, notif Notification) error {
|
|
return n.send(userID, notif)
|
|
}
|
|
|
|
func (n AppAPINotifications) send(userID string, notif Notification) error {
|
|
if userID == "" {
|
|
return fmt.Errorf("notification recipient user id is required")
|
|
}
|
|
if notif.Subject == "" {
|
|
return fmt.Errorf("notification subject is required")
|
|
}
|
|
ocs := n.ocsClient()
|
|
ocs.Cred = ocs.Cred.WithUser(userID)
|
|
richSubjectParams := notif.SubjectParams
|
|
if richSubjectParams == nil {
|
|
richSubjectParams = map[string]any{}
|
|
}
|
|
subjectParams := map[string]any{
|
|
"rich_subject": notif.Subject,
|
|
"rich_subject_params": richSubjectParams,
|
|
}
|
|
if notif.Message != "" {
|
|
richMessageParams := notif.MessageParams
|
|
if richMessageParams == nil {
|
|
richMessageParams = map[string]any{}
|
|
}
|
|
subjectParams["rich_message"] = notif.Message
|
|
subjectParams["rich_message_params"] = richMessageParams
|
|
}
|
|
if notif.Link != "" {
|
|
subjectParams["link"] = notif.Link
|
|
}
|
|
body, _ := json.Marshal(map[string]any{
|
|
"params": map[string]any{
|
|
"object": "app_api",
|
|
"object_id": "app_api_id",
|
|
"subject_type": "app_api_ex_app",
|
|
"subject_params": subjectParams,
|
|
},
|
|
})
|
|
_, err := ocs.Call(http.MethodPost, "apps/app_api/api/v1/notification", body)
|
|
return err
|
|
}
|