user.js 4.1 KB

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