|
@@ -7,14 +7,24 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
set (val) {
|
|
|
this.setDataValue('content', marked(val))
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
+ replyingToUsername: DataTypes.STRING
|
|
|
}, {
|
|
|
+ instanceMethods: {
|
|
|
+ getReplyingTo () {
|
|
|
+ return Post.findByPrimary(this.replyId)
|
|
|
+ },
|
|
|
+ setReplyingTo (post) {
|
|
|
+ return post.getUser().then(user => {
|
|
|
+ return this.update({ replyingToUsername: user.username, replyId: post.id })
|
|
|
+ })
|
|
|
+ }
|
|
|
+ },
|
|
|
classMethods: {
|
|
|
associate (models) {
|
|
|
Post.belongsTo(models.User)
|
|
|
Post.belongsTo(models.Thread)
|
|
|
- Post.hasMany(models.Post, { as: 'Replies' })
|
|
|
- Post.hasOne(models.Post, { as: 'ReplyingTo', foreignKey: 'ReplyId' })
|
|
|
+ Post.hasMany(models.Post, { as: 'Replies', foreignKey: 'replyId' })
|
|
|
},
|
|
|
includeOptions () {
|
|
|
let models = sequelize.models
|
|
@@ -23,9 +33,6 @@ module.exports = (sequelize, DataTypes) => {
|
|
|
{ model: models.User, attributes: ['username', 'createdAt', 'id'] },
|
|
|
{ model: models.Thread, include: [models.Category]} ,
|
|
|
{
|
|
|
- model: models.Post, as: 'ReplyingTo', include:
|
|
|
- [{ model: models.User, attributes: ['username', 'id'] }]
|
|
|
- }, {
|
|
|
model: models.Post, as: 'Replies', include:
|
|
|
[{ model: models.User, attributes: ['username', 'id'] }]
|
|
|
}
|