Some checks failed
SonarQube / SonarQube (push) Failing after 5m28s
E2e test on main branch push / server and client test (map[name:admin port:8080]) (push) Failing after 3m33s
Server and lib tests / Library test (js) (push) Failing after 1m36s
Server and lib tests / Library test (vue) (push) Successful in 2m24s
Server and lib tests / Web client test (admin) (push) Successful in 2m52s
Server and lib tests / Web client test (compose) (push) Successful in 3m23s
Server and lib tests / Web client test (discovery) (push) Successful in 2m49s
Server and lib tests / Web client test (one) (push) Successful in 2m52s
Server and lib tests / Web client test (privacy) (push) Successful in 2m44s
Server and lib tests / Web client test (reporter) (push) Successful in 2m56s
Server and lib tests / Web client test (workflow) (push) Successful in 3m0s
Server and lib tests / Server tests (push) Failing after 4m55s
E2e test on main branch push / notify-failure (push) Failing after 9s
Server and lib tests / Send matrix testing notification (push) Successful in 2s
Mark stale issues and pull requests / stale (push) Successful in 40s
130 lines
3.0 KiB
Go
130 lines
3.0 KiB
Go
package rest
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"github.com/cortezaproject/corteza/server/federation/rest/request"
|
|
"github.com/cortezaproject/corteza/server/federation/service"
|
|
"github.com/cortezaproject/corteza/server/federation/types"
|
|
"github.com/cortezaproject/corteza/server/pkg/errors"
|
|
"github.com/cortezaproject/corteza/server/pkg/federation"
|
|
"github.com/cortezaproject/corteza/server/pkg/filter"
|
|
)
|
|
|
|
type (
|
|
SyncStructure struct{}
|
|
)
|
|
|
|
func (SyncStructure) New() *SyncStructure {
|
|
return &SyncStructure{}
|
|
}
|
|
|
|
// ReadExposedInternal gets the exposed module info and serves
|
|
// the internal Corteza format of the structure
|
|
func (ctrl SyncStructure) ReadExposedInternal(ctx context.Context, r *request.SyncStructureReadExposedInternal) (interface{}, error) {
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
var (
|
|
err error
|
|
ef federation.EncodingFormat = federation.CortezaInternalStructure
|
|
)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
fEncoder := federation.NewEncoder(w, service.DefaultOptions)
|
|
|
|
payload, err := ctrl.readExposedAll(ctx, r)
|
|
|
|
if err != nil {
|
|
errors.ServeHTTP(w, req, err, false)
|
|
return
|
|
}
|
|
|
|
err = fEncoder.Encode(*payload, ef)
|
|
|
|
if err != nil {
|
|
errors.ServeHTTP(w, req, err, false)
|
|
return
|
|
}
|
|
|
|
return
|
|
}, nil
|
|
}
|
|
|
|
// ReadExposedSocial gets the exposed module info and serves
|
|
// the activity streams format of the structure
|
|
func (ctrl SyncStructure) ReadExposedSocial(ctx context.Context, r *request.SyncStructureReadExposedSocial) (interface{}, error) {
|
|
return func(w http.ResponseWriter, req *http.Request) {
|
|
var (
|
|
err error
|
|
ef federation.EncodingFormat = federation.ActivityStreamsStructure
|
|
)
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
fEncoder := federation.NewEncoder(w, service.DefaultOptions)
|
|
|
|
rr := request.SyncStructureReadExposedInternal{
|
|
NodeID: r.NodeID,
|
|
LastSync: r.LastSync,
|
|
Query: r.Query,
|
|
Limit: r.Limit,
|
|
PageCursor: r.PageCursor,
|
|
Sort: r.Sort,
|
|
}
|
|
|
|
payload, err := ctrl.readExposedAll(ctx, &rr)
|
|
|
|
if err != nil {
|
|
errors.ServeHTTP(w, req, err, false)
|
|
return
|
|
}
|
|
|
|
err = fEncoder.Encode(*payload, ef)
|
|
|
|
if err != nil {
|
|
errors.ServeHTTP(w, req, err, false)
|
|
return
|
|
}
|
|
|
|
return
|
|
}, nil
|
|
}
|
|
|
|
// readExposedAll fetches the exposed modules for the specific node
|
|
func (ctrl SyncStructure) readExposedAll(ctx context.Context, r *request.SyncStructureReadExposedInternal) (*federation.ListStructurePayload, error) {
|
|
var (
|
|
err error
|
|
node *types.Node
|
|
)
|
|
|
|
if node, err = service.DefaultNode.FindBySharedNodeID(ctx, r.NodeID); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
f := types.ExposedModuleFilter{
|
|
NodeID: node.ID,
|
|
LastSync: r.LastSync,
|
|
}
|
|
|
|
if f.Paging, err = filter.NewPaging(r.Limit, r.PageCursor); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if f.Sorting, err = filter.NewSorting(r.Sort); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
list, f, err := (service.ExposedModule()).Find(ctx, f)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &federation.ListStructurePayload{
|
|
NodeID: node.ID,
|
|
Filter: &f,
|
|
Set: &list,
|
|
}, nil
|
|
}
|