From ba6e370ca71abacaa30234cb164427d27c86df13 Mon Sep 17 00:00:00 2001 From: Joram Wilander Date: Wed, 21 Dec 2016 16:35:01 -0500 Subject: PLT-5012 Combine updateLastViewedAt, setLastViewedAt and setActiveChannel into a single API (#4840) * Combine updateLastViewedAt, setLastViewedAt and setActiveChannel into a single API * Remove preference DB writes --- webapp/actions/channel_actions.jsx | 2 +- webapp/actions/global_actions.jsx | 2 +- webapp/actions/post_actions.jsx | 2 +- webapp/actions/websocket_actions.jsx | 2 +- webapp/client/client.jsx | 13 ++++ webapp/components/needs_team.jsx | 4 +- webapp/components/post_view/post_view_cache.jsx | 2 +- webapp/tests/client_channel.test.jsx | 17 +++++ webapp/utils/async_client.jsx | 94 +++---------------------- 9 files changed, 46 insertions(+), 92 deletions(-) (limited to 'webapp') diff --git a/webapp/actions/channel_actions.jsx b/webapp/actions/channel_actions.jsx index ad2f315ee..952c8ada3 100644 --- a/webapp/actions/channel_actions.jsx +++ b/webapp/actions/channel_actions.jsx @@ -53,7 +53,7 @@ export function executeCommand(message, args, success, error) { export function setChannelAsRead(channelIdParam) { const channelId = channelIdParam || ChannelStore.getCurrentId(); - AsyncClient.updateLastViewedAt(); + AsyncClient.viewChannel(); ChannelStore.resetCounts(channelId); ChannelStore.emitChange(); if (channelId === ChannelStore.getCurrentId()) { diff --git a/webapp/actions/global_actions.jsx b/webapp/actions/global_actions.jsx index 9d135dd26..e1009e9c2 100644 --- a/webapp/actions/global_actions.jsx +++ b/webapp/actions/global_actions.jsx @@ -48,7 +48,7 @@ export function emitChannelClickEvent(channel) { getMyChannelMembersPromise.then(() => { AsyncClient.getChannelStats(chan.id, true); - AsyncClient.updateLastViewedAt(chan.id); + AsyncClient.viewChannel(chan.id, ChannelStore.getCurrentId()); loadPosts(chan.id); trackPage(); }); diff --git a/webapp/actions/post_actions.jsx b/webapp/actions/post_actions.jsx index d1e69cda7..71b9e826e 100644 --- a/webapp/actions/post_actions.jsx +++ b/webapp/actions/post_actions.jsx @@ -22,7 +22,7 @@ export function handleNewPost(post, msg) { if (ChannelStore.getCurrentId() === post.channel_id) { if (window.isActive) { - AsyncClient.updateLastViewedAt(null, false); + AsyncClient.viewChannel(); } else { AsyncClient.getChannel(post.channel_id); } diff --git a/webapp/actions/websocket_actions.jsx b/webapp/actions/websocket_actions.jsx index 6c81a4ac9..f24802003 100644 --- a/webapp/actions/websocket_actions.jsx +++ b/webapp/actions/websocket_actions.jsx @@ -205,7 +205,7 @@ function handlePostEditEvent(msg) { // Update channel state if (ChannelStore.getCurrentId() === msg.broadcast.channel_id) { if (window.isActive) { - AsyncClient.updateLastViewedAt(null, false); + AsyncClient.viewChannel(); } } } diff --git a/webapp/client/client.jsx b/webapp/client/client.jsx index 3ec36644f..c5743ae7b 100644 --- a/webapp/client/client.jsx +++ b/webapp/client/client.jsx @@ -1187,6 +1187,7 @@ export default class Client { end(this.handleResponse.bind(this, 'getStatuses', success, error)); } + // SCHEDULED FOR DEPRECATION IN 3.8 - use viewChannel instead setActiveChannel(id, success, error) { request. post(`${this.getUsersRoute()}/status/set_active_channel`). @@ -1366,6 +1367,17 @@ export default class Client { this.track('api', 'api_channels_delete'); } + viewChannel(channelId, prevChannelId = '', time = 0, success, error) { + request. + post(`${this.getChannelsRoute()}/view`). + set(this.defaultHeaders). + type('application/json'). + accept('application/json'). + send({channel_id: channelId, prev_channel_id: prevChannelId, time}). + end(this.handleResponse.bind(this, 'viewChannel', success, error)); + } + + // SCHEDULED FOR DEPRECATION IN 3.8 - use viewChannel instead updateLastViewedAt(channelId, active, success, error) { request. post(`${this.getChannelNeededRoute(channelId)}/update_last_viewed_at`). @@ -1376,6 +1388,7 @@ export default class Client { end(this.handleResponse.bind(this, 'updateLastViewedAt', success, error)); } + // SCHEDULED FOR DEPRECATION IN 3.8 - use viewChannel instead setLastViewedAt(channelId, lastViewedAt, success, error) { request. post(`${this.getChannelNeededRoute(channelId)}/set_last_viewed_at`). diff --git a/webapp/components/needs_team.jsx b/webapp/components/needs_team.jsx index f90297065..0b91814c3 100644 --- a/webapp/components/needs_team.jsx +++ b/webapp/components/needs_team.jsx @@ -94,7 +94,7 @@ export default class NeedsTeam extends React.Component { // Set up tracking for whether the window is active window.isActive = true; $(window).on('focus', () => { - AsyncClient.updateLastViewedAt(); + AsyncClient.viewChannel(); ChannelStore.resetCounts(ChannelStore.getCurrentId()); ChannelStore.emitChange(); window.isActive = true; @@ -103,7 +103,7 @@ export default class NeedsTeam extends React.Component { $(window).on('blur', () => { window.isActive = false; if (UserStore.getCurrentUser()) { - AsyncClient.setActiveChannel(''); + AsyncClient.viewChannel(''); } }); diff --git a/webapp/components/post_view/post_view_cache.jsx b/webapp/components/post_view/post_view_cache.jsx index 3b6123b09..7de11d667 100644 --- a/webapp/components/post_view/post_view_cache.jsx +++ b/webapp/components/post_view/post_view_cache.jsx @@ -32,7 +32,7 @@ export default class PostViewCache extends React.Component { componentWillUnmount() { if (UserStore.getCurrentUser()) { - AsyncClient.setActiveChannel(''); + AsyncClient.viewChannel(''); } ChannelStore.removeChangeListener(this.onChannelChange); } diff --git a/webapp/tests/client_channel.test.jsx b/webapp/tests/client_channel.test.jsx index 08c821f3c..77f9f9653 100644 --- a/webapp/tests/client_channel.test.jsx +++ b/webapp/tests/client_channel.test.jsx @@ -211,6 +211,23 @@ describe('Client.Channels', function() { }); }); + it('viewChannel', function(done) { + TestHelper.initBasic(() => { + var channel = TestHelper.basicChannel(); + TestHelper.basicClient().viewChannel( + channel.id, + '', + 0, + function() { + done(); + }, + function(err) { + done(new Error(err.message)); + } + ); + }); + }); + it('updateLastViewedAt', function(done) { TestHelper.initBasic(() => { var channel = TestHelper.basicChannel(); diff --git a/webapp/utils/async_client.jsx b/webapp/utils/async_client.jsx index 2d8e76fc2..66f13f04d 100644 --- a/webapp/utils/async_client.jsx +++ b/webapp/utils/async_client.jsx @@ -138,33 +138,20 @@ export function getMyChannelMembers() { }); } -export function updateLastViewedAt(id, active) { - let channelId; - if (id) { - channelId = id; - } else { - channelId = ChannelStore.getCurrentId(); - } - +export function viewChannel(channelId = ChannelStore.getCurrentId(), prevChannelId = '', time = 0) { if (channelId == null) { return; } - if (isCallInProgress(`updateLastViewed${channelId}`)) { + if (isCallInProgress(`viewChannel${channelId}`)) { return; } - let isActive; - if (active == null) { - isActive = true; - } else { - isActive = active; - } - - callTracker[`updateLastViewed${channelId}`] = utils.getTimestamp(); - Client.updateLastViewedAt( + callTracker[`viewChannel${channelId}`] = utils.getTimestamp(); + Client.viewChannel( channelId, - isActive, + prevChannelId, + time, () => { AppDispatcher.handleServerAction({ type: ActionTypes.RECEIVED_PREFERENCE, @@ -175,59 +162,14 @@ export function updateLastViewedAt(id, active) { } }); - callTracker[`updateLastViewed${channelId}`] = 0; + callTracker[`viewChannel${channelId}`] = 0; ErrorStore.clearLastError(); }, (err) => { - callTracker[`updateLastViewed${channelId}`] = 0; + callTracker[`viewChannel${channelId}`] = 0; const count = ErrorStore.getConnectionErrorCount(); ErrorStore.setConnectionErrorCount(count + 1); - dispatchError(err, 'updateLastViewedAt'); - } - ); -} - -export function setLastViewedAt(lastViewedAt, id) { - let channelId; - if (id) { - channelId = id; - } else { - channelId = ChannelStore.getCurrentId(); - } - - if (channelId == null) { - return; - } - - if (lastViewedAt == null) { - return; - } - - if (isCallInProgress(`setLastViewedAt${channelId}${lastViewedAt}`)) { - return; - } - - callTracker[`setLastViewedAt${channelId}${lastViewedAt}`] = utils.getTimestamp(); - Client.setLastViewedAt( - channelId, - lastViewedAt, - () => { - AppDispatcher.handleServerAction({ - type: ActionTypes.RECEIVED_PREFERENCE, - preference: { - category: 'last', - name: TeamStore.getCurrentId(), - value: channelId - } - }); - callTracker[`setLastViewedAt${channelId}${lastViewedAt}`] = 0; - ErrorStore.clearLastError(); - }, - (err) => { - callTracker[`setLastViewedAt${channelId}${lastViewedAt}`] = 0; - var count = ErrorStore.getConnectionErrorCount(); - ErrorStore.setConnectionErrorCount(count + 1); - dispatchError(err, 'setLastViewedAt'); + dispatchError(err, 'viewChannel'); } ); } @@ -795,24 +737,6 @@ export function getStatuses() { ); } -export function setActiveChannel(channelId) { - if (isCallInProgress(`setActiveChannel${channelId}`)) { - return; - } - - callTracker[`setActiveChannel${channelId}`] = utils.getTimestamp(); - Client.setActiveChannel( - channelId, - () => { - callTracker[`setActiveChannel${channelId}`] = 0; - }, - (err) => { - callTracker[`setActiveChannel${channelId}`] = 0; - dispatchError(err, 'setActiveChannel'); - } - ); -} - export function getMyTeam() { if (isCallInProgress('getMyTeam')) { return null; -- cgit v1.2.3-1-g7c22