This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
outline/server/api/collections.js

261 lines
6.3 KiB
JavaScript
Raw Normal View History

// @flow
2016-05-07 18:52:08 +00:00
import Router from 'koa-router';
import auth from '../middlewares/authentication';
2016-05-07 18:52:08 +00:00
import pagination from './middlewares/pagination';
import { presentCollection, presentUser } from '../presenters';
import { Collection, CollectionUser, Team, User } from '../models';
import { ValidationError, InvalidRequestError } from '../errors';
import { exportCollection, exportCollections } from '../logistics';
import policy from '../policies';
import events from '../events';
2016-05-07 18:52:08 +00:00
const { authorize } = policy;
2016-05-07 18:52:08 +00:00
const router = new Router();
2017-04-27 04:47:03 +00:00
router.post('collections.create', auth(), async ctx => {
2017-10-30 06:22:46 +00:00
const { name, color, description, type } = ctx.body;
const isPrivate = ctx.body.private;
2016-07-22 05:06:30 +00:00
ctx.assertPresent(name, 'name is required');
2017-10-30 06:22:46 +00:00
if (color)
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
2016-07-22 05:06:30 +00:00
const user = ctx.state.user;
authorize(user, 'create', Collection);
2016-07-22 05:06:30 +00:00
const collection = await Collection.create({
2016-07-22 05:06:30 +00:00
name,
description,
2017-10-30 06:22:46 +00:00
color,
2016-07-22 05:06:30 +00:00
type: type || 'atlas',
teamId: user.teamId,
creatorId: user.id,
private: isPrivate,
2016-07-22 05:06:30 +00:00
});
events.add({
name: 'collections.create',
modelId: collection.id,
teamId: collection.teamId,
actorId: user.id,
});
2016-07-22 05:06:30 +00:00
ctx.body = {
data: await presentCollection(collection),
};
});
router.post('collections.info', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertUuid(id, 'id is required');
const collection = await Collection.findByPk(id);
authorize(ctx.state.user, 'read', collection);
ctx.body = {
data: await presentCollection(collection),
};
});
router.post('collections.add_user', auth(), async ctx => {
const { id, userId, permission = 'read_write' } = ctx.body;
ctx.assertUuid(id, 'id is required');
ctx.assertUuid(userId, 'userId is required');
const collection = await Collection.findByPk(id);
authorize(ctx.state.user, 'update', collection);
if (!collection.private) {
throw new InvalidRequestError('Collection must be private to add users');
}
const user = await User.findByPk(userId);
authorize(ctx.state.user, 'read', user);
await CollectionUser.create({
collectionId: id,
userId,
permission,
createdById: ctx.state.user.id,
});
events.add({
name: 'collections.add_user',
modelId: userId,
collectionId: collection.id,
teamId: collection.teamId,
actorId: ctx.state.user.id,
});
ctx.body = {
success: true,
};
});
router.post('collections.remove_user', auth(), async ctx => {
const { id, userId } = ctx.body;
ctx.assertUuid(id, 'id is required');
ctx.assertUuid(userId, 'userId is required');
const collection = await Collection.findByPk(id);
authorize(ctx.state.user, 'update', collection);
if (!collection.private) {
throw new InvalidRequestError('Collection must be private to remove users');
}
const user = await User.findByPk(userId);
authorize(ctx.state.user, 'read', user);
await collection.removeUser(user);
events.add({
name: 'collections.remove_user',
modelId: userId,
collectionId: collection.id,
teamId: collection.teamId,
actorId: ctx.state.user.id,
});
ctx.body = {
success: true,
};
});
router.post('collections.users', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertUuid(id, 'id is required');
const collection = await Collection.findByPk(id);
authorize(ctx.state.user, 'read', collection);
const users = await collection.getUsers();
ctx.body = {
data: users.map(presentUser),
};
});
router.post('collections.export', auth(), async ctx => {
const { id } = ctx.body;
ctx.assertUuid(id, 'id is required');
const user = ctx.state.user;
const collection = await Collection.findByPk(id);
authorize(user, 'export', collection);
// async operation to create zip archive and email user
exportCollection(id, user.email);
ctx.body = {
success: true,
};
});
router.post('collections.exportAll', auth(), async ctx => {
const user = ctx.state.user;
const team = await Team.findByPk(user.teamId);
authorize(user, 'export', team);
// async operation to create zip archive and email user
exportCollections(user.teamId, user.email);
ctx.body = {
success: true,
};
});
router.post('collections.update', auth(), async ctx => {
2018-11-18 09:13:28 +00:00
const { id, name, description, color } = ctx.body;
const isPrivate = ctx.body.private;
ctx.assertPresent(name, 'name is required');
2017-10-30 06:22:46 +00:00
if (color)
ctx.assertHexColor(color, 'Invalid hex value (please use format #FFFFFF)');
const user = ctx.state.user;
const collection = await Collection.findByPk(id);
authorize(user, 'update', collection);
if (isPrivate && !collection.private) {
await CollectionUser.findOrCreate({
where: {
collectionId: collection.id,
userId: user.id,
},
defaults: {
permission: 'read_write',
createdById: user.id,
},
});
}
collection.name = name;
2018-11-18 09:13:28 +00:00
collection.description = description;
2017-10-30 06:22:46 +00:00
collection.color = color;
collection.private = isPrivate;
await collection.save();
events.add({
name: 'collections.update',
modelId: collection.id,
teamId: collection.teamId,
actorId: user.id,
});
ctx.body = {
data: presentCollection(collection),
2016-07-22 05:06:30 +00:00
};
});
2017-04-27 04:47:03 +00:00
router.post('collections.list', auth(), pagination(), async ctx => {
2016-06-20 07:18:03 +00:00
const user = ctx.state.user;
const collectionIds = await user.collectionIds();
let collections = await Collection.findAll({
2016-05-07 18:52:08 +00:00
where: {
2016-06-20 07:18:03 +00:00
teamId: user.teamId,
id: collectionIds,
2016-05-07 18:52:08 +00:00
},
2017-04-27 04:47:03 +00:00
order: [['updatedAt', 'DESC']],
2016-05-07 18:52:08 +00:00
offset: ctx.state.pagination.offset,
limit: ctx.state.pagination.limit,
});
const data = await Promise.all(
collections.map(async collection => await presentCollection(collection))
2017-04-27 04:47:03 +00:00
);
2016-05-20 07:33:52 +00:00
2016-05-07 18:52:08 +00:00
ctx.body = {
pagination: ctx.state.pagination,
2016-07-26 06:01:14 +00:00
data,
2016-05-07 18:52:08 +00:00
};
});
router.post('collections.delete', auth(), async ctx => {
const { id } = ctx.body;
const user = ctx.state.user;
ctx.assertUuid(id, 'id is required');
const collection = await Collection.findByPk(id);
authorize(user, 'delete', collection);
const total = await Collection.count();
2018-02-20 07:31:18 +00:00
if (total === 1) throw new ValidationError('Cannot delete last collection');
2018-02-20 07:31:18 +00:00
await collection.destroy();
events.add({
name: 'collections.delete',
modelId: collection.id,
teamId: collection.teamId,
actorId: user.id,
});
ctx.body = {
success: true,
};
});
2016-06-22 07:01:57 +00:00
export default router;