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
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
139cb52c99
Коммит
ba6e370ca7
@@ -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()) {
|
||||
|
||||
@@ -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();
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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`).
|
||||
|
||||
@@ -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('');
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -32,7 +32,7 @@ export default class PostViewCache extends React.Component {
|
||||
|
||||
componentWillUnmount() {
|
||||
if (UserStore.getCurrentUser()) {
|
||||
AsyncClient.setActiveChannel('');
|
||||
AsyncClient.viewChannel('');
|
||||
}
|
||||
ChannelStore.removeChangeListener(this.onChannelChange);
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Ссылка в новой задаче
Block a user