models.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const ObjectID = require("mongodb").ObjectID;
  2. const {connect} = require('mm')
  3. module.exports = async (dbName='player') => {
  4. const {Savable, slice} = await connect(dbName)
  5. async function getModels({id}){
  6. const SlicedSavable = slice([id, 'user'])
  7. class User extends SlicedSavable {
  8. constructor(...params){
  9. super(...params)
  10. //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
  11. //cached like count, which incremented and decremented
  12. //
  13. //following and followers array
  14. }
  15. static get relations(){ //don't needed due to ___owner in most cases
  16. return {
  17. avatar : "userAvatar",
  18. }
  19. }
  20. async getACL(){
  21. return [this._id.toString(), "user"]
  22. }
  23. }
  24. SlicedSavable.addClass(User)
  25. class OwnerSlicedSavable extends SlicedSavable {
  26. get owner(){
  27. if (!this.___owner) return this.___owner
  28. return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
  29. }
  30. }
  31. class Image extends OwnerSlicedSavable {
  32. constructor(...params){
  33. super(...params)
  34. }
  35. static async fromFileData(fileData){
  36. let image = new Image({})
  37. image.fileData = fileData
  38. image.url = `images/${fileData.filename}`
  39. image.originalFileName = fileData.originalname
  40. await image.save()
  41. return image;
  42. }
  43. async save(...params){
  44. if (this.userAvatar){
  45. if (this.userAvatar._id.toString() !== id){
  46. throw new ReferenceError(`You can't set ava for other user`)
  47. }
  48. }
  49. return await super.save(...params)
  50. }
  51. static get relations(){
  52. return {
  53. userAvatar: "avatar", //if it is ava
  54. }
  55. }
  56. }
  57. SlicedSavable.addClass(Image)
  58. class Track extends OwnerSlicedSavable {
  59. constructor(...params){
  60. super(...params)
  61. }
  62. static async fromFileData(fileData){
  63. try {
  64. let track = new Track({})
  65. track.fileData = fileData
  66. track.url = `track/${fileData.filename}`
  67. track.originalFileName = fileData.originalname
  68. const NodeID3 = require('node-id3')
  69. track.id3 = await NodeID3.read(fileData.path)
  70. await track.save()
  71. return track;
  72. }
  73. catch(e){
  74. return {error:e}
  75. }
  76. }
  77. static get relations(){
  78. return {
  79. playlists: ["tracks"], //if it is ava
  80. }
  81. }
  82. static get guestRelations(){
  83. return ["playlists"]
  84. }
  85. }
  86. SlicedSavable.addClass(Track)
  87. class Playlist extends OwnerSlicedSavable {
  88. constructor(...params){
  89. super(...params)
  90. }
  91. static get relations(){
  92. return {
  93. tracks: ["playlists"]
  94. }
  95. }
  96. static get defaultPermissions(){
  97. return {
  98. read: ['owner', 'user', 'admin'],
  99. create: ['user', 'admin'],
  100. write: ['owner', 'admin'],
  101. delete: ['owner', 'admin'],
  102. }
  103. }
  104. }
  105. SlicedSavable.addClass(Playlist)
  106. const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
  107. return {models: {
  108. SlicedSavable, ...SlicedSavable.classes
  109. },
  110. thisUser}
  111. }
  112. return {
  113. Savable,
  114. slice,
  115. getModels
  116. }
  117. }