Include collaborator count and limit frontend to last 5
This commit is contained in:
@ -3,6 +3,7 @@ import React, { Component } from 'react';
|
|||||||
import styled from 'styled-components';
|
import styled from 'styled-components';
|
||||||
import type { User } from 'types';
|
import type { User } from 'types';
|
||||||
import { Flex } from 'reflexbox';
|
import { Flex } from 'reflexbox';
|
||||||
|
import _ from 'lodash';
|
||||||
|
|
||||||
const Container = styled(Flex)`
|
const Container = styled(Flex)`
|
||||||
justify-content: space-between;
|
justify-content: space-between;
|
||||||
@ -19,7 +20,7 @@ class AuthorInfo extends Component {
|
|||||||
render() {
|
render() {
|
||||||
const { collaborators, views } = this.props;
|
const { collaborators, views } = this.props;
|
||||||
|
|
||||||
const users = collaborators.map((user, index, arr) => (
|
const users = _.takeRight(collaborators, 5).map((user, index, arr) => (
|
||||||
<span key={user.id}>
|
<span key={user.id}>
|
||||||
{user.name} {arr.length > 1 && (index < arr.length - 1 ? ',' : 'and')}
|
{user.name} {arr.length > 1 && (index < arr.length - 1 ? ',' : 'and')}
|
||||||
</span>
|
</span>
|
||||||
|
@ -1,9 +1,16 @@
|
|||||||
import { Collection, Star, User, View } from '../models';
|
// @flow
|
||||||
|
import { Collection, Star, User, View, Document } from '../models';
|
||||||
import presentUser from './user';
|
import presentUser from './user';
|
||||||
import presentCollection from './collection';
|
import presentCollection from './collection';
|
||||||
import _ from 'lodash';
|
import _ from 'lodash';
|
||||||
|
|
||||||
async function present(ctx, document, options) {
|
type Options = {
|
||||||
|
includeCollection?: boolean,
|
||||||
|
includeCollaborators?: boolean,
|
||||||
|
includeViews?: boolean,
|
||||||
|
};
|
||||||
|
|
||||||
|
async function present(ctx: Object, document: Document, options: Options) {
|
||||||
options = {
|
options = {
|
||||||
includeCollection: true,
|
includeCollection: true,
|
||||||
includeCollaborators: true,
|
includeCollaborators: true,
|
||||||
@ -13,7 +20,7 @@ async function present(ctx, document, options) {
|
|||||||
ctx.cache.set(document.id, document);
|
ctx.cache.set(document.id, document);
|
||||||
|
|
||||||
const userId = ctx.state.user.id;
|
const userId = ctx.state.user.id;
|
||||||
const data = {
|
let data = {
|
||||||
id: document.id,
|
id: document.id,
|
||||||
url: document.getUrl(),
|
url: document.getUrl(),
|
||||||
private: document.private,
|
private: document.private,
|
||||||
@ -23,6 +30,7 @@ async function present(ctx, document, options) {
|
|||||||
preview: document.preview,
|
preview: document.preview,
|
||||||
createdAt: document.createdAt,
|
createdAt: document.createdAt,
|
||||||
createdBy: undefined,
|
createdBy: undefined,
|
||||||
|
starred: false,
|
||||||
updatedAt: document.updatedAt,
|
updatedAt: document.updatedAt,
|
||||||
updatedBy: undefined,
|
updatedBy: undefined,
|
||||||
team: document.teamId,
|
team: document.teamId,
|
||||||
@ -34,12 +42,14 @@ async function present(ctx, document, options) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (options.includeViews) {
|
if (options.includeViews) {
|
||||||
|
// $FlowIssue not found in object literal?
|
||||||
data.views = await View.sum('count', {
|
data.views = await View.sum('count', {
|
||||||
where: { documentId: document.id },
|
where: { documentId: document.id },
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.includeCollection) {
|
if (options.includeCollection) {
|
||||||
|
// $FlowIssue not found in object literal?
|
||||||
data.collection = await ctx.cache.get(document.atlasId, async () => {
|
data.collection = await ctx.cache.get(document.atlasId, async () => {
|
||||||
const collection =
|
const collection =
|
||||||
options.collection ||
|
options.collection ||
|
||||||
@ -54,13 +64,15 @@ async function present(ctx, document, options) {
|
|||||||
|
|
||||||
if (options.includeCollaborators) {
|
if (options.includeCollaborators) {
|
||||||
// This could be further optimized by using ctx.cache
|
// This could be further optimized by using ctx.cache
|
||||||
data.collaborators = await User.findAll({
|
data['collaborators'] = await User.findAll({
|
||||||
where: {
|
where: {
|
||||||
id: {
|
id: {
|
||||||
$in: _.takeRight(document.collaboratorIds, 7) || [],
|
$in: _.takeRight(document.collaboratorIds, 10) || [],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}).map(user => presentUser(ctx, user));
|
}).map(user => presentUser(ctx, user));
|
||||||
|
// $FlowIssue not found in object literal?
|
||||||
|
data.collaboratorCount = document.collaboratorIds.length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const createdBy = await ctx.cache.get(
|
const createdBy = await ctx.cache.get(
|
||||||
|
Reference in New Issue
Block a user