feat: Add reordering to starred documents (#2953)

* draft

* reordering

* JIT Index stars on first load

* test

* Remove unused code on client

* small unrefactor
This commit is contained in:
Tom Moor
2022-01-21 18:11:50 -08:00
committed by GitHub
parent 49533d7a3f
commit 79e2cad5b9
32 changed files with 931 additions and 132 deletions

View File

@@ -25,7 +25,7 @@ import {
presentCollectionGroupMembership,
presentFileOperation,
} from "@server/presenters";
import collectionIndexing from "@server/utils/collectionIndexing";
import { collectionIndexing } from "@server/utils/indexing";
import removeIndexCollision from "@server/utils/removeIndexCollision";
import {
assertUuid,
@@ -623,12 +623,12 @@ router.post("collections.list", auth(), pagination(), async (ctx) => {
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const nullIndexCollection = collections.findIndex(
const nullIndex = collections.findIndex(
(collection) => collection.index === null
);
if (nullIndexCollection !== -1) {
const indexedCollections = await collectionIndexing(ctx.state.user.teamId);
if (nullIndex !== -1) {
const indexedCollections = await collectionIndexing(user.teamId);
collections.forEach((collection) => {
collection.index = indexedCollections[collection.id];
});

View File

@@ -312,6 +312,7 @@ router.post("documents.viewed", auth(), pagination(), async (ctx) => {
};
});
// Deprecated use stars.list instead
router.post("documents.starred", auth(), pagination(), async (ctx) => {
let { direction } = ctx.body;
const { sort = "updatedAt" } = ctx.body;
@@ -864,6 +865,7 @@ router.post("documents.search", auth(), pagination(), async (ctx) => {
};
});
// Deprecated use stars.create instead
router.post("documents.star", auth(), async (ctx) => {
const { id } = ctx.body;
assertPresent(id, "id is required");
@@ -898,6 +900,7 @@ router.post("documents.star", auth(), async (ctx) => {
};
});
// Deprecated use stars.delete instead
router.post("documents.unstar", auth(), async (ctx) => {
const { id } = ctx.body;
assertPresent(id, "id is required");

View File

@@ -22,6 +22,7 @@ import pins from "./pins";
import revisions from "./revisions";
import searches from "./searches";
import shares from "./shares";
import stars from "./stars";
import team from "./team";
import users from "./users";
import utils from "./utils";
@@ -58,6 +59,7 @@ router.use("/", hooks.routes());
router.use("/", apiKeys.routes());
router.use("/", searches.routes());
router.use("/", shares.routes());
router.use("/", stars.routes());
router.use("/", team.routes());
router.use("/", integrations.routes());
router.use("/", notificationSettings.routes());

View File

@@ -0,0 +1,86 @@
import TestServer from "fetch-test-server";
import webService from "@server/services/web";
import { buildUser, buildStar, buildDocument } from "@server/test/factories";
import { flushdb } from "@server/test/support";
const app = webService();
const server = new TestServer(app.callback());
beforeEach(() => flushdb());
afterAll(() => server.close());
describe("#stars.create", () => {
it("should create a star", async () => {
const user = await buildUser();
const document = await buildDocument({
userId: user.id,
teamId: user.teamId,
});
const res = await server.post("/api/stars.create", {
body: {
token: user.getJwtToken(),
documentId: document.id,
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.documentId).toEqual(document.id);
});
it("should require authentication", async () => {
const res = await server.post("/api/stars.create");
expect(res.status).toEqual(401);
});
});
describe("#stars.list", () => {
it("should list users stars", async () => {
const user = await buildUser();
await buildStar();
const star = await buildStar({
userId: user.id,
});
const res = await server.post("/api/stars.list", {
body: {
token: user.getJwtToken(),
},
});
const body = await res.json();
expect(res.status).toEqual(200);
expect(body.data.stars.length).toEqual(1);
expect(body.data.stars[0].id).toEqual(star.id);
});
it("should require authentication", async () => {
const res = await server.post("/api/stars.list");
expect(res.status).toEqual(401);
});
});
describe("#stars.delete", () => {
it("should delete users star", async () => {
const user = await buildUser();
const star = await buildStar({
userId: user.id,
});
const res = await server.post("/api/stars.delete", {
body: {
id: star.id,
token: user.getJwtToken(),
},
});
expect(res.status).toEqual(200);
});
it("should require authentication", async () => {
const res = await server.post("/api/stars.delete");
expect(res.status).toEqual(401);
});
});

134
server/routes/api/stars.ts Normal file
View File

@@ -0,0 +1,134 @@
import Router from "koa-router";
import { Sequelize } from "sequelize";
import starCreator from "@server/commands/starCreator";
import starDestroyer from "@server/commands/starDestroyer";
import starUpdater from "@server/commands/starUpdater";
import auth from "@server/middlewares/authentication";
import { Document, Star } from "@server/models";
import { authorize } from "@server/policies";
import {
presentStar,
presentDocument,
presentPolicies,
} from "@server/presenters";
import { starIndexing } from "@server/utils/indexing";
import { assertUuid, assertIndexCharacters } from "@server/validation";
import pagination from "./middlewares/pagination";
const router = new Router();
router.post("stars.create", auth(), async (ctx) => {
const { documentId } = ctx.body;
const { index } = ctx.body;
assertUuid(documentId, "documentId is required");
const { user } = ctx.state;
const document = await Document.findByPk(documentId, {
userId: user.id,
});
authorize(user, "star", document);
if (index) {
assertIndexCharacters(index);
}
const star = await starCreator({
user,
documentId,
ip: ctx.request.ip,
index,
});
ctx.body = {
data: presentStar(star),
policies: presentPolicies(user, [star]),
};
});
router.post("stars.list", auth(), pagination(), async (ctx) => {
const { user } = ctx.state;
const [stars, collectionIds] = await Promise.all([
Star.findAll({
where: {
userId: user.id,
},
order: [
Sequelize.literal('"star"."index" collate "C"'),
["updatedAt", "DESC"],
],
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
}),
user.collectionIds(),
]);
const nullIndex = stars.findIndex((star) => star.index === null);
if (nullIndex !== -1) {
const indexedStars = await starIndexing(user.id);
stars.forEach((star) => {
star.index = indexedStars[star.id];
});
}
const documents = await Document.defaultScopeWithUser(user.id).findAll({
where: {
id: stars.map((star) => star.documentId),
collectionId: collectionIds,
},
});
const policies = presentPolicies(user, [...documents, ...stars]);
ctx.body = {
pagination: ctx.state.pagination,
data: {
stars: stars.map(presentStar),
documents: await Promise.all(
documents.map((document: Document) => presentDocument(document))
),
},
policies,
};
});
router.post("stars.update", auth(), async (ctx) => {
const { id, index } = ctx.body;
assertUuid(id, "id is required");
assertIndexCharacters(index);
const { user } = ctx.state;
let star = await Star.findByPk(id);
authorize(user, "update", star);
star = await starUpdater({
user,
star,
ip: ctx.request.ip,
index,
});
ctx.body = {
data: presentStar(star),
policies: presentPolicies(user, [star]),
};
});
router.post("stars.delete", auth(), async (ctx) => {
const { id } = ctx.body;
assertUuid(id, "id is required");
const { user } = ctx.state;
const star = await Star.findByPk(id);
authorize(user, "delete", star);
await starDestroyer({ user, star, ip: ctx.request.ip });
ctx.body = {
success: true,
};
});
export default router;