From 78d86f8612e6c9a67b1b7faa72633198b21cd6c4 Mon Sep 17 00:00:00 2001 From: hippoz Date: Fri, 22 Oct 2021 01:21:03 +0300 Subject: [PATCH] refactor!: completely remove legacy "posts" system --- api/v1/content.js | 56 +++-------------------------------------- api/v2/gateway/index.js | 4 +-- config.js | 1 - models/Channel.js | 5 +--- models/Post.js | 10 -------- 5 files changed, 6 insertions(+), 70 deletions(-) delete mode 100755 models/Post.js diff --git a/api/v1/content.js b/api/v1/content.js index 03bae5b..3393698 100755 --- a/api/v1/content.js +++ b/api/v1/content.js @@ -1,6 +1,5 @@ const User = require("../../models/User"); const Channel = require("../../models/Channel"); -const Post = require("../../models/Post"); const Message = require("../../models/Message"); const config = require("../../config"); const { authenticateEndpoint } = require("./../../common/auth/authfunctions"); @@ -33,8 +32,7 @@ app.post("/channel/create", [ const title = req.body.title; const channel = await Channel.create({ title: title, - creator: user._id, - posts: [] + creator: user._id }); res.status(200).json({ @@ -44,53 +42,6 @@ app.post("/channel/create", [ }); }, undefined, config.roleMap.USER)); -app.post("/post/create", [ - createLimiter, - body("channel").not().isEmpty().trim().escape().isLength({ min: 24, max: 24 }), - body("title").not().isEmpty().trim().isLength({ min: 3, max: 32 }).escape(), - body("body").not().isEmpty().trim().isLength({ min: 3, max: 1000 }).escape(), -], authenticateEndpoint(async (req, res, user) => { - if (!config.policies.allowPostCreation) return res.status(403).json({ error: true, message: "ERROR_FORBIDDEN_BY_POLICY" }); - - const errors = validationResult(req); - if (!errors.isEmpty()) { - res.status(400).json({ error: true, message: "ERROR_REQUEST_INVALID_DATA", errors: errors.array() }); - return; - } - - const channel = req.body.channel; - const title = req.body.title; - const content = req.body.body; - - const post = new Post(); - post.title = title; - post.body = content; - post.creator = user._id; - post.channel = channel; - - const r = await Channel.updateOne({ - _id: channel - }, { - $push: { posts: post } - }); - - if (r.n < 1) { - res.status(404).json({ - error: true, - message: "ERROR_CHANNEL_NOT_FOUND" - }); - return; - } - - res.status(200).json({ - error: false, - message: "SUCCESS_POST_CREATED", - post: { - _id: post._id - } - }); -}, undefined, config.roleMap.USER)); - app.get("/channel/:channel/messages", [ param("channel").not().isEmpty().trim().escape().isLength({ min: 24, max: 24 }) ], authenticateEndpoint(async (req, res) => { @@ -137,8 +88,7 @@ app.get("/channel/:channel/info", [ return; } - const channel = await Channel.findById(req.params.channel).populate("posts.creator", User.getPulicFields()); - + const channel = await Channel.findById(req.params.channel); const users = await User.find().sort({ _id: -1 }).limit(50).select(User.getPulicFields()); if (!channel) { @@ -167,7 +117,7 @@ app.get("/channel/list", authenticateEndpoint(async (req, res) => { } // TODO: This is probably not efficient - const channels = await Channel.find().lean().sort({ _id: -1 }).limit(count).select("-posts -__v").populate("creator", User.getPulicFields()); + const channels = await Channel.find().lean().sort({ _id: -1 }).limit(count).select("-__v").populate("creator", User.getPulicFields()); res.status(200).json({ error: false, diff --git a/api/v2/gateway/index.js b/api/v2/gateway/index.js index ea60640..4cb6244 100644 --- a/api/v2/gateway/index.js +++ b/api/v2/gateway/index.js @@ -175,7 +175,7 @@ class GatewayHandler { } if (!this.addSessionCounter(session.user._id)) return {error: wsCloseCodes.TOO_MANY_SESSIONS}; - const channels = (await Channel.find().lean().sort({ _id: -1 }).limit(50).select("-posts -__v").populate("creator", User.getPulicFields(true))) || []; + const channels = (await Channel.find().lean().sort({ _id: -1 }).limit(50).select("-__v").populate("creator", User.getPulicFields(true))) || []; session.channels = channels.map(x => x._id.toString()); if (data.attributes) { @@ -292,7 +292,7 @@ class GatewayServer { if (isBinary || !ws.session) return ws.close(wsCloseCodes.PAYLOAD_ERROR[0], wsCloseCodes.PAYLOAD_ERROR[1]); if (data.byteLength > config.gatewayMaxPayloadBytes) - return ws.close(wsCloseCodes.ILLEGAL_PAYLOAD_SIZE[0], wsCloseCodes.ILLEGAL_PAYLOAD_SIZE[0]); + return ws.close(wsCloseCodes.ILLEGAL_PAYLOAD_SIZE[0], wsCloseCodes.ILLEGAL_PAYLOAD_SIZE[1]); const status = await this.handler.handleMessage(ws.session, parseMessage(data.toString())); if (status && status.error) { return ws.close(status.error[0], status.error[1]); diff --git a/config.js b/config.js index 8c54939..2656377 100755 --- a/config.js +++ b/config.js @@ -14,7 +14,6 @@ module.exports = { policies: { // Currently, policies apply to all users - no matter the role. allowChannelCreation: true, - allowPostCreation: false, allowAccountCreation: true, allowLogin: true, allowGatewayConnection: true, diff --git a/models/Channel.js b/models/Channel.js index b565435..7e4283b 100755 --- a/models/Channel.js +++ b/models/Channel.js @@ -1,18 +1,15 @@ const mongoose = require("mongoose"); -const Post = require("./Post"); const User = require("./User"); const channelSchema = new mongoose.Schema({ title: String, - creator: {type: mongoose.Schema.Types.ObjectId, ref: "User"}, - posts: [Post.schema] + creator: {type: mongoose.Schema.Types.ObjectId, ref: "User"} }); channelSchema.method("getPublicObject", function() { return { title: this.title, creator: this.populate("creator", User.getPulicFields()).creator, - posts: this.posts, _id: this._id }; }); diff --git a/models/Post.js b/models/Post.js deleted file mode 100755 index c4612a8..0000000 --- a/models/Post.js +++ /dev/null @@ -1,10 +0,0 @@ -const mongoose = require("mongoose"); - -const Post = mongoose.model("Post", { - title: String, - body: String, - creator: {type: mongoose.Schema.Types.ObjectId, ref: "User"}, - channelId: {type: mongoose.Schema.Types.ObjectId, ref: "Channel"} -}); - -module.exports = Post; \ No newline at end of file