From 408b6c3dc59baacbda9b3dd297cb75fe1d7f76f9 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Wed, 15 Feb 2023 23:36:19 +0100 Subject: [PATCH] finish share api --- config.json | 4 +-- router/album.js | 46 ++++++++++++++++++++-------- router/box.js | 57 ++++++++++++++++++++++++++++++----- services/database/albums.js | 27 ++++++++++++----- services/database/boxes.js | 24 +++++++++++---- services/database/index.js | 2 ++ services/database/share.js | 60 +++++++++++++++++++++++++++++++++++++ services/redis/index.js | 33 ++++++++++++-------- 8 files changed, 205 insertions(+), 48 deletions(-) create mode 100644 services/database/share.js diff --git a/config.json b/config.json index 15616a3..2f2b3bf 100644 --- a/config.json +++ b/config.json @@ -10,12 +10,12 @@ "http://localhost" ], "database": { - "host": "database", + "host": "localhost", "port": 27017, "name": "webplay" }, "redis": { - "host": "redis", + "host": "localhost", "port": 6379 }, "album_cover_files": [ diff --git a/router/album.js b/router/album.js index a38a496..036ea28 100644 --- a/router/album.js +++ b/router/album.js @@ -8,11 +8,12 @@ const checkGuest = server.checkGuest; const resize_image_for_album = require("../services/cover/resizer").resize_image_for_album const router = new express.Router(); +const { ObjectId } = require('mongodb'); router.route("/favourites") .get(passport.authenticate("jwt", { session: false }), (req, res) => { database.albums.favourites(req.user._id, result => { - res.json(result).end(); + res.json(result).status(200).end(); }); }) @@ -25,7 +26,7 @@ router.route("/page/:page") } database.albums.collection(req.params.page, filter, result => { process.stdout.write("router/album GET albums page " + req.params.page + " DB result\n"); - res.json(result); + res.json(result).status(200).end(); }); }); @@ -38,7 +39,7 @@ router.route("/newest/:count") } database.albums.newest(parseInt(req.params.count), filter, result => { process.stdout.write("router/album GET newest " + req.params.count + " DB result\n"); - res.json(result); + res.json(result).status(200).end(); }); }); @@ -47,7 +48,7 @@ router.route("/filter/:term") process.stdout.write("router/album GET filter by term " + req.params.term + "\n"); database.albums.filter(req.params.term, result => { process.stdout.write("router/album GET filter by term " + req.params.term + " DB result\n"); - res.json(result); + res.json(result).status(200).end(); }); }) @@ -60,7 +61,7 @@ router.route("/:id") } database.albums.byId(req.params.id, filter, result => { process.stdout.write("router/album GET album by id " + req.params.id + " DB result\n"); - res.json(result); + res.json(result).status(200).end(); }); }) .put(passport.authenticate("jwt", { session: false }), (req, res) => { @@ -76,7 +77,7 @@ router.route("/:id/cover") if (req.files.file) { resize_image_for_album(req.files.file.data, (result) => { database.albums.updateCovers({ _id: req.params.id }, result); - res.json(result); + res.json(result).status(200).end(); }); } } @@ -105,7 +106,6 @@ router.route("/:id/move") database.albums.delete(source, () => { res.status(200).end(); }); - } else { res.status(403).end(); } @@ -118,18 +118,38 @@ router.route("/:id/move") router.route("/:id/share") .post(passport.authenticate("jwt", { session: false }), (req, res) => { - database.albums.byId(req.body.source, undefined, (result) => { + let album_id = req.params.id + process.stdout.write("router/album POST albums share " + album_id + "\n"); + database.albums.byId(album_id, undefined, (result) => { if (result != undefined && req.user.roles.indexOf("admin") > -1) { - process.write("Add shared abum"); + database.share.exists(album_id, result => { + if (result == null) { + let item = { + user_id: req.user._id, + object_id: new ObjectId(album_id), + type: "album" + } + database.share.add(item, (result) => { + res.json(result).status(200).end(); + }); + } else { + res.json(result).status(200).end(); + } + }); } }); }) .delete(passport.authenticate("jwt", { session: false }), (req, res) => { - database.albums.byId(req.body.source, undefined, (result) => { - if (result != undefined && req.user.roles.indexOf("admin") > -1) { - process.write("Delete shared abum"); + let album_id = req.params.id + if (req.user.roles.indexOf("admin") > -1) { + let item = { + object_id: new ObjectId(album_id), + type: "album" } - }); + database.share.delete(item, () => { + res.status(200).end(); + }); + } }); module.exports = router; \ No newline at end of file diff --git a/router/box.js b/router/box.js index 22da745..c9d17b3 100644 --- a/router/box.js +++ b/router/box.js @@ -8,6 +8,7 @@ const checkGuest = server.checkGuest; const resize_image_for_box = require("../services/cover/resizer").resize_image_for_box const router = new express.Router(); +const { ObjectId } = require('mongodb'); router.route("/favourites") .get(passport.authenticate("jwt", { session: false }), (req, res) => { @@ -44,6 +45,15 @@ router }); }); +router.route("/filter/:term") + .get(passport.authenticate("jwt", { session: false }), (req, res) => { + process.stdout.write("router/boxes GET filter by term " + req.params.term + "\n"); + database.boxes.filter(req.params.term, result => { + process.stdout.write("router/boxes GET filter by term " + req.params.term + " DB result\n"); + res.json(result); + }); + }) + router .route("/:id") .get(checkGuest, (req, res) => { @@ -63,14 +73,6 @@ router res.end(); }); -router.route("/filter/:term") - .get(passport.authenticate("jwt", { session: false }), (req, res) => { - process.stdout.write("router/boxes GET filter by term " + req.params.term + "\n"); - database.boxes.filter(req.params.term, result => { - process.stdout.write("router/boxes GET filter by term " + req.params.term + " DB result\n"); - res.json(result); - }); - }) router.route("/:id/cover") .put(passport.authenticate("jwt", { session: false }), (req, res) => { @@ -115,4 +117,43 @@ router.route("/:id/move") } }); }); + +router.route("/:id/share") + .post(passport.authenticate("jwt", { session: false }), (req, res) => { + let box_id = req.params.id + process.stdout.write("router/box POST box share " + box_id + "\n"); + database.boxes.byId(box_id, undefined, (result) => { + if (result != undefined && req.user.roles.indexOf("admin") > -1) { + database.share.exists(box_id, result => { + if (result == null) { + let item = { + user_id: req.user._id, + object_id: new ObjectId(box_id), + type: "box" + } + database.share.add(item, (result) => { + res.json(result).status(200).end(); + }); + } else { + res.json(result).status(200).end(); + } + }); + } + }); + }) + .delete(passport.authenticate("jwt", { session: false }), (req, res) => { + let box_id = req.params.id + if (req.user.roles.indexOf("admin") > -1) { + let item = { + object_id: new ObjectId(box_id), + type: "box" + } + database.share.delete(item, () => { + res.status(200).end(); + }); + } + }); + + + module.exports = router; diff --git a/services/database/albums.js b/services/database/albums.js index c804f35..09c9e43 100644 --- a/services/database/albums.js +++ b/services/database/albums.js @@ -31,8 +31,6 @@ function updateArtistName() { }); } - - exports.collection = function (page, filter, callback) { process.stdout.write("services/db_manager ALBUMS Collection: " + page + "\n"); let redis_key = "albumsCollection_" + (filter || '') + '_' + page; @@ -70,6 +68,7 @@ exports.collection = function (page, filter, callback) { result.forEach(album => { album.type = "album"; album.tracks = []; + album.share = {}; }); } process.stdout.write("services/db_manager ALBUMS Collection MONGO: " + page + "\n"); @@ -149,6 +148,13 @@ exports.byId = function (id, filter, callback) { foreignField: "album_id", as: "tracks" } + }, { + $lookup: { + from: "shares", + localField: "_id", + foreignField: "object_id", + as: "share" + } }, { $match: { _id: ObjectId(id) } } ] @@ -167,6 +173,11 @@ exports.byId = function (id, filter, callback) { if (result) { result.forEach(album => { album.type = "album"; + if (album.share.length > 0) { + album.share = album.share[0]; + } else { + album.share = {}; + } }); } process.stdout.write("services/db_manager ALBUM by id MONGO: " + id + "\n"); @@ -224,12 +235,12 @@ exports.tracks = function (id, callback) { exports.delete = function (album, callback) { dbo.collection("albums") - .deleteOne({ _id: ObjectId(album._id) }, err => { - if (err) throw err; - if (callback) { - callback(); - } - }); + .deleteOne({ _id: ObjectId(album._id) }, err => { + if (err) throw err; + if (callback) { + callback(); + } + }); }; exports.update = function (album, callback) { diff --git a/services/database/boxes.js b/services/database/boxes.js index d38d7e7..8575b21 100644 --- a/services/database/boxes.js +++ b/services/database/boxes.js @@ -70,9 +70,10 @@ exports.collection = function (page, filter, callback) { .toArray((err, result) => { if (err) throw err; if (result) { - result.forEach(item => { - item.type = "box"; - item.videos = []; + result.forEach(box => { + box.type = "box"; + box.videos = []; + box.share = {}; }); } process.stdout.write("services/db_manager BOXES Collection MONGO: " + page + "\n"); @@ -148,6 +149,14 @@ exports.byId = function (id, filter, callback) { let aggregate = [ box_lookup_videos, box_project, + { + $lookup: { + from: "shares", + localField: "_id", + foreignField: "object_id", + as: "share" + } + }, { $match: { _id: ObjectId(id) } } ]; if (filter) { @@ -162,8 +171,13 @@ exports.byId = function (id, filter, callback) { .toArray((err, result) => { if (err) throw err; if (result) { - result.forEach(item => { - item.type = "box"; + result.forEach(box => { + box.type = "box"; + if (box.share.length > 0) { + box.share = box.share[0]; + } else { + box.share = {}; + } }); } process.stdout.write("services/db_manager BOX by id MONGO: " + id + "\n"); diff --git a/services/database/index.js b/services/database/index.js index bd3ac29..72ed6fe 100644 --- a/services/database/index.js +++ b/services/database/index.js @@ -71,6 +71,8 @@ exports.users = users; const system = require("./system"); exports.system = system; +const share = require("./share"); +exports.share = share; exports.artist_count = function (callback) { return dbo.collection("artists").countDocuments(callback); diff --git a/services/database/share.js b/services/database/share.js new file mode 100644 index 0000000..05f78d6 --- /dev/null +++ b/services/database/share.js @@ -0,0 +1,60 @@ +const redis = require("../redis") + +const { ObjectId } = require('mongodb'); +const connector = require("./CONNECTOR"); +var dbo; +connector.connect().then((ret) => { + dbo = ret; +}); + +exports.byId = function (id, callback) { + dbo.collection("shares") + .findOne({ _id: ObjectId(id) }) + .then((result) => { + callback(result); + }); +} + +exports.exists = function (object_id, callback) { + dbo.collection("shares") + .findOne({ object_id: ObjectId(object_id) }) + .then(result => { + callback(result); + }); +} + +exports.add = function (item, callback) { + let redis_key = item.type + "Id__" + item.object_id; + redis.del(redis_key); + + dbo.collection("shares") + .insertOne(item, err => { + if (err) throw err; + if (callback) { + dbo.collection("shares") + .aggregate([ + { $sort: { _id: -1 } }, + { $limit: 1 } + ]) + .toArray((err, result) => { + if (err) throw err; + callback(result[0]); + }) + } + }); +} + +exports.delete = function (item, callback) { + let redis_key = item.type + "Id__" + item.object_id; + redis.del(redis_key); + + dbo.collection("shares") + .deleteMany({ object_id: ObjectId(item.object_id) }, (err) => { + if (err) throw err; + if (callback) { + callback(); + } + }); + + +} \ No newline at end of file diff --git a/services/redis/index.js b/services/redis/index.js index e784d41..8080adf 100644 --- a/services/redis/index.js +++ b/services/redis/index.js @@ -5,7 +5,7 @@ const config = server.config; const redisUrl = "redis://" + config.redis.host + ":" + config.redis.port const client = createClient({ - url: redisUrl + url: redisUrl }); client.on('error', (err) => console.log('Redis Client Error', err)); @@ -16,21 +16,30 @@ client.flushAll(); const expire = 57600; // 24h exports.set = function (key, value) { - if (value) { - client.set(key, JSON.stringify(value)); - client.expire(key, expire); - } + if (value) { + client.set(key, JSON.stringify(value)); + client.expire(key, expire); + } } exports.get = function (key, callback) { - process.stdout.write("services/redis get '" + key + "'\n"); - client.get(key).then(value => { - callback(JSON.parse(value)); - }); - client.expire(key, expire); + process.stdout.write("services/redis get '" + key + "'\n"); + client.get(key).then(value => { + callback(JSON.parse(value)); + }); + client.expire(key, expire); +} + +exports.del = function (key, callback) { + process.stdout.write("services/redis del '" + key + "'\n"); + client.del(key).then(() => { + if (callback) { + callback(); + } + }); } exports.flushAll = function () { - client.flushAll(); - process.stdout.write("services/redis flushAll()\n"); + client.flushAll(); + process.stdout.write("services/redis flushAll()\n"); } \ No newline at end of file