summaryrefslogtreecommitdiffstats
path: root/webapp/actions/post_actions.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'webapp/actions/post_actions.jsx')
-rw-r--r--webapp/actions/post_actions.jsx153
1 files changed, 152 insertions, 1 deletions
diff --git a/webapp/actions/post_actions.jsx b/webapp/actions/post_actions.jsx
index 63e3feec5..462576021 100644
--- a/webapp/actions/post_actions.jsx
+++ b/webapp/actions/post_actions.jsx
@@ -8,6 +8,8 @@ import PostStore from 'stores/post_store.jsx';
import TeamStore from 'stores/team_store.jsx';
import UserStore from 'stores/user_store.jsx';
+import {loadStatusesForChannel} from 'actions/status_actions.jsx';
+
import * as PostUtils from 'utils/post_utils.jsx';
import Client from 'client/web_client.jsx';
import * as AsyncClient from 'utils/async_client.jsx';
@@ -52,6 +54,8 @@ export function handleNewPost(post, msg) {
post,
websocketMessageProps
});
+
+ loadProfilesForPosts(data.posts);
},
(err) => {
AsyncClient.dispatchError(err, 'getPost');
@@ -115,7 +119,7 @@ export function setUnreadPost(channelId, postId) {
member.last_viewed_at = lastViewed;
member.msg_count = channel.total_msg_count - unreadPosts;
member.mention_count = 0;
- ChannelStore.setChannelMember(member);
+ ChannelStore.storeMyChannelMember(member);
ChannelStore.setUnreadCount(channelId);
AsyncClient.setLastViewedAt(lastViewed, channelId);
}
@@ -153,9 +157,156 @@ export function getFlaggedPosts() {
results: data,
is_flagged_posts: true
});
+
+ loadProfilesForPosts(data.posts);
},
(err) => {
AsyncClient.dispatchError(err, 'getFlaggedPosts');
}
);
}
+
+export function loadPosts(channelId = ChannelStore.getCurrentId()) {
+ const postList = PostStore.getAllPosts(channelId);
+ const latestPostTime = PostStore.getLatestPostFromPageTime(channelId);
+
+ if (!postList || Object.keys(postList).length === 0 || postList.order.length < Constants.POST_CHUNK_SIZE || latestPostTime === 0) {
+ loadPostsPage(channelId, Constants.POST_CHUNK_SIZE);
+ return;
+ }
+
+ Client.getPosts(
+ channelId,
+ latestPostTime,
+ (data) => {
+ AppDispatcher.handleServerAction({
+ type: ActionTypes.RECEIVED_POSTS,
+ id: channelId,
+ before: true,
+ numRequested: 0,
+ post_list: data
+ });
+
+ loadProfilesForPosts(data.posts);
+ loadStatusesForChannel(channelId);
+ },
+ (err) => {
+ AsyncClient.dispatchError(err, 'loadPosts');
+ }
+ );
+}
+
+export function loadPostsPage(channelId = ChannelStore.getCurrentId(), max = Constants.POST_CHUNK_SIZE) {
+ const postList = PostStore.getAllPosts(channelId);
+
+ // if we already have more than POST_CHUNK_SIZE posts,
+ // let's get the amount we have but rounded up to next multiple of POST_CHUNK_SIZE,
+ // with a max
+ let numPosts = Math.min(max, Constants.POST_CHUNK_SIZE);
+ if (postList && postList.order.length > 0) {
+ numPosts = Math.min(max, Constants.POST_CHUNK_SIZE * Math.ceil(postList.order.length / Constants.POST_CHUNK_SIZE));
+ }
+
+ Client.getPostsPage(
+ channelId,
+ 0,
+ numPosts,
+ (data) => {
+ AppDispatcher.handleServerAction({
+ type: ActionTypes.RECEIVED_POSTS,
+ id: channelId,
+ before: true,
+ numRequested: numPosts,
+ checkLatest: true,
+ post_list: data
+ });
+
+ loadProfilesForPosts(data.posts);
+ loadStatusesForChannel(channelId);
+ },
+ (err) => {
+ AsyncClient.dispatchError(err, 'loadPostsPage');
+ }
+ );
+}
+
+export function loadPostsBefore(postId, offset, numPost, isPost) {
+ const channelId = ChannelStore.getCurrentId();
+ if (channelId == null) {
+ return;
+ }
+
+ Client.getPostsBefore(
+ channelId,
+ postId,
+ offset,
+ numPost,
+ (data) => {
+ AppDispatcher.handleServerAction({
+ type: ActionTypes.RECEIVED_POSTS,
+ id: channelId,
+ before: true,
+ numRequested: numPost,
+ post_list: data,
+ isPost
+ });
+
+ loadProfilesForPosts(data.posts);
+ loadStatusesForChannel(channelId);
+ },
+ (err) => {
+ AsyncClient.dispatchError(err, 'loadPostsBefore');
+ }
+ );
+}
+
+export function loadPostsAfter(postId, offset, numPost, isPost) {
+ const channelId = ChannelStore.getCurrentId();
+ if (channelId == null) {
+ return;
+ }
+
+ Client.getPostsAfter(
+ channelId,
+ postId,
+ offset,
+ numPost,
+ (data) => {
+ AppDispatcher.handleServerAction({
+ type: ActionTypes.RECEIVED_POSTS,
+ id: channelId,
+ before: false,
+ numRequested: numPost,
+ post_list: data,
+ isPost
+ });
+
+ loadProfilesForPosts(data.posts);
+ loadStatusesForChannel(channelId);
+ },
+ (err) => {
+ AsyncClient.dispatchError(err, 'loadPostsAfter');
+ }
+ );
+}
+
+function loadProfilesForPosts(posts) {
+ const profilesToLoad = {};
+ for (const pid in posts) {
+ if (!posts.hasOwnProperty(pid)) {
+ continue;
+ }
+
+ const post = posts[pid];
+ if (!UserStore.hasProfile(post.user_id)) {
+ profilesToLoad[post.user_id] = true;
+ }
+ }
+
+ const list = Object.keys(profilesToLoad);
+ if (list.length === 0) {
+ return;
+ }
+
+ AsyncClient.getProfilesByIds(list);
+}