[chore] added prettier

This commit is contained in:
Jori Lallo
2017-04-26 21:47:03 -07:00
parent fcdeb67bc5
commit 08b1609440
53 changed files with 1983 additions and 928 deletions

View File

@@ -1,28 +1,33 @@
import {
DataTypes,
sequelize,
} from '../sequelize';
import { DataTypes, sequelize } from '../sequelize';
import randomstring from 'randomstring';
const Team = sequelize.define('team', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
name: DataTypes.STRING,
secret: { type: DataTypes.STRING, unique: true },
userId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
const Team = sequelize.define(
'team',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: DataTypes.STRING,
secret: { type: DataTypes.STRING, unique: true },
userId: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
},
},
},
}, {
tableName: 'apiKeys',
paranoid: true,
hooks: {
beforeValidate: (key) => {
key.secret = randomstring.generate(38);
{
tableName: 'apiKeys',
paranoid: true,
hooks: {
beforeValidate: key => {
key.secret = randomstring.generate(38);
},
},
},
});
}
);
export default Team;

View File

@@ -1,9 +1,6 @@
import slug from 'slug';
import randomstring from 'randomstring';
import {
DataTypes,
sequelize,
} from '../sequelize';
import { DataTypes, sequelize } from '../sequelize';
import _ from 'lodash';
import Document from './Document';
@@ -11,184 +8,207 @@ slug.defaults.mode = 'rfc3986';
const allowedAtlasTypes = [['atlas', 'journal']];
const Atlas = sequelize.define('atlas', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
urlId: { type: DataTypes.STRING, unique: true },
name: DataTypes.STRING,
description: DataTypes.STRING,
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes } },
creatorId: DataTypes.UUID,
/* type: atlas */
navigationTree: DataTypes.JSONB,
}, {
tableName: 'atlases',
paranoid: true,
hooks: {
beforeValidate: (collection) => {
collection.urlId = collection.urlId || randomstring.generate(10);
const Atlas = sequelize.define(
'atlas',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
afterCreate: async (collection) => {
if (collection.type !== 'atlas') return;
urlId: { type: DataTypes.STRING, unique: true },
name: DataTypes.STRING,
description: DataTypes.STRING,
type: { type: DataTypes.STRING, validate: { isIn: allowedAtlasTypes } },
creatorId: DataTypes.UUID,
await Document.create({
parentDocumentId: null,
atlasId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
createdById: collection.creatorId,
title: 'Introduction',
text: '# Introduction\n\nLets get started...',
});
await collection.buildStructure();
await collection.save();
},
/* type: atlas */
navigationTree: DataTypes.JSONB,
},
instanceMethods: {
getUrl() {
// const slugifiedName = slug(this.name);
// return `/${slugifiedName}-c${this.urlId}`;
return `/collections/${this.id}`;
},
async buildStructure() {
if (this.navigationTree) return this.navigationTree;
{
tableName: 'atlases',
paranoid: true,
hooks: {
beforeValidate: collection => {
collection.urlId = collection.urlId || randomstring.generate(10);
},
afterCreate: async collection => {
if (collection.type !== 'atlas') return;
const getNodeForDocument = async (document) => {
const children = await Document.findAll({ where: {
parentDocumentId: document.id,
atlasId: this.id,
} });
const childNodes = [];
await Promise.all(children.map(async (child) => {
return childNodes.push(await getNodeForDocument(child));
}));
return {
title: document.title,
id: document.id,
url: document.getUrl(),
children: childNodes,
};
};
const rootDocument = await Document.findOne({
where: {
await Document.create({
parentDocumentId: null,
atlasId: this.id,
},
});
this.navigationTree = await getNodeForDocument(rootDocument);
return this.navigationTree;
atlasId: collection.id,
teamId: collection.teamId,
userId: collection.creatorId,
lastModifiedById: collection.creatorId,
createdById: collection.creatorId,
title: 'Introduction',
text: '# Introduction\n\nLets get started...',
});
await collection.buildStructure();
await collection.save();
},
},
async updateNavigationTree(tree = this.navigationTree) {
const nodeIds = [];
nodeIds.push(tree.id);
instanceMethods: {
getUrl() {
// const slugifiedName = slug(this.name);
// return `/${slugifiedName}-c${this.urlId}`;
return `/collections/${this.id}`;
},
async buildStructure() {
if (this.navigationTree) return this.navigationTree;
const rootDocument = await Document.findOne({
where: {
id: tree.id,
atlasId: this.id,
},
});
if (!rootDocument) throw new Error;
const newTree = {
id: tree.id,
title: rootDocument.title,
url: rootDocument.getUrl(),
children: [],
};
const getIdsForChildren = async (children) => {
const childNodes = [];
for (const child of children) {
const childDocument = await Document.findOne({
const getNodeForDocument = async document => {
const children = await Document.findAll({
where: {
id: child.id,
parentDocumentId: document.id,
atlasId: this.id,
},
});
if (childDocument) {
childNodes.push({
id: childDocument.id,
title: childDocument.title,
url: childDocument.getUrl(),
children: await getIdsForChildren(child.children),
});
nodeIds.push(child.id);
}
}
return childNodes;
};
newTree.children = await getIdsForChildren(tree.children);
const documents = await Document.findAll({
attributes: ['id'],
where: {
atlasId: this.id,
},
});
const documentIds = documents.map(doc => doc.id);
const childNodes = [];
await Promise.all(
children.map(async child => {
return childNodes.push(await getNodeForDocument(child));
})
);
if (!_.isEqual(nodeIds.sort(), documentIds.sort())) {
throw new Error('Invalid navigation tree');
}
return {
title: document.title,
id: document.id,
url: document.getUrl(),
children: childNodes,
};
};
this.navigationTree = newTree;
await this.save();
return newTree;
},
async addNodeToNavigationTree(document) {
const newNode = {
id: document.id,
title: document.title,
url: document.getUrl(),
children: [],
};
const insertNode = (node) => {
if (document.parentDocumentId === node.id) {
node.children.push(newNode);
} else {
node.children = node.children.map(childNode => {
return insertNode(childNode);
});
}
return node;
};
this.navigationTree = insertNode(this.navigationTree);
return this.navigationTree;
},
async deleteDocument(document) {
const deleteNodeAndDocument = async (node, documentId, shouldDelete = false) => {
// Delete node if id matches
if (document.id === node.id) shouldDelete = true;
const newChildren = [];
node.children.forEach(async childNode => {
const child = await deleteNodeAndDocument(childNode, documentId, shouldDelete);
if (child) newChildren.push(child);
const rootDocument = await Document.findOne({
where: {
parentDocumentId: null,
atlasId: this.id,
},
});
node.children = newChildren;
if (shouldDelete) {
const doc = await Document.findById(node.id);
await doc.destroy();
this.navigationTree = await getNodeForDocument(rootDocument);
return this.navigationTree;
},
async updateNavigationTree(tree = this.navigationTree) {
const nodeIds = [];
nodeIds.push(tree.id);
const rootDocument = await Document.findOne({
where: {
id: tree.id,
atlasId: this.id,
},
});
if (!rootDocument) throw new Error();
const newTree = {
id: tree.id,
title: rootDocument.title,
url: rootDocument.getUrl(),
children: [],
};
const getIdsForChildren = async children => {
const childNodes = [];
for (const child of children) {
const childDocument = await Document.findOne({
where: {
id: child.id,
atlasId: this.id,
},
});
if (childDocument) {
childNodes.push({
id: childDocument.id,
title: childDocument.title,
url: childDocument.getUrl(),
children: await getIdsForChildren(child.children),
});
nodeIds.push(child.id);
}
}
return childNodes;
};
newTree.children = await getIdsForChildren(tree.children);
const documents = await Document.findAll({
attributes: ['id'],
where: {
atlasId: this.id,
},
});
const documentIds = documents.map(doc => doc.id);
if (!_.isEqual(nodeIds.sort(), documentIds.sort())) {
throw new Error('Invalid navigation tree');
}
return shouldDelete ? null : node;
};
this.navigationTree = newTree;
await this.save();
this.navigationTree = await deleteNodeAndDocument(this.navigationTree, document.id);
return newTree;
},
async addNodeToNavigationTree(document) {
const newNode = {
id: document.id,
title: document.title,
url: document.getUrl(),
children: [],
};
const insertNode = node => {
if (document.parentDocumentId === node.id) {
node.children.push(newNode);
} else {
node.children = node.children.map(childNode => {
return insertNode(childNode);
});
}
return node;
};
this.navigationTree = insertNode(this.navigationTree);
return this.navigationTree;
},
async deleteDocument(document) {
const deleteNodeAndDocument = async (
node,
documentId,
shouldDelete = false
) => {
// Delete node if id matches
if (document.id === node.id) shouldDelete = true;
const newChildren = [];
node.children.forEach(async childNode => {
const child = await deleteNodeAndDocument(
childNode,
documentId,
shouldDelete
);
if (child) newChildren.push(child);
});
node.children = newChildren;
if (shouldDelete) {
const doc = await Document.findById(node.id);
await doc.destroy();
}
return shouldDelete ? null : node;
};
this.navigationTree = await deleteNodeAndDocument(
this.navigationTree,
document.id
);
},
},
},
});
}
);
Atlas.hasMany(Document, { as: 'documents', foreignKey: 'atlasId' });

View File

@@ -1,22 +1,15 @@
import slug from 'slug';
import _ from 'lodash';
import randomstring from 'randomstring';
import {
DataTypes,
sequelize,
} from '../sequelize';
import {
convertToMarkdown,
} from '../../frontend/utils/markdown';
import {
truncateMarkdown,
} from '../utils/truncate';
import { DataTypes, sequelize } from '../sequelize';
import { convertToMarkdown } from '../../frontend/utils/markdown';
import { truncateMarkdown } from '../utils/truncate';
import User from './User';
import Revision from './Revision';
slug.defaults.mode = 'rfc3986';
const createRevision = async (doc) => {
const createRevision = async doc => {
// Create revision of the current (latest)
await Revision.create({
title: doc.title,
@@ -28,7 +21,7 @@ const createRevision = async (doc) => {
});
};
const documentBeforeSave = async (doc) => {
const documentBeforeSave = async doc => {
doc.html = convertToMarkdown(doc.text);
doc.preview = truncateMarkdown(doc.text, 160);
@@ -52,50 +45,58 @@ const documentBeforeSave = async (doc) => {
return doc;
};
const Document = sequelize.define('document', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
urlId: { type: DataTypes.STRING, primaryKey: true },
private: { type: DataTypes.BOOLEAN, defaultValue: true },
title: DataTypes.STRING,
text: DataTypes.TEXT,
html: DataTypes.TEXT,
preview: DataTypes.TEXT,
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0 },
const Document = sequelize.define(
'document',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
urlId: { type: DataTypes.STRING, primaryKey: true },
private: { type: DataTypes.BOOLEAN, defaultValue: true },
title: DataTypes.STRING,
text: DataTypes.TEXT,
html: DataTypes.TEXT,
preview: DataTypes.TEXT,
revisionCount: { type: DataTypes.INTEGER, defaultValue: 0 },
parentDocumentId: DataTypes.UUID,
createdById: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
parentDocumentId: DataTypes.UUID,
createdById: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
},
},
},
lastModifiedById: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
lastModifiedById: {
type: DataTypes.UUID,
allowNull: false,
references: {
model: 'users',
},
},
collaboratorIds: DataTypes.ARRAY(DataTypes.UUID),
},
collaboratorIds: DataTypes.ARRAY(DataTypes.UUID),
}, {
paranoid: true,
hooks: {
beforeValidate: (doc) => {
doc.urlId = doc.urlId || randomstring.generate(10);
{
paranoid: true,
hooks: {
beforeValidate: doc => {
doc.urlId = doc.urlId || randomstring.generate(10);
},
beforeCreate: documentBeforeSave,
beforeUpdate: documentBeforeSave,
afterCreate: async doc => await createRevision(doc),
afterUpdate: async doc => await createRevision(doc),
},
beforeCreate: documentBeforeSave,
beforeUpdate: documentBeforeSave,
afterCreate: async (doc) => await createRevision(doc),
afterUpdate: async (doc) => await createRevision(doc),
},
instanceMethods: {
getUrl() {
const slugifiedTitle = slug(this.title);
return `/d/${slugifiedTitle}-${this.urlId}`;
instanceMethods: {
getUrl() {
const slugifiedTitle = slug(this.title);
return `/d/${slugifiedTitle}-${this.urlId}`;
},
},
},
});
}
);
Document.belongsTo(User);
@@ -112,18 +113,14 @@ Document.searchForUser = async (user, query, options = {}) => {
LIMIT :limit OFFSET :offset;
`;
const documents = await sequelize
.query(
sql,
{
replacements: {
query,
limit,
offset,
},
model: Document,
}
);
const documents = await sequelize.query(sql, {
replacements: {
query,
limit,
offset,
},
model: Document,
});
return documents;
};

View File

@@ -1,10 +1,11 @@
import {
DataTypes,
sequelize,
} from '../sequelize';
import { DataTypes, sequelize } from '../sequelize';
const Revision = sequelize.define('revision', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
title: DataTypes.STRING,
text: DataTypes.TEXT,
html: DataTypes.TEXT,
@@ -15,7 +16,7 @@ const Revision = sequelize.define('revision', {
allowNull: false,
references: {
model: 'users',
}
},
},
documentId: {
@@ -24,7 +25,7 @@ const Revision = sequelize.define('revision', {
references: {
model: 'documents',
onDelete: 'CASCADE',
}
},
},
});

View File

@@ -1,36 +1,41 @@
import {
DataTypes,
sequelize,
} from '../sequelize';
import { DataTypes, sequelize } from '../sequelize';
import Atlas from './Atlas';
import Document from './Document';
import User from './User';
const Team = sequelize.define('team', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
name: DataTypes.STRING,
slackId: { type: DataTypes.STRING, allowNull: true },
slackData: DataTypes.JSONB,
}, {
instanceMethods: {
async createFirstAtlas(userId) {
const atlas = await Atlas.create({
name: this.name,
description: 'Your first Atlas',
type: 'atlas',
teamId: this.id,
creatorId: userId,
});
return atlas;
const Team = sequelize.define(
'team',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
name: DataTypes.STRING,
slackId: { type: DataTypes.STRING, allowNull: true },
slackData: DataTypes.JSONB,
},
indexes: [
{
unique: true,
fields: ['slackId'],
{
instanceMethods: {
async createFirstAtlas(userId) {
const atlas = await Atlas.create({
name: this.name,
description: 'Your first Atlas',
type: 'atlas',
teamId: this.id,
creatorId: userId,
});
return atlas;
},
},
],
});
indexes: [
{
unique: true,
fields: ['slackId'],
},
],
}
);
Team.hasMany(Atlas, { as: 'atlases' });
Team.hasMany(Document, { as: 'documents' });

View File

@@ -1,61 +1,65 @@
import crypto from 'crypto';
import bcrypt from 'bcrypt';
import {
DataTypes,
sequelize,
encryptedFields,
} from '../sequelize';
import { DataTypes, sequelize, encryptedFields } from '../sequelize';
import JWT from 'jsonwebtoken';
const BCRYPT_COST = process.env.NODE_ENV !== 'production' ? 4 : 12;
const User = sequelize.define('user', {
id: { type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4, primaryKey: true },
email: { type: DataTypes.STRING, unique: true },
username: { type: DataTypes.STRING, unique: true },
name: DataTypes.STRING,
password: DataTypes.VIRTUAL,
passwordDigest: DataTypes.STRING,
isAdmin: DataTypes.BOOLEAN,
slackAccessToken: encryptedFields.vault('slackAccessToken'),
slackId: { type: DataTypes.STRING, allowNull: true },
slackData: DataTypes.JSONB,
jwtSecret: encryptedFields.vault('jwtSecret'),
}, {
instanceMethods: {
getJwtToken() {
return JWT.sign({ id: this.id }, this.jwtSecret);
const User = sequelize.define(
'user',
{
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
primaryKey: true,
},
async getTeam() {
return this.team;
},
verifyPassword(password) {
return new Promise((resolve, reject) => {
if (!this.passwordDigest) {
resolve(false);
return;
}
bcrypt.compare(password, this.passwordDigest, (err, ok) => {
if (err) {
reject(err);
email: { type: DataTypes.STRING, unique: true },
username: { type: DataTypes.STRING, unique: true },
name: DataTypes.STRING,
password: DataTypes.VIRTUAL,
passwordDigest: DataTypes.STRING,
isAdmin: DataTypes.BOOLEAN,
slackAccessToken: encryptedFields.vault('slackAccessToken'),
slackId: { type: DataTypes.STRING, allowNull: true },
slackData: DataTypes.JSONB,
jwtSecret: encryptedFields.vault('jwtSecret'),
},
{
instanceMethods: {
getJwtToken() {
return JWT.sign({ id: this.id }, this.jwtSecret);
},
async getTeam() {
return this.team;
},
verifyPassword(password) {
return new Promise((resolve, reject) => {
if (!this.passwordDigest) {
resolve(false);
return;
}
resolve(ok);
});
});
},
},
indexes: [
{
fields: ['email'],
},
],
});
bcrypt.compare(password, this.passwordDigest, (err, ok) => {
if (err) {
reject(err);
return;
}
const setRandomJwtSecret = (model) => {
resolve(ok);
});
});
},
},
indexes: [
{
fields: ['email'],
},
],
}
);
const setRandomJwtSecret = model => {
model.jwtSecret = crypto.randomBytes(64).toString('hex');
};
const hashPassword = function hashPassword(model) {

View File

@@ -5,11 +5,4 @@ import Document from './Document';
import Revision from './Revision';
import ApiKey from './ApiKey';
export {
User,
Team,
Atlas,
Document,
Revision,
ApiKey,
};
export { User, Team, Atlas, Document, Revision, ApiKey };