Use the new API for view channel (#155)

* Use the new API for view channel

* Fix view channel on websocket events
This commit is contained in:
enahum 2016-12-29 11:31:11 -03:00 committed by GitHub
parent 4ecd3758e7
commit aa5a77ae86
6 changed files with 46 additions and 19 deletions

View file

@ -366,27 +366,39 @@ export function deleteChannel(teamId, channelId) {
};
}
export function updateLastViewedAt(teamId, channelId, active) {
export function viewChannel(teamId, channelId, prevChannelId = '') {
return async (dispatch, getState) => {
dispatch({type: ChannelTypes.UPDATE_LAST_VIEWED_REQUEST}, getState);
try {
// this API should return the timestamp that was set
await Client.updateLastViewedAt(teamId, channelId, active);
await Client.viewChannel(teamId, channelId, prevChannelId);
} catch (error) {
forceLogoutIfNecessary(error, dispatch);
dispatch({type: ChannelTypes.UPDATE_LAST_VIEWED_FAILURE, error}, getState);
return;
}
dispatch(batchActions([
{
const actions = [{
type: ChannelTypes.RECEIVED_LAST_VIEWED,
data: {
channel_id: channelId,
last_viewed_at: new Date().getTime()
}
}];
if (prevChannelId) {
actions.push({
type: ChannelTypes.RECEIVED_LAST_VIEWED,
data: {
channel_id: channelId,
channel_id: prevChannelId,
last_viewed_at: new Date().getTime()
}
},
});
}
dispatch(batchActions([
...actions,
{
type: ChannelTypes.UPDATE_LAST_VIEWED_SUCCESS
}
@ -505,7 +517,7 @@ export default {
leaveChannel,
joinChannel,
deleteChannel,
updateLastViewedAt,
viewChannel,
getMoreChannels,
getChannelStats,
addChannelMember,

View file

@ -19,7 +19,7 @@ import {
fetchMyChannelsAndMembers,
getChannel,
getChannelStats,
updateLastViewedAt
viewChannel
} from 'service/actions/channels';
import {
@ -158,7 +158,7 @@ function handleNewPostEvent(msg, dispatch, getState) {
if (post.channel_id === channels.currentId) {
if (isActive) {
updateLastViewedAt(teamId, post.channel_id)(dispatch, getState);
viewChannel(teamId, post.channel_id)(dispatch, getState);
} else {
getChannel(teamId, post.channel_id)(dispatch, getState);
}
@ -208,7 +208,7 @@ function handlePostEdited(msg, dispatch, getState) {
if (msg.broadcast.channel_id === channels.currentId && isActive) {
// FIXME: Update post should include team_id in the message cause its not always the current team
// updateLastViewedAt(msg.data.team_id, data.channel_id)(dispatch, getState);
// viewChannel(msg.data.team_id, data.channel_id)(dispatch, getState);
}
}

View file

@ -486,10 +486,15 @@ export default class Client {
);
};
updateLastViewedAt = async (teamId, channelId, active) => {
viewChannel = async (teamId, channelId, prevChannelId = '') => {
const data = {
channel_id: channelId,
prev_channel_id: prevChannelId
};
return this.doFetch(
`${this.getChannelNeededRoute(teamId, channelId)}/update_last_viewed_at`,
{method: 'post', body: JSON.stringify({active})}
`${this.getChannelsRoute(teamId)}/view`,
{method: 'post', body: JSON.stringify(data)}
);
};

View file

@ -72,6 +72,8 @@ function myMembers(state = {}, action) {
case ChannelTypes.RECEIVED_LAST_VIEWED: {
const member = {...state[action.data.channel_id]};
member.last_viewed_at = action.data.last_viewed_at;
member.mention_count = 0;
member.msg_count = 0;
return {
...state,

View file

@ -257,16 +257,23 @@ describe('Actions.Channels', () => {
assert.ifError(myMembers[secondChannel.id]);
});
it('updateLastViewedAt', async () => {
it('viewChannel', async () => {
const userChannel = await Client.createChannel(
TestHelper.fakeChannel(TestHelper.basicTeam.id)
);
await Actions.fetchMyChannelsAndMembers(TestHelper.basicTeam.id)(store.dispatch, store.getState);
let members = store.getState().entities.channels.myMembers;
let member = members[TestHelper.basicChannel.id];
let otherMember = members[userChannel.id];
assert.ok(member);
assert.ok(otherMember);
const lastViewed = member.last_viewed_at;
await Actions.updateLastViewedAt(
await Actions.viewChannel(
TestHelper.basicTeam.id,
TestHelper.basicChannel.id, true)(store.dispatch, store.getState);
TestHelper.basicChannel.id,
userChannel.id
)(store.dispatch, store.getState);
const updateRequest = store.getState().requests.channels.updateLastViewedAt;
if (updateRequest.status === RequestStatus.FAILURE) {
@ -275,7 +282,9 @@ describe('Actions.Channels', () => {
members = store.getState().entities.channels.myMembers;
member = members[TestHelper.basicChannel.id];
otherMember = members[userChannel.id];
assert.ok(member.last_viewed_at > lastViewed);
assert.ok(otherMember.last_viewed_at > lastViewed);
});
it('getMoreChannels', async () => {

View file

@ -208,10 +208,9 @@ describe('Actions.Websocket', () => {
async function test() {
await TeamActions.selectTeam(TestHelper.basicTeam)(store.dispatch, store.getState);
await Client.updateLastViewedAt(
await Client.viewChannel(
TestHelper.basicTeam.id,
TestHelper.basicChannel.id,
false
TestHelper.basicChannel.id
);
setTimeout(() => {