import mongooseUniqueValidator from "mongoose-unique-validator"; import {AppMaster} from "@onebro/appmaster"; import { DeepPartial, IModelCreator, IStatus, IQuery, mapSelectedData, getSchemaRefs, ObjectId} from "@onebro/oba-common"; import { FirstlineUserProfileConfig, FirstlineUserProfileModel, FirstlineUserProfile, FirstlineUserProfileJson, FirstlineUserProfilePreview, FirstlineUserProfileStatuses as statuses, FirstlineUserProfileStatus, FirstlineRoles, FirstlineUserProfileUpdateFollowersOrFollowing} from "../types"; import {firstlineUserProfileSchema as profile} from "../schemas"; const refs = getSchemaRefs(profile); export const firstlineUserProfileModels: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?:FirstlineUserProfile){ 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 FirstlineUserProfile.findById(o): await FirstlineUserProfile.findOne(FirstlineUserProfile.translateAliases(o)); if(!p) throw m.e.doesNotExist("profile"); return p;}; profile.statics._create = async function(o:FirstlineUserProfileConfig){return new FirstlineUserProfile({ ...o, img:o.img || "", settings:{...o.settings,permissions:[{name:"cookies",time:Date.now()}]}, //merge permissions correctly });}; profile.statics._update = async function(o:FirstlineUserProfile,p:DeepPartial & FirstlineUserProfileUpdateFollowersOrFollowing){ 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 FirstlineUserProfile._populate(o);}; profile.statics._remove = async function(o:FirstlineUserProfile){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:FirstlineUserProfile[] = 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 FirstlineUserProfile; const preview:FirstlineUserProfilePreview = {id,username,img,role:FirstlineRoles[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 FirstlineUserProfile; const json:FirstlineUserProfileJson = { id,created,updated,lastStatus:status,statusStr, username,role:FirstlineRoles[role], bio,motto,img,lang, settings:(settings as any).json(), rating,expLvl,expYrs, following:following.map(o => o.preview() as FirstlineUserProfilePreview), followers:followers.map(o => o.preview() as FirstlineUserProfilePreview), socials,memberSince:created as any, }; return json;}; const FirstlineUserProfile = m.db.model("onebrother","FirstlineUserProfile",profile); await FirstlineUserProfile.init(); return {FirstlineUserProfile}; };