Move title validation to server (#552)

closes issue-546
This commit is contained in:
Tom Moor
2018-01-31 19:23:33 -08:00
committed by Jori Lallo
parent c24ef20a24
commit ae4aed6fe0
4 changed files with 54 additions and 17 deletions

View File

@ -13,6 +13,7 @@ import Revision from './Revision';
const Markdown = new MarkdownSerializer();
const URL_REGEX = /^[a-zA-Z0-9-]*-([a-zA-Z0-9]{10,15})$/;
const DEFAULT_TITLE = 'Untitled document';
// $FlowIssue invalid flow-typed
slug.defaults.mode = 'rfc3986';
@ -36,14 +37,19 @@ const createUrlId = doc => {
};
const beforeSave = async doc => {
const { emoji } = parseTitle(doc.text);
const { emoji, title } = parseTitle(doc.text);
// emoji in the title is split out for easier display
doc.emoji = emoji;
doc.revisionCount += 1;
// Collaborators
// ensure document has a title
if (!title) {
doc.title = DEFAULT_TITLE;
doc.text = doc.text.replace(/^.*$/m, `# ${DEFAULT_TITLE}`);
}
// calculate collaborators
let ids = [];
// Only get previous user IDs if the document already exists
if (doc.id) {
ids = await Revision.findAll({
attributes: [[DataTypes.literal('DISTINCT "userId"'), 'userId']],
@ -52,10 +58,14 @@ const beforeSave = async doc => {
},
}).map(rev => rev.userId);
}
// We'll add the current user as revision hasn't been generated yet
// add the current user as revision hasn't been generated yet
ids.push(doc.lastModifiedById);
doc.collaboratorIds = _.uniq(ids);
// increment revision
doc.revisionCount += 1;
return doc;
};