mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
use new routes
This commit is contained in:
parent
09a53555a5
commit
741ce66df5
@ -16,11 +16,25 @@ export default class Post {
|
|||||||
throw new Error("Post ID is required")
|
throw new Error("Post ID is required")
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = Post.bridge.get.post(undefined, {
|
const { data } = await app.api.customRequest("main", {
|
||||||
post_id,
|
method: "GET",
|
||||||
|
url: `/posts/${post_id}`,
|
||||||
})
|
})
|
||||||
|
|
||||||
return request
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getPostComments({ post_id }) {
|
||||||
|
if (!post_id) {
|
||||||
|
throw new Error("Post ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await app.api.customRequest("main", {
|
||||||
|
method: "GET",
|
||||||
|
url: `/posts/${post_id}/comments`,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
static async sendComment({ post_id, comment }) {
|
static async sendComment({ post_id, comment }) {
|
||||||
@ -30,7 +44,7 @@ export default class Post {
|
|||||||
|
|
||||||
const request = await app.api.customRequest("main", {
|
const request = await app.api.customRequest("main", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
url: `/post/${post_id}/comment`,
|
url: `/posts/${post_id}/comment`,
|
||||||
data: {
|
data: {
|
||||||
message: comment,
|
message: comment,
|
||||||
},
|
},
|
||||||
@ -46,7 +60,7 @@ export default class Post {
|
|||||||
|
|
||||||
const request = await app.api.customRequest("main", {
|
const request = await app.api.customRequest("main", {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
url: `/post/${post_id}/comment/${comment_id}`,
|
url: `/posts/${post_id}/comment/${comment_id}`,
|
||||||
})
|
})
|
||||||
|
|
||||||
return request
|
return request
|
||||||
@ -57,25 +71,16 @@ export default class Post {
|
|||||||
throw new Error("Bridge is not available")
|
throw new Error("Bridge is not available")
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = Post.bridge.get.explorePosts(undefined, {
|
const { data } = await app.api.customRequest("main", {
|
||||||
trim: trim ?? 0,
|
method: "GET",
|
||||||
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
url: `/posts/explore`,
|
||||||
|
params: {
|
||||||
|
trim: trim ?? 0,
|
||||||
|
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return request
|
return data
|
||||||
}
|
|
||||||
|
|
||||||
static async getFeed({ trim, limit }) {
|
|
||||||
if (!Post.bridge) {
|
|
||||||
throw new Error("Bridge is not available")
|
|
||||||
}
|
|
||||||
|
|
||||||
const request = Post.bridge.get.feed(undefined, {
|
|
||||||
trim: trim ?? 0,
|
|
||||||
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
|
||||||
})
|
|
||||||
|
|
||||||
return request
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getSavedPosts({ trim, limit }) {
|
static async getSavedPosts({ trim, limit }) {
|
||||||
@ -83,12 +88,16 @@ export default class Post {
|
|||||||
throw new Error("Bridge is not available")
|
throw new Error("Bridge is not available")
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = Post.bridge.get.savedPosts(undefined, {
|
const { data } = await app.api.customRequest("main", {
|
||||||
trim: trim ?? 0,
|
method: "GET",
|
||||||
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
url: `/posts/saved`,
|
||||||
|
params: {
|
||||||
|
trim: trim ?? 0,
|
||||||
|
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return request
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getUserPosts({ user_id, trim, limit }) {
|
static async getUserPosts({ user_id, trim, limit }) {
|
||||||
@ -101,12 +110,80 @@ export default class Post {
|
|||||||
user_id = app.userData?._id
|
user_id = app.userData?._id
|
||||||
}
|
}
|
||||||
|
|
||||||
const request = Post.bridge.get.userPosts(undefined, {
|
const { data } = await app.api.customRequest("main", {
|
||||||
user_id,
|
method: "GET",
|
||||||
trim: trim ?? 0,
|
url: `/posts/user/${user_id}`,
|
||||||
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
params: {
|
||||||
|
trim: trim ?? 0,
|
||||||
|
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
return request
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
static async toogleLike({ post_id }) {
|
||||||
|
if (!Post.bridge) {
|
||||||
|
throw new Error("Bridge is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!post_id) {
|
||||||
|
throw new Error("Post ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await app.api.customRequest("main", {
|
||||||
|
method: "POST",
|
||||||
|
url: `/posts/${post_id}/toogle_like`,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
static async toogleSave({ post_id }) {
|
||||||
|
if (!Post.bridge) {
|
||||||
|
throw new Error("Bridge is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!post_id) {
|
||||||
|
throw new Error("Post ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await app.api.customRequest("main", {
|
||||||
|
method: "POST",
|
||||||
|
url: `/posts/${post_id}/toogle_save`,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
static async create(payload) {
|
||||||
|
if (!Post.bridge) {
|
||||||
|
throw new Error("Bridge is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await app.api.customRequest("main", {
|
||||||
|
method: "POST",
|
||||||
|
url: `/posts/new`,
|
||||||
|
data: payload,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
||||||
|
static async deletePost({ post_id }) {
|
||||||
|
if (!Post.bridge) {
|
||||||
|
throw new Error("Bridge is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!post_id) {
|
||||||
|
throw new Error("Post ID is required")
|
||||||
|
}
|
||||||
|
|
||||||
|
const { data } = await app.api.customRequest("main", {
|
||||||
|
method: "DELETE",
|
||||||
|
url: `/posts/${post_id}`,
|
||||||
|
})
|
||||||
|
|
||||||
|
return data
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,5 +1,4 @@
|
|||||||
import { Controller } from "linebridge/dist/server"
|
import { Controller } from "linebridge/dist/server"
|
||||||
import { User, Post, Comment } from "../../models"
|
|
||||||
import { Schematized } from "../../lib"
|
import { Schematized } from "../../lib"
|
||||||
|
|
||||||
import getComments from "./methods/getComments"
|
import getComments from "./methods/getComments"
|
||||||
@ -10,7 +9,7 @@ export default class CommentsController extends Controller {
|
|||||||
static refName = "CommentsController"
|
static refName = "CommentsController"
|
||||||
|
|
||||||
get = {
|
get = {
|
||||||
"/post/:post_id/comments": {
|
"/posts/:post_id/comments": {
|
||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
const { post_id } = req.params
|
const { post_id } = req.params
|
||||||
|
|
||||||
@ -30,7 +29,7 @@ export default class CommentsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
post = {
|
post = {
|
||||||
"/post/:post_id/comment": {
|
"/posts/:post_id/comment": {
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
required: ["message"],
|
required: ["message"],
|
||||||
@ -57,7 +56,7 @@ export default class CommentsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete = {
|
delete = {
|
||||||
"/post/:post_id/comment/:comment_id": {
|
"/posts/:post_id/comment/:comment_id": {
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
const result = await deleteComment({
|
const result = await deleteComment({
|
||||||
|
@ -1,19 +1,23 @@
|
|||||||
import { Controller } from "linebridge/dist/server"
|
import { Controller } from "linebridge/dist/server"
|
||||||
import { Schematized } from "../../lib"
|
|
||||||
|
|
||||||
import getPosts from "./methods/getPosts"
|
import getPosts from "./methods/getPosts"
|
||||||
|
|
||||||
export default class FeedController extends Controller {
|
export default class FeedController extends Controller {
|
||||||
static refName = "FeedController"
|
static refName = "FeedController"
|
||||||
|
static useRoute = "/feed"
|
||||||
|
|
||||||
get = {
|
get = {
|
||||||
"/feed": {
|
"/posts": {
|
||||||
middlewares: ["withOptionalAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: Schematized({
|
fn: async (req, res) => {
|
||||||
select: ["user_id"]
|
|
||||||
}, async (req, res) => {
|
|
||||||
const for_user_id = req.user?._id.toString()
|
const for_user_id = req.user?._id.toString()
|
||||||
|
|
||||||
|
if (!for_user_id) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: "Invalid user id"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
let feed = []
|
let feed = []
|
||||||
|
|
||||||
// fetch posts
|
// fetch posts
|
||||||
@ -26,7 +30,7 @@ export default class FeedController extends Controller {
|
|||||||
feed = feed.concat(posts)
|
feed = feed.concat(posts)
|
||||||
|
|
||||||
return res.json(feed)
|
return res.json(feed)
|
||||||
})
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,10 +5,10 @@ import { CreatePost, ToogleLike, GetPostData, DeletePost, ToogleSavePost } from
|
|||||||
|
|
||||||
export default class PostsController extends Controller {
|
export default class PostsController extends Controller {
|
||||||
static refName = "PostsController"
|
static refName = "PostsController"
|
||||||
//static useMiddlewares = ["withAuthentication"]
|
static useRoute = "/posts"
|
||||||
|
|
||||||
get = {
|
get = {
|
||||||
"/explore/posts": {
|
"/explore": {
|
||||||
middlewares: ["withOptionalAuthentication"],
|
middlewares: ["withOptionalAuthentication"],
|
||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
select: ["user_id"]
|
select: ["user_id"]
|
||||||
@ -23,7 +23,7 @@ export default class PostsController extends Controller {
|
|||||||
return res.json(posts)
|
return res.json(posts)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
"/saved_posts": {
|
"/saved": {
|
||||||
middlewares: ["withOptionalAuthentication"],
|
middlewares: ["withOptionalAuthentication"],
|
||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
select: ["user_id"]
|
select: ["user_id"]
|
||||||
@ -38,30 +38,26 @@ export default class PostsController extends Controller {
|
|||||||
return res.json(posts)
|
return res.json(posts)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
"/user_posts": {
|
"/user/:user_id": {
|
||||||
middlewares: ["withOptionalAuthentication"],
|
middlewares: ["withOptionalAuthentication"],
|
||||||
fn: Schematized({
|
fn: async (req, res) => {
|
||||||
required: ["user_id"],
|
|
||||||
select: ["user_id"]
|
|
||||||
}, async (req, res) => {
|
|
||||||
let posts = await GetPostData({
|
let posts = await GetPostData({
|
||||||
limit: req.query?.limit,
|
limit: req.query?.limit,
|
||||||
skip: req.query?.trim,
|
skip: req.query?.trim,
|
||||||
for_user_id: req.user?._id.toString(),
|
for_user_id: req.user?._id.toString(),
|
||||||
from_user_id: req.query?.user_id,
|
from_user_id: req.params.user_id,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
console.log(req.query)
|
||||||
|
|
||||||
return res.json(posts)
|
return res.json(posts)
|
||||||
})
|
}
|
||||||
},
|
},
|
||||||
"/post": {
|
"/:post_id": {
|
||||||
middlewares: ["withOptionalAuthentication"],
|
middlewares: ["withOptionalAuthentication"],
|
||||||
fn: Schematized({
|
fn: async (req, res) => {
|
||||||
select: ["post_id"],
|
|
||||||
required: ["post_id"]
|
|
||||||
}, async (req, res) => {
|
|
||||||
let post = await GetPostData({
|
let post = await GetPostData({
|
||||||
post_id: req.query?.post_id,
|
post_id: req.params.post_id,
|
||||||
for_user_id: req.user?._id.toString(),
|
for_user_id: req.user?._id.toString(),
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
res.status(404).json({ error: error.message })
|
res.status(404).json({ error: error.message })
|
||||||
@ -72,16 +68,22 @@ export default class PostsController extends Controller {
|
|||||||
if (!post) return
|
if (!post) return
|
||||||
|
|
||||||
return res.json(post)
|
return res.json(post)
|
||||||
})
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
put = {
|
put = {
|
||||||
|
"/:post_id": {
|
||||||
|
middlewares: ["withAuthentication"],
|
||||||
|
fn: (req, res) => {
|
||||||
|
// TODO: Implement Post update
|
||||||
|
return res.status(501).json({ error: "Not implemented" })
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
post = {
|
post = {
|
||||||
"/post": {
|
"/new": {
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
required: ["timestamp"],
|
required: ["timestamp"],
|
||||||
@ -99,32 +101,7 @@ export default class PostsController extends Controller {
|
|||||||
return res.json(post)
|
return res.json(post)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
"/toogle_like": {
|
"/:post_id/toogle_like": {
|
||||||
middlewares: ["withAuthentication"],
|
|
||||||
fn: Schematized({
|
|
||||||
required: ["post_id"],
|
|
||||||
select: ["post_id", "to"],
|
|
||||||
}, async (req, res) => {
|
|
||||||
const post = await ToogleLike({
|
|
||||||
user_id: req.user._id.toString(),
|
|
||||||
post_id: req.selection.post_id,
|
|
||||||
to: req.selection.to,
|
|
||||||
}).catch((err) => {
|
|
||||||
res.status(400).json({
|
|
||||||
error: err.message
|
|
||||||
})
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!post) return
|
|
||||||
|
|
||||||
return res.json({
|
|
||||||
success: true,
|
|
||||||
post
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
|
||||||
"/post/:post_id/toogle_like": {
|
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
select: ["to"],
|
select: ["to"],
|
||||||
@ -148,31 +125,7 @@ export default class PostsController extends Controller {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
"/post/toogle_save": {
|
"/:post_id/toogle_save": {
|
||||||
middlewares: ["withAuthentication"],
|
|
||||||
fn: Schematized({
|
|
||||||
required: ["post_id"],
|
|
||||||
select: ["post_id"],
|
|
||||||
}, async (req, res) => {
|
|
||||||
const post = await ToogleSavePost({
|
|
||||||
user_id: req.user._id.toString(),
|
|
||||||
post_id: req.selection.post_id,
|
|
||||||
}).catch((err) => {
|
|
||||||
res.status(400).json({
|
|
||||||
error: err.message
|
|
||||||
})
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!post) return
|
|
||||||
|
|
||||||
return res.json({
|
|
||||||
success: true,
|
|
||||||
post
|
|
||||||
})
|
|
||||||
})
|
|
||||||
},
|
|
||||||
"/post/:post_id/save": {
|
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: async (req, res) => {
|
fn: async (req, res) => {
|
||||||
const post = await ToogleSavePost({
|
const post = await ToogleSavePost({
|
||||||
@ -196,14 +149,11 @@ export default class PostsController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
delete = {
|
delete = {
|
||||||
"/post": {
|
"/:post_id": {
|
||||||
middlewares: ["withAuthentication"],
|
middlewares: ["withAuthentication"],
|
||||||
fn: Schematized({
|
fn: async (req, res) => {
|
||||||
required: ["post_id"],
|
|
||||||
select: ["post_id"],
|
|
||||||
}, async (req, res) => {
|
|
||||||
const post = await DeletePost({
|
const post = await DeletePost({
|
||||||
post_id: req.selection.post_id,
|
post_id: req.params.post_id,
|
||||||
by_user_id: req.user._id.toString(),
|
by_user_id: req.user._id.toString(),
|
||||||
}).catch((err) => {
|
}).catch((err) => {
|
||||||
res.status(400).json({
|
res.status(400).json({
|
||||||
@ -219,7 +169,7 @@ export default class PostsController extends Controller {
|
|||||||
success: true,
|
success: true,
|
||||||
post
|
post
|
||||||
})
|
})
|
||||||
})
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user