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..86fe374 100644 --- a/services/database/share.js +++ b/services/database/share.js @@ -2,16 +2,43 @@ 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": + database.albums.byId(result.object_id, undefined, (album) => { + result.object = album; + cb(callback, result); + }); + break; + case "box": + database.boxes.byId(result.object_id, undefined, (box) => { + result.object = box; + cb(callback, result); + }); + break; + default: + cb(callback, result); + } + } else { + cb(callback); + } }); } @@ -51,10 +78,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