AnoPaper/core.js
Artemy Egorov cf5151de7e
Stats (#8)
* feat: sent notes stats

* fix: unused var in notes

* refactor: stats increment

* feat: received notes, deleted notes

* fix: received notes stats inc

* refactor: stats

* fix: inc received

* feat: api for stats

* doc: update changelog
2023-04-25 17:42:50 +03:00

87 lines
2.1 KiB
JavaScript

// Copyright (c) 2023 artegoser (Artemy Egorov)
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
const mongoClient = require("mongodb").MongoClient;
const sha3 = require("js-sha3").sha3_512;
class NotesCore {
constructor() {}
async connect() {
let connection = await mongoClient.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
});
let db = connection.db(process.env.MONGO_DB);
this.notes = db.collection("notes");
this.stats = db.collection("stats");
}
async getNote(_id) {
try {
let note = await this.notes.findOne({ _id });
if (note !== null) await this.incStats("receivedNotes");
return note;
} catch {
return null;
}
}
async getStats(_id) {
try {
return await this.stats.findOne({ _id });
} catch {
return null;
}
}
async deleteNote(_id) {
try {
await this.incStats("deletedNotes");
return await this.notes.deleteOne({ _id });
} catch {
return null;
}
}
async publishNote(note) {
try {
note._id = sha3(JSON.stringify(note));
note.time = Date.now();
note.pub = true;
await this.notes.updateOne(
{ _id: note._id },
{ $set: note },
{ upsert: true }
);
await this.incStats("sentNotes");
return note._id;
} catch {
return null;
}
}
async incStats(_id) {
await this.stats.updateOne(
{ _id },
{ $inc: { value: 1 } },
{ upsert: true }
);
}
}
module.exports = {
NotesCore,
};