Skip to content

Commit 07c8238

Browse files
committed
Merge branch 'space-templates'
2 parents 81cd22e + 5a2c447 commit 07c8238

File tree

19 files changed

+322
-113
lines changed

19 files changed

+322
-113
lines changed

domain/conversion/conversion.go

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
171171
return
172172
}
173173

174-
//err = processPage(documentID, fileResult.PageFiles, fileResult.Pages.Children[0], 1, p)
175-
176174
for k, v := range fileResult.Pages {
177175
var p page.Page
178176
p.OrgID = ctx.OrgID
@@ -196,7 +194,6 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
196194
model.Meta = meta
197195

198196
err = store.Page.Add(ctx, model)
199-
200197
if err != nil {
201198
ctx.Transaction.Rollback()
202199
err = errors.Wrap(err, "cannot insert new page for new document")
@@ -216,15 +213,24 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
216213
a.RefID = refID
217214

218215
err = store.Attachment.Add(ctx, a)
219-
220216
if err != nil {
221217
ctx.Transaction.Rollback()
222218
err = errors.Wrap(err, "cannot insert attachment for new document")
223219
return
224220
}
225221
}
226222

227-
ctx.Transaction.Commit()
223+
store.Activity.RecordUserActivity(ctx, activity.UserActivity{
224+
LabelID: newDocument.LabelID,
225+
SourceID: newDocument.RefID,
226+
SourceType: activity.SourceTypeDocument,
227+
ActivityType: activity.TypeCreated})
228+
229+
err = ctx.Transaction.Commit()
230+
if err != nil {
231+
err = errors.Wrap(err, "cannot commit new document import")
232+
return
233+
}
228234

229235
newDocument, err = store.Document.Get(ctx, documentID)
230236
if err != nil {
@@ -233,19 +239,6 @@ func processDocument(ctx domain.RequestContext, r *env.Runtime, store *domain.St
233239
return
234240
}
235241

236-
// err = store.Document.Update(ctx, newDocument) // TODO review - this seems to write-back an unaltered record from that read above, but within that it calls searches.UpdateDocument() to reindex the doc.
237-
// if err != nil {
238-
// ctx.Transaction.Rollback()
239-
// err = errors.Wrap(err, "cannot updater new document")
240-
// return
241-
// }
242-
243-
store.Activity.RecordUserActivity(ctx, activity.UserActivity{
244-
LabelID: newDocument.LabelID,
245-
SourceID: newDocument.RefID,
246-
SourceType: activity.SourceTypeDocument,
247-
ActivityType: activity.TypeCreated})
248-
249242
store.Audit.Record(ctx, audit.EventTypeDocumentUpload)
250243

251244
return

domain/document/mysql/store.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
package mysql
1313

1414
import (
15+
"database/sql"
1516
"fmt"
1617
"time"
1718

@@ -199,6 +200,11 @@ func (s Scope) TemplatesBySpace(ctx domain.RequestContext, spaceID string) (docu
199200
ctx.OrgID,
200201
ctx.UserID)
201202

203+
if err == sql.ErrNoRows {
204+
err = nil
205+
documents = []doc.Document{}
206+
}
207+
202208
if err != nil {
203209
err = errors.Wrap(err, "select space document templates")
204210
return
@@ -241,6 +247,11 @@ func (s Scope) DocumentList(ctx domain.RequestContext) (documents []doc.Document
241247
ctx.OrgID,
242248
ctx.UserID)
243249

250+
if err == sql.ErrNoRows {
251+
err = nil
252+
documents = []doc.Document{}
253+
}
254+
244255
if err != nil {
245256
err = errors.Wrap(err, "select documents list")
246257
return

domain/link/mysql/store.go

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ package mysql
1313

1414
import (
1515
"fmt"
16+
"strings"
1617
"time"
1718

1819
"github.com/documize/community/core/env"
@@ -162,15 +163,18 @@ func (s Scope) DeleteLink(ctx domain.RequestContext, id string) (rows int64, err
162163
// SearchCandidates returns matching documents, sections and attachments using keywords.
163164
func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (docs []link.Candidate,
164165
pages []link.Candidate, attachments []link.Candidate, err error) {
166+
165167
// find matching documents
166168
temp := []link.Candidate{}
167-
likeQuery := "title LIKE '%" + keywords + "%'"
169+
keywords = strings.TrimSpace(strings.ToLower(keywords))
170+
likeQuery := "LOWER(title) LIKE '%" + keywords + "%'"
168171

169172
err = s.Runtime.Db.Select(&temp,
170-
`SELECT refid as documentid, labelid as folderid,title from document WHERE orgid=? AND `+likeQuery+` AND labelid IN
171-
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
172-
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))
173-
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)))
173+
`SELECT d.refid as documentid, d. labelid as folderid, d.title, l.label as context
174+
FROM document d LEFT JOIN label l ON d.labelid=l.refid WHERE l.orgid=? AND `+likeQuery+` AND d.labelid IN
175+
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
176+
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))
177+
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)))
174178
ORDER BY title`,
175179
ctx.OrgID,
176180
ctx.OrgID,
@@ -194,22 +198,22 @@ func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (doc
194198
TargetID: r.DocumentID,
195199
LinkType: "document",
196200
Title: r.Title,
197-
Context: "",
201+
Context: r.Context,
198202
}
199203

200204
docs = append(docs, c)
201205
}
202206

203207
// find matching sections
204-
likeQuery = "p.title LIKE '%" + keywords + "%'"
208+
likeQuery = "LOWER(p.title) LIKE '%" + keywords + "%'"
205209
temp = []link.Candidate{}
206210

207211
err = s.Runtime.Db.Select(&temp,
208-
`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
209-
LEFT JOIN document d ON d.refid=p.documentid WHERE p.orgid=? AND `+likeQuery+` AND d.labelid IN
210-
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
211-
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))
212-
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)))
212+
`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
213+
FROM page p LEFT JOIN document d ON d.refid=p.documentid WHERE p.orgid=? AND `+likeQuery+` AND d.labelid IN
214+
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
215+
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))
216+
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)))
213217
ORDER BY p.title`,
214218
ctx.OrgID,
215219
ctx.OrgID,
@@ -240,15 +244,15 @@ func (s Scope) SearchCandidates(ctx domain.RequestContext, keywords string) (doc
240244
}
241245

