Add AppAPI Notifications and Users and Groups reads.
ExApps can Send/SendTo a bell for one Recipient and read group membership and directory OCS as the Requesting user, with Access Gate using UserGroups. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
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 the Requesting user on the Credentials.
|
||||
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, impersonating that Recipient.
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user