pixelplanet/src/store/reducers/chat.js

194 lines
4.2 KiB
JavaScript
Raw Normal View History

2022-07-11 12:42:09 +00:00
import { MAX_CHAT_MESSAGES } from '../../core/constants';
const initialState = {
2020-11-26 18:17:00 +00:00
/*
* {
* cid: [
* name,
* type,
* lastTs,
* ],
* cid2: [
* name,
* type,
* lastTs,
* dmUserId,
2020-11-26 18:17:00 +00:00
* ],
* ...
* }
*/
channels: {},
// [[uId, userName], [userId2, userName2],...]
2020-11-24 15:44:33 +00:00
blocked: [],
// { cid: [message1,message2,message3,...]}
messages: {},
};
// used to give every message a unique incrementing key
let msgId = 0;
export default function chat(
state = initialState,
action,
) {
switch (action.type) {
2022-09-04 21:11:42 +00:00
case 's/REC_ME':
case 's/LOGIN': {
// making sure object keys are numbers
const channels = {};
const channelsJson = action.channels;
const cids = Object.keys(channelsJson);
for (let i = 0; i < cids.length; i += 1) {
const cid = cids[i];
channels[Number(cid)] = channelsJson[cid];
}
return {
...state,
channels,
2020-11-24 15:44:33 +00:00
blocked: action.blocked,
};
}
2022-09-04 21:11:42 +00:00
case 's/LOGOUT': {
2020-11-28 00:11:08 +00:00
const channels = { ...state.channels };
const messages = { ...state.messages };
const keys = Object.keys(channels);
for (let i = 0; i < keys.length; i += 1) {
2020-11-28 00:11:08 +00:00
const cid = keys[i];
if (channels[cid][1] !== 0) {
2020-11-28 00:11:08 +00:00
delete messages[cid];
delete channels[cid];
}
}
return {
...state,
channels,
blocked: [],
messages,
};
}
2022-09-04 21:11:42 +00:00
case 's/BLOCK_USER': {
2020-11-24 15:44:33 +00:00
const { userId, userName } = action;
const blocked = [
...state.blocked,
[userId, userName],
];
/*
* remove DM channel if exists
*/
const channels = { ...state.channels };
const chanKeys = Object.keys(channels);
for (let i = 0; i < chanKeys.length; i += 1) {
const cid = chanKeys[i];
if (channels[cid][1] === 1 && channels[cid][3] === userId) {
delete channels[cid];
return {
...state,
channels,
blocked,
};
}
}
2020-11-24 15:44:33 +00:00
return {
...state,
blocked,
2020-11-24 15:44:33 +00:00
};
}
2022-09-04 21:11:42 +00:00
case 's/UNBLOCK_USER': {
2020-11-24 15:44:33 +00:00
const { userId } = action;
const blocked = state.blocked.filter((bl) => (bl[0] !== userId));
return {
...state,
blocked,
};
}
case 's/ADD_CHAT_CHANNEL': {
const { channel } = action;
const cid = Number(Object.keys(channel)[0]);
if (state.channels[cid]) {
return state;
}
return {
...state,
2020-11-26 18:17:00 +00:00
channels: {
...state.channels,
...channel,
},
};
}
case 's/REMOVE_CHAT_CHANNEL': {
const { cid } = action;
if (!state.channels[cid]) {
return state;
}
2020-11-26 18:17:00 +00:00
const channels = { ...state.channels };
const messages = { ...state.messages };
delete messages[cid];
2020-11-26 18:17:00 +00:00
delete channels[cid];
return {
...state,
channels,
messages,
};
}
case 's/REC_CHAT_MESSAGE': {
const {
2020-11-08 14:36:22 +00:00
name, text, country, channel, user,
} = action;
2020-11-26 18:17:00 +00:00
if (!state.messages[channel] || !state.channels[channel]) {
return state;
}
const ts = Math.round(Date.now() / 1000);
msgId += 1;
const messages = {
...state.messages,
[channel]: [
...state.messages[channel],
[name, text, country, user, ts, msgId],
],
};
if (messages[channel].length > MAX_CHAT_MESSAGES) {
messages[channel].splice(0, 2);
}
2020-11-26 18:17:00 +00:00
/*
* update timestamp of last message
*/
const channelArray = [...state.channels[channel]];
channelArray[2] = Date.now();
return {
...state,
2020-11-26 18:17:00 +00:00
channels: {
...state.channels,
[channel]: channelArray,
},
messages,
};
}
case 's/REC_CHAT_HISTORY': {
const { cid, history } = action;
for (let i = 0; i < history.length; i += 1) {
msgId += 1;
history[i].push(msgId);
}
return {
...state,
messages: {
...state.messages,
[cid]: history,
},
};
}
default:
return state;
}
}