-
+
+ {this.store.isFetching && }
{this.props.notFound &&
Not Found
diff --git a/frontend/scenes/Search/SearchStore.js b/frontend/scenes/Search/SearchStore.js
index 058ca7744..68088ce80 100644
--- a/frontend/scenes/Search/SearchStore.js
+++ b/frontend/scenes/Search/SearchStore.js
@@ -7,7 +7,6 @@ import Document from 'models/Document';
class SearchStore {
@observable documents: Array = [];
@observable searchTerm: ?string = null;
-
@observable isFetching = false;
/* Actions */
diff --git a/server/api/documents.js b/server/api/documents.js
index 36accaf2d..5205e2c59 100644
--- a/server/api/documents.js
+++ b/server/api/documents.js
@@ -101,9 +101,7 @@ router.post('documents.info', auth(), async ctx => {
}
ctx.body = {
- data: await presentDocument(ctx, document, {
- includeViews: true,
- }),
+ data: await presentDocument(ctx, document),
};
});
diff --git a/server/models/Document.js b/server/models/Document.js
index 363f164bb..b912bc30f 100644
--- a/server/models/Document.js
+++ b/server/models/Document.js
@@ -119,6 +119,9 @@ Document.associate = models => {
Document.hasMany(models.Star, {
as: 'starred',
});
+ Document.hasMany(models.View, {
+ as: 'views',
+ });
Document.addScope(
'defaultScope',
{
@@ -130,6 +133,11 @@ Document.associate = models => {
},
{ override: true }
);
+ Document.addScope('withViews', userId => ({
+ include: [
+ { model: models.View, as: 'views', where: { userId }, required: false },
+ ],
+ }));
Document.addScope('withStarred', userId => ({
include: [
{ model: models.Star, as: 'starred', where: { userId }, required: false },
@@ -174,7 +182,9 @@ Document.searchForUser = async (user, query, options = {}) => {
model: Document,
})
.map(document => document.id);
- return Document.findAll({
+
+ const withViewsScope = { method: ['withViews', user.id] };
+ return Document.scope('defaultScope', withViewsScope).findAll({
where: { id: ids },
});
};
diff --git a/server/presenters/document.js b/server/presenters/document.js
index a3c9162a8..0cf8db4cb 100644
--- a/server/presenters/document.js
+++ b/server/presenters/document.js
@@ -1,18 +1,16 @@
// @flow
import _ from 'lodash';
-import { User, Document, View } from '../models';
+import { User, Document } from '../models';
import presentUser from './user';
import presentCollection from './collection';
type Options = {
includeCollaborators?: boolean,
- includeViews?: boolean,
};
async function present(ctx: Object, document: Document, options: ?Options) {
options = {
includeCollaborators: true,
- includeViews: false,
...options,
};
ctx.cache.set(document.id, document);
@@ -28,6 +26,8 @@ async function present(ctx: Object, document: Document, options: ?Options) {
createdBy: presentUser(ctx, document.createdBy),
updatedAt: document.updatedAt,
updatedBy: presentUser(ctx, document.updatedBy),
+ firstViewedAt: undefined,
+ lastViewedAt: undefined,
team: document.teamId,
collaborators: [],
starred: !!document.starred,
@@ -40,10 +40,10 @@ async function present(ctx: Object, document: Document, options: ?Options) {
data.collection = await presentCollection(ctx, document.collection);
}
- if (options.includeViews) {
- data.views = await View.sum('count', {
- where: { documentId: document.id },
- });
+ if (document.views && document.views.length === 1) {
+ data.views = document.views[0].count;
+ data.firstViewedAt = document.views[0].createdAt;
+ data.lastViewedAt = document.views[0].updatedAt;
}
if (options.includeCollaborators) {