From 50c682e31f7072427d6d77e8055c9d2b963d4c18 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Thu, 16 Feb 2023 18:19:42 +0100 Subject: [PATCH 1/3] wip sharing api --- config.json | 4 ++-- router/album.js | 3 +-- router/box.js | 3 +-- router/index.js | 1 + router/share.js | 12 +++++++++++ server.js | 1 + services/database/share.js | 44 ++++++++++++++++++++++++++++++++------ 7 files changed, 56 insertions(+), 12 deletions(-) create mode 100644 router/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 036ea28..e47ea18 100644 --- a/router/album.js +++ b/router/album.js @@ -143,8 +143,7 @@ router.route("/:id/share") let album_id = req.params.id if (req.user.roles.indexOf("admin") > -1) { let item = { - object_id: new ObjectId(album_id), - type: "album" + object_id: new ObjectId(album_id) } database.share.delete(item, () => { res.status(200).end(); diff --git a/router/box.js b/router/box.js index c9d17b3..7e71b00 100644 --- a/router/box.js +++ b/router/box.js @@ -145,8 +145,7 @@ router.route("/:id/share") let box_id = req.params.id if (req.user.roles.indexOf("admin") > -1) { let item = { - object_id: new ObjectId(box_id), - type: "box" + object_id: new ObjectId(box_id) } database.share.delete(item, () => { res.status(200).end(); diff --git a/router/index.js b/router/index.js index 06abcf0..794f65c 100644 --- a/router/index.js +++ b/router/index.js @@ -12,5 +12,6 @@ exports.systemRouter = require("./system") exports.statusRouter = require("./status"); exports.settingsRouter = require("./settings"); exports.searchRouter = require("./search"); +exports.shareRouter = require("./share"); // exports.activitypubRouter = require("./activitypub"); // exports.wellknownRouter = require("./well-known"); \ No newline at end of file diff --git a/router/share.js b/router/share.js new file mode 100644 index 0000000..0e4d774 --- /dev/null +++ b/router/share.js @@ -0,0 +1,12 @@ +console.log("router/share INIT"); +var express = require("express"); +var router = new express.Router(); +const database = require("../services/database"); + +router.route("/:id").get((req, res) => { + database.share.byId(req.params.id, (result) => { + res.json(result).status(200).end(); + }); +}); + +module.exports = router; diff --git a/server.js b/server.js index e630a2a..ceaee47 100644 --- a/server.js +++ b/server.js @@ -135,6 +135,7 @@ app.use("/api/search", router.searchRouter); app.use("/api/settings", router.settingsRouter); app.use("/api/status", router.statusRouter); app.use("/api/system", router.systemRouter); +app.use("/api/shares", router.shareRouter); app.use("/api/tracks", router.trackRouter); app.use("/api/user", router.loginRouter); app.use("/api/user", router.userRouter); diff --git a/services/database/share.js b/services/database/share.js index 05f78d6..e6569a9 100644 --- a/services/database/share.js +++ b/services/database/share.js @@ -2,16 +2,46 @@ const redis = require("../redis") const { ObjectId } = require('mongodb'); const connector = require("./CONNECTOR"); +const database = require("./index"); var dbo; connector.connect().then((ret) => { dbo = ret; }); exports.byId = function (id, callback) { + let _id; + try { + _id = ObjectId(id); + } catch { + cb(callback); + return; + } dbo.collection("shares") - .findOne({ _id: ObjectId(id) }) + .findOne({ _id: _id }) .then((result) => { - callback(result); + if (result) { + switch (result.type) { + case "album": + console.log(result.type); + database.albums.byId(result.object_id, undefined, (album) => { + result.object = album; + cb(callback, result); + }); + break; + case "box": + console.log(result.type); + database.boxes.byId(result.object_id, undefined, (box) => { + result.object = box; + cb(callback, result); + }); + break; + default: + console.log(result.type); + cb(callback, result); + } + } else { + cb(callback); + } }); } @@ -51,10 +81,12 @@ exports.delete = function (item, callback) { dbo.collection("shares") .deleteMany({ object_id: ObjectId(item.object_id) }, (err) => { if (err) throw err; - if (callback) { - callback(); - } + cb(callback); }); +} - +function cb(callback, value) { + if (callback) { + callback(value); + } } \ No newline at end of file From f984acea9bb6997d8e7ef709e3f6eff42d02191f Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Thu, 16 Feb 2023 23:55:40 +0100 Subject: [PATCH 2/3] small clean ups --- router/album.js | 3 ++- router/box.js | 3 ++- services/database/share.js | 3 --- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/router/album.js b/router/album.js index e47ea18..036ea28 100644 --- a/router/album.js +++ b/router/album.js @@ -143,7 +143,8 @@ router.route("/:id/share") let album_id = req.params.id if (req.user.roles.indexOf("admin") > -1) { let item = { - object_id: new ObjectId(album_id) + object_id: new ObjectId(album_id), + type: "album" } database.share.delete(item, () => { res.status(200).end(); diff --git a/router/box.js b/router/box.js index 7e71b00..c9d17b3 100644 --- a/router/box.js +++ b/router/box.js @@ -145,7 +145,8 @@ router.route("/:id/share") let box_id = req.params.id if (req.user.roles.indexOf("admin") > -1) { let item = { - object_id: new ObjectId(box_id) + object_id: new ObjectId(box_id), + type: "box" } database.share.delete(item, () => { res.status(200).end(); diff --git a/services/database/share.js b/services/database/share.js index e6569a9..86fe374 100644 --- a/services/database/share.js +++ b/services/database/share.js @@ -22,21 +22,18 @@ exports.byId = function (id, callback) { if (result) { switch (result.type) { case "album": - console.log(result.type); database.albums.byId(result.object_id, undefined, (album) => { result.object = album; cb(callback, result); }); break; case "box": - console.log(result.type); database.boxes.byId(result.object_id, undefined, (box) => { result.object = box; cb(callback, result); }); break; default: - console.log(result.type); cb(callback, result); } } else { From 3b91eddcdd6a5dde9a3130d5633063c74f599692 Mon Sep 17 00:00:00 2001 From: Artem Anufrij Date: Fri, 17 Feb 2023 00:00:56 +0100 Subject: [PATCH 3/3] set config for production --- config.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.json b/config.json index 2f2b3bf..15616a3 100644 --- a/config.json +++ b/config.json @@ -10,12 +10,12 @@ "http://localhost" ], "database": { - "host": "localhost", + "host": "database", "port": 27017, "name": "webplay" }, "redis": { - "host": "localhost", + "host": "redis", "port": 6379 }, "album_cover_files": [