104 lines
3.3 KiB
JavaScript
104 lines
3.3 KiB
JavaScript
/*Canopy - The next generation of stoner streaming software
|
|
Copyright (C) 2024 Rainbownapkin and the TTN Community
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
|
|
|
|
//NPM Imports
|
|
const {mongoose} = require('mongoose');
|
|
|
|
//Local Imports
|
|
const config = require('./../../config.json');
|
|
|
|
const statSchema = new mongoose.Schema({
|
|
//This does NOT handle deleted accounts/channels. Use userModel.estimatedDocumentCount() for number of active users.
|
|
userCount: {
|
|
type: mongoose.SchemaTypes.Number,
|
|
required: true,
|
|
default: 0
|
|
},
|
|
channelCount: {
|
|
type: mongoose.SchemaTypes.Number,
|
|
required: true,
|
|
default: 0
|
|
},
|
|
launchCount: {
|
|
type: mongoose.SchemaTypes.Number,
|
|
required: true,
|
|
default: 0
|
|
}
|
|
});
|
|
|
|
//statics
|
|
statSchema.statics.getStats = async function(){
|
|
//Get the first document we find
|
|
var stats = await this.findOne({});
|
|
|
|
if(stats){
|
|
//If we found something then the statistics document exist and this is it,
|
|
//So long as no one else has fucked with the database it should be the only one. (is this forshadowing for a future bug?)
|
|
return stats;
|
|
}else{
|
|
//Otherwise this is the first launch of the install, say hello
|
|
console.log("First launch detected! Initializing statistics document in Database!");
|
|
|
|
//create and save the statistics document
|
|
stats = await this.create({});
|
|
await stats.save();
|
|
|
|
//live up to the name of the function
|
|
return stats;
|
|
}
|
|
}
|
|
|
|
statSchema.statics.incrementLaunchCount = async function(){
|
|
//get our statistics document
|
|
const stats = await this.getStats();
|
|
|
|
//increment counter and save
|
|
stats.launchCount++;
|
|
stats.save();
|
|
|
|
//print bootup message to console.
|
|
console.log(`${config.instanceName}(Powered by Canopy) initialized. This server has booted ${stats.launchCount} time${stats.launchCount == 1 ? '' : 's'}.`)
|
|
}
|
|
|
|
statSchema.statics.incrementUserCount = async function(){
|
|
//get our statistics document
|
|
const stats = await this.getStats();
|
|
//temporarily keep old count so we can return it for the users ID
|
|
const oldCount = stats.userCount;
|
|
|
|
//increment counter and save
|
|
stats.userCount++;
|
|
stats.save();
|
|
|
|
//return the count from beggining of function for user ID
|
|
return oldCount;
|
|
}
|
|
|
|
statSchema.statics.incrementChannelCount = async function(){
|
|
//get our statistics document
|
|
const stats = await this.getStats();
|
|
//temporarily keep old count so we can return it for the channel ID
|
|
const oldCount = stats.channelCount;
|
|
|
|
//increment counter and save
|
|
stats.channelCount++;
|
|
stats.save();
|
|
|
|
//return the count from beggining of function for channel ID
|
|
return oldCount;
|
|
}
|
|
|
|
module.exports = mongoose.model("statistics", statSchema); |