user.js 4.0 KB

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