implement user_posts endpoint

This commit is contained in:
srgooglo 2022-11-14 02:29:54 +00:00
parent 5714a16524
commit 8a859d0b4f
3 changed files with 114 additions and 7 deletions

View File

@ -90,4 +90,23 @@ export default class Post {
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
}
}

View File

@ -1,11 +1,83 @@
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) => {
return <div className="posts">
<PostsFeed
fromUserId={props.state.user._id}
/>
const emptyListRender = () => {
return <div className="emptyFeed">
<h2>
This user has no posts yet.
</h2>
</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>
}
}

View File

@ -38,6 +38,22 @@ export default class PostsController extends Controller {
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": {
middlewares: ["withOptionalAuthentication"],
fn: Schematized({