242246
// find matching attachments
243-
likeQuery = "a.filename LIKE '%" + keywords + "%'"
247+
likeQuery = "LOWER(a.filename) LIKE '%" + keywords + "%'"
244248
temp = []link.Candidate{}
245249

246250
err = s.Runtime.Db.Select(&temp,
247-
`SELECT a.refid as targetid, a.documentid as documentid, a.filename as title, a.extension as context, d.labelid as folderid from attachment a
248-
LEFT JOIN document d ON d.refid=a.documentid WHERE a.orgid=? AND `+likeQuery+` AND d.labelid IN
249-
(SELECT refid from label WHERE orgid=? AND type=2 AND userid=?
250-
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))
251-
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)))
251+
`SELECT a.refid as targetid, a.documentid as documentid, a.filename as title, a.extension as context, d.labelid as folderid
252+
FROM attachment a LEFT JOIN document d ON d.refid=a.documentid WHERE a.orgid=? AND `+likeQuery+` AND d.labelid IN
253+
(SELECT refid FROM label WHERE orgid=? AND type=2 AND userid=?
254+
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))
255+
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)))
252256
ORDER BY a.filename`,
253257
ctx.OrgID,
254258
ctx.OrgID,

domain/space/endpoint.go

Lines changed: 153 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ import (
3232
"github.com/documize/community/domain/mail"
3333
"github.com/documize/community/model/account"
3434
"github.com/documize/community/model/audit"
35+
"github.com/documize/community/model/doc"
36+
"github.com/documize/community/model/page"
3537
"github.com/documize/community/model/space"
3638
"github.com/documize/community/model/user"
39+
uuid "github.com/nu7hatch/gouuid"
3740
)
3841

3942
// Handler contains the runtime information such as logging and database.
@@ -65,15 +68,17 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
6568
return
6669
}
6770

68-
var sp = space.Space{}
69-
err = json.Unmarshal(body, &sp)
71+
var model = space.NewSpaceRequest{}
72+
err = json.Unmarshal(body, &model)
7073
if err != nil {
7174
response.WriteServerError(w, method, err)
7275
h.Runtime.Log.Error(method, err)
7376
return
7477
}
7578

76-
if len(sp.Name) == 0 {
79+
model.Name = strings.TrimSpace(model.Name)
80+
model.CloneID = strings.TrimSpace(model.CloneID)
81+
if len(model.Name) == 0 {
7782
response.WriteMissingDataError(w, method, "name")
7883
return
7984
}
@@ -85,6 +90,8 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
8590
return
8691
}
8792

93+
var sp space.Space
94+
sp.Name = model.Name
8895
sp.RefID = uniqueid.Generate()
8996
sp.OrgID = ctx.OrgID
9097
sp.Type = space.ScopePrivate
@@ -118,8 +125,151 @@ func (h *Handler) Add(w http.ResponseWriter, r *http.Request) {
118125

119126
h.Store.Audit.Record(ctx, audit.EventTypeSpaceAdd)
120127

128+
// Get back new space
121129
sp, _ = h.Store.Space.Get(ctx, sp.RefID)
122130

131+
// clone existing space?
132+
if model.CloneID != "" && (model.CopyDocument || model.CopyPermission || model.CopyTemplate) {
133+
ctx.Transaction, err = h.Runtime.Db.Beginx()
134+
if err != nil {
135+
response.WriteServerError(w, method, err)
136+
h.Runtime.Log.Error(method, err)
137+
return
138+
}
139+
140+
spCloneRoles, err := h.Store.Space.GetRoles(ctx, model.CloneID)
141+
if err != nil {
142+
response.WriteServerError(w, method, err)
143+
h.Runtime.Log.Error(method, err)
144+
return
145+
}
146+
147+
if model.CopyPermission {
148+
for _, r := range spCloneRoles {
149+
r.RefID = uniqueid.Generate()
150+
r.LabelID = sp.RefID
151+
152+
err = h.Store.Space.AddRole(ctx, r)
153+
if err != nil {
154+
ctx.Transaction.Rollback()
155+
response.WriteServerError(w, method, err)
156+
h.Runtime.Log.Error(method, err)
157+
return
158+
}
159+
}
160+
}
161+
162+
toCopy := []doc.Document{}
163+
spCloneTemplates, err := h.Store.Document.TemplatesBySpace(ctx, model.CloneID)
164+
if err != nil {
165+
response.WriteServerError(w, method, err)
166+
h.Runtime.Log.Error(method, err)
167+
return
168+
}
169+
toCopy = append(toCopy, spCloneTemplates...)
170+
171+
if model.CopyDocument {
172+
docs, err := h.Store.Document.GetBySpace(ctx, model.CloneID)
173+
174+
if err != nil {
175+
ctx.Transaction.Rollback()
176+
response.WriteServerError(w, method, err)
177+
h.Runtime.Log.Error(method, err)
178+
return
179+
}
180+
181+
toCopy = append(toCopy, docs...)
182+
}
183+
184+
if len(toCopy) > 0 {
185+
for _, t := range toCopy {
186+
origID := t.RefID
187+
188+
documentID := uniqueid.Generate()
189+
t.RefID = documentID
190+
t.LabelID = sp.RefID
191+
192+
err = h.Store.Document.Add(ctx, t)
193+
if err != nil {
194+
ctx.Transaction.Rollback()
195+
response.WriteServerError(w, method, err)
196+
h.Runtime.Log.Error(method, err)
197+
return
198+
}
199+
200+
pages, _ := h.Store.Page.GetPages(ctx, origID)
201+
for _, p := range pages {
202+
meta, err2 := h.Store.Page.GetPageMeta(ctx, p.RefID)
203+
if err2 != nil {
204+
ctx.Transaction.Rollback()
205+
response.WriteServerError(w, method, err)
206+
h.Runtime.Log.Error(method, err)
207+
return
208+
}
209+
210+
p.DocumentID = documentID
211+
pageID := uniqueid.Generate()
212+
p.RefID = pageID
213+
214+
meta.PageID = pageID
215+
meta.DocumentID = documentID
216+
217+
model := page.NewPage{}
218+
model.Page = p
219+
model.Meta = meta
220+
221+
err = h.Store.Page.Add(ctx, model)
222+
223+
if err != nil {
224+
ctx.Transaction.Rollback()
225+
response.WriteServerError(w, method, err)
226+
h.Runtime.Log.Error(method, err)
227+
return
228+
}
229+
}
230+
231+
newUUID, _ := uuid.NewV4()
232+
attachments, _ := h.Store.Attachment.GetAttachmentsWithData(ctx, origID)
233+
for _, a := range attachments {
234+
attachmentID := uniqueid.Generate()
235+
a.RefID = attachmentID
236+
a.DocumentID = documentID
237+
a.Job = newUUID.String()
238+
random := secrets.GenerateSalt()
239+
a.FileID = random[0:9]
240+
241+
err = h.Store.Attachment.Add(ctx, a)
242+
if err != nil {
243+
ctx.Transaction.Rollback()
244+
response.WriteServerError(w, method, err)
245+
h.Runtime.Log.Error(method, err)
246+
return
247+
}
248+
}
249+
}
250+
}
251+
252+
if model.CopyTemplate {
253+
blocks, err := h.Store.Block.GetBySpace(ctx, model.CloneID)
254+
255+
for _, b := range blocks {
256+
b.RefID = uniqueid.Generate()
257+
b.LabelID = sp.RefID
258+
b.UserID = ctx.UserID
259+
260+
err = h.Store.Block.Add(ctx, b)
261+
if err != nil {
262+
ctx.Transaction.Rollback()
263+
response.WriteServerError(w, method, err)
264+
h.Runtime.Log.Error(method, err)
265+
return
266+
}
267+
}
268+
}
269+
270+
ctx.Transaction.Commit()
271+
}
272+
123273
response.WriteJSON(w, sp)
124274
}
125275

0 commit comments

Comments
 (0)