user.js 3.9 KB

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