2016-11-09 14:29:30 +00:00
|
|
|
const { find, omit, mapValues, values } = require('lodash')
|
2016-10-21 13:29:04 +00:00
|
|
|
const {
|
|
|
|
JOIN_MAP,
|
|
|
|
LEAVE_MAP,
|
|
|
|
JOIN_CALL,
|
|
|
|
LEAVE_CALL
|
|
|
|
} = require('../frontend/src/Metamaps/Realtime/events')
|
|
|
|
|
|
|
|
const NOT_IN_CONVERSATION = 0
|
|
|
|
const IN_CONVERSATION = 1
|
|
|
|
|
2016-11-08 19:42:22 +00:00
|
|
|
const addMapperToMap = (map, userId) => Object.assign({}, map, { [userId]: NOT_IN_CONVERSATION })
|
2016-11-09 14:29:30 +00:00
|
|
|
const userStillPresent = (userId, liveMaps) => {
|
2016-11-08 19:42:22 +00:00
|
|
|
if (!userId) return false
|
|
|
|
let stillPresent = false
|
|
|
|
const userIdString = userId.toString()
|
|
|
|
values(liveMaps).forEach(presentUsers => {
|
|
|
|
if (find(Object.keys(presentUsers), id => id === userIdString)) stillPresent = true
|
|
|
|
})
|
|
|
|
return stillPresent
|
|
|
|
}
|
2016-10-21 13:29:04 +00:00
|
|
|
|
|
|
|
const reducer = (state = { connectedPeople: {}, liveMaps: {} }, action) => {
|
|
|
|
const { type, payload } = action
|
|
|
|
const { connectedPeople, liveMaps } = state
|
|
|
|
const map = payload && liveMaps[payload.mapid]
|
|
|
|
const mapWillEmpty = map && Object.keys(map).length === 1
|
|
|
|
const callWillFinish = map && (type === LEAVE_CALL || type === 'DISCONNECT') && Object.keys(map).length === 2
|
|
|
|
|
|
|
|
switch (type) {
|
2016-11-07 20:25:08 +00:00
|
|
|
case JOIN_MAP:
|
|
|
|
return Object.assign({}, state, {
|
|
|
|
connectedPeople: Object.assign({}, connectedPeople, {
|
|
|
|
[payload.userid]: {
|
|
|
|
id: payload.userid,
|
|
|
|
username: payload.username,
|
|
|
|
avatar: payload.avatar
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
liveMaps: Object.assign({}, liveMaps, {
|
|
|
|
[payload.mapid]: addMapperToMap(map || {}, payload.userid)
|
|
|
|
})
|
2016-10-21 13:29:04 +00:00
|
|
|
})
|
2016-11-07 20:25:08 +00:00
|
|
|
case LEAVE_MAP:
|
2016-10-21 13:29:04 +00:00
|
|
|
// if the map will empty, remove it from liveMaps, if the map will not empty, just remove the mapper
|
2016-11-07 20:25:08 +00:00
|
|
|
const newLiveMaps = mapWillEmpty
|
2016-10-21 13:29:04 +00:00
|
|
|
? omit(liveMaps, payload.mapid)
|
|
|
|
: Object.assign({}, liveMaps, { [payload.mapid]: omit(map, payload.userid) })
|
2016-11-09 14:29:30 +00:00
|
|
|
delete newLiveMaps[undefined]
|
|
|
|
delete newLiveMaps[null]
|
|
|
|
const updateConnectedPeople = userStillPresent(payload.userid, newLiveMaps) ? connectedPeople : omit(connectedPeople, payload.userid)
|
2016-10-21 13:29:04 +00:00
|
|
|
|
2016-11-07 20:25:08 +00:00
|
|
|
return {
|
2016-11-09 14:29:30 +00:00
|
|
|
connectedPeople: updateConnectedPeople,
|
|
|
|
liveMaps: newLiveMaps
|
2016-11-07 20:25:08 +00:00
|
|
|
}
|
|
|
|
case JOIN_CALL:
|
2016-10-21 13:29:04 +00:00
|
|
|
// update the user (payload.id is user id) in the given map to be marked in the conversation
|
2016-11-07 20:25:08 +00:00
|
|
|
return Object.assign({}, state, {
|
|
|
|
liveMaps: Object.assign({}, liveMaps, {
|
|
|
|
[payload.mapid]: Object.assign({}, map, {
|
|
|
|
[payload.id]: IN_CONVERSATION
|
|
|
|
})
|
2016-10-21 13:29:04 +00:00
|
|
|
})
|
|
|
|
})
|
2016-11-07 20:25:08 +00:00
|
|
|
case LEAVE_CALL:
|
|
|
|
const newMap = callWillFinish
|
2016-10-21 13:29:04 +00:00
|
|
|
? mapValues(map, () => NOT_IN_CONVERSATION)
|
|
|
|
: Object.assign({}, map, { [payload.userid]: NOT_IN_CONVERSATION })
|
|
|
|
|
2016-11-07 20:25:08 +00:00
|
|
|
return Object.assign({}, state, {
|
2016-11-09 14:29:30 +00:00
|
|
|
liveMaps: Object.assign({}, liveMaps, { [payload.mapid]: newMap })
|
2016-11-07 20:25:08 +00:00
|
|
|
})
|
|
|
|
case 'DISCONNECT':
|
|
|
|
const mapWithoutUser = omit(map, payload.userid)
|
|
|
|
const newMapWithoutUser = callWillFinish ? mapValues(mapWithoutUser, () => NOT_IN_CONVERSATION) : mapWithoutUser
|
|
|
|
const newLiveMapsWithoutUser = mapWillEmpty ? omit(liveMaps, payload.mapid) : Object.assign({}, liveMaps, { [payload.mapid]: newMapWithoutUser })
|
2016-11-09 14:29:30 +00:00
|
|
|
delete newLiveMapsWithoutUser[undefined]
|
|
|
|
delete newLiveMapsWithoutUser[null]
|
|
|
|
const newConnectedPeople = userStillPresent(payload.userid, newLiveMapsWithoutUser) ? connectedPeople : omit(connectedPeople, payload.userid)
|
2016-11-07 20:25:08 +00:00
|
|
|
return {
|
2016-11-09 14:29:30 +00:00
|
|
|
connectedPeople: newConnectedPeople,
|
|
|
|
liveMaps: newLiveMapsWithoutUser
|
2016-11-07 20:25:08 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
return state
|
2016-10-21 13:29:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = reducer
|