mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-09 10:34:17 +00:00
implement PostController
This commit is contained in:
parent
c8a6ba6dfb
commit
6e614f77a1
68
packages/server/src/controllers/PostsController/index.js
Normal file
68
packages/server/src/controllers/PostsController/index.js
Normal file
@ -0,0 +1,68 @@
|
||||
import { ComplexController } from "linebridge/dist/classes"
|
||||
import { Schematized } from "../../lib"
|
||||
import { Post, User } from "../../models"
|
||||
|
||||
export default class PostsController extends ComplexController {
|
||||
static refName = "PostsController"
|
||||
static useMiddlewares = ["withAuthentication"]
|
||||
|
||||
methods = {
|
||||
createPost: async (payload) => {
|
||||
const { user_id, message } = payload
|
||||
const userData = await User.findById(user_id)
|
||||
|
||||
const post = new Post({
|
||||
user_id: typeof user_id === "object" ? user_id.toString() : user_id,
|
||||
message: String(message).toString(),
|
||||
})
|
||||
|
||||
await post.save()
|
||||
|
||||
global.wsInterface.io.emit(`new.post`, {
|
||||
...post.toObject(),
|
||||
user: userData.toObject(),
|
||||
})
|
||||
global.wsInterface.io.emit(`new.post.${post.user_id}`, {
|
||||
...post.toObject(),
|
||||
user: userData.toObject(),
|
||||
})
|
||||
|
||||
return post
|
||||
},
|
||||
}
|
||||
|
||||
get = {
|
||||
"/feed": Schematized({
|
||||
select: ["user_id"]
|
||||
}, async (req, res) => {
|
||||
let posts = await Post.find(req.selection)
|
||||
|
||||
posts = posts.map(async (post) => {
|
||||
const user = await User.findById(post.user_id)
|
||||
|
||||
return {
|
||||
...post.toObject(),
|
||||
user: user.toObject(),
|
||||
}
|
||||
})
|
||||
|
||||
posts = await Promise.all(posts)
|
||||
|
||||
return res.json(posts)
|
||||
}),
|
||||
}
|
||||
|
||||
put = {
|
||||
"/post": Schematized({
|
||||
required: ["message"],
|
||||
select: ["message"],
|
||||
}, async (req, res) => {
|
||||
const post = await this.methods.createPost({
|
||||
user_id: req.user.id,
|
||||
message: req.selection.message,
|
||||
})
|
||||
|
||||
return res.json(post)
|
||||
})
|
||||
}
|
||||
}
|
@ -4,8 +4,10 @@ import { default as SessionController } from "./SessionController"
|
||||
import { default as UserController } from "./UserController"
|
||||
import { default as FilesController } from "./FilesController"
|
||||
import { default as PublicController } from "./PublicController"
|
||||
import { default as PostsController } from "./PostsController"
|
||||
|
||||
export default [
|
||||
PostsController,
|
||||
ConfigController,
|
||||
PublicController,
|
||||
RolesController,
|
||||
|
@ -16,4 +16,5 @@ const schemas = getSchemas()
|
||||
export const Config = mongoose.model("Config", schemas.Config, "config")
|
||||
export const User = mongoose.model("User", schemas.User, "accounts")
|
||||
export const Session = mongoose.model("Session", schemas.Session, "sessions")
|
||||
export const Role = mongoose.model("Role", schemas.Role, "roles")
|
||||
export const Role = mongoose.model("Role", schemas.Role, "roles")
|
||||
export const Post = mongoose.model("Post", schemas.Post, "posts")
|
@ -1,4 +1,5 @@
|
||||
export { default as User } from "./user"
|
||||
export { default as Role } from "./role"
|
||||
export { default as Session } from "./session"
|
||||
export { default as Config } from "./config"
|
||||
export { default as Config } from "./config"
|
||||
export { default as Post } from "./post"
|
4
packages/server/src/schemas/post/index.js
Normal file
4
packages/server/src/schemas/post/index.js
Normal file
@ -0,0 +1,4 @@
|
||||
export default {
|
||||
user_id: String,
|
||||
message: String,
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user