user.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. let bcrypt = require('bcryptjs')
  2. let randomColor = require('randomcolor')
  3. let pagination = require('../lib/pagination.js')
  4. const Errors = require('../lib/errors.js')
  5. module.exports = (sequelize, DataTypes) => {
  6. let User = sequelize.define('User', {
  7. username: {
  8. type: DataTypes.STRING,
  9. unique: true,
  10. validate: {
  11. len: {
  12. args: [6, 50],
  13. msg: 'username must be between 6 and 50 characters'
  14. },
  15. isString (val) {
  16. if(typeof val !== 'string') {
  17. throw new sequelize.ValidationError('username must be a string')
  18. }
  19. }
  20. }
  21. },
  22. description: {
  23. type: DataTypes.TEXT,
  24. validate: {
  25. isString (val) {
  26. if(typeof val !== 'string') {
  27. throw new sequelize.ValidationError('description must be a string')
  28. }
  29. },
  30. len: {
  31. args: [0, 1024],
  32. msg: 'description must be less than 1024 characters'
  33. }
  34. }
  35. },
  36. color: {
  37. type: DataTypes.STRING,
  38. defaultValue () {
  39. return randomColor()
  40. }
  41. },
  42. hash: {
  43. type: DataTypes.STRING,
  44. allowNull: false,
  45. validate: {
  46. len: {
  47. args: [6, 100],
  48. msg: 'password must be between 6 and 100 characters'
  49. },
  50. isString (val) {
  51. if(typeof val !== 'string') {
  52. throw new sequelize.ValidationError('password must be a string')
  53. }
  54. }
  55. }
  56. },
  57. admin: {
  58. type: DataTypes.BOOLEAN,
  59. defaultValue: false
  60. }
  61. }, {
  62. instanceMethods: {
  63. async updatePassword (currentPassword, newPassword) {
  64. if(currentPassword === newPassword) {
  65. throw Errors.passwordSame
  66. } else if(typeof currentPassword !== 'string' || typeof newPassword !== 'string') {
  67. throw new sequelize.ValidationError('password must be a string')
  68. }
  69. let correctPassword = await bcrypt.compare(currentPassword, this.hash)
  70. if(correctPassword) {
  71. await this.update({ hash: newPassword })
  72. } else {
  73. throw Errors.invalidLoginCredentials
  74. }
  75. },
  76. async comparePassword (password) {
  77. return await bcrypt.compare(password, this.hash)
  78. },
  79. async getMeta (limit) {
  80. let Post = sequelize.models.Post
  81. let meta = {}
  82. let nextId = await pagination.getNextIdDesc(Post, { userId: this.id }, this.Posts)
  83. if(nextId === null) {
  84. meta.nextURL = null
  85. meta.nextPostsCount = 0
  86. } else {
  87. meta.nextURL =
  88. `/api/v1/user/${user.username}?posts=true&limit=${limit}&from=${nextId - 1}`
  89. meta.nextPostsCount = await pagination.getNextCount(
  90. Post, this.Posts, limit,
  91. { UserId: this.id },
  92. true
  93. )
  94. }
  95. return meta
  96. }
  97. },
  98. classMethods: {
  99. associate (models) {
  100. User.hasMany(models.Post)
  101. User.hasMany(models.Thread)
  102. },
  103. includeOptions (from, limit) {
  104. let models = sequelize.models
  105. let options = models.Post.includeOptions()
  106. return [{
  107. model: models.Post,
  108. include: options,
  109. limit,
  110. where: { postNumber: { $gte: from } },
  111. order: [['id', 'ASC']]
  112. }]
  113. },
  114. async canBeAdmin (token) {
  115. let { User, AdminToken } = sequelize.models
  116. let adminUser = await User.findOne({ where: {
  117. admin: true
  118. }})
  119. if(adminUser) {
  120. if(token) {
  121. let adminToken = await AdminToken.findOne({ where: { token } })
  122. if(adminToken && adminToken.isValid()) {
  123. await adminToken.destroy()
  124. return true
  125. } else {
  126. throw Errors.invalidToken
  127. }
  128. } else {
  129. throw Errors.missingParameter('token')
  130. }
  131. } else {
  132. return true
  133. }
  134. }
  135. },
  136. hooks: {
  137. async afterValidate(user, options) {
  138. if(user.changed('hash') && user.hash.length <= 50) {
  139. user.hash = await bcrypt.hash(user.hash, 12)
  140. }
  141. options.hooks = false
  142. return options
  143. }
  144. }
  145. })
  146. return User
  147. }