Tidy, move recent documents to query scope
This commit is contained in:
@ -18,7 +18,7 @@ class DocumentViewersStore {
|
|||||||
this.isFetching = true;
|
this.isFetching = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await client.get(
|
const res = await client.post(
|
||||||
'/views.list',
|
'/views.list',
|
||||||
{
|
{
|
||||||
id: this.documentId,
|
id: this.documentId,
|
||||||
|
@ -24,7 +24,7 @@ router.post('collections.create', auth(), async ctx => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentCollection(ctx, atlas, true),
|
data: await presentCollection(ctx, atlas),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -33,7 +33,7 @@ router.post('collections.info', auth(), async ctx => {
|
|||||||
ctx.assertPresent(id, 'id is required');
|
ctx.assertPresent(id, 'id is required');
|
||||||
|
|
||||||
const user = ctx.state.user;
|
const user = ctx.state.user;
|
||||||
const atlas = await Collection.findOne({
|
const atlas = await Collection.scope('withRecentDocuments').findOne({
|
||||||
where: {
|
where: {
|
||||||
id,
|
id,
|
||||||
teamId: user.teamId,
|
teamId: user.teamId,
|
||||||
@ -43,13 +43,13 @@ router.post('collections.info', auth(), async ctx => {
|
|||||||
if (!atlas) throw httpErrors.NotFound();
|
if (!atlas) throw httpErrors.NotFound();
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentCollection(ctx, atlas, true),
|
data: await presentCollection(ctx, atlas),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post('collections.list', auth(), pagination(), async ctx => {
|
router.post('collections.list', auth(), pagination(), async ctx => {
|
||||||
const user = ctx.state.user;
|
const user = ctx.state.user;
|
||||||
const collections = await Collection.findAll({
|
const collections = await Collection.scope('withRecentDocuments').findAll({
|
||||||
where: {
|
where: {
|
||||||
teamId: user.teamId,
|
teamId: user.teamId,
|
||||||
},
|
},
|
||||||
@ -58,16 +58,10 @@ router.post('collections.list', auth(), pagination(), async ctx => {
|
|||||||
limit: ctx.state.pagination.limit,
|
limit: ctx.state.pagination.limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
// Collectiones
|
const data = await Promise.all(
|
||||||
let data = [];
|
collections.map(async atlas => await presentCollection(ctx, atlas))
|
||||||
await Promise.all(
|
|
||||||
collections.map(async atlas => {
|
|
||||||
return data.push(await presentCollection(ctx, atlas, true));
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
data = _.orderBy(data, ['updatedAt'], ['desc']);
|
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
pagination: ctx.state.pagination,
|
pagination: ctx.state.pagination,
|
||||||
data,
|
data,
|
||||||
|
@ -21,7 +21,9 @@ router.post('documents.list', auth(), pagination(), async ctx => {
|
|||||||
include: [{ model: Star, as: 'starred', where: { userId: user.id } }],
|
include: [{ model: Star, as: 'starred', where: { userId: user.id } }],
|
||||||
});
|
});
|
||||||
|
|
||||||
let data = await Promise.all(documents.map(doc => presentDocument(ctx, doc)));
|
const data = await Promise.all(
|
||||||
|
documents.map(document => presentDocument(ctx, document))
|
||||||
|
);
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
pagination: ctx.state.pagination,
|
pagination: ctx.state.pagination,
|
||||||
@ -42,7 +44,7 @@ router.post('documents.viewed', auth(), pagination(), async ctx => {
|
|||||||
limit: ctx.state.pagination.limit,
|
limit: ctx.state.pagination.limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
let data = await Promise.all(
|
const data = await Promise.all(
|
||||||
views.map(view => presentDocument(ctx, view.document))
|
views.map(view => presentDocument(ctx, view.document))
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -70,7 +72,7 @@ router.post('documents.starred', auth(), pagination(), async ctx => {
|
|||||||
limit: ctx.state.pagination.limit,
|
limit: ctx.state.pagination.limit,
|
||||||
});
|
});
|
||||||
|
|
||||||
let data = await Promise.all(
|
const data = await Promise.all(
|
||||||
views.map(view => presentDocument(ctx, view.document))
|
views.map(view => presentDocument(ctx, view.document))
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -99,8 +101,7 @@ router.post('documents.info', auth(), async ctx => {
|
|||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentDocument(ctx, document, {
|
data: await presentDocument(ctx, document, {
|
||||||
includeCollection: document.private,
|
includeViews: true,
|
||||||
includeCollaborators: true,
|
|
||||||
}),
|
}),
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
@ -113,15 +114,8 @@ router.post('documents.search', auth(), async ctx => {
|
|||||||
|
|
||||||
const documents = await Document.searchForUser(user, query);
|
const documents = await Document.searchForUser(user, query);
|
||||||
|
|
||||||
const data = [];
|
const data = await Promise.all(
|
||||||
await Promise.all(
|
documents.map(async document => await presentDocument(ctx, document))
|
||||||
documents.map(async document => {
|
|
||||||
data.push(
|
|
||||||
await presentDocument(ctx, document, {
|
|
||||||
includeCollaborators: true,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
@ -204,9 +198,7 @@ router.post('documents.create', auth(), async ctx => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentDocument(ctx, newDocument, {
|
data: await presentDocument(ctx, newDocument),
|
||||||
includeCollaborators: true,
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -232,9 +224,7 @@ router.post('documents.update', auth(), async ctx => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentDocument(ctx, document, {
|
data: await presentDocument(ctx, document),
|
||||||
includeCollaborators: true,
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -273,10 +263,7 @@ router.post('documents.move', auth(), async ctx => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
data: await presentDocument(ctx, document, {
|
data: await presentDocument(ctx, document),
|
||||||
includeCollaborators: true,
|
|
||||||
collection: collection,
|
|
||||||
}),
|
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -60,6 +60,16 @@ const Collection = sequelize.define(
|
|||||||
as: 'documents',
|
as: 'documents',
|
||||||
foreignKey: 'atlasId',
|
foreignKey: 'atlasId',
|
||||||
});
|
});
|
||||||
|
Collection.addScope('withRecentDocuments', {
|
||||||
|
include: [
|
||||||
|
{
|
||||||
|
as: 'documents',
|
||||||
|
limit: 10,
|
||||||
|
model: models.Document,
|
||||||
|
order: [['updatedAt', 'DESC']],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
instanceMethods: {
|
instanceMethods: {
|
||||||
|
@ -116,6 +116,7 @@ const Document = sequelize.define(
|
|||||||
classMethods: {
|
classMethods: {
|
||||||
associate: models => {
|
associate: models => {
|
||||||
Document.belongsTo(models.Collection, {
|
Document.belongsTo(models.Collection, {
|
||||||
|
as: 'collection',
|
||||||
foreignKey: 'atlasId',
|
foreignKey: 'atlasId',
|
||||||
});
|
});
|
||||||
Document.belongsTo(models.User, {
|
Document.belongsTo(models.User, {
|
||||||
@ -133,7 +134,7 @@ const Document = sequelize.define(
|
|||||||
'defaultScope',
|
'defaultScope',
|
||||||
{
|
{
|
||||||
include: [
|
include: [
|
||||||
{ model: models.Collection },
|
{ model: models.Collection, as: 'collection' },
|
||||||
{ model: models.User, as: 'createdBy' },
|
{ model: models.User, as: 'createdBy' },
|
||||||
{ model: models.User, as: 'updatedBy' },
|
{ model: models.User, as: 'updatedBy' },
|
||||||
],
|
],
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
|
// @flow
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
import { Document } from '../models';
|
import { Collection } from '../models';
|
||||||
import presentDocument from './document';
|
import presentDocument from './document';
|
||||||
|
|
||||||
async function present(ctx, collection, includeRecentDocuments = false) {
|
async function present(ctx: Object, collection: Collection) {
|
||||||
ctx.cache.set(collection.id, collection);
|
ctx.cache.set(collection.id, collection);
|
||||||
|
|
||||||
const data = {
|
const data = {
|
||||||
@ -13,31 +14,21 @@ async function present(ctx, collection, includeRecentDocuments = false) {
|
|||||||
type: collection.type,
|
type: collection.type,
|
||||||
createdAt: collection.createdAt,
|
createdAt: collection.createdAt,
|
||||||
updatedAt: collection.updatedAt,
|
updatedAt: collection.updatedAt,
|
||||||
|
recentDocuments: undefined,
|
||||||
|
documents: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (collection.type === 'atlas')
|
if (collection.type === 'atlas') {
|
||||||
data.documents = await collection.getDocumentsStructure();
|
data.documents = await collection.getDocumentsStructure();
|
||||||
|
}
|
||||||
|
|
||||||
if (includeRecentDocuments) {
|
if (collection.documents) {
|
||||||
const documents = await Document.findAll({
|
data.recentDocuments = await Promise.all(
|
||||||
where: {
|
collection.documents.map(
|
||||||
atlasId: collection.id,
|
async document =>
|
||||||
},
|
await presentDocument(ctx, document, { includeCollaborators: true })
|
||||||
limit: 10,
|
)
|
||||||
order: [['updatedAt', 'DESC']],
|
|
||||||
});
|
|
||||||
|
|
||||||
const recentDocuments = [];
|
|
||||||
await Promise.all(
|
|
||||||
documents.map(async document => {
|
|
||||||
recentDocuments.push(
|
|
||||||
await presentDocument(ctx, document, {
|
|
||||||
includeCollaborators: true,
|
|
||||||
})
|
|
||||||
);
|
|
||||||
})
|
|
||||||
);
|
);
|
||||||
data.recentDocuments = _.orderBy(recentDocuments, ['updatedAt'], ['desc']);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return data;
|
return data;
|
||||||
|
@ -5,9 +5,8 @@ import presentCollection from './collection';
|
|||||||
|
|
||||||
async function present(ctx: Object, document: Document, options: Object = {}) {
|
async function present(ctx: Object, document: Document, options: Object = {}) {
|
||||||
options = {
|
options = {
|
||||||
includeCollection: true,
|
|
||||||
includeCollaborators: true,
|
includeCollaborators: true,
|
||||||
includeViews: true,
|
includeViews: false,
|
||||||
...options,
|
...options,
|
||||||
};
|
};
|
||||||
ctx.cache.set(document.id, document);
|
ctx.cache.set(document.id, document);
|
||||||
@ -30,8 +29,8 @@ async function present(ctx: Object, document: Document, options: Object = {}) {
|
|||||||
views: undefined,
|
views: undefined,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (options.includeCollection) {
|
if (document.private) {
|
||||||
data.collection = presentCollection(ctx, document.collection);
|
data.collection = await presentCollection(ctx, document.collection);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.includeViews) {
|
if (options.includeViews) {
|
||||||
|
Reference in New Issue
Block a user