models.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='graphql-chat') => {
  4. const {Savable, slice} = await connect(dbName)
  5. async function getModels({id, acl}, onMsgSave){
  6. const SlicedSavable = slice(id === 'anon' ? [id] : [...acl])
  7. class User extends SlicedSavable {
  8. constructor(...params){
  9. super(...params)
  10. this.originalACL = this.acl && [...this.acl]
  11. }
  12. async save(...params){
  13. console.log(this)
  14. let otherUserWithThisLogin = this.login && await Savable.m.User.findOne({login: this.login})
  15. if (this._id){
  16. if (otherUserWithThisLogin && otherUserWithThisLogin._id.toString() !== this._id.toString()){
  17. throw new ReferenceError(`User ${this.login} already exists`)
  18. }
  19. if (!acl.includes('admin') && this.originalACL.toString() !== this.acl.toString())
  20. throw new ReferenceError(`Not enough permissions for changing acl on ${this.login}`)
  21. else return await super.save(...params)
  22. }
  23. else {
  24. if (otherUserWithThisLogin){
  25. throw new ReferenceError(`User ${this.login} already exists`)
  26. }
  27. await super.save(...params)
  28. this.___owner = this._id.toString()
  29. this.acl = [this.___owner, "user"]
  30. return await Savable.prototype.save.call(this, ...params)
  31. }
  32. }
  33. static get relations(){ //don't needed due to ___owner in most cases
  34. return {
  35. avatar : "userAvatar",
  36. chats: ["members"]
  37. }
  38. }
  39. static get defaultPermissions(){
  40. return {
  41. create: ['anon'],
  42. read: ['owner', 'user', 'admin'],
  43. write: ['owner', 'admin'],
  44. delete: []
  45. }
  46. }
  47. static get guestRelations(){
  48. return ["chats"];
  49. }
  50. }
  51. SlicedSavable.addClass(User)
  52. class OwnerSlicedSavable extends SlicedSavable {
  53. get owner(){
  54. if (!this.___owner) return this.___owner
  55. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  56. }
  57. }
  58. class Media extends OwnerSlicedSavable {
  59. constructor(...params){
  60. super(...params)
  61. }
  62. static async fromFileData(fileData){
  63. let media = new Media({})
  64. media.fileData = fileData
  65. media.url = `images/${fileData.filename}`
  66. media.originalFileName = fileData.originalname
  67. media.type = fileData.mimetype
  68. await media.save()
  69. return media;
  70. }
  71. //async save(...params){
  72. //if (this.userAvatar){
  73. //if (this.userAvatar._id.toString() !== id){
  74. //throw new ReferenceError(`You can't set ava for other user`)
  75. //}
  76. //}
  77. //return await super.save(...params)
  78. //}
  79. static get relations(){
  80. return {
  81. userAvatar: "avatar", //if it is ava
  82. chatAvatars: "avatar",
  83. messages: ["media"]
  84. }
  85. }
  86. static get defaultPermissions(){
  87. return {
  88. create: ['user', 'admin'],
  89. read: ['anon', 'user', 'admin'],
  90. write: ["owner", 'admin'],
  91. delete: ['admin']
  92. }
  93. }
  94. }
  95. SlicedSavable.addClass(Media)
  96. class Message extends OwnerSlicedSavable {
  97. async save(...params){
  98. if (!this.chat){
  99. throw new ReferenceError("You should set chat")
  100. }
  101. const chatId = this.chat._id.toString()
  102. this.___permissions.read.push(chatId)
  103. this.___permissions.read = [...new Set(this.___permissions.read)]
  104. let result = await super.save(...params)
  105. onMsgSave(this)
  106. return result
  107. }
  108. static get relations(){
  109. return {
  110. media: ['messages'],
  111. replyTo: ['replies'],
  112. replies: 'replyTo',
  113. forwardWith: 'forwarded',
  114. forwarded: ['forwardWith'],
  115. chat: ['messages']
  116. }
  117. }
  118. static get defaultPermissions(){
  119. return {
  120. create: ['user', 'admin'],
  121. read: ['user', 'owner', 'admin'],
  122. write: ['owner','admin'],
  123. delete: ['admin']
  124. }
  125. }
  126. static get guestRelations(){
  127. return ['replies']
  128. return ['forwardWith']
  129. }
  130. }
  131. SlicedSavable.addClass(Message)
  132. class Chat extends OwnerSlicedSavable {
  133. constructor(...params){
  134. super(...params)
  135. this.members = Savable.arrize(this.members)
  136. }
  137. async save(...params){
  138. if (!this._id){
  139. await super.save(...params)
  140. this.___permissions.read.push(this._id)
  141. }
  142. return await super.save(...params)
  143. }
  144. static get relations(){
  145. return {
  146. members: ['chats'],
  147. messages: 'chat',
  148. avatar: ["chatAvatars"]
  149. }
  150. }
  151. static get defaultPermissions(){
  152. return {
  153. create: ['user'],
  154. read: ['owner', 'admin', 'user'],
  155. write: ['owner'],
  156. delete: []
  157. }
  158. }
  159. static get guestRelations(){
  160. return ['messages']
  161. }
  162. }
  163. SlicedSavable.addClass(Chat)
  164. const thisUser = id !== 'anon' && await Savable.m.User.findOne({_id: ObjectID(id)})
  165. return {models: {
  166. SlicedSavable, ...SlicedSavable.classes
  167. },
  168. thisUser}
  169. }
  170. return {
  171. Savable,
  172. slice,
  173. getModels
  174. }
  175. }