feat(documents): rebuild project document workbench
This commit is contained in:
@@ -92,31 +92,40 @@ func (s *Service) Search(userID uint, query string) ([]SearchResultDTO, error) {
|
||||
})
|
||||
}
|
||||
|
||||
var notes []models.SaNote
|
||||
noteFilter := fmt.Sprintf(`(sa_projects.owner_id = ? OR EXISTS (
|
||||
var documents []models.SaDocument
|
||||
documentFilter := fmt.Sprintf(`(sa_projects.owner_id = ? OR EXISTS (
|
||||
SELECT 1 FROM sa_task_shares
|
||||
JOIN sa_tasks ON sa_tasks.id = sa_task_shares.task_id
|
||||
WHERE sa_task_shares.object_type = 'note'
|
||||
AND sa_task_shares.object_id = sa_notes.id
|
||||
AND sa_tasks.project_id = sa_notes.project_id
|
||||
WHERE sa_task_shares.object_type = 'document'
|
||||
AND sa_task_shares.object_id = sa_documents.id
|
||||
AND sa_tasks.project_id = sa_documents.project_id
|
||||
AND sa_tasks.assignee_id = ?
|
||||
)) AND (sa_notes.title %s ? ESCAPE '!' OR sa_notes.markdown %s ? ESCAPE '!')`, operator, operator)
|
||||
if err := s.database().Select("sa_notes.*").Joins("JOIN sa_projects ON sa_projects.id = sa_notes.project_id").
|
||||
Where(noteFilter, userID, userID, like, like).
|
||||
Order("sa_notes.updated_at DESC").Order("sa_notes.identity ASC").
|
||||
)) AND (sa_documents.name %s ? ESCAPE '!' OR EXISTS (
|
||||
SELECT 1 FROM sa_document_contents
|
||||
WHERE sa_document_contents.document_id = sa_documents.id
|
||||
AND sa_document_contents.markdown %s ? ESCAPE '!'
|
||||
))`, operator, operator)
|
||||
if err := s.database().Select("sa_documents.*").Joins("JOIN sa_projects ON sa_projects.id = sa_documents.project_id").
|
||||
Where(documentFilter, userID, userID, like, like).
|
||||
Order("sa_documents.updated_at DESC").Order("sa_documents.identity ASC").
|
||||
Limit(maxSearchResults).
|
||||
Find(¬es).Error; err != nil {
|
||||
Find(&documents).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
noteResults := make([]rankedSearchResult, 0, len(notes))
|
||||
for _, note := range notes {
|
||||
noteResults = append(noteResults, rankedSearchResult{
|
||||
result: SearchResultDTO{Type: "note", ID: note.Identity, ProjectID: note.ProjectIdentity, Title: note.Title, Snippet: note.Markdown},
|
||||
updatedAt: note.UpdatedAt,
|
||||
documentResults := make([]rankedSearchResult, 0, len(documents))
|
||||
for _, document := range documents {
|
||||
var content models.SaDocumentContent
|
||||
_ = s.database().Where("document_id = ?", document.ID).First(&content).Error
|
||||
documentResults = append(documentResults, rankedSearchResult{
|
||||
result: SearchResultDTO{
|
||||
Type: "document", ID: document.Identity, ProjectID: document.ProjectIdentity,
|
||||
Title: document.Name, Snippet: content.Markdown,
|
||||
},
|
||||
updatedAt: document.UpdatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
return stableFairLimit(projectResults, taskResults, noteResults), nil
|
||||
return stableFairLimit(projectResults, taskResults, documentResults), nil
|
||||
}
|
||||
|
||||
func stableFairLimit(buckets ...[]rankedSearchResult) []SearchResultDTO {
|
||||
|
||||
Reference in New Issue
Block a user