From a8da028bc77ffe63c8996750c3f82c4ee51d02d5 Mon Sep 17 00:00:00 2001 From: Miguel de la Cruz Date: Wed, 22 Sep 2021 21:57:00 +0200 Subject: [PATCH] Adds workspaceID to blocks and makes board page only reacts to the workspace updates (#1299) --- server/app/blocks.go | 2 +- server/model/block.go | 4 + server/services/store/sqlstore/blocks.go | 127 ++++-------------- server/ws/plugin_adapter.go | 1 + server/ws/server.go | 1 + webapp/src/blocks/block.ts | 3 + .../content/checkboxElement.test.tsx | 1 + webapp/src/pages/boardPage.tsx | 13 +- 8 files changed, 47 insertions(+), 105 deletions(-) diff --git a/server/app/blocks.go b/server/app/blocks.go index ff35e4f8a..6cd920030 100644 --- a/server/app/blocks.go +++ b/server/app/blocks.go @@ -76,7 +76,7 @@ func (a *App) InsertBlocks(c store.Container, blocks []model.Block, userID strin if err != nil { return err } - + blocks[i].WorkspaceID = c.WorkspaceID needsNotify = append(needsNotify, blocks[i]) a.wsAdapter.BroadcastBlockChange(c.WorkspaceID, blocks[i]) diff --git a/server/model/block.go b/server/model/block.go index 21fafd358..6288459aa 100644 --- a/server/model/block.go +++ b/server/model/block.go @@ -55,6 +55,10 @@ type Block struct { // The deleted time. Set to indicate this block is deleted // required: false DeleteAt int64 `json:"deleteAt"` + + // The workspace id that the block belongs to + // required: true + WorkspaceID string `json:"workspaceId"` } // BlockPatch is a patch for modify blocks diff --git a/server/services/store/sqlstore/blocks.go b/server/services/store/sqlstore/blocks.go index 8269a4e8c..58402bec4 100644 --- a/server/services/store/sqlstore/blocks.go +++ b/server/services/store/sqlstore/blocks.go @@ -32,22 +32,27 @@ func (be BlockNotFoundErr) Error() string { return fmt.Sprintf("block not found (block id: %s", be.blockID) } +func (s *SQLStore) blockFields() []string { + return []string{ + "id", + "parent_id", + "root_id", + "created_by", + "modified_by", + s.escapeField("schema"), + "type", + "title", + "COALESCE(fields, '{}')", + "create_at", + "update_at", + "delete_at", + "COALESCE(workspace_id, '0')", + } +} + func (s *SQLStore) GetBlocksWithParentAndType(c store.Container, parentID string, blockType string) ([]model.Block, error) { query := s.getQueryBuilder(). - Select( - "id", - "parent_id", - "root_id", - "created_by", - "modified_by", - s.escapeField("schema"), - "type", - "title", - "COALESCE(fields, '{}')", - "create_at", - "update_at", - "delete_at", - ). + Select(s.blockFields()...). From(s.tablePrefix + "blocks"). Where(sq.Eq{"COALESCE(workspace_id, '0')": c.WorkspaceID}). Where(sq.Eq{"parent_id": parentID}). @@ -66,20 +71,7 @@ func (s *SQLStore) GetBlocksWithParentAndType(c store.Container, parentID string func (s *SQLStore) GetBlocksWithParent(c store.Container, parentID string) ([]model.Block, error) { query := s.getQueryBuilder(). - Select( - "id", - "parent_id", - "root_id", - "created_by", - "modified_by", - s.escapeField("schema"), - "type", - "title", - "COALESCE(fields, '{}')", - "create_at", - "update_at", - "delete_at", - ). + Select(s.blockFields()...). From(s.tablePrefix + "blocks"). Where(sq.Eq{"parent_id": parentID}). Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID}) @@ -97,20 +89,7 @@ func (s *SQLStore) GetBlocksWithParent(c store.Container, parentID string) ([]mo func (s *SQLStore) GetBlocksWithRootID(c store.Container, rootID string) ([]model.Block, error) { query := s.getQueryBuilder(). - Select( - "id", - "parent_id", - "root_id", - "created_by", - "modified_by", - s.escapeField("schema"), - "type", - "title", - "COALESCE(fields, '{}')", - "create_at", - "update_at", - "delete_at", - ). + Select(s.blockFields()...). From(s.tablePrefix + "blocks"). Where(sq.Eq{"root_id": rootID}). Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID}) @@ -128,20 +107,7 @@ func (s *SQLStore) GetBlocksWithRootID(c store.Container, rootID string) ([]mode func (s *SQLStore) GetBlocksWithType(c store.Container, blockType string) ([]model.Block, error) { query := s.getQueryBuilder(). - Select( - "id", - "parent_id", - "root_id", - "created_by", - "modified_by", - s.escapeField("schema"), - "type", - "title", - "COALESCE(fields, '{}')", - "create_at", - "update_at", - "delete_at", - ). + Select(s.blockFields()...). From(s.tablePrefix + "blocks"). Where(sq.Eq{"type": blockType}). Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID}) @@ -160,20 +126,7 @@ func (s *SQLStore) GetBlocksWithType(c store.Container, blockType string) ([]mod // GetSubTree2 returns blocks within 2 levels of the given blockID. func (s *SQLStore) GetSubTree2(c store.Container, blockID string) ([]model.Block, error) { query := s.getQueryBuilder(). - Select( - "id", - "parent_id", - "root_id", - "created_by", - "modified_by", - s.escapeField("schema"), - "type", - "title", - "COALESCE(fields, '{}')", - "create_at", - "update_at", - "delete_at", - ). + Select(s.blockFields()...). From(s.tablePrefix + "blocks"). Where(sq.Or{sq.Eq{"id": blockID}, sq.Eq{"parent_id": blockID}}). Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID}) @@ -205,6 +158,7 @@ func (s *SQLStore) GetSubTree3(c store.Container, blockID string) ([]model.Block "l3.create_at", "l3.update_at", "l3.delete_at", + "COALESCE(l3.workspace_id, '0')", ). From(s.tablePrefix + "blocks as l1"). Join(s.tablePrefix + "blocks as l2 on l2.parent_id = l1.id or l2.id = l1.id"). @@ -231,20 +185,7 @@ func (s *SQLStore) GetSubTree3(c store.Container, blockID string) ([]model.Block func (s *SQLStore) GetAllBlocks(c store.Container) ([]model.Block, error) { query := s.getQueryBuilder(). - Select( - "id", - "parent_id", - "root_id", - "created_by", - "modified_by", - s.escapeField("schema"), - "type", - "title", - "COALESCE(fields, '{}')", - "create_at", - "update_at", - "delete_at", - ). + Select(s.blockFields()...). From(s.tablePrefix + "blocks"). Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID}) @@ -279,7 +220,8 @@ func (s *SQLStore) blocksFromRows(rows *sql.Rows) ([]model.Block, error) { &fieldsJSON, &block.CreateAt, &block.UpdateAt, - &block.DeleteAt) + &block.DeleteAt, + &block.WorkspaceID) if err != nil { // handle this error s.logger.Error(`ERROR blocksFromRows`, mlog.Err(err)) @@ -568,20 +510,7 @@ func (s *SQLStore) GetBlockCountsByType() (map[string]int64, error) { func (s *SQLStore) GetBlock(c store.Container, blockID string) (*model.Block, error) { query := s.getQueryBuilder(). - Select( - "id", - "parent_id", - "root_id", - "created_by", - "modified_by", - s.escapeField("schema"), - "type", - "title", - "COALESCE(fields, '{}')", - "create_at", - "update_at", - "delete_at", - ). + Select(s.blockFields()...). From(s.tablePrefix + "blocks"). Where(sq.Eq{"id": blockID}). Where(sq.Eq{"coalesce(workspace_id, '0')": c.WorkspaceID}) diff --git a/server/ws/plugin_adapter.go b/server/ws/plugin_adapter.go index 80805126d..e8e4f862c 100644 --- a/server/ws/plugin_adapter.go +++ b/server/ws/plugin_adapter.go @@ -358,6 +358,7 @@ func (pa *PluginAdapter) BroadcastBlockDelete(workspaceID, blockID, parentID str block.ParentID = parentID block.UpdateAt = now block.DeleteAt = now + block.WorkspaceID = workspaceID pa.BroadcastBlockChange(workspaceID, block) } diff --git a/server/ws/server.go b/server/ws/server.go index 72fec6fa6..276a26ecf 100644 --- a/server/ws/server.go +++ b/server/ws/server.go @@ -470,6 +470,7 @@ func (ws *Server) BroadcastBlockDelete(workspaceID, blockID, parentID string) { block.ParentID = parentID block.UpdateAt = now block.DeleteAt = now + block.WorkspaceID = workspaceID ws.BroadcastBlockChange(workspaceID, block) } diff --git a/webapp/src/blocks/block.ts b/webapp/src/blocks/block.ts index 36ec906bc..3d2929d2a 100644 --- a/webapp/src/blocks/block.ts +++ b/webapp/src/blocks/block.ts @@ -9,6 +9,7 @@ type ContentBlockTypes = typeof contentBlockTypes[number] type BlockTypes = typeof blockTypes[number] interface BlockPatch { + workspaceId?: string parentId?: string rootId?: string schema?: number @@ -21,6 +22,7 @@ interface BlockPatch { interface Block { id: string + workspaceId: string parentId: string rootId: string createdBy: string @@ -41,6 +43,7 @@ function createBlock(block?: Block): Block { return { id: block?.id || Utils.createGuid(), schema: 1, + workspaceId: block?.workspaceId || '', parentId: block?.parentId || '', rootId: block?.rootId || '', createdBy: block?.createdBy || '', diff --git a/webapp/src/components/content/checkboxElement.test.tsx b/webapp/src/components/content/checkboxElement.test.tsx index 3cf5eff2d..d6b912ecf 100644 --- a/webapp/src/components/content/checkboxElement.test.tsx +++ b/webapp/src/components/content/checkboxElement.test.tsx @@ -17,6 +17,7 @@ const wrapIntl = (children: any) => {children} { const defaultBlock: ContentBlock = { id: 'test-id', + workspaceId: '', parentId: '', rootId: '', modifiedBy: 'test-user-id', diff --git a/webapp/src/pages/boardPage.tsx b/webapp/src/pages/boardPage.tsx index bd15f2ba2..5935963a4 100644 --- a/webapp/src/pages/boardPage.tsx +++ b/webapp/src/pages/boardPage.tsx @@ -152,12 +152,15 @@ const BoardPage = (props: Props) => { } const incrementalUpdate = (_: WSClient, blocks: Block[]) => { + // only takes into account the blocks that belong to the workspace + const workspaceBlocks = blocks.filter((b: Block) => b.workspaceId === '0' || b.workspaceId === workspaceId) + batch(() => { - dispatch(updateBoards(blocks.filter((b: Block) => b.type === 'board' || b.deleteAt !== 0) as Board[])) - dispatch(updateViews(blocks.filter((b: Block) => b.type === 'view' || b.deleteAt !== 0) as BoardView[])) - dispatch(updateCards(blocks.filter((b: Block) => b.type === 'card' || b.deleteAt !== 0) as Card[])) - dispatch(updateComments(blocks.filter((b: Block) => b.type === 'comment' || b.deleteAt !== 0) as CommentBlock[])) - dispatch(updateContents(blocks.filter((b: Block) => b.type !== 'card' && b.type !== 'view' && b.type !== 'board' && b.type !== 'comment') as ContentBlock[])) + dispatch(updateBoards(workspaceBlocks.filter((b: Block) => b.type === 'board' || b.deleteAt !== 0) as Board[])) + dispatch(updateViews(workspaceBlocks.filter((b: Block) => b.type === 'view' || b.deleteAt !== 0) as BoardView[])) + dispatch(updateCards(workspaceBlocks.filter((b: Block) => b.type === 'card' || b.deleteAt !== 0) as Card[])) + dispatch(updateComments(workspaceBlocks.filter((b: Block) => b.type === 'comment' || b.deleteAt !== 0) as CommentBlock[])) + dispatch(updateContents(workspaceBlocks.filter((b: Block) => b.type !== 'card' && b.type !== 'view' && b.type !== 'board' && b.type !== 'comment') as ContentBlock[])) }) }