oc-shared/controllers/collaborative_area.go

359 lines
14 KiB
Go
Raw Normal View History

2024-08-12 14:12:51 +02:00
package controllers
import (
"encoding/json"
2024-08-27 15:39:08 +02:00
"fmt"
"slices"
2024-08-12 14:12:51 +02:00
oclib "cloud.o-forge.io/core/oc-lib"
2025-01-17 17:20:37 +01:00
"cloud.o-forge.io/core/oc-lib/config"
2024-08-13 11:42:09 +02:00
"cloud.o-forge.io/core/oc-lib/tools"
2024-08-12 14:12:51 +02:00
beego "github.com/beego/beego/v2/server/web"
)
// Operations about workspace
2024-09-27 14:08:14 +02:00
type CollaborativeAreaController struct {
2024-08-12 14:12:51 +02:00
beego.Controller
}
2024-10-02 14:23:05 +02:00
var paths = map[tools.DataType]map[tools.METHOD]string{ // paths used to call other OC services
tools.COLLABORATIVE_AREA: {
2024-10-15 11:00:39 +02:00
tools.DELETE: "/collaborative_area/:id?is_remote=true",
tools.POST: "/collaborative_area/?is_remote=true",
2024-10-02 14:23:05 +02:00
},
tools.WORKSPACE: {
2024-10-15 11:00:39 +02:00
tools.DELETE: "/:id?is_remote=true",
tools.POST: "/?is_remote=true",
2024-10-02 14:23:05 +02:00
},
tools.WORKFLOW: {
2024-10-15 11:00:39 +02:00
tools.DELETE: "/:id?is_remote=true",
tools.POST: "/?is_remote=true",
2024-10-02 14:23:05 +02:00
},
tools.PEER: {
2024-10-15 11:00:39 +02:00
tools.POST: "/status/",
2024-10-02 14:23:05 +02:00
},
}
2024-08-12 14:12:51 +02:00
// @Title Search
// @Description search shared workspace
// @Param search path string true "the word search you want to get"
2025-01-17 17:20:37 +01:00
// @Param is_draft query string false "draft wished"
2024-08-12 14:12:51 +02:00
// @Success 200 {shared workspace} models.shared_workspace
// @router /search/:search [get]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) Search() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-12 14:12:51 +02:00
// store and return Id or post with UUID
search := o.Ctx.Input.Param(":search")
2025-01-17 17:20:37 +01:00
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")
2024-08-12 14:12:51 +02:00
o.ServeJSON()
}
// @Title Update
// @Description create shared workspaces
// @Param id path string true "the shared workspace id you want to get"
// @Param body body models.workspace true "The shared workspace content"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id [put]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) Put() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-12 14:12:51 +02:00
// 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)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-27 15:39:08 +02:00
fmt.Println("UPDATE", res)
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(res, id)
2024-08-12 14:12:51 +02:00
o.ServeJSON()
}
// @Title Create
// @Description create shared workspace
// @Param data body json true "body for data content (Json format)"
// @Success 200 {shared workspace} models.shared_workspace
// @router / [post]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) Post() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-12 14:12:51 +02:00
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).StoreOne(res)
2024-08-12 14:12:51 +02:00
o.ServeJSON()
}
// @Title GetAll
// @Description find shared workspace by id
2025-01-17 17:20:37 +01:00
// @Param is_draft query string false "draft wished"
2024-08-12 14:12:51 +02:00
// @Success 200 {shared_workspace} models.shared_workspace
// @router / [get]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) GetAll() {
2025-01-17 17:20:37 +01:00
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")
2024-08-12 14:12:51 +02:00
o.ServeJSON()
}
// @Title Get
// @Description find shared workspace by id
// @Param id path string true "the id you want to get"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id [get]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) Get() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-12 14:12:51 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-08-12 14:12:51 +02:00
o.ServeJSON()
}
2024-11-19 15:07:47 +01:00
// @Title Remove Workspace
2024-08-26 13:45:50 +02:00
// @Description find shared workspace by id
2024-08-26 15:39:56 +02:00
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workspace/:id2 [delete]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) RemoveWorkspace() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 15:39:56 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-01-17 17:20:37 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2024-08-26 15:39:56 +02:00
newWorkspace := []string{}
2024-08-30 10:35:12 +02:00
if slices.Contains(shared.Workspaces, id2) {
2024-08-26 15:39:56 +02:00
for _, w := range shared.Workspaces {
if w != id2 {
newWorkspace = append(newWorkspace, w)
}
}
shared.Workspaces = newWorkspace
}
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
2024-08-26 15:39:56 +02:00
o.ServeJSON()
}
// @Title Remove Workflow
// @Description find shared workspace by id
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workflow/:id2 [delete]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) RemoveWorkflow() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 15:39:56 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-01-17 17:20:37 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2024-08-26 15:39:56 +02:00
newWorkflows := []string{}
2024-11-19 15:07:47 +01:00
for _, w := range shared.Workflows {
if w != id2 {
newWorkflows = append(newWorkflows, w)
2024-08-26 15:39:56 +02:00
}
}
2024-11-19 15:07:47 +01:00
shared.Workflows = newWorkflows
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
2024-08-26 15:39:56 +02:00
o.ServeJSON()
}
// @Title Remove Peer
// @Description find shared workspace by id
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/peer/:id2 [delete]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) RemovePeer() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 15:39:56 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-01-17 17:20:37 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2025-01-17 17:20:37 +01:00
newPeers := map[string][]string{}
2025-02-05 08:38:19 +01:00
if shared.CreatorID == id2 {
2024-08-26 15:39:56 +02:00
o.Data["json"] = map[string]interface{}{
2024-08-27 15:39:08 +02:00
"data": nil,
"code": 409,
2024-08-26 15:39:56 +02:00
"error": "You can't remove the creator from the shared workspace",
}
o.ServeJSON()
return
}
2025-01-17 17:20:37 +01:00
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{}
}
}
2024-08-26 15:39:56 +02:00
}
2025-01-17 17:20:37 +01:00
shared.AllowedPeersGroup = newPeers
2024-08-26 15:39:56 +02:00
}
}
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
2024-08-26 15:39:56 +02:00
o.ServeJSON()
}
// @Title Remove Rule
// @Description find shared workspace by id
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/rule/:id2 [delete]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) RemoveRule() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 15:39:56 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-01-17 17:20:37 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2024-08-26 15:39:56 +02:00
newRules := []string{}
2024-08-30 10:35:12 +02:00
if shared != nil && slices.Contains(shared.Rules, id2) {
2024-08-26 15:39:56 +02:00
for _, rule := range shared.Rules {
if rule != id2 {
newRules = append(newRules, rule)
}
}
shared.Rules = newRules
}
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
2024-08-26 15:39:56 +02:00
o.ServeJSON()
}
// @Title Add Workspace
// @Description find shared workspace by id
2024-08-26 13:45:50 +02:00
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workspace/:id2 [post]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) AddWorkspace() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 13:45:50 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-01-17 17:20:37 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-08-27 15:39:08 +02:00
if r.Code != 200 {
o.Data["json"] = r
o.ServeJSON()
return
}
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2024-08-30 10:35:12 +02:00
if shared != nil && !slices.Contains(shared.Workspaces, id2) {
2024-08-26 13:45:50 +02:00
shared.Workspaces = append(shared.Workspaces, id2)
}
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
2024-08-26 13:45:50 +02:00
o.ServeJSON()
}
// @Title Add Workflow
// @Description find shared workspace by id
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/workflow/:id2 [post]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) AddWorkflow() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 13:45:50 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-02-05 08:38:19 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
if r.Code != 200 {
o.Data["json"] = r
o.ServeJSON()
return
}
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2024-08-30 10:35:12 +02:00
if shared != nil && !slices.Contains(shared.Workflows, id2) {
2024-08-26 13:45:50 +02:00
shared.Workflows = append(shared.Workflows, id2)
}
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
2024-08-26 13:45:50 +02:00
o.ServeJSON()
}
// @Title Add Peer
// @Description find shared workspace by id
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
2025-02-05 08:38:19 +01:00
// @Param body body models.workspace true "The shared workspace content"
2024-08-26 13:45:50 +02:00
// @Success 200 {shared workspace} models.shared_workspace
// @router /:id/peer/:id2 [post]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) AddPeer() {
2025-02-05 08:38:19 +01:00
var res []string
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 13:45:50 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-01-17 17:20:37 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2025-02-05 08:38:19 +01:00
if shared.AllowedPeersGroup == nil {
shared.AllowedPeersGroup = map[string][]string{}
}
if config.GetConfig().Whitelist && len(res) == 0 {
shared.AllowedPeersGroup[id2] = []string{"*"}
} else {
shared.AllowedPeersGroup[id2] = res
2024-08-26 13:45:50 +02:00
}
2025-02-05 08:38:19 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(
shared.Serialize(shared), id)
2024-08-26 13:45:50 +02:00
o.ServeJSON()
}
2024-08-26 13:50:11 +02:00
// @Title Add Rule
2024-08-26 13:45:50 +02:00
// @Description find shared workspace by id
// @Param id path string true "the id you want to get"
// @Param id2 path string true "the id you want to add"
// @Success 200 {shared workspace} models.shared_workspace
2024-08-26 13:50:11 +02:00
// @router /:id/rule/:id2 [post]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) AddRule() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2024-08-26 13:45:50 +02:00
id := o.Ctx.Input.Param(":id")
id2 := o.Ctx.Input.Param(":id2")
2025-01-17 17:20:37 +01:00
r := oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, nil).LoadOne(id)
2024-09-27 14:08:14 +02:00
shared := r.ToCollaborativeArea()
2024-08-30 10:35:12 +02:00
if shared != nil && !slices.Contains(shared.Rules, id2) {
2024-08-26 13:45:50 +02:00
shared.Rules = append(shared.Rules, id2)
}
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).UpdateOne(shared.Serialize(shared), id)
2024-08-26 13:45:50 +02:00
o.ServeJSON()
}
2024-08-12 14:12:51 +02:00
// @Title Delete
// @Description delete the shared workspace
// @Param id path string true "The id you want to delete"
// @Success 200 {shared workspace} delete success!
// @router /:id [delete]
2024-09-27 14:08:14 +02:00
func (o *CollaborativeAreaController) Delete() {
2025-01-17 17:20:37 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-12 14:12:51 +02:00
id := o.Ctx.Input.Param(":id")
2024-08-30 10:35:12 +02:00
caller := tools.NewHTTPCaller(paths) // caller used to send to peers
2024-10-02 14:23:05 +02:00
caller.Disabled = oclib.IsQueryParamsEquals(o.Ctx.Input, "is_remote", true)
2025-01-17 17:20:37 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.COLLABORATIVE_AREA), user, peerID, groups, caller).DeleteOne(id)
2024-08-12 14:12:51 +02:00
o.ServeJSON()
}