You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
166 lines
4.9 KiB
166 lines
4.9 KiB
import { Action } from 'redux'
|
|
|
|
import { apiFetch } from 'src/api'
|
|
import { setEntities } from 'src/actions/entities'
|
|
import { listSet, listAppend } from 'src/actions/lists'
|
|
import { startRequest, finishRequest } from 'src/actions/requests'
|
|
|
|
import { objectToQuerystring } from 'src/utils'
|
|
import { normalize } from 'src/utils/normalization'
|
|
import { AppThunkAction, Entity, RequestKey, EntityType, User, Post, Attachment, EntityListKey } from 'src/types'
|
|
|
|
interface CreatePostResponse {
|
|
id: string
|
|
}
|
|
|
|
interface CreatePostOptions {
|
|
visible: boolean
|
|
text?: string
|
|
cover?: string
|
|
attachments: Attachment[]
|
|
data?: object
|
|
parent?: string
|
|
}
|
|
|
|
export const createPost = (options: CreatePostOptions): AppThunkAction<string> => {
|
|
return async dispatch => {
|
|
const { visible, text, cover, attachments, data, parent } = options
|
|
dispatch(startRequest(RequestKey.CreatePost))
|
|
|
|
try {
|
|
const post = await apiFetch<CreatePostResponse>({
|
|
path: `/api/post`,
|
|
method: 'post',
|
|
body: {
|
|
visible,
|
|
text,
|
|
cover,
|
|
attachments,
|
|
data,
|
|
parent,
|
|
},
|
|
})
|
|
|
|
dispatch(finishRequest(RequestKey.CreatePost, true))
|
|
return post.id
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.CreatePost, false))
|
|
throw err
|
|
}
|
|
}
|
|
}
|
|
|
|
interface FetchPostResponse {
|
|
post: Post,
|
|
parents: Post[],
|
|
children: Post[],
|
|
users: User[],
|
|
}
|
|
|
|
export const fetchPost = (id: string): AppThunkAction => {
|
|
return async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchPost))
|
|
|
|
try {
|
|
const response = await apiFetch<FetchPostResponse>({
|
|
path: `/api/post/${id}`
|
|
})
|
|
|
|
const parents = normalize(response.parents.map(p => ({
|
|
...p,
|
|
user: response.users.find(u => u.id === p.userId),
|
|
userId: undefined,
|
|
})), EntityType.Post)
|
|
|
|
dispatch(setEntities(parents.entities))
|
|
dispatch(listSet(`post:${id}:parents`, parents.keys))
|
|
|
|
const children = normalize(response.children.map(p => ({
|
|
...p,
|
|
user: response.users.find(u => u.id === p.userId),
|
|
userId: undefined,
|
|
})), EntityType.Post)
|
|
|
|
dispatch(setEntities(children.entities))
|
|
dispatch(listSet(`post:${id}:children`, children.keys))
|
|
|
|
const post: Entity = {
|
|
...response.post,
|
|
user: response.users.find(u => u.id === response.post.userId),
|
|
userId: undefined,
|
|
}
|
|
|
|
const posts = normalize([post], EntityType.Post)
|
|
dispatch(setEntities(posts.entities))
|
|
|
|
dispatch(finishRequest(RequestKey.FetchPost, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchPost, false))
|
|
throw err
|
|
}
|
|
}
|
|
}
|
|
|
|
interface TimelineResponse {
|
|
posts: Post[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchTimeline = (continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchTimeline))
|
|
|
|
try {
|
|
const response = await apiFetch<TimelineResponse>({
|
|
path: `/api/timeline?${objectToQuerystring({ continuation })}`,
|
|
})
|
|
|
|
const posts = normalize(response.posts, EntityType.Post)
|
|
|
|
dispatch(setEntities(posts.entities))
|
|
|
|
if (continuation) {
|
|
dispatch(listAppend(EntityListKey.Timeline, posts.keys, response.continuation))
|
|
} else {
|
|
dispatch(listSet(EntityListKey.Timeline, posts.keys, response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchTimeline, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchTimeline, false))
|
|
throw err
|
|
}
|
|
}
|
|
|
|
interface UserPostsResponse {
|
|
user: User
|
|
posts: Post[]
|
|
continuation?: string
|
|
}
|
|
|
|
export const fetchUserPosts = (id: string, continuation?: string): AppThunkAction => async dispatch => {
|
|
dispatch(startRequest(RequestKey.FetchUserPosts))
|
|
|
|
try {
|
|
const response = await apiFetch<UserPostsResponse>({
|
|
path: `/api/user/${id}/posts`,
|
|
})
|
|
|
|
const posts = normalize(response.posts.map(p => ({
|
|
...p,
|
|
user: response.user,
|
|
})), EntityType.Post)
|
|
|
|
dispatch(setEntities(posts.entities))
|
|
|
|
if (continuation) {
|
|
dispatch(listAppend(`posts:${id}`, posts.keys, response.continuation))
|
|
} else {
|
|
dispatch(listSet(`posts:${id}`, posts.keys, response.continuation))
|
|
}
|
|
|
|
dispatch(finishRequest(RequestKey.FetchUserPosts, true))
|
|
} catch (err) {
|
|
dispatch(finishRequest(RequestKey.FetchUserPosts, false))
|
|
throw err
|
|
}
|
|
}
|