mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
implement user_posts
endpoint
This commit is contained in:
parent
5714a16524
commit
8a859d0b4f
@ -90,4 +90,23 @@ export default class Post {
|
|||||||
|
|
||||||
return request
|
return request
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static async getUserPosts({ user_id, trim, limit }) {
|
||||||
|
if (!Post.bridge) {
|
||||||
|
throw new Error("Bridge is not available")
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!user_id) {
|
||||||
|
// use current user_id
|
||||||
|
user_id = app.userData?._id
|
||||||
|
}
|
||||||
|
|
||||||
|
const request = Post.bridge.get.userPosts(undefined, {
|
||||||
|
user_id,
|
||||||
|
trim: trim ?? 0,
|
||||||
|
limit: limit ?? window.app.settings.get("feed_max_fetch"),
|
||||||
|
})
|
||||||
|
|
||||||
|
return request
|
||||||
|
}
|
||||||
}
|
}
|
@ -1,11 +1,83 @@
|
|||||||
import React from "react"
|
import React from "react"
|
||||||
|
import { Skeleton } from "antd"
|
||||||
|
import { Icons } from "components/Icons"
|
||||||
|
|
||||||
import { PostsFeed } from "components"
|
import { PostsList } from "components"
|
||||||
|
import Post from "models/post"
|
||||||
|
|
||||||
export default React.memo((props) => {
|
const emptyListRender = () => {
|
||||||
return <div className="posts">
|
return <div className="emptyFeed">
|
||||||
<PostsFeed
|
<h2>
|
||||||
fromUserId={props.state.user._id}
|
This user has no posts yet.
|
||||||
/>
|
</h2>
|
||||||
</div>
|
</div>
|
||||||
})
|
}
|
||||||
|
|
||||||
|
export default class UserPosts extends React.Component {
|
||||||
|
state = {
|
||||||
|
loading: true,
|
||||||
|
initialLoading: true,
|
||||||
|
hasMorePosts: true,
|
||||||
|
posts: [],
|
||||||
|
}
|
||||||
|
|
||||||
|
loadData = async ({
|
||||||
|
trim,
|
||||||
|
replace = false
|
||||||
|
} = {}) => {
|
||||||
|
await this.setState({
|
||||||
|
loading: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const result = await Post.getUserPosts({
|
||||||
|
user_id: this.props.state.user._id,
|
||||||
|
trim: trim ?? this.state.posts.length,
|
||||||
|
})
|
||||||
|
|
||||||
|
console.log("Loaded data => \n", result)
|
||||||
|
|
||||||
|
if (result) {
|
||||||
|
if (result.length === 0) {
|
||||||
|
await this.setState({
|
||||||
|
hasMorePosts: false,
|
||||||
|
loading: false,
|
||||||
|
initialLoading: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.setState({
|
||||||
|
posts: replace ? result : [...this.state.posts, ...result],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.setState({
|
||||||
|
loading: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (this.state.initialLoading) {
|
||||||
|
await this.setState({
|
||||||
|
initialLoading: false,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
componentDidMount() {
|
||||||
|
this.loadData()
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
return <div className="userPosts">
|
||||||
|
{
|
||||||
|
this.state.initialLoading ? <Skeleton active /> : <PostsList
|
||||||
|
loading={this.state.loading}
|
||||||
|
hasMorePosts={this.state.hasMorePosts}
|
||||||
|
emptyListRender={emptyListRender}
|
||||||
|
onLoadMore={this.loadData}
|
||||||
|
posts={this.state.posts}
|
||||||
|
/>
|
||||||
|
}
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
}
|
@ -38,6 +38,22 @@ export default class PostsController extends Controller {
|
|||||||
return res.json(posts)
|
return res.json(posts)
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
"/user_posts": {
|
||||||
|
middlewares: ["withOptionalAuthentication"],
|
||||||
|
fn: Schematized({
|
||||||
|
required: ["user_id"],
|
||||||
|
select: ["user_id"]
|
||||||
|
}, async (req, res) => {
|
||||||
|
let posts = await GetPostData({
|
||||||
|
limit: req.query?.limit,
|
||||||
|
skip: req.query?.trim,
|
||||||
|
for_user_id: req.user?._id.toString(),
|
||||||
|
from_user_id: req.query?.user_id,
|
||||||
|
})
|
||||||
|
|
||||||
|
return res.json(posts)
|
||||||
|
})
|
||||||
|
},
|
||||||
"/post": {
|
"/post": {
|
||||||
middlewares: ["withOptionalAuthentication"],
|
middlewares: ["withOptionalAuthentication"],
|
||||||
fn: Schematized({
|
fn: Schematized({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user