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"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// Groups reads Users and Groups from Provisioning OCS.
|
||||
type Groups struct {
|
||||
Cred Credentials
|
||||
Client *http.Client
|
||||
OCS OCSClient
|
||||
}
|
||||
|
||||
// NewGroups returns a directory reader using cred for AppAPI auth.
|
||||
func NewGroups(cred Credentials) Groups {
|
||||
return Groups{
|
||||
Cred: cred,
|
||||
OCS: OCSClient{Cred: cred},
|
||||
}
|
||||
}
|
||||
|
||||
func (g Groups) ocsClient() OCSClient {
|
||||
c := g.OCS
|
||||
if c.Cred.BaseURL == "" {
|
||||
c.Cred = g.Cred
|
||||
}
|
||||
if c.Client == nil {
|
||||
c.Client = g.Client
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
// UserGroups returns the Nextcloud group ids of userID, calling OCS as that user.
|
||||
func (g Groups) UserGroups(userID string) ([]string, error) {
|
||||
ocs := g.ocsClient()
|
||||
ocs.Cred = ocs.Cred.WithUser(userID)
|
||||
path := "cloud/users/" + url.PathEscape(userID) + "/groups"
|
||||
raw, err := ocs.Call(http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeUserGroups(raw)
|
||||
}
|
||||
|
||||
// GroupMembers returns the user ids in groupID, calling OCS as the Requesting user.
|
||||
func (g Groups) GroupMembers(groupID string) ([]string, error) {
|
||||
ocs := g.ocsClient()
|
||||
path := "cloud/groups/" + url.PathEscape(groupID)
|
||||
raw, err := ocs.Call(http.MethodGet, path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeGroupMembers(raw)
|
||||
}
|
||||
|
||||
func decodeGroupMembers(raw []byte) ([]string, error) {
|
||||
var parsed struct {
|
||||
OCS struct {
|
||||
Data struct {
|
||||
Users []string `json:"users"`
|
||||
} `json:"data"`
|
||||
} `json:"ocs"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &parsed); err != nil {
|
||||
return nil, fmt.Errorf("group members decode: %w", err)
|
||||
}
|
||||
return parsed.OCS.Data.Users, nil
|
||||
}
|
||||
|
||||
// ListGroups returns instance group ids, calling OCS as the Requesting user.
|
||||
func (g Groups) ListGroups() ([]string, error) {
|
||||
ocs := g.ocsClient()
|
||||
raw, err := ocs.Call(http.MethodGet, "cloud/groups", nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return decodeUserGroups(raw)
|
||||
}
|
||||
|
||||
func decodeUserGroups(raw []byte) ([]string, error) {
|
||||
var parsed struct {
|
||||
OCS struct {
|
||||
Data struct {
|
||||
Groups []string `json:"groups"`
|
||||
} `json:"data"`
|
||||
} `json:"ocs"`
|
||||
}
|
||||
if err := json.Unmarshal(raw, &parsed); err != nil {
|
||||
return nil, fmt.Errorf("user groups decode: %w", err)
|
||||
}
|
||||
return parsed.OCS.Data.Groups, nil
|
||||
}
|
||||
Reference in New Issue
Block a user