post.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. let marked = require('marked')
  2. marked.setOptions({
  3. highlight: function (code) {
  4. return require('highlight.js').highlightAuto(code).value;
  5. },
  6. sanitize: true
  7. });
  8. module.exports = (sequelize, DataTypes) => {
  9. let Post = sequelize.define('Post', {
  10. content: {
  11. type: DataTypes.TEXT,
  12. set (val) {
  13. this.setDataValue('content', marked(val))
  14. }
  15. },
  16. replyingToUsername: DataTypes.STRING
  17. }, {
  18. instanceMethods: {
  19. getReplyingTo () {
  20. return Post.findByPrimary(this.replyId)
  21. },
  22. setReplyingTo (post) {
  23. return post.getUser().then(user => {
  24. return this.update({ replyingToUsername: user.username, replyId: post.id })
  25. })
  26. }
  27. },
  28. classMethods: {
  29. associate (models) {
  30. Post.belongsTo(models.User)
  31. Post.belongsTo(models.Thread)
  32. Post.hasMany(models.Post, { as: 'Replies', foreignKey: 'replyId' })
  33. },
  34. includeOptions () {
  35. let models = sequelize.models
  36. return [
  37. { model: models.User, attributes: ['username', 'createdAt', 'id', 'color'] },
  38. { model: models.Thread, include: [models.Category]} ,
  39. {
  40. model: models.Post, as: 'Replies', include:
  41. [{ model: models.User, attributes: ['username', 'id', 'color'] }]
  42. }
  43. ]
  44. }
  45. }
  46. })
  47. return Post
  48. }