Files
EMI-Backend/dbTools/profile.js
T
2021-09-14 11:19:44 -07:00

193 lines
5.6 KiB
JavaScript

const DBName = "EMI_SOCIAL";
let userProfileCache = {};
userDB = (DB) => {
DB.profileCols = DB.db.db(DBName).collection("profiles");
DB.newProfile = (profileObj) => {
return DB.profileCols.insertOne(profileObj.toObj()).catch((err) => {
console.log(err);
return false;
});
}
DB.getProfile = async (profileId) => {
//if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId];
const _id = DB.ObjectID(profileId);
let r = await DB.profileCols.findOne({ _id }).catch((err) => {
console.log(err);
return false;
});
if (r) userProfileCache[profileId] = r;
return r;
}
DB.getProfileCache = async (profileId) => {
if (userProfileCache[profileId] && !userProfileCache[profileId].isGroup) return userProfileCache[profileId];
const _id = DB.ObjectID(profileId);
let r = await DB.profileCols.findOne({ _id }).catch((err) => {
console.log(err);
return false;
});
if (r) userProfileCache[profileId] = r;
return r;
}
DB.getProfiles = async (query) => {
let r = await DB.profileCols.find({isGroup: false})
.sort({ lastUpdate: -1 }).limit(20)
.toArray().catch((err) => {
console.log(err);
return false;
});
return r;
}
DB.getUserProfiles = async (userId) => {
const userid = DB.ObjectID(userId);
return await DB.profileCols.find({ userid }).toArray().catch((err) => {
console.log(err);
return false;
});
}
DB.latestProfile = async (userId) => {
const userid = DB.ObjectID(userId);
let r = await DB.profileCols.find({ userid })
.sort({ lastUpdate: -1 })
.toArray().catch((err) => {
console.log(err);
return false;
});
let index = 0;
while(r[index].isGroup) index += 1;
if (r[index]) userProfileCache[r[index].id] = r[index];
return r[index];
}
DB.followProfile = async (profileId, followProfileId)=>{
const _id = DB.ObjectID(profileId);
let update = {
$addToSet:{
following: followProfileId
}
}
return DB.profileCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.unfollowProfile = async (profileId, followProfileId)=>{
const _id = DB.ObjectID(profileId);
let update = {
$pull:{
following: followProfileId
}
}
return DB.profileCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.getData = async (profileid, key) => {
let profile = await DB.getProfile(profileid);
return profile.data[key];
}
DB.setData = async (profileid, key, value) => {
const _id = DB.ObjectID(profileid);
let update = {
$set:{
["data." + key]: value
}
}
return DB.profileCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.addNotification = async (profileid, message) => {
const _id = DB.ObjectID(profileid);
let update = {
$push:{
notifications: {
ts: new Date(),
body: message
}
}
}
return DB.profileCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
//Groups
DB.getGroups = async () => {
let r = await DB.profileCols.find({isGroup: true, isCourse: {$ne: true}})
.sort({ lastUpdate: -1 }).limit(10)
.toArray().catch((err) => {
console.log(err);
return false;
});
return r;
}
DB.getGroup = async (groupid) => {
const _id = DB.ObjectID(groupid);
//if(userProfileCache[groupid]) return userProfileCache[groupid];
let r = await DB.profileCols.findOne({_id, isGroup: true})
.toArray().catch((err) => {
console.log(err);
return false;
});
if (r) userProfileCache[r.id] = r;
return r;
}
DB.subscribeToGroup = async (profileid, groupid) => {
const _id = DB.ObjectID(groupid);
let update = {
$set:{
["subscribed." + profileid]: new Date()
}
}
DB.followProfile(profileid, groupid)
return DB.profileCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
DB.unsubscribeToGroup = async (profileid, groupid) => {
const _id = DB.ObjectID(groupid);
let update = {
$unset:{
["subscribed." + profileid]: "",
}
}
DB.unfollowProfile(profileid, groupid)
return DB.profileCols.updateOne({_id}, update).catch((err)=>{
console.log(err);
return false;
});
}
//Courses
DB.getCourses = async () => {
let r = await DB.profileCols.find({isGroup: true, isCourse: true})
.sort({ lastUpdate: -1 }).limit(10)
.toArray().catch((err) => {
console.log(err);
return false;
});
return r;
}
}
module.exports = userDB;