Draft Documents (#518)
* Mostly there * Fix up specs * Working scope, updated tests * Don't record view on draft * PR feedback * Highlight drafts nav item * Bugaboos * Styling * Refactoring, gradually addressing Jori feedback * Show collection in drafts list Flow fixes * Ensure menu actions are hidden when draft
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// @flow
|
||||
import Router from 'koa-router';
|
||||
|
||||
import { Op } from 'sequelize';
|
||||
import auth from './middlewares/authentication';
|
||||
import pagination from './middlewares/pagination';
|
||||
import { presentDocument, presentRevision } from '../presenters';
|
||||
@@ -102,6 +102,28 @@ router.post('documents.starred', auth(), pagination(), async ctx => {
|
||||
};
|
||||
});
|
||||
|
||||
router.post('documents.drafts', auth(), pagination(), async ctx => {
|
||||
let { sort = 'updatedAt', direction } = ctx.body;
|
||||
if (direction !== 'ASC') direction = 'DESC';
|
||||
|
||||
const user = ctx.state.user;
|
||||
const documents = await Document.findAll({
|
||||
where: { userId: user.id, publishedAt: { [Op.eq]: null } },
|
||||
order: [[sort, direction]],
|
||||
offset: ctx.state.pagination.offset,
|
||||
limit: ctx.state.pagination.limit,
|
||||
});
|
||||
|
||||
const data = await Promise.all(
|
||||
documents.map(document => presentDocument(ctx, document))
|
||||
);
|
||||
|
||||
ctx.body = {
|
||||
pagination: ctx.state.pagination,
|
||||
data,
|
||||
};
|
||||
});
|
||||
|
||||
router.post('documents.info', auth(), async ctx => {
|
||||
const { id } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
@@ -188,9 +210,10 @@ router.post('documents.unstar', auth(), async ctx => {
|
||||
});
|
||||
|
||||
router.post('documents.create', auth(), async ctx => {
|
||||
const { collection, title, text, parentDocument, index } = ctx.body;
|
||||
ctx.assertPresent(collection, 'collection is required');
|
||||
ctx.assertUuid(collection, 'collection must be an uuid');
|
||||
const { title, text, publish, parentDocument, index } = ctx.body;
|
||||
const collectionId = ctx.body.collection;
|
||||
ctx.assertPresent(collectionId, 'collection is required');
|
||||
ctx.assertUuid(collectionId, 'collection must be an uuid');
|
||||
ctx.assertPresent(title, 'title is required');
|
||||
ctx.assertPresent(text, 'text is required');
|
||||
if (parentDocument)
|
||||
@@ -200,44 +223,48 @@ router.post('documents.create', auth(), async ctx => {
|
||||
const user = ctx.state.user;
|
||||
authorize(user, 'create', Document);
|
||||
|
||||
const ownerCollection = await Collection.findOne({
|
||||
const collection = await Collection.findOne({
|
||||
where: {
|
||||
id: collection,
|
||||
id: collectionId,
|
||||
teamId: user.teamId,
|
||||
},
|
||||
});
|
||||
authorize(user, 'publish', ownerCollection);
|
||||
authorize(user, 'publish', collection);
|
||||
|
||||
let parentDocumentObj = {};
|
||||
if (parentDocument && ownerCollection.type === 'atlas') {
|
||||
if (parentDocument && collection.type === 'atlas') {
|
||||
parentDocumentObj = await Document.findOne({
|
||||
where: {
|
||||
id: parentDocument,
|
||||
atlasId: ownerCollection.id,
|
||||
atlasId: collection.id,
|
||||
},
|
||||
});
|
||||
authorize(user, 'read', parentDocumentObj);
|
||||
}
|
||||
|
||||
const newDocument = await Document.create({
|
||||
const publishedAt = publish === false ? null : new Date();
|
||||
let document = await Document.create({
|
||||
parentDocumentId: parentDocumentObj.id,
|
||||
atlasId: ownerCollection.id,
|
||||
atlasId: collection.id,
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
lastModifiedById: user.id,
|
||||
createdById: user.id,
|
||||
publishedAt,
|
||||
title,
|
||||
text,
|
||||
});
|
||||
|
||||
// reload to get all of the data needed to present (user, collection etc)
|
||||
const document = await Document.findById(newDocument.id);
|
||||
|
||||
if (ownerCollection.type === 'atlas') {
|
||||
await ownerCollection.addDocumentToStructure(document, index);
|
||||
if (publishedAt && collection.type === 'atlas') {
|
||||
await collection.addDocumentToStructure(document, index);
|
||||
}
|
||||
|
||||
document.collection = ownerCollection;
|
||||
// reload to get all of the data needed to present (user, collection etc)
|
||||
// we need to specify publishedAt to bypass default scope that only returns
|
||||
// published documents
|
||||
document = await Document.find({
|
||||
where: { id: document.id, publishedAt },
|
||||
});
|
||||
|
||||
ctx.body = {
|
||||
data: await presentDocument(ctx, document),
|
||||
@@ -245,7 +272,7 @@ router.post('documents.create', auth(), async ctx => {
|
||||
});
|
||||
|
||||
router.post('documents.update', auth(), async ctx => {
|
||||
const { id, title, text, lastRevision } = ctx.body;
|
||||
const { id, title, text, publish, lastRevision } = ctx.body;
|
||||
ctx.assertPresent(id, 'id is required');
|
||||
ctx.assertPresent(title || text, 'title or text is required');
|
||||
|
||||
@@ -259,6 +286,7 @@ router.post('documents.update', auth(), async ctx => {
|
||||
}
|
||||
|
||||
// Update document
|
||||
if (publish) document.publishedAt = new Date();
|
||||
if (title) document.title = title;
|
||||
if (text) document.text = text;
|
||||
document.lastModifiedById = user.id;
|
||||
@@ -266,7 +294,11 @@ router.post('documents.update', auth(), async ctx => {
|
||||
await document.save();
|
||||
const collection = document.collection;
|
||||
if (collection.type === 'atlas') {
|
||||
await collection.updateDocument(document);
|
||||
if (document.publishedAt) {
|
||||
await collection.updateDocument(document);
|
||||
} else if (publish) {
|
||||
await collection.addDocumentToStructure(document);
|
||||
}
|
||||
}
|
||||
|
||||
document.collection = collection;
|
||||
|
||||
@@ -10,9 +10,37 @@ const server = new TestServer(app.callback());
|
||||
beforeEach(flushdb);
|
||||
afterAll(server.close);
|
||||
|
||||
describe('#documents.info', async () => {
|
||||
it('should return published document', async () => {
|
||||
const { user, document } = await seed();
|
||||
const res = await server.post('/api/documents.info', {
|
||||
body: { token: user.getJwtToken(), id: document.id },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.id).toEqual(document.id);
|
||||
});
|
||||
|
||||
it('should return drafts', async () => {
|
||||
const { user, document } = await seed();
|
||||
document.publishedAt = null;
|
||||
await document.save();
|
||||
|
||||
const res = await server.post('/api/documents.info', {
|
||||
body: { token: user.getJwtToken(), id: document.id },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.id).toEqual(document.id);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.list', async () => {
|
||||
it('should return documents', async () => {
|
||||
const { user, document } = await seed();
|
||||
|
||||
const res = await server.post('/api/documents.list', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
@@ -23,6 +51,20 @@ describe('#documents.list', async () => {
|
||||
expect(body.data[0].id).toEqual(document.id);
|
||||
});
|
||||
|
||||
it('should not return unpublished documents', async () => {
|
||||
const { user, document } = await seed();
|
||||
document.publishedAt = null;
|
||||
await document.save();
|
||||
|
||||
const res = await server.post('/api/documents.list', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(1);
|
||||
});
|
||||
|
||||
it('should allow changing sort direction', async () => {
|
||||
const { user, document } = await seed();
|
||||
const res = await server.post('/api/documents.list', {
|
||||
@@ -54,6 +96,22 @@ describe('#documents.list', async () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.drafts', async () => {
|
||||
it('should return unpublished documents', async () => {
|
||||
const { user, document } = await seed();
|
||||
document.publishedAt = null;
|
||||
await document.save();
|
||||
|
||||
const res = await server.post('/api/documents.drafts', {
|
||||
body: { token: user.getJwtToken() },
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.length).toEqual(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.revision', async () => {
|
||||
it("should return a document's revisions", async () => {
|
||||
const { user, document } = await seed();
|
||||
@@ -263,6 +321,7 @@ describe('#documents.create', async () => {
|
||||
collection: collection.id,
|
||||
title: 'new document',
|
||||
text: 'hello',
|
||||
publish: true,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -293,7 +352,7 @@ describe('#documents.create', async () => {
|
||||
expect(body.data.text).toBe('# Untitled document');
|
||||
});
|
||||
|
||||
it('should create as a child', async () => {
|
||||
it('should create as a child and add to collection if published', async () => {
|
||||
const { user, document, collection } = await seed();
|
||||
const res = await server.post('/api/documents.create', {
|
||||
body: {
|
||||
@@ -302,6 +361,7 @@ describe('#documents.create', async () => {
|
||||
title: 'new document',
|
||||
text: 'hello',
|
||||
parentDocument: document.id,
|
||||
publish: true,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
@@ -328,6 +388,24 @@ describe('#documents.create', async () => {
|
||||
expect(res.status).toEqual(403);
|
||||
expect(body).toMatchSnapshot();
|
||||
});
|
||||
|
||||
it('should create as a child and not add to collection', async () => {
|
||||
const { user, document, collection } = await seed();
|
||||
const res = await server.post('/api/documents.create', {
|
||||
body: {
|
||||
token: user.getJwtToken(),
|
||||
collection: collection.id,
|
||||
title: 'new document',
|
||||
text: 'hello',
|
||||
parentDocument: document.id,
|
||||
},
|
||||
});
|
||||
const body = await res.json();
|
||||
|
||||
expect(res.status).toEqual(200);
|
||||
expect(body.data.title).toBe('new document');
|
||||
expect(body.data.collection.documents.length).toBe(2);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#documents.update', async () => {
|
||||
|
||||
27
server/migrations/20180115021837-add-drafts.js
Normal file
27
server/migrations/20180115021837-add-drafts.js
Normal file
@@ -0,0 +1,27 @@
|
||||
module.exports = {
|
||||
up: async (queryInterface, Sequelize) => {
|
||||
|
||||
await queryInterface.addColumn('documents', 'publishedAt', {
|
||||
type: Sequelize.DATE,
|
||||
allowNull: true,
|
||||
});
|
||||
|
||||
const [documents, metaData] = await queryInterface.sequelize.query(`SELECT * FROM documents`);
|
||||
for (const document of documents) {
|
||||
await queryInterface.sequelize.query(`
|
||||
update documents
|
||||
set "publishedAt" = '${new Date(document.createdAt).toISOString()}'
|
||||
where id = '${document.id}'
|
||||
`)
|
||||
}
|
||||
|
||||
await queryInterface.removeIndex('documents', ['id', 'atlasId']);
|
||||
await queryInterface.addIndex('documents', ['id', 'atlasId', 'publishedAt']);
|
||||
},
|
||||
|
||||
down: async (queryInterface, Sequelize) => {
|
||||
await queryInterface.removeColumn('documents', 'publishedAt');
|
||||
await queryInterface.removeIndex('documents', ['id', 'atlasId', 'publishedAt']);
|
||||
await queryInterface.addIndex('documents', ['id', 'atlasId']);
|
||||
}
|
||||
};
|
||||
@@ -58,6 +58,7 @@ const Collection = sequelize.define(
|
||||
userId: collection.creatorId,
|
||||
lastModifiedById: collection.creatorId,
|
||||
createdById: collection.creatorId,
|
||||
publishedAt: new Date(),
|
||||
title: 'Welcome to Outline',
|
||||
text: welcomeMessage(collection.id),
|
||||
});
|
||||
|
||||
@@ -234,6 +234,7 @@ describe('#removeDocument', () => {
|
||||
userId: collection.creatorId,
|
||||
lastModifiedById: collection.creatorId,
|
||||
createdById: collection.creatorId,
|
||||
publishedAt: new Date(),
|
||||
title: 'Child document',
|
||||
text: 'content',
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import _ from 'lodash';
|
||||
import randomstring from 'randomstring';
|
||||
import MarkdownSerializer from 'slate-md-serializer';
|
||||
import Plain from 'slate-plain-serializer';
|
||||
import { Op } from 'sequelize';
|
||||
|
||||
import isUUID from 'validator/lib/isUUID';
|
||||
import { DataTypes, sequelize } from '../sequelize';
|
||||
@@ -82,6 +83,7 @@ const Document = sequelize.define(
|
||||
title: DataTypes.STRING,
|
||||
text: DataTypes.TEXT,
|
||||
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0 },
|
||||
publishedAt: DataTypes.DATE,
|
||||
parentDocumentId: DataTypes.UUID,
|
||||
createdById: {
|
||||
type: DataTypes.UUID,
|
||||
@@ -145,9 +147,21 @@ Document.associate = models => {
|
||||
{ model: models.User, as: 'createdBy' },
|
||||
{ model: models.User, as: 'updatedBy' },
|
||||
],
|
||||
where: {
|
||||
publishedAt: {
|
||||
[Op.ne]: null,
|
||||
},
|
||||
},
|
||||
},
|
||||
{ override: true }
|
||||
);
|
||||
Document.addScope('withUnpublished', {
|
||||
include: [
|
||||
{ model: models.Collection, as: 'collection' },
|
||||
{ model: models.User, as: 'createdBy' },
|
||||
{ model: models.User, as: 'updatedBy' },
|
||||
],
|
||||
});
|
||||
Document.addScope('withViews', userId => ({
|
||||
include: [
|
||||
{ model: models.View, as: 'views', where: { userId }, required: false },
|
||||
@@ -161,12 +175,14 @@ Document.associate = models => {
|
||||
};
|
||||
|
||||
Document.findById = async id => {
|
||||
const scope = Document.scope('withUnpublished');
|
||||
|
||||
if (isUUID(id)) {
|
||||
return Document.findOne({
|
||||
return scope.findOne({
|
||||
where: { id },
|
||||
});
|
||||
} else if (id.match(URL_REGEX)) {
|
||||
return Document.findOne({
|
||||
return scope.findOne({
|
||||
where: {
|
||||
urlId: id.match(URL_REGEX)[1],
|
||||
},
|
||||
@@ -225,9 +241,12 @@ Document.addHook('afterDestroy', model =>
|
||||
events.add({ name: 'documents.delete', model })
|
||||
);
|
||||
|
||||
Document.addHook('afterUpdate', model =>
|
||||
events.add({ name: 'documents.update', model })
|
||||
);
|
||||
Document.addHook('afterUpdate', model => {
|
||||
if (!model.previous('publishedAt') && model.publishedAt) {
|
||||
events.add({ name: 'documents.publish', model });
|
||||
}
|
||||
events.add({ name: 'documents.update', model });
|
||||
});
|
||||
|
||||
// Instance methods
|
||||
|
||||
|
||||
@@ -258,7 +258,7 @@ export default function Pricing() {
|
||||
</Method>
|
||||
|
||||
<Method method="documents.list" label="List your documents">
|
||||
<Description>List all your documents.</Description>
|
||||
<Description>List all published documents.</Description>
|
||||
<Arguments pagination>
|
||||
<Argument
|
||||
id="collection"
|
||||
@@ -267,6 +267,10 @@ export default function Pricing() {
|
||||
</Arguments>
|
||||
</Method>
|
||||
|
||||
<Method method="documents.drafts" label="List your draft documents">
|
||||
<Description>List all your draft documents.</Description>
|
||||
</Method>
|
||||
|
||||
<Method method="documents.info" label="Get a document">
|
||||
<Description>
|
||||
<p>
|
||||
@@ -338,6 +342,15 @@ export default function Pricing() {
|
||||
</span>
|
||||
}
|
||||
/>
|
||||
<Argument
|
||||
id="publish"
|
||||
description={
|
||||
<span>
|
||||
<code>true</code> by default. Pass <code>false</code> to
|
||||
create a draft.
|
||||
</span>
|
||||
}
|
||||
/>
|
||||
</Arguments>
|
||||
</Method>
|
||||
|
||||
@@ -356,6 +369,14 @@ export default function Pricing() {
|
||||
id="text"
|
||||
description="Content of the document in Markdown"
|
||||
/>
|
||||
<Argument
|
||||
id="publish"
|
||||
description={
|
||||
<span>
|
||||
Pass <code>true</code> to publish a draft
|
||||
</span>
|
||||
}
|
||||
/>
|
||||
</Arguments>
|
||||
</Method>
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ async function present(ctx: Object, document: Document, options: ?Options) {
|
||||
createdBy: presentUser(ctx, document.createdBy),
|
||||
updatedAt: document.updatedAt,
|
||||
updatedBy: presentUser(ctx, document.updatedBy),
|
||||
publishedAt: document.publishedAt,
|
||||
firstViewedAt: undefined,
|
||||
lastViewedAt: undefined,
|
||||
team: document.teamId,
|
||||
|
||||
@@ -67,6 +67,7 @@ const seed = async () => {
|
||||
userId: collection.creatorId,
|
||||
lastModifiedById: collection.creatorId,
|
||||
createdById: collection.creatorId,
|
||||
publishedAt: new Date(),
|
||||
title: 'Second document',
|
||||
text: '# Much guidance',
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user