From 3c2b2c8a91a28970a199f5d0eb80f49f5966ae4f Mon Sep 17 00:00:00 2001 From: Saumya Pandey Date: Tue, 9 Mar 2021 14:41:44 +0530 Subject: [PATCH] Work on review and make reordering predictable --- app/scenes/CollectionNew.js | 2 - app/stores/CollectionsStore.js | 2 +- server/api/collections.js | 32 ++++++++++++- server/utils/removeIndexCollisions.js | 69 +++++++++++++++++++++++++++ 4 files changed, 101 insertions(+), 4 deletions(-) create mode 100644 server/utils/removeIndexCollisions.js diff --git a/app/scenes/CollectionNew.js b/app/scenes/CollectionNew.js index cdcb18d543..3b16118bf3 100644 --- a/app/scenes/CollectionNew.js +++ b/app/scenes/CollectionNew.js @@ -38,7 +38,6 @@ class CollectionNew extends React.Component { handleSubmit = async (ev: SyntheticEvent<>) => { ev.preventDefault(); this.isSaving = true; - const { collections } = this.props; const collection = new Collection( { name: this.name, @@ -46,7 +45,6 @@ class CollectionNew extends React.Component { icon: this.icon, color: this.color, private: this.private, - index: collections.indexForNew, }, this.props.collections ); diff --git a/app/stores/CollectionsStore.js b/app/stores/CollectionsStore.js index 2a8a3222f0..28c4622756 100644 --- a/app/stores/CollectionsStore.js +++ b/app/stores/CollectionsStore.js @@ -42,7 +42,7 @@ export default class CollectionsStore extends BaseStore { return collections.sort((a, b) => { if (a.index === b.index) { - return 0; + return a.updatedAt > b.updatedAt ? -1 : 1; } return a.index < b.index ? -1 : 1; }); diff --git a/server/api/collections.js b/server/api/collections.js index 1d1ede7ad1..15d19669c0 100644 --- a/server/api/collections.js +++ b/server/api/collections.js @@ -1,5 +1,6 @@ // @flow import fs from "fs"; +import fractionalIndex from "fractional-index"; import Router from "koa-router"; import { ValidationError } from "../errors"; import { exportCollections } from "../exporter"; @@ -25,6 +26,7 @@ import { } from "../presenters"; import { Op } from "../sequelize"; import collectionIndexing from "../utils/collectionIndexing"; +import removeIndexCollisions from "../utils/removeIndexCollisions"; import { archiveCollection, archiveCollections } from "../utils/zip"; import pagination from "./middlewares/pagination"; @@ -39,8 +41,9 @@ router.post("collections.create", auth(), async (ctx) => { sharing, icon, sort = Collection.DEFAULT_SORT, - index, } = ctx.body; + + let { index } = ctx.body; const isPrivate = ctx.body.private; ctx.assertPresent(name, "name is required"); @@ -51,6 +54,29 @@ router.post("collections.create", auth(), async (ctx) => { const user = ctx.state.user; authorize(user, "create", Collection); + const collections = await Collection.findAll({ + where: { teamId: user.teamId, deletedAt: null }, + attributes: ["id", "index", "updatedAt"], + }); + + if (index) { + const allowedASCII = new RegExp(/^[\x21-\x7E]+$/); + if (!allowedASCII.test(index)) { + throw new ValidationError( + "Index characters must be between x21 to x7E ASCII" + ); + } + } else { + collections.sort((a, b) => { + if (a.index === b.index) { + return a.updatedAt > b.updatedAt ? -1 : 1; + } + return a.index < b.index ? -1 : 1; + }); + + index = fractionalIndex(collections[collections.length - 1].index, null); + } + let collection = await Collection.create({ name, description, @@ -64,6 +90,8 @@ router.post("collections.create", auth(), async (ctx) => { index, }); + removeIndexCollisions(user.teamId, index, collections); + await Event.create({ name: "collections.create", collectionId: collection.id, @@ -633,6 +661,8 @@ router.post("collections.move", auth(), async (ctx) => { await collection.update({ index }); + removeIndexCollisions(user.teamId); + await Event.create({ name: "collections.move", collectionId: collection.id, diff --git a/server/utils/removeIndexCollisions.js b/server/utils/removeIndexCollisions.js new file mode 100644 index 0000000000..21f7cbe123 --- /dev/null +++ b/server/utils/removeIndexCollisions.js @@ -0,0 +1,69 @@ +// @flow +import fractionalIndex from "fractional-index"; +import { Collection } from "../models"; + +export default async function removeIndexCollisions( + teamId: string, + index?: string, + collections?: Collection[] +) { + collections = collections + ? collections + : await Collection.findAll({ + where: { teamId, deletedAt: null }, //no point in maintaining index of deleted collections. + attributes: ["id", "index", "updatedAt"], + }); + + // use updatedAt because in case of index collision we need to have a predictable order + collections.sort((a, b) => { + if (a.index === b.index) { + return a.updatedAt > b.updatedAt ? -1 : 1; + } + return a.index < b.index ? -1 : 1; + }); + + collections = collections.map((collection) => { + return [collection, collection.index]; + }); + + // a set to store the index value of collections. + const indexSet = new Set(); + + if (index) { + indexSet.add(index); + } + + let collision = false; + + // make the index null, if there is index collision + collections = collections.map((collection) => { + if (indexSet.has(collection[1])) { + collision = true; + collection[1] = null; + return collection; + } + indexSet.add(collection[1]); + return collection; + }); + + let indexArray = Array.from(indexSet); + indexArray.sort(); + + if (!collision) { + return; + } + + for (const [i, collection] of collections.entries()) { + if (collection[1] === null) { + const previousCollectionIndex = i - 1 < 0 ? null : indexArray[i - 1]; + const nextCollectionIndex = + i === indexArray.length ? null : indexArray[i]; + const newIndex = fractionalIndex( + previousCollectionIndex, + nextCollectionIndex + ); + indexArray.splice(i, 0, newIndex); + await collection[0].update({ index: newIndex }); + } + } +}