123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- const ObjectID = require("mongodb").ObjectID;
- const {connect} = require('mm')
- module.exports = async (dbName='player') => {
- const {Savable, slice} = await connect(dbName)
- async function getModels({id}){
- const SlicedSavable = slice([id, 'user'])
- class User extends SlicedSavable {
- constructor(...params){
- super(...params)
- //TODO: calc likes count by getter (no two-way relation for this to avoid overflow on many Kilos of likes
- //cached like count, which incremented and decremented
- //
- //following and followers array
- }
- static get relations(){ //don't needed due to ___owner in most cases
- return {
- avatar : "userAvatar",
- }
- }
- async getACL(){
- return [this._id.toString(), "user"]
- }
- }
- SlicedSavable.addClass(User)
- class OwnerSlicedSavable extends SlicedSavable {
- get owner(){
- if (!this.___owner) return this.___owner
- return SlicedSavable.m.User.findOne({_id: ObjectID(this.___owner)})
- }
- }
- class Image extends OwnerSlicedSavable {
- constructor(...params){
- super(...params)
- }
- static async fromFileData(fileData){
- let image = new Image({})
- image.fileData = fileData
- image.url = `images/${fileData.filename}`
- image.originalFileName = fileData.originalname
- await image.save()
- return image;
- }
- async save(...params){
- if (this.userAvatar){
- if (this.userAvatar._id.toString() !== id){
- throw new ReferenceError(`You can't set ava for other user`)
- }
- }
- return await super.save(...params)
- }
- static get relations(){
- return {
- userAvatar: "avatar", //if it is ava
- }
- }
- }
- SlicedSavable.addClass(Image)
- class Track extends OwnerSlicedSavable {
- constructor(...params){
- super(...params)
- }
- static async fromFileData(fileData){
- try {
- let track = new Track({})
- track.fileData = fileData
- track.url = `track/${fileData.filename}`
- track.originalFileName = fileData.originalname
- const NodeID3 = require('node-id3')
- track.id3 = await NodeID3.read(fileData.path)
- await track.save()
- return track;
- }
- catch(e){
- return {error:e}
- }
- }
- static get relations(){
- return {
- playlists: ["tracks"], //if it is ava
- }
- }
- static get guestRelations(){
- return ["playlists"]
- }
-
- }
- SlicedSavable.addClass(Track)
- class Playlist extends OwnerSlicedSavable {
- constructor(...params){
- super(...params)
- }
- static get relations(){
- return {
- tracks: ["playlists"]
- }
- }
- static get defaultPermissions(){
- return {
- read: ['owner', 'user', 'admin'],
- create: ['user', 'admin'],
- write: ['owner', 'admin'],
- delete: ['owner', 'admin'],
- }
- }
- }
- SlicedSavable.addClass(Playlist)
- const thisUser = await Savable.m.User.findOne({_id: ObjectID(id)})
- return {models: {
- SlicedSavable, ...SlicedSavable.classes
- },
- thisUser}
- }
- return {
- Savable,
- slice,
- getModels
- }
- }
|