working oc-shared

This commit is contained in:
mr
2025-01-17 17:20:37 +01:00
parent 3b65fb934b
commit 7d391e4059
7 changed files with 201 additions and 40 deletions

View File

@@ -6,6 +6,7 @@ import (
"slices"
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/config"
"cloud.o-forge.io/core/oc-lib/tools"
beego "github.com/beego/beego/v2/server/web"
)
@@ -36,12 +37,15 @@ var paths = map[tools.DataType]map[tools.METHOD]string{ // paths used to call ot
// @Title Search
// @Description search shared workspace
// @Param search path string true "the word search you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {shared workspace} models.shared_workspace
// @router /search/:search [get]
func (o *CollaborativeAreaController) Search() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return Id or post with UUID
search := o.Ctx.Input.Param(":search")
o.Data["json"] = oclib.Search(nil, search, oclib.LibDataEnum(tools.COLLABORATIVE_AREA))
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).Search(nil, search, isDraft == "true")
o.ServeJSON()
}
@@ -52,6 +56,7 @@ func (o *CollaborativeAreaController) Search() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id [put]
func (o *CollaborativeAreaController) Put() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return Id or post with UUID
var res map[string]interface{}
id := o.Ctx.Input.Param(":id")
@@ -59,7 +64,7 @@ func (o *CollaborativeAreaController) Put() {
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
fmt.Println("UPDATE", res)
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), res, id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(res, id)
o.ServeJSON()
}
@@ -69,20 +74,24 @@ func (o *CollaborativeAreaController) Put() {
// @Success 200 {shared workspace} models.shared_workspace
// @router / [post]
func (o *CollaborativeAreaController) Post() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
o.Data["json"] = oclib.StoreOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), res, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).StoreOne(res)
o.ServeJSON()
}
// @Title GetAll
// @Description find shared workspace by id
// @Param is_draft query string false "draft wished"
// @Success 200 {shared_workspace} models.shared_workspace
// @router / [get]
func (o *CollaborativeAreaController) GetAll() {
o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(tools.COLLABORATIVE_AREA))
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadAll(isDraft == "true")
o.ServeJSON()
}
@@ -92,8 +101,9 @@ func (o *CollaborativeAreaController) GetAll() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id [get]
func (o *CollaborativeAreaController) Get() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
o.ServeJSON()
}
@@ -104,11 +114,12 @@ func (o *CollaborativeAreaController) Get() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workspace/:id2 [delete]
func (o *CollaborativeAreaController) RemoveWorkspace() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
shared := r.ToCollaborativeArea()
newWorkspace := []string{}
if slices.Contains(shared.Workspaces, id2) {
@@ -119,7 +130,8 @@ func (o *CollaborativeAreaController) RemoveWorkspace() {
}
shared.Workspaces = newWorkspace
}
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -130,11 +142,12 @@ func (o *CollaborativeAreaController) RemoveWorkspace() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workflow/:id2 [delete]
func (o *CollaborativeAreaController) RemoveWorkflow() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
shared := r.ToCollaborativeArea()
newWorkflows := []string{}
for _, w := range shared.Workflows {
@@ -143,7 +156,8 @@ func (o *CollaborativeAreaController) RemoveWorkflow() {
}
}
shared.Workflows = newWorkflows
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -154,13 +168,14 @@ func (o *CollaborativeAreaController) RemoveWorkflow() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/peer/:id2 [delete]
func (o *CollaborativeAreaController) RemovePeer() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
shared := r.ToCollaborativeArea()
newPeers := []string{}
newPeers := map[string][]string{}
if shared.CreatorID != id2 {
o.Data["json"] = map[string]interface{}{
"data": nil,
@@ -170,15 +185,28 @@ func (o *CollaborativeAreaController) RemovePeer() {
o.ServeJSON()
return
}
if slices.Contains(shared.Peers, id) && shared.CreatorID != id2 {
for _, peer := range shared.Peers {
if peer != id2 {
newPeers = append(newPeers, peer)
}
if shared.AllowedPeersGroup != nil {
keys := make([]string, len(shared.AllowedPeersGroup))
i := 0
for k := range shared.AllowedPeersGroup {
keys[i] = k
i++
}
if slices.Contains(keys, id) && shared.CreatorID != id2 {
for _, peer := range keys {
if peer != id2 {
if config.GetConfig().Whitelist {
newPeers[peer] = []string{"*"}
} else {
newPeers[peer] = []string{}
}
}
}
shared.AllowedPeersGroup = newPeers
}
shared.Peers = newPeers
}
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -189,11 +217,12 @@ func (o *CollaborativeAreaController) RemovePeer() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/rule/:id2 [delete]
func (o *CollaborativeAreaController) RemoveRule() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
shared := r.ToCollaborativeArea()
newRules := []string{}
if shared != nil && slices.Contains(shared.Rules, id2) {
@@ -204,7 +233,8 @@ func (o *CollaborativeAreaController) RemoveRule() {
}
shared.Rules = newRules
}
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -215,11 +245,12 @@ func (o *CollaborativeAreaController) RemoveRule() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workspace/:id2 [post]
func (o *CollaborativeAreaController) AddWorkspace() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
if r.Code != 200 {
o.Data["json"] = r
o.ServeJSON()
@@ -229,7 +260,8 @@ func (o *CollaborativeAreaController) AddWorkspace() {
if shared != nil && !slices.Contains(shared.Workspaces, id2) {
shared.Workspaces = append(shared.Workspaces, id2)
}
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -240,16 +272,18 @@ func (o *CollaborativeAreaController) AddWorkspace() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workflow/:id2 [post]
func (o *CollaborativeAreaController) AddWorkflow() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).LoadOne(id)
shared := r.ToCollaborativeArea()
if shared != nil && !slices.Contains(shared.Workflows, id2) {
shared.Workflows = append(shared.Workflows, id2)
}
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -260,16 +294,29 @@ func (o *CollaborativeAreaController) AddWorkflow() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/peer/:id2 [post]
func (o *CollaborativeAreaController) AddPeer() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
shared := r.ToCollaborativeArea()
if shared != nil && !slices.Contains(shared.Peers, id2) {
shared.Peers = append(shared.Peers, id2)
if shared.AllowedPeersGroup != nil {
keys := make([]string, len(shared.AllowedPeersGroup))
i := 0
for k := range shared.AllowedPeersGroup {
keys[i] = k
i++
}
if shared != nil && !slices.Contains(keys, id2) {
if config.GetConfig().Whitelist {
shared.AllowedPeersGroup[id2] = []string{"*"}
} else {
shared.AllowedPeersGroup[id2] = []string{}
}
}
}
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -280,16 +327,17 @@ func (o *CollaborativeAreaController) AddPeer() {
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/rule/:id2 [post]
func (o *CollaborativeAreaController) AddRule() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
r := oclib.LoadOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id)
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
shared := r.ToCollaborativeArea()
if shared != nil && !slices.Contains(shared.Rules, id2) {
shared.Rules = append(shared.Rules, id2)
}
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), shared.Serialize(), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(shared.Serialize(shared), id)
o.ServeJSON()
}
@@ -299,9 +347,10 @@ func (o *CollaborativeAreaController) AddRule() {
// @Success 200 {shared workspace} delete success!
// @router /:id [delete]
func (o *CollaborativeAreaController) Delete() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(tools.COLLABORATIVE_AREA), id, caller)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).DeleteOne(id)
o.ServeJSON()
}

View File

@@ -15,12 +15,15 @@ type RuleController struct {
// @Title Search
// @Description search rule
// @Param search path string true "the word search you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {rule} models.rule
// @router /search/:search [get]
func (o *RuleController) Search() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return Id or post with UUID
search := o.Ctx.Input.Param(":search")
o.Data["json"] = oclib.Search(nil, search, oclib.LibDataEnum(oclib.RULE))
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).Search(nil, search, isDraft == "true")
o.ServeJSON()
}
@@ -31,11 +34,12 @@ func (o *RuleController) Search() {
// @Success 200 {rule} models.rule
// @router /:id [put]
func (o *RuleController) Put() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return ID or post with UUID
var res map[string]interface{}
id := o.Ctx.Input.Param(":id")
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
o.Data["json"] = oclib.UpdateOne(oclib.LibDataEnum(oclib.RULE), res, id)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).UpdateOne(res, id)
o.ServeJSON()
}
@@ -45,18 +49,22 @@ func (o *RuleController) Put() {
// @Success 200 {rule} models.rule
// @router / [post]
func (o *RuleController) Post() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
o.Data["json"] = oclib.StoreOne(oclib.LibDataEnum(oclib.RULE), res)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).StoreOne(res)
o.ServeJSON()
}
// @Title GetAll
// @Description find rule by id
// @Param is_draft query string false "draft wished"
// @Success 200 {rule} models.rule
// @router / [get]
func (o *RuleController) GetAll() {
o.Data["json"] = oclib.LoadAll(oclib.LibDataEnum(oclib.RULE))
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).LoadAll(isDraft == "true")
o.ServeJSON()
}
@@ -66,8 +74,9 @@ func (o *RuleController) GetAll() {
// @Success 200 {rule} models.rule
// @router /:id [get]
func (o *RuleController) Get() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.LoadOne(oclib.LibDataEnum(oclib.RULE), id)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).LoadOne(id)
o.ServeJSON()
}
@@ -77,7 +86,8 @@ func (o *RuleController) Get() {
// @Success 200 {rule} delete success!
// @router /:id [delete]
func (o *RuleController) Delete() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.DeleteOne(oclib.LibDataEnum(oclib.RULE), id)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.RULE), user, peerID, groups, nil).DeleteOne(id)
o.ServeJSON()
}