user.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. canCreatePosts: {
  58. type: DataTypes.BOOLEAN,
  59. defaultValue: true
  60. },
  61. canCreateThreads: {
  62. type: DataTypes.BOOLEAN,
  63. defaultValue: true
  64. },
  65. admin: {
  66. type: DataTypes.BOOLEAN,
  67. defaultValue: false
  68. }
  69. }, {
  70. instanceMethods: {
  71. async updatePassword (currentPassword, newPassword) {
  72. if(currentPassword === newPassword) {
  73. throw Errors.passwordSame
  74. } else if(typeof currentPassword !== 'string' || typeof newPassword !== 'string') {
  75. throw new sequelize.ValidationError('password must be a string')
  76. }
  77. let correctPassword = await bcrypt.compare(currentPassword, this.hash)
  78. if(correctPassword) {
  79. await this.update({ hash: newPassword })
  80. } else {
  81. throw Errors.invalidLoginCredentials
  82. }
  83. },
  84. async comparePassword (password) {
  85. return await bcrypt.compare(password, this.hash)
  86. },
  87. async getMeta (limit) {
  88. let Post = sequelize.models.Post
  89. let meta = {}
  90. let nextId = await pagination.getNextIdDesc(Post, { userId: this.id }, this.Posts)
  91. if(nextId === null) {
  92. meta.nextURL = null
  93. meta.nextPostsCount = 0
  94. } else {
  95. meta.nextURL =
  96. `/api/v1/user/${user.username}?posts=true&limit=${limit}&from=${nextId - 1}`
  97. meta.nextPostsCount = await pagination.getNextCount(
  98. Post, this.Posts, limit,
  99. { UserId: this.id },
  100. true
  101. )
  102. }
  103. return meta
  104. }
  105. },
  106. classMethods: {
  107. associate (models) {
  108. User.hasMany(models.Post)
  109. User.hasMany(models.Thread)
  110. },
  111. includeOptions (from, limit) {
  112. let models = sequelize.models
  113. let options = models.Post.includeOptions()
  114. return [{
  115. model: models.Post,
  116. include: options,
  117. limit,
  118. where: { postNumber: { $gte: from } },
  119. order: [['id', 'ASC']]
  120. }]
  121. },
  122. async canBeAdmin (token) {
  123. let { User, AdminToken } = sequelize.models
  124. let adminUser = await User.findOne({ where: {
  125. admin: true
  126. }})
  127. if(adminUser) {
  128. if(token) {
  129. let adminToken = await AdminToken.findOne({ where: { token } })
  130. if(adminToken && adminToken.isValid()) {
  131. await adminToken.destroy()
  132. return true
  133. } else {
  134. throw Errors.invalidToken
  135. }
  136. } else {
  137. throw Errors.missingParameter('token')
  138. }
  139. } else {
  140. return true
  141. }
  142. }
  143. },
  144. hooks: {
  145. async afterValidate(user, options) {
  146. if(user.changed('hash') && user.hash.length <= 50) {
  147. user.hash = await bcrypt.hash(user.hash, 12)
  148. }
  149. options.hooks = false
  150. return options
  151. }
  152. }
  153. })
  154. return User
  155. }