Skip to content

Commit

Permalink
PLT-5012 Combine updateLastViewedAt, setLastViewedAt and setActiveCha…
Browse files Browse the repository at this point in the history
…nnel into a single API (mattermost#4840)

* Combine updateLastViewedAt, setLastViewedAt and setActiveChannel into a single API

* Remove preference DB writes
  • Loading branch information
jwilander authored and crspeller committed Dec 21, 2016
1 parent 113fde9 commit b4ba818
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 92 deletions.
2 changes: 1 addition & 1 deletion actions/channel_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
2 changes: 1 addition & 1 deletion actions/global_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
2 changes: 1 addition & 1 deletion actions/post_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Expand Down
13 changes: 13 additions & 0 deletions client/client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Expand Down Expand Up @@ -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`).
Expand All @@ -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`).
Expand Down
4 changes: 2 additions & 2 deletions components/needs_team.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -103,7 +103,7 @@ export default class NeedsTeam extends React.Component {
$(window).on('blur', () => {
window.isActive = false;
if (UserStore.getCurrentUser()) {
AsyncClient.setActiveChannel('');
AsyncClient.viewChannel('');
}
});

Expand Down
2 changes: 1 addition & 1 deletion components/post_view/post_view_cache.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export default class PostViewCache extends React.Component {

componentWillUnmount() {
if (UserStore.getCurrentUser()) {
AsyncClient.setActiveChannel('');
AsyncClient.viewChannel('');
}
ChannelStore.removeChangeListener(this.onChannelChange);
}
Expand Down
17 changes: 17 additions & 0 deletions tests/client_channel.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
94 changes: 9 additions & 85 deletions utils/async_client.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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');
}
);
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit b4ba818

Please sign in to comment.