mirror of
https://github.com/documize/community.git
synced 2025-08-04 21:15:24 +02:00
clone space!
This commit is contained in:
parent
866b4eba8a
commit
bf390ed0b9
18 changed files with 302 additions and 108 deletions
|
@ -12,6 +12,7 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
|
@ -199,6 +200,11 @@ func (s Scope) TemplatesBySpace(ctx domain.RequestContext, spaceID string) (docu
|
|||
ctx.OrgID,
|
||||
ctx.UserID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
documents = []doc.Document{}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "select space document templates")
|
||||
return
|
||||
|
@ -241,6 +247,11 @@ func (s Scope) DocumentList(ctx domain.RequestContext) (documents []doc.Document
|
|||
ctx.OrgID,
|
||||
ctx.UserID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
err = nil
|
||||
documents = []doc.Document{}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
err = errors.Wrap(err, "select documents list")
|
||||
return
|
||||
|
|
|
@ -13,6 +13,7 @@ package mysql
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/documize/community/core/env"
|
||||
|
@ -162,15 +163,18 @@ func (s Scope) DeleteLink(ctx domain.RequestContext, id string) (rows int64, err
|
|||
// SearchCandidates returns matching documents, sections and attachments using keywords.
|
||||
func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (docs []link.Candidate,
|
||||
pages []link.Candidate, attachments []link.Candidate, err error) {
|
||||
|
||||
// find matching documents
|
||||
temp := []link.Candidate{}
|
||||
likeQuery := "title LIKE '%" + keywords + "%'"
|
||||
keywords = strings.TrimSpace(strings.ToLower(keywords))
|
||||
likeQuery := "LOWER(title) LIKE '%" + keywords + "%'"
|
||||
|
||||
err = s.Runtime.Db.Select(&temp,
|
||||
`SELECT refid as documentid, labelid as folderid,title from document WHERE orgid=? AND `+likeQuery+` AND labelid IN
|
||||
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
|
||||
UNION ALL SELECT refid FROM label a where orgid=? AND type=1 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
|
||||
UNION ALL SELECT refid FROM label a where orgid=? AND type=3 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
|
||||
`SELECT d.refid as documentid, d. labelid as folderid, d.title, l.label as context
|
||||
FROM document d LEFT JOIN label l ON d.labelid=l.refid WHERE l.orgid=? AND `+likeQuery+` AND d.labelid IN
|
||||
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
|
||||
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=1 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
|
||||
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=3 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
|
||||
ORDER BY title`,
|
||||
ctx.OrgID,
|
||||
ctx.OrgID,
|
||||
|
@ -194,22 +198,22 @@ func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (doc
|
|||
TargetID: r.DocumentID,
|
||||
LinkType: "document",
|
||||
Title: r.Title,
|
||||
Context: "",
|
||||
Context: r.Context,
|
||||
}
|
||||
|
||||
docs = append(docs, c)
|
||||
}
|
||||
|
||||
// find matching sections
|
||||
likeQuery = "p.title LIKE '%" + keywords + "%'"
|
||||
likeQuery = "LOWER(p.title) LIKE '%" + keywords + "%'"
|
||||
temp = []link.Candidate{}
|
||||
|
||||
err = s.Runtime.Db.Select(&temp,
|
||||
`SELECT p.refid as targetid, p.documentid as documentid, p.title as title, p.pagetype as linktype, d.title as context, d.labelid as folderid from page p
|
||||
LEFT JOIN document d ON d.refid=p.documentid WHERE p.orgid=? AND `+likeQuery+` AND d.labelid IN
|
||||
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
|
||||
UNION ALL SELECT refid FROM label a where orgid=? AND type=1 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
|
||||
UNION ALL SELECT refid FROM label a where orgid=? AND type=3 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
|
||||
`SELECT p.refid as targetid, p.documentid as documentid, p.title as title, p.pagetype as linktype, d.title as context, d.labelid as folderid
|
||||
FROM page p LEFT JOIN document d ON d.refid=p.documentid WHERE p.orgid=? AND `+likeQuery+` AND d.labelid IN
|
||||
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
|
||||
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=1 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
|
||||
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=3 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
|
||||
ORDER BY p.title`,
|
||||
ctx.OrgID,
|
||||
ctx.OrgID,
|
||||
|
@ -240,15 +244,15 @@ func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (doc
|
|||
}
|
||||
|
||||
// find matching attachments
|
||||
likeQuery = "a.filename LIKE '%" + keywords + "%'"
|
||||
likeQuery = "LOWER(a.filename) LIKE '%" + keywords + "%'"
|
||||
temp = []link.Candidate{}
|
||||
|
||||
err = s.Runtime.Db.Select(&temp,
|
||||
`SELECT a.refid as targetid, a.documentid as documentid, a.filename as title, a.extension as context, d.labelid as folderid from attachment a
|
||||
LEFT JOIN document d ON d.refid=a.documentid WHERE a.orgid=? AND `+likeQuery+` AND d.labelid IN
|
||||
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
|
||||
UNION ALL SELECT refid FROM label a where orgid=? AND type=1 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
|
||||
UNION ALL SELECT refid FROM label a where orgid=? AND type=3 AND refid IN (SELECT labelid from labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
|
||||
`SELECT a.refid as targetid, a.documentid as documentid, a.filename as title, a.extension as context, d.labelid as folderid
|
||||
FROM attachment a LEFT JOIN document d ON d.refid=a.documentid WHERE a.orgid=? AND `+likeQuery+` AND d.labelid IN
|
||||
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
|
||||
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=1 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid='' AND (canedit=1 OR canview=1))
|
||||
UNION ALL SELECT refid FROM label a WHERE orgid=? AND type=3 AND refid IN (SELECT labelid FROM labelrole WHERE orgid=? AND userid=? AND (canedit=1 OR canview=1)))
|
||||
ORDER BY a.filename`,
|
||||
ctx.OrgID,
|
||||
ctx.OrgID,
|
||||
|
|
|
@ -32,8 +32,11 @@ import (
|
|||
"github.com/documize/community/domain/mail"
|
||||
"github.com/documize/community/model/account"
|
||||
"github.com/documize/community/model/audit"
|
||||
"github.com/documize/community/model/doc"
|
||||
"github.com/documize/community/model/page"
|
||||
"github.com/documize/community/model/space"
|
||||
"github.com/documize/community/model/user"
|
||||
uuid "github.com/nu7hatch/gouuid"
|
||||
)
|
||||
|
||||
// Handler contains the runtime information such as logging and database.
|
||||
|
@ -65,15 +68,17 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
var sp = space.Space{}
|
||||
err = json.Unmarshal(body, &sp)
|
||||
var model = space.NewSpaceRequest{}
|
||||
err = json.Unmarshal(body, &model)
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
if len(sp.Name) == 0 {
|
||||
model.Name = strings.TrimSpace(model.Name)
|
||||
model.CloneID = strings.TrimSpace(model.CloneID)
|
||||
if len(model.Name) == 0 {
|
||||
response.WriteMissingDataError(w, method, "name")
|
||||
return
|
||||
}
|
||||
|
@ -85,6 +90,8 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
var sp space.Space
|
||||
sp.Name = model.Name
|
||||
sp.RefID = uniqueid.Generate()
|
||||
sp.OrgID = ctx.OrgID
|
||||
sp.Type = space.ScopePrivate
|
||||
|
@ -118,8 +125,135 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
h.Store.Audit.Record(ctx, audit.EventTypeSpaceAdd)
|
||||
|
||||
// Get back new space
|
||||
sp, _ = h.Store.Space.Get(ctx, sp.RefID)
|
||||
|
||||
fmt.Println(model)
|
||||
|
||||
// clone existing space?
|
||||
if model.CloneID != "" && (model.CopyDocument || model.CopyPermission || model.CopyTemplate) {
|
||||
ctx.Transaction, err = h.Runtime.Db.Beginx()
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
spCloneRoles, err := h.Store.Space.GetRoles(ctx, model.CloneID)
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
if model.CopyPermission {
|
||||
for _, r := range spCloneRoles {
|
||||
r.RefID = uniqueid.Generate()
|
||||
r.LabelID = sp.RefID
|
||||
|
||||
err = h.Store.Space.AddRole(ctx, r)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
toCopy := []doc.Document{}
|
||||
spCloneTemplates, err := h.Store.Document.TemplatesBySpace(ctx, model.CloneID)
|
||||
if err != nil {
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
toCopy = append(toCopy, spCloneTemplates...)
|
||||
|
||||
if model.CopyDocument {
|
||||
docs, err := h.Store.Document.GetBySpace(ctx, model.CloneID)
|
||||
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
toCopy = append(toCopy, docs...)
|
||||
}
|
||||
|
||||
if len(toCopy) > 0 {
|
||||
for _, t := range toCopy {
|
||||
origID := t.RefID
|
||||
|
||||
documentID := uniqueid.Generate()
|
||||
t.RefID = documentID
|
||||
t.LabelID = sp.RefID
|
||||
|
||||
err = h.Store.Document.Add(ctx, t)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
pages, _ := h.Store.Page.GetPages(ctx, origID)
|
||||
for _, p := range pages {
|
||||
meta, err2 := h.Store.Page.GetPageMeta(ctx, p.RefID)
|
||||
if err2 != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
|
||||
p.DocumentID = documentID
|
||||
pageID := uniqueid.Generate()
|
||||
p.RefID = pageID
|
||||
|
||||
meta.PageID = pageID
|
||||
meta.DocumentID = documentID
|
||||
|
||||
model := page.NewPage{}
|
||||
model.Page = p
|
||||
model.Meta = meta
|
||||
|
||||
err = h.Store.Page.Add(ctx, model)
|
||||
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
newUUID, _ := uuid.NewV4()
|
||||
attachments, _ := h.Store.Attachment.GetAttachmentsWithData(ctx, origID)
|
||||
for _, a := range attachments {
|
||||
attachmentID := uniqueid.Generate()
|
||||
a.RefID = attachmentID
|
||||
a.DocumentID = documentID
|
||||
a.Job = newUUID.String()
|
||||
random := secrets.GenerateSalt()
|
||||
a.FileID = random[0:9]
|
||||
|
||||
err = h.Store.Attachment.Add(ctx, a)
|
||||
if err != nil {
|
||||
ctx.Transaction.Rollback()
|
||||
response.WriteServerError(w, method, err)
|
||||
h.Runtime.Log.Error(method, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx.Transaction.Commit()
|
||||
}
|
||||
|
||||
response.WriteJSON(w, sp)
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue