user.js 4.2 KB

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