From c3a4921ac3a0661dc5f266392e568ab9e5e7ef8f Mon Sep 17 00:00:00 2001 From: Chen-I Lim <46905241+chenilim@users.noreply.github.com> Date: Tue, 29 Mar 2022 14:47:49 -0700 Subject: [PATCH] GetBoardMetadata API (#2569) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * wip * Added data migration for populating categories * wip * Added data migration for populating categories * Store WIP * migration WIP * category CRUD APIs complete * category block API WIP * block category update API done * Fetcehed data into store * Started displayting sidebar data * sidebar WIP * Dashboard - basic changes * Sidebar dashboard btn and board switcher UI only * Sidebar dashboard btn and board switcher UI only * create category dialog WIP * Create category webapp side done * Integrated move card to other category * board to block * Disabled dashboard route for now as we'll implement it in phase 2 * WIP * Added logic to open last board/view on per team level * Add workspace to teams and boards migrations (#1986) * Add workspace to teams and boards migrations * Update json annotations on board models * boards search dialog WIP * Seach dialog WIP * Implemented opening boiard from search results * Boards switcher styliung * Handled update category WS event * Template support * personal server support and styling fixes * test fix WIP * Fixed a bug causing boards to not be moved correctly beteen categories * Fixed webapp tests * fix * Store changes (#2011) * Permissions phase 1 - Websocket updates (#2014) * Store changes * Websockets changes * Permissions phase 1 - Permissions service (#2015) * Store changes * Websockets changes * Permissions service * Api and app updates (#2016) * Store changes * Websockets changes * Permissions service * New API and App changes * Delete and Patch boards and blocks endpoints * Used correct variable * Webapp changes WIP * Open correct team URL * Fixed get block API * Used React context for workspace users * WIP * On load navigation sorted out * WIP * Nav fix * categories WS broadcast * Used real search API * Fixed unfurl ppreview * set active team in sidebar * IMplemented navigation on changing team in sidebar * Misc fixes * close rows inside transaction (#2045) * update syntax for mysql (#2044) * Upadted mutator for new patchBlock API * Updated patchBlock API to use new URL * Listeining to correct event in plugin mode * Implemented WS messages for category operations: * Fix duplicated build tags on Makefile * Sidebar enhancements * Add missing prefix to SQLite migration and fix flaky tests * Sidebar boards menu enhancement * Fix board page interactions (#2144) * Fix patch board card properties error * Fix board interactions * Fix insert blocks interactions * Fix app tests (#2104) * Add json1 tag to vscode launch (#2157) * Fix add, delete and update boards and add board patch generation (#2146) * Fix update boards and add board patch generation * Make add board and add template work, as well as deleting a board * Update the state on board deletion * Delete unused variable * Fix bad parenthesis * Fix board creation inside plugin, options were coming null due websocket message serialization * update property type mutators to use boards API (#2168) * Add permissions modal (#2196) * Initial integration * Permissions modal, websocket updates and API tests implemented * Avoid updating/removing user if there is only one admin left * Fix duplicates on board search * Adds integration test * Addressing PR review comments Co-authored-by: Jesús Espino * Merge * I'm able to compile now * Some fixes around tests execution * Fixing migrations * Fixing migrations order * WIP * Fixing some other compilation problems on tests * Some typescript tests fixed * Fixing javascript tests * Fixing compilation * Fixing some problems to create boards * Load the templates on initial load * Improvements over initial team templates import * Adding new fields in the database * Working on adding duplicate board api * Removing RootID concept entirely * Improving a bit the subscriptions * Fixing store tests for notificationHints * Fixing more tests * fixing tests * Fixing tests * Fixing tests * Fixing some small bugs related to templates * Fixing registration link generation/regeneration * Fixing cypress tests * Adding store tests for duplicateBoard and duplicateBlock * Addressing some TODO comments * Making the export api simpler * Add redirect component for old workspace urls * Removing Dashboard code * Delete only the built-in templates on update * fixing tests * Adding users autocompletion * Updating snapshots * Fixing bad merge * fix panic when creating new card in notifysubscriptions (#2352) * fix lint errors (#2353) * fix lint errors * fix panic when creating new card in notifysubscriptions (#2352) * fix lint errors * fix unit test * Revert "fix unit test" This reverts commit 0ad78aed65745521c0bb45790c9ea91b6c316c44. Co-authored-by: Doug Lauder * fix sql syntax error for SearchUsersByTeam (#2357) * Fix mentions delivery (#2358) * fix sql syntax error for SearchUsersByTeam * fix mentions delivery Co-authored-by: Mattermod * update api for octoClient calls, pass correct variables to mutator (#2359) * Fixing tests after merge * Fix sidebar context menu UI issue (#2399) * Fix notification diff for text blocks (#2386) * fix notification diff for text blocks; fix various linter errors. * fix URLs to cards Co-authored-by: Mattermod * Permissions branch: Fix card links (#2391) * fix notification diff for text blocks; fix various linter errors. * fix URLs to cards Co-authored-by: Mattermod * Fixing sqlite tests * Fixing server tests * Update migrations to create global templates. (#2397) * fix duplicate templates * revert migrate.go * update UI for empty templates * implement updating built-in templates as global (teamId = 0) * handle error if board not found * update unit test * fix more tests * Update blocks_test.go Fix merge issue * fix migration sql error (#2414) * Fixing frontend tests * Set target team ID when using a global template (#2419) * Fix some server tests * Fixing onboarding creation * Permissions branch: Fix unit tests and CI errors (part 1) (#2425) * Fixing some small memory leaks (#2400) * Fixing some small memory leaks * fixing tests * passing the tags to all test targets * Increasing the timeout of the tests * Fix some type checkings * Permissions branch: Fixes all the linter errors (#2429) * fix linter errors * Reestructuring the router and splitting in more subcomponents (#2403) * Reestructuring the router and splitting in more subcomponents * Removing console.log calls * Removing unneeded selector * Addressing PR comment * Fix redirection to one team when you load directly the boards home path * Using properly the lastTeamID to redirect the user if needed * don't allow last admin change/deleted (#2416) * don't allow last admin change/deleted * update for i18-extract * fixed en.json Co-authored-by: Mattermod Co-authored-by: Harshil Sharma * Splitting BoardPage component into simpler/smaller components (#2435) * Splitting BoardPage component into simpler/smaller components * Removing unneeded import * Replace go migrate with morph permissions (#2424) * merge origin/replace-go-migrate-with-morph * run go mod tidy on mattermost-plugin and increase test timeout * fix merge issue temprorarily * remove some debug changes * fixing the linter * Allow always team 0 (global) templates fetch (#2472) * Fix problem with viewId 0 in the URL (#2473) * Migrate from binddata to goembed (#2471) * Adding join logic to the board switcher (#2434) * Adding join logic to the board switcher * Using already existing client function and removing the joinBoard one * Adding support for autojoin based on url * Fixing frontend tests * fix webapp compile error, missing enableSharedBoards (#2501) * Fixing duplication on postgres * Adding back views to the sidebar (#2494) * Fix #2507. Update Swagger comments (#2508) * Fix the flash of the template selector on board/team switch (#2490) * Fix the flash of the template selector on board/team switch * More fixes specially around error handling * Fixing the bot badge (#2487) * simplifying a bit the team store sync between channels and focalboard (#2481) * Fix menu tests (#2528) * fix failing menu tests * fix lint error * Added keyboard shortcut for boards switcher (#2407) * Added keyboard shortcut for boards switcher * Fixed a type error * Added some inline comments * Fixed lint * Fixed bug with scroll jumping when the card is opened: (#2477) - avoid remounting of `ScrollingComponent` for each render of `Kanban` component - property `autoFocus` set to false for `CalculationOptions` because it triggers `blur` even for the button in Jest tests and closes the menu - snapshots for tests with `CalculationOptions` updated * Adding the frontend support for permissions and applying it to a big part of the interface. (#2536) * Initial work on permissions gates * Applying permissions gates in more places * Adding more checks to the interface * Adding more permissions gates and keeping the store up to date * fixing some tests * Fixing some more tests * Fixing another test * Fixing all tests and adding some more * Adding no-permission snapshot tests * Addressing PR review comments * Fixing invert behavior * Permissions branch: No sqlstore calls after app shutdown (#2530) * fix webapp compile error, missing enableSharedBoards * refactor app init wip * - ensure all block change notifications are finished before shutting down app - fix unit tests for mysql (insert_at only has 1 second resolution!) * adjust logging Co-authored-by: Mattermod * Fixed migrations to allow upgrading from previous version (#2535) * Added mechanism to check if schema migration is needed * WIP * WIP * WIP * WIP * Fixed migration * Fixed for SQLite * minor cleaniup * Deleted old schema migration table after running migrations * Removed a debug log * Fixed a bug where the code always tried to delete a table which may or may not exist * Show properly the user avatar in the ShareBoard component (#2542) * Fixing the last CI problems from the permissions-branch (#2541) * Fix history ordering * Giving some times to avoid possible race conditions * Empty * Reverting accidental change in the config.json * Optimizing table view (#2540) * Optimizing table view * Reducing the amount of rendering for tables * Some other performance improvements * Improve the activeView updates * Some extra simplifications * Another small improvement * Fixing tests * Fixing linter errors * Reducing a bit the amount of dependency with big objects in the store * Small simplification * Removing Commenter role from the user role selector (#2561) * Shareboard cleanup (#2550) * Initial work on permissions gates * Applying permissions gates in more places * Adding more checks to the interface * Adding more permissions gates and keeping the store up to date * fixing some tests * Fixing some more tests * Fixing another test * Fixing all tests and adding some more * Adding no-permission snapshot tests * Addressing PR review comments * cleanup some shareboard settings * remove unused property, fix for user items being displayed for non admin * revert change, allow users to show Co-authored-by: Jesús Espino Co-authored-by: Mattermod * GetBoardMetadata API * Integration tests. WIP * getBoardHistory * Working integration test * Fix ordering, add store tests * Fix: Update board_history update_at on patch * sqltests * Fix unmarshall delete boards_history * testGetBlockMetadata with delete and undelete * Handle board not found * Fixing comments and cards with the new optimizations in the store (#2560) * Fixing property creation (#2563) * Cleanup * Fix user selection in table view (#2565) * Fixing focus new row in table view (#2567) * Permissions branch: Fix sqlite table lock (CI) (#2568) * fix sqlite table lock * remove test db on teardown * revert .gitignore * fix goimport on migration code * fix typo * more linter fixes * clean up tmp db for sqlstore tests Co-authored-by: Mattermod * Fixing snapshots * Remove debug log * Return metadata for deleted boards * Migrating center panel to functional component (#2562) * Migrating center panel to functional component * Fixing some tests * Fixing another test * Fixing linter errors * Fixing types errors * Fixing linter error * Fixing cypress tests * Fixing the last cypress test * Simpliying a bit the code * Making property insertion more robust * Updating checkbox test * License check * Cleanup and update Swagger docs * Merge from main * Fix bad merge * Fix Linux-app build break * do mod tidy * Fix server lint * Require credentials (not only read token) * Add missing defer CloseRows * do mod tidy Co-authored-by: Harshil Sharma Co-authored-by: Miguel de la Cruz Co-authored-by: Scott Bishel Co-authored-by: Jesús Espino Co-authored-by: Doug Lauder Co-authored-by: Mattermod Co-authored-by: Harshil Sharma <18575143+harshilsharma63@users.noreply.github.com> Co-authored-by: Ibrahim Serdar Acikgoz Co-authored-by: kamre --- server/api/api.go | 76 +++++++ server/app/boards.go | 101 ++++++++++ server/client/client.go | 19 ++ server/integrationtests/board_test.go | 180 +++++++++++++++++ server/integrationtests/clienttestlib.go | 39 +++- server/integrationtests/teststore.go | 110 ++++++++++ server/model/board.go | 30 +++ .../generators/transactional_store.go.tmpl | 1 + .../mattermostauthlayer.go | 5 + server/services/store/mockstore/mockstore.go | 45 +++++ server/services/store/sqlstore/blocks.go | 39 +++- server/services/store/sqlstore/board.go | 125 +++++++++--- .../services/store/sqlstore/public_methods.go | 16 ++ server/services/store/sqlstore/sqlstore.go | 5 + server/services/store/store.go | 5 + server/services/store/storetests/blocks.go | 190 ++++++++++++++++++ server/services/store/storetests/boards.go | 109 ++++++++++ server/swagger/swagger.yml | 60 ++++++ 18 files changed, 1129 insertions(+), 26 deletions(-) create mode 100644 server/integrationtests/teststore.go diff --git a/server/api/api.go b/server/api/api.go index aacf53274..44bb148c3 100644 --- a/server/api/api.go +++ b/server/api/api.go @@ -89,6 +89,7 @@ func (a *API) RegisterRoutes(r *mux.Router) { apiv1.HandleFunc("/boards/{boardID}/blocks/{blockID}/undelete", a.sessionRequired(a.handleUndeleteBlock)).Methods("POST") apiv1.HandleFunc("/boards/{boardID}/blocks/{blockID}/subtree", a.attachSession(a.handleGetSubTree, false)).Methods("GET") apiv1.HandleFunc("/boards/{boardID}/blocks/{blockID}/duplicate", a.attachSession(a.handleDuplicateBlock, false)).Methods("POST") + apiv1.HandleFunc("/boards/{boardID}/metadata", a.sessionRequired(a.handleGetBoardMetadata)).Methods("GET") // Import&Export APIs apiv1.HandleFunc("/boards/{boardID}/blocks/export", a.sessionRequired(a.handleExport)).Methods("GET") @@ -2969,6 +2970,81 @@ func (a *API) handleDuplicateBlock(w http.ResponseWriter, r *http.Request) { auditRec.Success() } +func (a *API) handleGetBoardMetadata(w http.ResponseWriter, r *http.Request) { + // swagger:operation GET /api/v1/boards/{boardID}/metadata getBoardMetadata + // + // Returns a board's metadata + // + // --- + // produces: + // - application/json + // parameters: + // - name: boardID + // in: path + // description: Board ID + // required: true + // type: string + // security: + // - BearerAuth: [] + // responses: + // '200': + // description: success + // schema: + // "$ref": "#/definitions/BoardMetadata" + // '404': + // description: board not found + // '501': + // description: required license not found + // default: + // description: internal error + // schema: + // "$ref": "#/definitions/ErrorResponse" + + boardID := mux.Vars(r)["boardID"] + userID := getUserID(r) + + board, boardMetadata, err := a.app.GetBoardMetadata(boardID) + if errors.Is(err, app.ErrInsufficientLicense) { + a.errorResponse(w, r.URL.Path, http.StatusNotImplemented, "", err) + return + } + if err != nil { + a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err) + return + } + if board == nil || boardMetadata == nil { + a.errorResponse(w, r.URL.Path, http.StatusNotFound, "", nil) + return + } + + if board.Type == model.BoardTypePrivate { + if !a.permissions.HasPermissionToBoard(userID, boardID, model.PermissionViewBoard) { + a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"}) + return + } + } else { + if !a.permissions.HasPermissionToTeam(userID, board.TeamID, model.PermissionViewTeam) { + a.errorResponse(w, r.URL.Path, http.StatusForbidden, "", PermissionError{"access denied to board"}) + return + } + } + + auditRec := a.makeAuditRecord(r, "getBoardMetadata", audit.Fail) + defer a.audit.LogRecord(audit.LevelRead, auditRec) + auditRec.AddMeta("boardID", boardID) + + data, err := json.Marshal(boardMetadata) + if err != nil { + a.errorResponse(w, r.URL.Path, http.StatusInternalServerError, "", err) + return + } + + // response + jsonBytesResponse(w, http.StatusOK, data) + + auditRec.Success() +} + func (a *API) handleSearchBoards(w http.ResponseWriter, r *http.Request) { // swagger:operation GET /api/v1/teams/{teamID}/boards/search searchBoards // diff --git a/server/app/boards.go b/server/app/boards.go index 6fc0b76bb..8500d4d51 100644 --- a/server/app/boards.go +++ b/server/app/boards.go @@ -3,6 +3,7 @@ package app import ( "database/sql" "errors" + "fmt" "github.com/mattermost/focalboard/server/model" "github.com/mattermost/focalboard/server/utils" @@ -11,6 +12,7 @@ import ( var ( ErrBoardMemberIsLastAdmin = errors.New("cannot leave a board with no admins") ErrNewBoardCannotHaveID = errors.New("new board cannot have an ID") + ErrInsufficientLicense = errors.New("appropriate license required") ) func (a *App) GetBoard(boardID string) (*model.Board, error) { @@ -24,6 +26,105 @@ func (a *App) GetBoard(boardID string) (*model.Board, error) { return board, nil } +func (a *App) GetBoardMetadata(boardID string) (*model.Board, *model.BoardMetadata, error) { + license := a.store.GetLicense() + if license == nil || !(*license.Features.Compliance) { + return nil, nil, ErrInsufficientLicense + } + + board, err := a.GetBoard(boardID) + if err != nil { + return nil, nil, err + } + if board == nil { + // Board may have been deleted, retrieve most recent history instead + board, err = a.getBoardHistory(boardID, true) + if err != nil { + return nil, nil, err + } + } + + if board == nil { + // Board not found + return nil, nil, nil + } + + earliestTime, _, err := a.getBoardDescendantModifiedInfo(boardID, false) + if err != nil { + return nil, nil, err + } + + latestTime, lastModifiedBy, err := a.getBoardDescendantModifiedInfo(boardID, true) + if err != nil { + return nil, nil, err + } + + boardMetadata := model.BoardMetadata{ + BoardID: boardID, + DescendantFirstUpdateAt: earliestTime, + DescendantLastUpdateAt: latestTime, + CreatedBy: board.CreatedBy, + LastModifiedBy: lastModifiedBy, + } + return board, &boardMetadata, nil +} + +func (a *App) getBoardHistory(boardID string, latest bool) (*model.Board, error) { + opts := model.QueryBlockHistoryOptions{ + Limit: 1, + Descending: latest, + } + boards, err := a.store.GetBoardHistory(boardID, opts) + if err != nil { + return nil, fmt.Errorf("could not get history for board: %w", err) + } + if len(boards) == 0 { + return nil, nil + } + + return boards[0], nil +} + +func (a *App) getBoardDescendantModifiedInfo(boardID string, latest bool) (int64, string, error) { + board, err := a.getBoardHistory(boardID, latest) + if err != nil { + return 0, "", err + } + if board == nil { + return 0, "", fmt.Errorf("history not found for board: %w", err) + } + + var timestamp int64 + modifiedBy := board.ModifiedBy + if latest { + timestamp = board.UpdateAt + } else { + timestamp = board.CreateAt + } + + // use block_history to fetch blocks in case they were deleted and no longer exist in blocks table. + opts := model.QueryBlockHistoryOptions{ + Limit: 1, + Descending: latest, + } + blocks, err := a.store.GetBlockHistoryDescendants(boardID, opts) + if err != nil { + return 0, "", fmt.Errorf("could not get blocks history descendants for board: %w", err) + } + if len(blocks) > 0 { + // Compare the board history info with the descendant block info, if it exists + block := &blocks[0] + if latest && block.UpdateAt > timestamp { + timestamp = block.UpdateAt + modifiedBy = block.ModifiedBy + } else if !latest && block.CreateAt < timestamp { + timestamp = block.CreateAt + modifiedBy = block.ModifiedBy + } + } + return timestamp, modifiedBy, nil +} + func (a *App) DuplicateBoard(boardID, userID, toTeam string, asTemplate bool) (*model.BoardsAndBlocks, []*model.BoardMember, error) { bab, members, err := a.store.DuplicateBoard(boardID, userID, toTeam, asTemplate) if err != nil { diff --git a/server/client/client.go b/server/client/client.go index 88a55b1e6..a497c5b19 100644 --- a/server/client/client.go +++ b/server/client/client.go @@ -176,6 +176,10 @@ func (c *Client) GetBoardRoute(boardID string) string { return fmt.Sprintf("%s/%s", c.GetBoardsRoute(), boardID) } +func (c *Client) GetBoardMetadataRoute(boardID string) string { + return fmt.Sprintf("%s/%s/metadata", c.GetBoardsRoute(), boardID) +} + func (c *Client) GetJoinBoardRoute(boardID string) string { return fmt.Sprintf("%s/%s/join", c.GetBoardsRoute(), boardID) } @@ -476,6 +480,21 @@ func (c *Client) GetBoard(boardID, readToken string) (*model.Board, *Response) { return model.BoardFromJSON(r.Body), BuildResponse(r) } +func (c *Client) GetBoardMetadata(boardID, readToken string) (*model.BoardMetadata, *Response) { + url := c.GetBoardMetadataRoute(boardID) + if readToken != "" { + url += fmt.Sprintf("?read_token=%s", readToken) + } + + r, err := c.DoAPIGet(url, "") + if err != nil { + return nil, BuildErrorResponse(r, err) + } + defer closeBody(r) + + return model.BoardMetadataFromJSON(r.Body), BuildResponse(r) +} + func (c *Client) GetBoardsForTeam(teamID string) ([]*model.Board, *Response) { r, err := c.DoAPIGet(c.GetTeamRoute(teamID)+"/boards", "") if err != nil { diff --git a/server/integrationtests/board_test.go b/server/integrationtests/board_test.go index f8b726c8b..e68d14d4b 100644 --- a/server/integrationtests/board_test.go +++ b/server/integrationtests/board_test.go @@ -3,6 +3,7 @@ package integrationtests import ( "encoding/json" "testing" + "time" "github.com/mattermost/focalboard/server/client" "github.com/mattermost/focalboard/server/model" @@ -469,6 +470,185 @@ func TestGetBoard(t *testing.T) { }) } +func TestGetBoardMetadata(t *testing.T) { + t.Run("a non authenticated user should be rejected", func(t *testing.T) { + th := SetupTestHelperWithLicense(t, LicenseEnterprise).InitBasic() + defer th.TearDown() + th.Logout(th.Client) + + boardMetadata, resp := th.Client.GetBoardMetadata("boar-id", "") + th.CheckUnauthorized(resp) + require.Nil(t, boardMetadata) + }) + + t.Run("getBoardMetadata query is correct", func(t *testing.T) { + th := SetupTestHelperWithLicense(t, LicenseEnterprise).InitBasic() + defer th.TearDown() + th.Server.Config().EnablePublicSharedBoards = true + + teamID := testTeamID + + board := &model.Board{ + Title: "public board where user1 is admin", + Type: model.BoardTypeOpen, + TeamID: teamID, + } + rBoard, err := th.Server.App().CreateBoard(board, th.GetUser1().ID, true) + require.NoError(t, err) + + // Check metadata + boardMetadata, resp := th.Client.GetBoardMetadata(rBoard.ID, "") + th.CheckOK(resp) + require.NotNil(t, boardMetadata) + + require.Equal(t, rBoard.CreatedBy, boardMetadata.CreatedBy) + require.Equal(t, rBoard.CreateAt, boardMetadata.DescendantFirstUpdateAt) + require.Equal(t, rBoard.UpdateAt, boardMetadata.DescendantLastUpdateAt) + require.Equal(t, rBoard.ModifiedBy, boardMetadata.LastModifiedBy) + + // Insert card1 + card1 := model.Block{ + ID: "card1", + BoardID: rBoard.ID, + Title: "Card 1", + } + time.Sleep(20 * time.Millisecond) + require.NoError(t, th.Server.App().InsertBlock(card1, th.GetUser2().ID)) + rCard1, err := th.Server.App().GetBlockByID(card1.ID) + require.NoError(t, err) + + // Check updated metadata + boardMetadata, resp = th.Client.GetBoardMetadata(rBoard.ID, "") + th.CheckOK(resp) + require.NotNil(t, boardMetadata) + + require.Equal(t, rBoard.CreatedBy, boardMetadata.CreatedBy) + require.Equal(t, rBoard.CreateAt, boardMetadata.DescendantFirstUpdateAt) + require.Equal(t, rCard1.UpdateAt, boardMetadata.DescendantLastUpdateAt) + require.Equal(t, rCard1.ModifiedBy, boardMetadata.LastModifiedBy) + + // Insert card2 + card2 := model.Block{ + ID: "card2", + BoardID: rBoard.ID, + Title: "Card 2", + } + time.Sleep(20 * time.Millisecond) + require.NoError(t, th.Server.App().InsertBlock(card2, th.GetUser1().ID)) + rCard2, err := th.Server.App().GetBlockByID(card2.ID) + require.NoError(t, err) + + // Check updated metadata + boardMetadata, resp = th.Client.GetBoardMetadata(rBoard.ID, "") + th.CheckOK(resp) + require.NotNil(t, boardMetadata) + require.Equal(t, rBoard.CreatedBy, boardMetadata.CreatedBy) + require.Equal(t, rBoard.CreateAt, boardMetadata.DescendantFirstUpdateAt) + require.Equal(t, rCard2.UpdateAt, boardMetadata.DescendantLastUpdateAt) + require.Equal(t, rCard2.ModifiedBy, boardMetadata.LastModifiedBy) + + t.Run("After delete board", func(t *testing.T) { + // Delete board + time.Sleep(20 * time.Millisecond) + require.NoError(t, th.Server.App().DeleteBoard(rBoard.ID, th.GetUser1().ID)) + + // Check updated metadata + boardMetadata, resp = th.Client.GetBoardMetadata(rBoard.ID, "") + th.CheckOK(resp) + require.NotNil(t, boardMetadata) + require.Equal(t, rBoard.CreatedBy, boardMetadata.CreatedBy) + require.Equal(t, rBoard.CreateAt, boardMetadata.DescendantFirstUpdateAt) + require.Greater(t, boardMetadata.DescendantLastUpdateAt, rCard2.UpdateAt) + require.Equal(t, th.GetUser1().ID, boardMetadata.LastModifiedBy) + }) + }) + + t.Run("getBoardMetadata should fail with no license", func(t *testing.T) { + th := SetupTestHelperWithLicense(t, LicenseNone).InitBasic() + defer th.TearDown() + th.Server.Config().EnablePublicSharedBoards = true + + teamID := testTeamID + + board := &model.Board{ + Title: "public board where user1 is admin", + Type: model.BoardTypeOpen, + TeamID: teamID, + } + rBoard, err := th.Server.App().CreateBoard(board, th.GetUser1().ID, true) + require.NoError(t, err) + + // Check metadata + boardMetadata, resp := th.Client.GetBoardMetadata(rBoard.ID, "") + th.CheckNotImplemented(resp) + require.Nil(t, boardMetadata) + }) + + t.Run("getBoardMetadata should fail on Professional license", func(t *testing.T) { + th := SetupTestHelperWithLicense(t, LicenseProfessional).InitBasic() + defer th.TearDown() + th.Server.Config().EnablePublicSharedBoards = true + + teamID := testTeamID + + board := &model.Board{ + Title: "public board where user1 is admin", + Type: model.BoardTypeOpen, + TeamID: teamID, + } + rBoard, err := th.Server.App().CreateBoard(board, th.GetUser1().ID, true) + require.NoError(t, err) + + // Check metadata + boardMetadata, resp := th.Client.GetBoardMetadata(rBoard.ID, "") + th.CheckNotImplemented(resp) + require.Nil(t, boardMetadata) + }) + + t.Run("valid read token should not get the board metadata", func(t *testing.T) { + th := SetupTestHelperWithLicense(t, LicenseEnterprise).InitBasic() + defer th.TearDown() + th.Server.Config().EnablePublicSharedBoards = true + + teamID := testTeamID + sharingToken := utils.NewID(utils.IDTypeToken) + userID := th.GetUser1().ID + + board := &model.Board{ + Title: "public board where user1 is admin", + Type: model.BoardTypeOpen, + TeamID: teamID, + } + rBoard, err := th.Server.App().CreateBoard(board, userID, true) + require.NoError(t, err) + + sharing := &model.Sharing{ + ID: rBoard.ID, + Enabled: true, + Token: sharingToken, + UpdateAt: 1, + } + + success, resp := th.Client.PostSharing(sharing) + th.CheckOK(resp) + require.True(t, success) + + // the client logs out + th.Logout(th.Client) + + // we make sure that the client cannot currently retrieve the + // board with no session + boardMetadata, resp := th.Client.GetBoardMetadata(rBoard.ID, "") + th.CheckUnauthorized(resp) + require.Nil(t, boardMetadata) + + // it should not be able to retrieve it with the read token either + boardMetadata, resp = th.Client.GetBoardMetadata(rBoard.ID, sharingToken) + th.CheckUnauthorized(resp) + require.Nil(t, boardMetadata) + }) +} + func TestPatchBoard(t *testing.T) { teamID := testTeamID diff --git a/server/integrationtests/clienttestlib.go b/server/integrationtests/clienttestlib.go index b46c3b045..3744f606a 100644 --- a/server/integrationtests/clienttestlib.go +++ b/server/integrationtests/clienttestlib.go @@ -13,6 +13,7 @@ import ( "github.com/mattermost/focalboard/server/server" "github.com/mattermost/focalboard/server/services/config" "github.com/mattermost/focalboard/server/services/permissions/localpermissions" + "github.com/mattermost/focalboard/server/services/store" "github.com/mattermost/focalboard/server/services/store/sqlstore" "github.com/mattermost/mattermost-server/v6/shared/mlog" @@ -26,6 +27,14 @@ const ( password = "Pa$$word" ) +type LicenseType int + +const ( + LicenseNone LicenseType = iota // 0 + LicenseProfessional // 1 + LicenseEnterprise // 2 +) + type TestHelper struct { T *testing.T Server *server.Server @@ -77,6 +86,10 @@ func getTestConfig() (*config.Configuration, error) { } func newTestServer(singleUserToken string) *server.Server { + return newTestServerWithLicense(singleUserToken, LicenseNone) +} + +func newTestServerWithLicense(singleUserToken string, licenseType LicenseType) *server.Server { cfg, err := getTestConfig() if err != nil { panic(err) @@ -86,11 +99,24 @@ func newTestServer(singleUserToken string) *server.Server { if err = logger.Configure("", cfg.LoggingCfgJSON, nil); err != nil { panic(err) } - db, err := server.NewStore(cfg, logger) + innerStore, err := server.NewStore(cfg, logger) if err != nil { panic(err) } + var db store.Store + + switch licenseType { + case LicenseProfessional: + db = NewTestProfessionalStore(innerStore) + case LicenseEnterprise: + db = NewTestEnterpriseStore(innerStore) + case LicenseNone: + fallthrough + default: + db = innerStore + } + permissionsService := localpermissions.New(db, logger) params := server.Params{ @@ -119,8 +145,12 @@ func SetupTestHelperWithToken(t *testing.T) *TestHelper { } func SetupTestHelper(t *testing.T) *TestHelper { + return SetupTestHelperWithLicense(t, LicenseNone) +} + +func SetupTestHelperWithLicense(t *testing.T, licenseType LicenseType) *TestHelper { th := &TestHelper{T: t} - th.Server = newTestServer("") + th.Server = newTestServerWithLicense("", licenseType) th.Client = client.NewClient(th.Server.Config().ServerRoot, "") th.Client2 = client.NewClient(th.Server.Config().ServerRoot, "") return th @@ -285,3 +315,8 @@ func (th *TestHelper) CheckForbidden(r *client.Response) { require.Equal(th.T, http.StatusForbidden, r.StatusCode) require.Error(th.T, r.Error) } + +func (th *TestHelper) CheckNotImplemented(r *client.Response) { + require.Equal(th.T, http.StatusNotImplemented, r.StatusCode) + require.Error(th.T, r.Error) +} diff --git a/server/integrationtests/teststore.go b/server/integrationtests/teststore.go new file mode 100644 index 000000000..0269b0c53 --- /dev/null +++ b/server/integrationtests/teststore.go @@ -0,0 +1,110 @@ +package integrationtests + +import ( + "github.com/mattermost/focalboard/server/services/store" + + mmModel "github.com/mattermost/mattermost-server/v6/model" +) + +type TestStore struct { + store.Store + license *mmModel.License +} + +func NewTestEnterpriseStore(store store.Store) *TestStore { + usersValue := 10000 + trueValue := true + falseValue := false + license := &mmModel.License{ + Features: &mmModel.Features{ + Users: &usersValue, + LDAP: &trueValue, + LDAPGroups: &trueValue, + MFA: &trueValue, + GoogleOAuth: &trueValue, + Office365OAuth: &trueValue, + OpenId: &trueValue, + Compliance: &trueValue, + Cluster: &trueValue, + Metrics: &trueValue, + MHPNS: &trueValue, + SAML: &trueValue, + Elasticsearch: &trueValue, + Announcement: &trueValue, + ThemeManagement: &trueValue, + EmailNotificationContents: &trueValue, + DataRetention: &trueValue, + MessageExport: &trueValue, + CustomPermissionsSchemes: &trueValue, + CustomTermsOfService: &trueValue, + GuestAccounts: &trueValue, + GuestAccountsPermissions: &trueValue, + IDLoadedPushNotifications: &trueValue, + LockTeammateNameDisplay: &trueValue, + EnterprisePlugins: &trueValue, + AdvancedLogging: &trueValue, + Cloud: &falseValue, + SharedChannels: &trueValue, + RemoteClusterService: &trueValue, + FutureFeatures: &trueValue, + }, + } + + testStore := &TestStore{ + Store: store, + license: license, + } + + return testStore +} + +func NewTestProfessionalStore(store store.Store) *TestStore { + usersValue := 10000 + trueValue := true + falseValue := false + license := &mmModel.License{ + Features: &mmModel.Features{ + Users: &usersValue, + LDAP: &falseValue, + LDAPGroups: &falseValue, + MFA: &trueValue, + GoogleOAuth: &trueValue, + Office365OAuth: &trueValue, + OpenId: &trueValue, + Compliance: &falseValue, + Cluster: &falseValue, + Metrics: &trueValue, + MHPNS: &trueValue, + SAML: &trueValue, + Elasticsearch: &trueValue, + Announcement: &trueValue, + ThemeManagement: &trueValue, + EmailNotificationContents: &trueValue, + DataRetention: &trueValue, + MessageExport: &trueValue, + CustomPermissionsSchemes: &trueValue, + CustomTermsOfService: &trueValue, + GuestAccounts: &trueValue, + GuestAccountsPermissions: &trueValue, + IDLoadedPushNotifications: &trueValue, + LockTeammateNameDisplay: &trueValue, + EnterprisePlugins: &falseValue, + AdvancedLogging: &trueValue, + Cloud: &falseValue, + SharedChannels: &trueValue, + RemoteClusterService: &falseValue, + FutureFeatures: &trueValue, + }, + } + + testStore := &TestStore{ + Store: store, + license: license, + } + + return testStore +} + +func (s *TestStore) GetLicense() *mmModel.License { + return s.license +} diff --git a/server/model/board.go b/server/model/board.go index 121076cc2..db1596c30 100644 --- a/server/model/board.go +++ b/server/model/board.go @@ -168,6 +168,30 @@ type BoardMember struct { SchemeViewer bool `json:"schemeViewer"` } +// BoardMetadata contains metadata for a Board +// swagger:model +type BoardMetadata struct { + // The ID for the board + // required: true + BoardID string `json:"boardId"` + + // The most recent time a descendant of this board was added, modified, or deleted + // required: true + DescendantLastUpdateAt int64 `json:"descendantLastUpdateAt"` + + // The earliest time a descendant of this board was added, modified, or deleted + // required: true + DescendantFirstUpdateAt int64 `json:"descendantFirstUpdateAt"` + + // The ID of the user that created the board + // required: true + CreatedBy string `json:"createdBy"` + + // The ID of the user that last modified the most recently modified descendant + // required: true + LastModifiedBy string `json:"lastModifiedBy"` +} + func BoardFromJSON(data io.Reader) *Board { var board *Board _ = json.NewDecoder(data).Decode(&board) @@ -192,6 +216,12 @@ func BoardMembersFromJSON(data io.Reader) []*BoardMember { return boardMembers } +func BoardMetadataFromJSON(data io.Reader) *BoardMetadata { + var boardMetadata *BoardMetadata + _ = json.NewDecoder(data).Decode(&boardMetadata) + return boardMetadata +} + // Patch returns an updated version of the board. func (p *BoardPatch) Patch(board *Board) *Board { if p.Type != nil { diff --git a/server/services/store/generators/transactional_store.go.tmpl b/server/services/store/generators/transactional_store.go.tmpl index b3d8b668e..50849cf49 100644 --- a/server/services/store/generators/transactional_store.go.tmpl +++ b/server/services/store/generators/transactional_store.go.tmpl @@ -19,6 +19,7 @@ import ( "github.com/mattermost/focalboard/server/model" "github.com/mattermost/mattermost-server/v6/shared/mlog" + mmModel "github.com/mattermost/mattermost-server/v6/model" ) {{range $index, $element := .Methods}} diff --git a/server/services/store/mattermostauthlayer/mattermostauthlayer.go b/server/services/store/mattermostauthlayer/mattermostauthlayer.go index 18f8e5be3..8174cd9be 100644 --- a/server/services/store/mattermostauthlayer/mattermostauthlayer.go +++ b/server/services/store/mattermostauthlayer/mattermostauthlayer.go @@ -12,6 +12,7 @@ import ( "github.com/mattermost/focalboard/server/services/store" "github.com/mattermost/focalboard/server/utils" + mmModel "github.com/mattermost/mattermost-server/v6/model" "github.com/mattermost/mattermost-server/v6/shared/mlog" ) @@ -385,3 +386,7 @@ func (s *MattermostAuthLayer) CreatePrivateWorkspace(userID string) (string, err return channel.Id, nil } + +func (s *MattermostAuthLayer) GetLicense() *mmModel.License { + return s.pluginAPI.GetLicense() +} diff --git a/server/services/store/mockstore/mockstore.go b/server/services/store/mockstore/mockstore.go index aca98c7c7..6bb48ae64 100644 --- a/server/services/store/mockstore/mockstore.go +++ b/server/services/store/mockstore/mockstore.go @@ -10,6 +10,7 @@ import ( gomock "github.com/golang/mock/gomock" model "github.com/mattermost/focalboard/server/model" + model0 "github.com/mattermost/mattermost-server/v6/model" ) // MockStore is a mock of Store interface. @@ -383,6 +384,21 @@ func (mr *MockStoreMockRecorder) GetBlockHistory(arg0, arg1 interface{}) *gomock return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBlockHistory", reflect.TypeOf((*MockStore)(nil).GetBlockHistory), arg0, arg1) } +// GetBlockHistoryDescendants mocks base method. +func (m *MockStore) GetBlockHistoryDescendants(arg0 string, arg1 model.QueryBlockHistoryOptions) ([]model.Block, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBlockHistoryDescendants", arg0, arg1) + ret0, _ := ret[0].([]model.Block) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetBlockHistoryDescendants indicates an expected call of GetBlockHistoryDescendants. +func (mr *MockStoreMockRecorder) GetBlockHistoryDescendants(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBlockHistoryDescendants", reflect.TypeOf((*MockStore)(nil).GetBlockHistoryDescendants), arg0, arg1) +} + // GetBlocksForBoard mocks base method. func (m *MockStore) GetBlocksForBoard(arg0 string) ([]model.Block, error) { m.ctrl.T.Helper() @@ -505,6 +521,21 @@ func (mr *MockStoreMockRecorder) GetBoardAndCardByID(arg0 interface{}) *gomock.C return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBoardAndCardByID", reflect.TypeOf((*MockStore)(nil).GetBoardAndCardByID), arg0) } +// GetBoardHistory mocks base method. +func (m *MockStore) GetBoardHistory(arg0 string, arg1 model.QueryBlockHistoryOptions) ([]*model.Board, error) { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetBoardHistory", arg0, arg1) + ret0, _ := ret[0].([]*model.Board) + ret1, _ := ret[1].(error) + return ret0, ret1 +} + +// GetBoardHistory indicates an expected call of GetBoardHistory. +func (mr *MockStoreMockRecorder) GetBoardHistory(arg0, arg1 interface{}) *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetBoardHistory", reflect.TypeOf((*MockStore)(nil).GetBoardHistory), arg0, arg1) +} + // GetBoardMemberHistory mocks base method. func (m *MockStore) GetBoardMemberHistory(arg0, arg1 string, arg2 uint64) ([]*model.BoardMemberHistoryEntry, error) { m.ctrl.T.Helper() @@ -550,6 +581,20 @@ func (mr *MockStoreMockRecorder) GetCategory(arg0 interface{}) *gomock.Call { return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCategory", reflect.TypeOf((*MockStore)(nil).GetCategory), arg0) } +// GetLicense mocks base method. +func (m *MockStore) GetLicense() *model0.License { + m.ctrl.T.Helper() + ret := m.ctrl.Call(m, "GetLicense") + ret0, _ := ret[0].(*model0.License) + return ret0 +} + +// GetLicense indicates an expected call of GetLicense. +func (mr *MockStoreMockRecorder) GetLicense() *gomock.Call { + mr.mock.ctrl.T.Helper() + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLicense", reflect.TypeOf((*MockStore)(nil).GetLicense)) +} + // GetMemberForBoard mocks base method. func (m *MockStore) GetMemberForBoard(arg0, arg1 string) (*model.BoardMember, error) { m.ctrl.T.Helper() diff --git a/server/services/store/sqlstore/blocks.go b/server/services/store/sqlstore/blocks.go index 237e7e54d..23ccaa072 100644 --- a/server/services/store/sqlstore/blocks.go +++ b/server/services/store/sqlstore/blocks.go @@ -18,6 +18,7 @@ import ( const ( maxSearchDepth = 50 + descClause = " DESC " ) type BoardIDNilError struct{} @@ -588,6 +589,7 @@ func (s *SQLStore) getBlock(db sq.BaseRunner, blockID string) (*model.Block, err s.logger.Error(`GetBlock ERROR`, mlog.Err(err)) return nil, err } + defer s.CloseRows(rows) blocks, err := s.blocksFromRows(rows) if err != nil { @@ -604,7 +606,7 @@ func (s *SQLStore) getBlock(db sq.BaseRunner, blockID string) (*model.Block, err func (s *SQLStore) getBlockHistory(db sq.BaseRunner, blockID string, opts model.QueryBlockHistoryOptions) ([]model.Block, error) { var order string if opts.Descending { - order = " DESC " + order = descClause } query := s.getQueryBuilder(db). @@ -630,6 +632,41 @@ func (s *SQLStore) getBlockHistory(db sq.BaseRunner, blockID string, opts model. s.logger.Error(`GetBlockHistory ERROR`, mlog.Err(err)) return nil, err } + defer s.CloseRows(rows) + + return s.blocksFromRows(rows) +} + +func (s *SQLStore) getBlockHistoryDescendants(db sq.BaseRunner, boardID string, opts model.QueryBlockHistoryOptions) ([]model.Block, error) { + var order string + if opts.Descending { + order = descClause + } + + query := s.getQueryBuilder(db). + Select(s.blockFields()...). + From(s.tablePrefix + "blocks_history"). + Where(sq.Eq{"board_id": boardID}). + OrderBy("insert_at " + order + ", update_at" + order) + + if opts.BeforeUpdateAt != 0 { + query = query.Where(sq.Lt{"update_at": opts.BeforeUpdateAt}) + } + + if opts.AfterUpdateAt != 0 { + query = query.Where(sq.Gt{"update_at": opts.AfterUpdateAt}) + } + + if opts.Limit != 0 { + query = query.Limit(opts.Limit) + } + + rows, err := query.Query() + if err != nil { + s.logger.Error(`GetBlockHistory ERROR`, mlog.Err(err)) + return nil, err + } + defer s.CloseRows(rows) return s.blocksFromRows(rows) } diff --git a/server/services/store/sqlstore/board.go b/server/services/store/sqlstore/board.go index 23db69963..20c2cc006 100644 --- a/server/services/store/sqlstore/board.go +++ b/server/services/store/sqlstore/board.go @@ -38,7 +38,7 @@ func boardFields(prefix string) []string { "is_template", "template_version", "COALESCE(properties, '{}')", - "COALESCE(card_properties, '{}')", + "COALESCE(card_properties, '[]')", "COALESCE(column_calculations, '{}')", "create_at", "update_at", @@ -60,6 +60,31 @@ func boardFields(prefix string) []string { return prefixedFields } +func boardHistoryFields() []string { + fields := []string{ + "id", + "team_id", + "COALESCE(channel_id, '')", + "COALESCE(created_by, '')", + "COALESCE(modified_by, '')", + "type", + "COALESCE(title, '')", + "COALESCE(description, '')", + "COALESCE(icon, '')", + "COALESCE(show_description, false)", + "COALESCE(is_template, false)", + "template_version", + "COALESCE(properties, '{}')", + "COALESCE(card_properties, '[]')", + "COALESCE(column_calculations, '{}')", + "COALESCE(create_at, 0)", + "COALESCE(update_at, 0)", + "COALESCE(delete_at, 0)", + } + + return fields +} + var boardMemberFields = []string{ "board_id", "user_id", @@ -274,6 +299,8 @@ func (s *SQLStore) insertBoard(db sq.BaseRunner, board *model.Board, userID stri insertQuery := s.getQueryBuilder(db).Insert(""). Columns(boardFields("")...) + now := utils.GetMillis() + insertQueryValues := map[string]interface{}{ "id": board.ID, "team_id": board.TeamID, @@ -291,12 +318,10 @@ func (s *SQLStore) insertBoard(db sq.BaseRunner, board *model.Board, userID stri "card_properties": cardPropertiesBytes, "column_calculations": columnCalculationsBytes, "create_at": board.CreateAt, - "update_at": board.UpdateAt, + "update_at": now, "delete_at": board.DeleteAt, } - now := utils.GetMillis() - if existingBoard != nil { query := s.getQueryBuilder(db).Update(s.tablePrefix+"boards"). Where(sq.Eq{"id": board.ID}). @@ -357,29 +382,49 @@ func (s *SQLStore) deleteBoard(db sq.BaseRunner, boardID, userID string) error { board, err := s.getBoard(db, boardID) if err != nil { - fmt.Printf("error on get board: %s\n", err) return err } - insertQuery := s.getQueryBuilder(db).Insert(s.tablePrefix+"boards_history"). - Columns( - "team_id", - "id", - "type", - "modified_by", - "update_at", - "delete_at", - ). - Values( - board.TeamID, - boardID, - board.Type, - userID, - now, - now, - ) + propertiesBytes, err := json.Marshal(board.Properties) + if err != nil { + return err + } + cardPropertiesBytes, err := json.Marshal(board.CardProperties) + if err != nil { + return err + } + columnCalculationsBytes, err := json.Marshal(board.ColumnCalculations) + if err != nil { + return err + } - if _, err := insertQuery.Exec(); err != nil { + insertQueryValues := map[string]interface{}{ + "id": board.ID, + "team_id": board.TeamID, + "channel_id": board.ChannelID, + "created_by": board.CreatedBy, + "modified_by": userID, + "type": board.Type, + "title": board.Title, + "description": board.Description, + "icon": board.Icon, + "show_description": board.ShowDescription, + "is_template": board.IsTemplate, + "template_version": board.TemplateVersion, + "properties": propertiesBytes, + "card_properties": cardPropertiesBytes, + "column_calculations": columnCalculationsBytes, + "create_at": board.CreateAt, + "update_at": now, + "delete_at": now, + } + + // writing board history + insertQuery := s.getQueryBuilder(db).Insert(""). + Columns(boardHistoryFields()...) + + query := insertQuery.SetMap(insertQueryValues).Into(s.tablePrefix + "boards_history") + if _, err := query.Exec(); err != nil { return err } @@ -605,6 +650,40 @@ func (s *SQLStore) searchBoardsForUserAndTeam(db sq.BaseRunner, term, userID, te return s.boardsFromRows(rows) } +func (s *SQLStore) getBoardHistory(db sq.BaseRunner, boardID string, opts model.QueryBlockHistoryOptions) ([]*model.Board, error) { + var order string + if opts.Descending { + order = " DESC " + } + + query := s.getQueryBuilder(db). + Select(boardHistoryFields()...). + From(s.tablePrefix + "boards_history"). + Where(sq.Eq{"id": boardID}). + OrderBy("insert_at " + order + ", update_at" + order) + + if opts.BeforeUpdateAt != 0 { + query = query.Where(sq.Lt{"update_at": opts.BeforeUpdateAt}) + } + + if opts.AfterUpdateAt != 0 { + query = query.Where(sq.Gt{"update_at": opts.AfterUpdateAt}) + } + + if opts.Limit != 0 { + query = query.Limit(opts.Limit) + } + + rows, err := query.Query() + if err != nil { + s.logger.Error(`getBoardHistory ERROR`, mlog.Err(err)) + return nil, err + } + defer s.CloseRows(rows) + + return s.boardsFromRows(rows) +} + func (s *SQLStore) getBoardMemberHistory(db sq.BaseRunner, boardID, userID string, limit uint64) ([]*model.BoardMemberHistoryEntry, error) { query := s.getQueryBuilder(db). Select("board_id", "user_id", "action", "insert_at"). diff --git a/server/services/store/sqlstore/public_methods.go b/server/services/store/sqlstore/public_methods.go index fa2fd53f5..0a334f7d1 100644 --- a/server/services/store/sqlstore/public_methods.go +++ b/server/services/store/sqlstore/public_methods.go @@ -18,6 +18,7 @@ import ( "github.com/mattermost/focalboard/server/model" + mmModel "github.com/mattermost/mattermost-server/v6/model" "github.com/mattermost/mattermost-server/v6/shared/mlog" ) @@ -269,6 +270,11 @@ func (s *SQLStore) GetBlockHistory(blockID string, opts model.QueryBlockHistoryO } +func (s *SQLStore) GetBlockHistoryDescendants(boardID string, opts model.QueryBlockHistoryOptions) ([]model.Block, error) { + return s.getBlockHistoryDescendants(s.db, boardID, opts) + +} + func (s *SQLStore) GetBlocksForBoard(boardID string) ([]model.Block, error) { return s.getBlocksForBoard(s.db, boardID) @@ -309,6 +315,11 @@ func (s *SQLStore) GetBoardAndCardByID(blockID string) (*model.Board, *model.Blo } +func (s *SQLStore) GetBoardHistory(boardID string, opts model.QueryBlockHistoryOptions) ([]*model.Board, error) { + return s.getBoardHistory(s.db, boardID, opts) + +} + func (s *SQLStore) GetBoardMemberHistory(boardID string, userID string, limit uint64) ([]*model.BoardMemberHistoryEntry, error) { return s.getBoardMemberHistory(s.db, boardID, userID, limit) @@ -324,6 +335,11 @@ func (s *SQLStore) GetCategory(id string) (*model.Category, error) { } +func (s *SQLStore) GetLicense() *mmModel.License { + return s.getLicense(s.db) + +} + func (s *SQLStore) GetMemberForBoard(boardID string, userID string) (*model.BoardMember, error) { return s.getMemberForBoard(s.db, boardID, userID) diff --git a/server/services/store/sqlstore/sqlstore.go b/server/services/store/sqlstore/sqlstore.go index 6de84bff5..d4af54c0b 100644 --- a/server/services/store/sqlstore/sqlstore.go +++ b/server/services/store/sqlstore/sqlstore.go @@ -10,6 +10,7 @@ import ( "github.com/mattermost/focalboard/server/model" "github.com/mattermost/mattermost-plugin-api/cluster" + mmModel "github.com/mattermost/mattermost-server/v6/model" "github.com/mattermost/mattermost-server/v6/shared/mlog" ) @@ -92,3 +93,7 @@ func (s *SQLStore) escapeField(fieldName string) string { //nolint:unparam } return fieldName } + +func (s *SQLStore) getLicense(db sq.BaseRunner) *mmModel.License { + return nil +} diff --git a/server/services/store/store.go b/server/services/store/store.go index ac448e68e..1dce5b066 100644 --- a/server/services/store/store.go +++ b/server/services/store/store.go @@ -8,6 +8,7 @@ import ( "time" "github.com/mattermost/focalboard/server/model" + mmModel "github.com/mattermost/mattermost-server/v6/model" ) // Store represents the abstraction of the data storage. @@ -32,6 +33,8 @@ type Store interface { // @withTransaction PatchBlock(blockID string, blockPatch *model.BlockPatch, userID string) error GetBlockHistory(blockID string, opts model.QueryBlockHistoryOptions) ([]model.Block, error) + GetBlockHistoryDescendants(boardID string, opts model.QueryBlockHistoryOptions) ([]model.Block, error) + GetBoardHistory(boardID string, opts model.QueryBlockHistoryOptions) ([]*model.Board, error) GetBoardAndCardByID(blockID string) (board *model.Board, card *model.Block, err error) GetBoardAndCard(block *model.Block) (board *model.Board, card *model.Block, err error) // @withTransaction @@ -131,6 +134,8 @@ type Store interface { DBType() string IsErrNotFound(err error) bool + + GetLicense() *mmModel.License } // ErrNotFound is an error type that can be returned by store APIs when a query unexpectedly fetches no records. diff --git a/server/services/store/storetests/blocks.go b/server/services/store/storetests/blocks.go index b889e433d..c87dd3ce1 100644 --- a/server/services/store/storetests/blocks.go +++ b/server/services/store/storetests/blocks.go @@ -75,6 +75,11 @@ func StoreTestBlocksStore(t *testing.T, setup func(t *testing.T) (store.Store, f defer tearDown() testDuplicateBlock(t, store) }) + t.Run("GetBlockMetadata", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testGetBlockMetadata(t, store) + }) } func testInsertBlock(t *testing.T, store store.Store) { @@ -873,3 +878,188 @@ func testDuplicateBlock(t *testing.T, store store.Store) { require.Nil(t, blocks) }) } + +func testGetBlockMetadata(t *testing.T, store store.Store) { + boardID := testBoardID + blocks, err := store.GetBlocksForBoard(boardID) + require.NoError(t, err) + + blocksToInsert := []model.Block{ + { + ID: "block1", + BoardID: boardID, + ParentID: "", + ModifiedBy: testUserID, + Type: "test", + }, + { + ID: "block2", + BoardID: boardID, + ParentID: "block1", + ModifiedBy: testUserID, + Type: "test", + }, + { + ID: "block3", + BoardID: boardID, + ParentID: "block1", + ModifiedBy: testUserID, + Type: "test", + }, + { + ID: "block4", + BoardID: boardID, + ParentID: "block1", + ModifiedBy: testUserID, + Type: "test2", + }, + { + ID: "block5", + BoardID: boardID, + ParentID: "block2", + ModifiedBy: testUserID, + Type: "test", + }, + } + + for _, v := range blocksToInsert { + time.Sleep(20 * time.Millisecond) + subBlocks := []model.Block{v} + InsertBlocks(t, store, subBlocks, testUserID) + } + defer DeleteBlocks(t, store, blocksToInsert, "test") + + t.Run("get full block history", func(t *testing.T) { + opts := model.QueryBlockHistoryOptions{ + Descending: false, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 5) + expectedBlock := blocksToInsert[0] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) + + t.Run("get full block history descending", func(t *testing.T) { + opts := model.QueryBlockHistoryOptions{ + Descending: true, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 5) + expectedBlock := blocksToInsert[len(blocksToInsert)-1] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) + + t.Run("get limited block history", func(t *testing.T) { + opts := model.QueryBlockHistoryOptions{ + Limit: 3, + Descending: false, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 3) + }) + + t.Run("get first block history", func(t *testing.T) { + opts := model.QueryBlockHistoryOptions{ + Limit: 1, + Descending: false, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 1) + expectedBlock := blocksToInsert[0] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) + + t.Run("get last block history", func(t *testing.T) { + opts := model.QueryBlockHistoryOptions{ + Limit: 1, + Descending: true, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 1) + expectedBlock := blocksToInsert[len(blocksToInsert)-1] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) + + t.Run("get block history after updateAt", func(t *testing.T) { + rBlocks, err2 := store.GetBlocksWithType(boardID, "test") + require.NoError(t, err2) + require.NotZero(t, rBlocks[2].UpdateAt) + + opts := model.QueryBlockHistoryOptions{ + AfterUpdateAt: rBlocks[2].UpdateAt, + Descending: false, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 2) + expectedBlock := blocksToInsert[3] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) + + t.Run("get block history before updateAt", func(t *testing.T) { + rBlocks, err2 := store.GetBlocksWithType(boardID, "test") + require.NoError(t, err2) + require.NotZero(t, rBlocks[2].UpdateAt) + + opts := model.QueryBlockHistoryOptions{ + BeforeUpdateAt: rBlocks[2].UpdateAt, + Descending: true, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 2) + expectedBlock := blocksToInsert[1] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) + + t.Run("get full block history after delete", func(t *testing.T) { + time.Sleep(20 * time.Millisecond) + err = store.DeleteBlock(blocksToInsert[0].ID, testUserID) + require.NoError(t, err) + + opts := model.QueryBlockHistoryOptions{ + Descending: true, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 6) + expectedBlock := blocksToInsert[0] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) + + t.Run("get full block history after undelete", func(t *testing.T) { + time.Sleep(20 * time.Millisecond) + err = store.UndeleteBlock(blocksToInsert[0].ID, testUserID) + require.NoError(t, err) + + opts := model.QueryBlockHistoryOptions{ + Descending: true, + } + blocks, err = store.GetBlockHistoryDescendants(boardID, opts) + require.NoError(t, err) + require.Len(t, blocks, 7) + expectedBlock := blocksToInsert[0] + block := blocks[0] + + require.Equal(t, expectedBlock.ID, block.ID) + }) +} diff --git a/server/services/store/storetests/boards.go b/server/services/store/storetests/boards.go index 426008c53..07cf21699 100644 --- a/server/services/store/storetests/boards.go +++ b/server/services/store/storetests/boards.go @@ -69,6 +69,11 @@ func StoreTestBoardStore(t *testing.T, setup func(t *testing.T) (store.Store, fu defer tearDown() testSearchBoardsForUserAndTeam(t, store) }) + t.Run("GetBoardHistory", func(t *testing.T) { + store, tearDown := setup(t) + defer tearDown() + testGetBoardHistory(t, store) + }) } func testGetBoard(t *testing.T, store store.Store) { @@ -800,3 +805,107 @@ func testSearchBoardsForUserAndTeam(t *testing.T, store store.Store) { }) } } + +func testGetBoardHistory(t *testing.T, store store.Store) { + userID := testUserID + + t.Run("testGetBoardHistory: create board", func(t *testing.T) { + originalTitle := "Board: original title" + boardID := utils.NewID(utils.IDTypeBoard) + board := &model.Board{ + ID: boardID, + Title: originalTitle, + TeamID: testTeamID, + Type: model.BoardTypeOpen, + } + + rBoard1, err := store.InsertBoard(board, userID) + require.NoError(t, err) + + opts := model.QueryBlockHistoryOptions{ + Limit: 0, + Descending: false, + } + + boards, err := store.GetBoardHistory(board.ID, opts) + require.NoError(t, err) + require.Len(t, boards, 1) + + // wait to avoid hitting pk uniqueness constraint in history + time.Sleep(10 * time.Millisecond) + + userID2 := "user-id-2" + newTitle := "Board: A new title" + newDescription := "A new description" + patch := &model.BoardPatch{Title: &newTitle, Description: &newDescription} + patchedBoard, err := store.PatchBoard(boardID, patch, userID2) + require.NoError(t, err) + + // Updated history + boards, err = store.GetBoardHistory(board.ID, opts) + require.NoError(t, err) + require.Len(t, boards, 2) + require.Equal(t, boards[0].Title, originalTitle) + require.Equal(t, boards[1].Title, newTitle) + require.Equal(t, boards[1].Description, newDescription) + + // Check history against latest board + rBoard2, err := store.GetBoard(board.ID) + require.NoError(t, err) + require.Equal(t, rBoard2.Title, newTitle) + require.Equal(t, rBoard2.Title, boards[1].Title) + require.NotZero(t, rBoard2.UpdateAt) + require.Equal(t, rBoard1.UpdateAt, boards[0].UpdateAt) + require.Equal(t, rBoard2.UpdateAt, patchedBoard.UpdateAt) + require.Equal(t, rBoard2.UpdateAt, boards[1].UpdateAt) + require.Equal(t, rBoard1, boards[0]) + require.Equal(t, rBoard2, boards[1]) + + // wait to avoid hitting pk uniqueness constraint in history + time.Sleep(10 * time.Millisecond) + + newTitle2 := "Board: A new title 2" + patch2 := &model.BoardPatch{Title: &newTitle2} + patchBoard2, err := store.PatchBoard(boardID, patch2, userID2) + require.NoError(t, err) + + // Updated history + opts = model.QueryBlockHistoryOptions{ + Limit: 1, + Descending: true, + } + boards, err = store.GetBoardHistory(board.ID, opts) + require.NoError(t, err) + require.Len(t, boards, 1) + require.Equal(t, boards[0].Title, newTitle2) + require.Equal(t, boards[0], patchBoard2) + + // Delete board + time.Sleep(10 * time.Millisecond) + err = store.DeleteBoard(boardID, userID) + require.NoError(t, err) + + // Updated history after delete + opts = model.QueryBlockHistoryOptions{ + Limit: 0, + Descending: true, + } + boards, err = store.GetBoardHistory(board.ID, opts) + require.NoError(t, err) + require.Len(t, boards, 4) + require.NotZero(t, boards[0].UpdateAt) + require.Greater(t, boards[0].UpdateAt, patchBoard2.UpdateAt) + require.NotZero(t, boards[0].DeleteAt) + require.Greater(t, boards[0].DeleteAt, patchBoard2.UpdateAt) + }) + + t.Run("testGetBoardHistory: nonexisting board", func(t *testing.T) { + opts := model.QueryBlockHistoryOptions{ + Limit: 0, + Descending: false, + } + boards, err := store.GetBoardHistory("nonexistent-id", opts) + require.NoError(t, err) + require.Len(t, boards, 0) + }) +} diff --git a/server/swagger/swagger.yml b/server/swagger/swagger.yml index 7f0d88b25..200252744 100644 --- a/server/swagger/swagger.yml +++ b/server/swagger/swagger.yml @@ -260,6 +260,39 @@ definitions: - schemeViewer type: object x-go-package: github.com/mattermost/focalboard/server/model + BoardMetadata: + description: BoardMetadata contains metadata for a Board + properties: + boardId: + description: The ID for the board + type: string + x-go-name: BoardID + createdBy: + description: The ID of the user that created the board + type: string + x-go-name: CreatedBy + descendantFirstUpdateAt: + description: The earliest time a descendant of this board was added, modified, or deleted + format: int64 + type: integer + x-go-name: DescendantFirstUpdateAt + descendantLastUpdateAt: + description: The most recent time a descendant of this board was added, modified, or deleted + format: int64 + type: integer + x-go-name: DescendantLastUpdateAt + lastModifiedBy: + description: The ID of the user that last modified the most recently modified descendant + type: string + x-go-name: LastModifiedBy + required: + - boardId + - descendantLastUpdateAt + - descendantFirstUpdateAt + - createdBy + - lastModifiedBy + type: object + x-go-package: github.com/mattermost/focalboard/server/model BoardPatch: description: BoardPatch is a patch for modify boards properties: @@ -1300,6 +1333,33 @@ paths: $ref: '#/definitions/ErrorResponse' security: - BearerAuth: [] + /api/v1/boards/{boardID}/metadata: + get: + description: Returns a board's metadata + operationId: getBoardMetadata + parameters: + - description: Board ID + in: path + name: boardID + required: true + type: string + produces: + - application/json + responses: + "200": + description: success + schema: + $ref: '#/definitions/BoardMetadata' + "404": + description: board not found + "501": + description: required license not found + default: + description: internal error + schema: + $ref: '#/definitions/ErrorResponse' + security: + - BearerAuth: [] /api/v1/boards/{boardID}/sharing: get: description: Returns sharing information for a board