import mongooseUniqueValidator from "mongoose-unique-validator"; import {AppMaster} from "@onebro/appmaster"; import { DeepPartial, IModelCreator, IStatus, IQuery, mapSelectedData, getSchemaRefs, ObjectId} from "@onebro/oba-common"; import { SageListUserProfileConfig, SageListUserProfileModel, SageListUserProfile, SageListUserProfileJson, SageListUserProfilePreview, SageListUserProfileStatuses as statuses, SageListUserProfileStatus, SageListRoles, SageListUserProfileUpdateFollowersOrFollowing} from "../types"; import {sageListUserProfileSchema as profile} from "../schemas"; const refs = getSchemaRefs(profile); export const sageListUserProfileModels:IModelCreator = async m => { profile.plugin(mongooseUniqueValidator); profile.virtual("lastStatus").get(function(){return this.status?(this.status[this.status.length - 1] as any).json(statuses):null;}); profile.virtual("lastStatus").set(function(o:IStatus){this.status = [...this.status,o];}); profile.virtual("statusStr").get(function(){return this.lastStatus?this.lastStatus.name + " @ " + this.lastStatus.time:"";}); profile.virtual("lang").get(function(){return this.settings.lang;}); profile.virtual("lang").set(function(lang:string){this.settings.lang = lang;}); profile.statics._populate = async function(o?:SageListUserProfile){ await o.populate(refs).execPopulate(); return o;}; profile.statics._find = async function(o:Partial|string){ if(!o) return m.e.badinfo(); const p = typeof o == "string"? await SageListUserProfile.findById(o): await SageListUserProfile.findOne(SageListUserProfile.translateAliases(o)); if(!p) throw m.e.doesNotExist("profile"); return p;}; profile.statics._create = async function(o:SageListUserProfileConfig){return new SageListUserProfile({ ...o, img:o.img || "", settings:{...o.settings,permissions:[{name:"cookies",time:Date.now()}]}, //merge permissions correctly });}; profile.statics._update = async function(o:SageListUserProfile,p:DeepPartial & SageListUserProfileUpdateFollowersOrFollowing){ if(p.settings) p.settings = {...(o.json() as any).settings,...p.settings}; if(p.addFollowers) p.followers = [...o.followers,...p.addFollowers]; if(p.removeFollowers) for(let i=0,l=p.removeFollowers.length;i f.id.toString() !== p.removeFollowers[i]); if(p.addFollowing) p.following = [...o.following,...p.addFollowing]; if(p.removeFollowing) for(let i=0,l=p.removeFollowing.length;i f.id.toString() !== p.removeFollowing[i]); await o.set(p).save(); return await SageListUserProfile._populate(o);}; profile.statics._remove = async function(o:SageListUserProfile){return await o.remove().then(() => ({removed:o._id}));}; profile.statics._query = async function(o:IQuery){ const {query,populate,limit,skip,sort,select} = o; const results:SageListUserProfile[] = await this.find(this.translateAliases(query)) .populate(populate) //.where(where) .limit(limit) .skip(skip) .sort(sort) .exec(); const profiles = mapSelectedData(select,results); return {profiles};}; profile.methods.preview = function(){ const {id,username,img,role} = this as SageListUserProfile; const preview:SageListUserProfilePreview = {id,username,img,role:SageListRoles[role]}; return preview;}; profile.methods.json = function(){ const { id,created,updated,lastStatus:status,statusStr, username,role,settings,img,bio,motto,lang, followers,following,rating,expYrs,expLvl,socials} = this as SageListUserProfile; const json:SageListUserProfileJson = { id,created,updated,lastStatus:status,statusStr, username,role:SageListRoles[role], bio,motto,img,lang, settings:(settings as any).json(), rating,expLvl,expYrs, following:following.map(o => o.preview() as SageListUserProfilePreview), followers:followers.map(o => o.preview() as SageListUserProfilePreview), socials,memberSince:created as any, }; return json;}; const SageListUserProfile = m.db.model("onebrother","SageListUserProfile",profile); await SageListUserProfile.init(); return {SageListUserProfile}; };