refactor!: completely remove legacy "posts" system
This commit is contained in:
parent
80ed58c856
commit
78d86f8612
5 changed files with 6 additions and 70 deletions
|
@ -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,
|
||||
|
|
|
@ -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]);
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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
|
||||
};
|
||||
});
|
||||
|
|
|
@ -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;
|
Reference in a new issue