Skip to content
This repository has been archived by the owner on Oct 1, 2019. It is now read-only.

Commit

Permalink
[#790] Add UserPost model
Browse files Browse the repository at this point in the history
  • Loading branch information
voidxnull committed Feb 14, 2017
1 parent 2f3ce67 commit bb519cc
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/api/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { promisify, promisifyAll } from 'bluebird';
import { hash as bcryptHash } from 'bcrypt';
import { break as breakGraphemes } from 'grapheme-breaker';
import { OnigRegExp } from 'oniguruma';
import Checkit from 'checkit';

import { uploadAttachment, downloadAttachment, generateName } from '../../utils/attachments';

Expand All @@ -50,6 +51,9 @@ export function initBookshelfFromKnex(knex) {
posts() {
return this.hasMany(Post, 'user_id');
},
user_posts() {
return this.hasMany(UserPost, 'user_id');
},
following() {
return this.belongsToMany(User, 'followers', 'user_id', 'following_user_id');
},
Expand Down Expand Up @@ -616,6 +620,36 @@ export function initBookshelfFromKnex(knex) {
}
});

const UserPost = bookshelf.Model.extend({
tableName: 'user_posts',
validations: {
text: ['required', 'string'],
html: ['string'],
type: [
'required',
val => {
if (!UserPost.TYPES.includes(val)) {
throw new Error('Invalid post type');
}
}
],
user_id: ['required', 'uuid']
},
initialize() {
this.on('saving', this.validate.bind(this));
},
validate() {
return new Checkit(this.validations).run(this.toJSON());
},
user() {
return this.belongsTo(User, 'user_id');
}
});

UserPost.TYPES = [
'text'
];

const Posts = bookshelf.Collection.extend({
model: Post
});
Expand All @@ -633,6 +667,7 @@ export function initBookshelfFromKnex(knex) {
bookshelf.model('Comment', Comment);
bookshelf.model('Quote', Quote);
bookshelf.model('UserMessage', UserMessage);
bookshelf.model('UserPost', UserPost);
bookshelf.collection('Posts', Posts);

return bookshelf;
Expand Down

0 comments on commit bb519cc

Please sign in to comment.