refactor: message handler
This commit is contained in:
@@ -1,6 +1,12 @@
|
|||||||
import { createSlice } from "@reduxjs/toolkit";
|
import { createSlice } from "@reduxjs/toolkit";
|
||||||
import { isObjectEqual } from "../../common/utils";
|
import {
|
||||||
|
msgReaction,
|
||||||
|
msgAdd,
|
||||||
|
msgSetRead,
|
||||||
|
msgClearUnread,
|
||||||
|
msgUpdate,
|
||||||
|
msgDelete,
|
||||||
|
} from "./message.handler";
|
||||||
const initialState = {};
|
const initialState = {};
|
||||||
|
|
||||||
const channelMsgSlice = createSlice({
|
const channelMsgSlice = createSlice({
|
||||||
@@ -14,85 +20,22 @@ const channelMsgSlice = createSlice({
|
|||||||
return action.payload;
|
return action.payload;
|
||||||
},
|
},
|
||||||
addChannelMsg(state, action) {
|
addChannelMsg(state, action) {
|
||||||
const {
|
msgAdd(state, action.payload);
|
||||||
id,
|
|
||||||
content,
|
|
||||||
created_at,
|
|
||||||
mid,
|
|
||||||
from_uid,
|
|
||||||
content_type,
|
|
||||||
unread = true,
|
|
||||||
} = action.payload;
|
|
||||||
const newMsg = { content, content_type, created_at, from_uid, unread };
|
|
||||||
if (state[id]) {
|
|
||||||
let replaceMsg = state[id][mid];
|
|
||||||
// 如果存在,并且新消息和缓存消息不一样,则替换掉,并且改为已读(可能有问题)
|
|
||||||
if (replaceMsg) {
|
|
||||||
const copyMsg = { ...replaceMsg };
|
|
||||||
if (!isObjectEqual(copyMsg, newMsg)) {
|
|
||||||
state[id][mid] = { ...newMsg, unread: false };
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state[id][mid] = newMsg;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state[id] = { [mid]: newMsg };
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
deleteChannelMsg(state, action) {
|
deleteChannelMsg(state, action) {
|
||||||
const { id, mid } = action.payload;
|
msgDelete(state, action.payload);
|
||||||
console.log("delete channel message", id, mid, state[id][mid]);
|
|
||||||
if (state[id][mid]) {
|
|
||||||
// 添加removed标识
|
|
||||||
// state[id][mid]["removed"] = true;
|
|
||||||
delete state[id][mid];
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
updateChannelMsg(state, action) {
|
updateChannelMsg(state, action) {
|
||||||
const { id, mid, content, time } = action.payload;
|
msgUpdate(state, action.payload);
|
||||||
console.log("update channel message", id, mid);
|
|
||||||
if (state[id][mid]) {
|
|
||||||
state[id][mid].content = content;
|
|
||||||
state[id][mid].edited = time;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
likeChannelMsg(state, action) {
|
likeChannelMsg(state, action) {
|
||||||
const { id, from_uid, mid, action: reaction } = action.payload;
|
msgReaction(state, action.payload);
|
||||||
console.log("channel likes: likes", id, mid, from_uid, reaction);
|
|
||||||
if (state[id] && state[id][mid]) {
|
|
||||||
if (!state[id][mid].likes) {
|
|
||||||
console.log("channel likes: initial ");
|
|
||||||
state[id][mid].likes = {};
|
|
||||||
}
|
|
||||||
const currLikes = state[id][mid].likes;
|
|
||||||
// state[id][mid].likes = currLikes ? [...currLikes, reaction] : [reaction];
|
|
||||||
if (currLikes[reaction]) {
|
|
||||||
if (currLikes[reaction].includes(from_uid)) {
|
|
||||||
const idx = currLikes[reaction].findIndex((id) => {
|
|
||||||
return id == from_uid;
|
|
||||||
});
|
|
||||||
console.log("remove reaction", currLikes[reaction], idx, from_uid);
|
|
||||||
currLikes[reaction].splice(idx, 1);
|
|
||||||
} else {
|
|
||||||
currLikes[reaction].push(from_uid);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currLikes[reaction] = [from_uid];
|
|
||||||
}
|
|
||||||
// state[id][mid].likes = currLikes;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
setChannelMsgRead(state, action) {
|
setChannelMsgRead(state, action) {
|
||||||
const { id, mid } = action.payload;
|
msgSetRead(state, action.payload);
|
||||||
console.log("set unread", id, mid);
|
|
||||||
state[id][mid].unread = false;
|
|
||||||
},
|
},
|
||||||
clearChannelMsgUnread(state, action) {
|
clearChannelMsgUnread(state, action) {
|
||||||
const gid = action.payload;
|
msgClearUnread(state, action.payload);
|
||||||
console.log("set channel all unread", gid);
|
|
||||||
Object.entries(state[gid]).forEach(([, obj]) => {
|
|
||||||
obj.unread = false;
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -0,0 +1,82 @@
|
|||||||
|
import { isObjectEqual } from "../../common/utils";
|
||||||
|
|
||||||
|
export const msgReaction = (state, payload) => {
|
||||||
|
const { id, from_uid, mid, action: reaction } = payload;
|
||||||
|
console.log("msg reaction: likes", id, mid, from_uid, reaction);
|
||||||
|
if (state[id] && state[id][mid]) {
|
||||||
|
if (!state[id][mid].likes) {
|
||||||
|
console.log("msg reaction: initial ");
|
||||||
|
state[id][mid].likes = {};
|
||||||
|
}
|
||||||
|
const currLikes = state[id][mid].likes;
|
||||||
|
// state[id][mid].likes = currLikes ? [...currLikes, reaction] : [reaction];
|
||||||
|
if (currLikes[reaction]) {
|
||||||
|
if (currLikes[reaction].includes(from_uid)) {
|
||||||
|
const idx = currLikes[reaction].findIndex((id) => {
|
||||||
|
return id == from_uid;
|
||||||
|
});
|
||||||
|
console.log("remove reaction", currLikes[reaction], idx, from_uid);
|
||||||
|
currLikes[reaction].splice(idx, 1);
|
||||||
|
} else {
|
||||||
|
currLikes[reaction].push(from_uid);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currLikes[reaction] = [from_uid];
|
||||||
|
}
|
||||||
|
// state[id][mid].likes = currLikes;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
export const msgAdd = (state, payload) => {
|
||||||
|
const {
|
||||||
|
id,
|
||||||
|
content,
|
||||||
|
created_at,
|
||||||
|
mid,
|
||||||
|
from_uid,
|
||||||
|
content_type,
|
||||||
|
unread = true,
|
||||||
|
} = payload;
|
||||||
|
const newMsg = { content, content_type, created_at, from_uid, unread };
|
||||||
|
if (state[id]) {
|
||||||
|
let replaceMsg = state[id][mid];
|
||||||
|
// 如果存在,并且新消息和缓存消息不一样,则替换掉,并且改为已读(可能有问题)
|
||||||
|
if (replaceMsg) {
|
||||||
|
const copyMsg = { ...replaceMsg };
|
||||||
|
if (!isObjectEqual(copyMsg, newMsg)) {
|
||||||
|
state[id][mid] = { ...newMsg, unread: false };
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state[id][mid] = newMsg;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
state[id] = { [mid]: newMsg };
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const msgSetRead = (state, payload) => {
|
||||||
|
const { id, mid } = payload;
|
||||||
|
console.log("set read", id, mid);
|
||||||
|
state[id][mid].unread = false;
|
||||||
|
};
|
||||||
|
export const msgDelete = (state, payload) => {
|
||||||
|
const { id, mid } = payload;
|
||||||
|
console.log("delete message", id, mid);
|
||||||
|
if (state[id][mid]) {
|
||||||
|
// state[id][mid].removed = true;
|
||||||
|
delete state[id][mid];
|
||||||
|
}
|
||||||
|
};
|
||||||
|
export const msgClearUnread = (state, id) => {
|
||||||
|
console.log("set all unread", id);
|
||||||
|
Object.entries(state[id]).forEach(([, obj]) => {
|
||||||
|
obj.unread = false;
|
||||||
|
});
|
||||||
|
};
|
||||||
|
export const msgUpdate = (state, payload) => {
|
||||||
|
const { id, mid, content, time } = payload;
|
||||||
|
console.log("update channel message", id, mid);
|
||||||
|
if (state[id][mid]) {
|
||||||
|
state[id][mid].content = content;
|
||||||
|
state[id][mid].edited = time;
|
||||||
|
}
|
||||||
|
};
|
||||||
@@ -1,6 +1,11 @@
|
|||||||
import { createSlice } from "@reduxjs/toolkit";
|
import { createSlice } from "@reduxjs/toolkit";
|
||||||
import { isObjectEqual } from "../../common/utils";
|
import {
|
||||||
|
msgReaction,
|
||||||
|
msgAdd,
|
||||||
|
msgSetRead,
|
||||||
|
msgUpdate,
|
||||||
|
msgDelete,
|
||||||
|
} from "./message.handler";
|
||||||
const initialState = {};
|
const initialState = {};
|
||||||
const userMsgSlice = createSlice({
|
const userMsgSlice = createSlice({
|
||||||
name: "userMessage",
|
name: "userMessage",
|
||||||
@@ -13,83 +18,19 @@ const userMsgSlice = createSlice({
|
|||||||
return action.payload;
|
return action.payload;
|
||||||
},
|
},
|
||||||
addUserMsg(state, action) {
|
addUserMsg(state, action) {
|
||||||
const {
|
msgAdd(state, action.payload);
|
||||||
id,
|
|
||||||
content,
|
|
||||||
content_type,
|
|
||||||
created_at,
|
|
||||||
mid,
|
|
||||||
from_uid,
|
|
||||||
unread = true,
|
|
||||||
} = action.payload;
|
|
||||||
const newMsg = { content, content_type, created_at, from_uid, unread };
|
|
||||||
if (state[id]) {
|
|
||||||
let replaceMsg = state[id][mid];
|
|
||||||
// 如果存在,并且新消息和缓存消息不一样,则替换掉
|
|
||||||
if (replaceMsg) {
|
|
||||||
const copyMsg = { ...replaceMsg };
|
|
||||||
if (!isObjectEqual(copyMsg, newMsg)) {
|
|
||||||
state[id][mid] = { ...newMsg, unread: false };
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state[id][mid] = newMsg;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
state[id] = { [mid]: newMsg };
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
likeUserMsg(state, action) {
|
likeUserMsg(state, action) {
|
||||||
const { id, from_uid, mid, action: reaction } = action.payload;
|
msgReaction(state, action.payload);
|
||||||
console.log("user likes: likes", id, mid, from_uid, reaction);
|
|
||||||
if (state[id] && state[id][mid]) {
|
|
||||||
if (!state[id][mid].likes) {
|
|
||||||
console.log("user likes: initial ");
|
|
||||||
state[id][mid].likes = {};
|
|
||||||
}
|
|
||||||
const currLikes = state[id][mid].likes;
|
|
||||||
// state[id][mid].likes = currLikes ? [...currLikes, reaction] : [reaction];
|
|
||||||
if (currLikes[reaction]) {
|
|
||||||
if (currLikes[reaction].includes(from_uid)) {
|
|
||||||
const idx = currLikes[reaction].findIndex((id) => {
|
|
||||||
return id == from_uid;
|
|
||||||
});
|
|
||||||
console.log("remove reaction", currLikes[reaction], idx, from_uid);
|
|
||||||
currLikes[reaction].splice(idx, 1);
|
|
||||||
} else {
|
|
||||||
currLikes[reaction].push(from_uid);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currLikes[reaction] = [from_uid];
|
|
||||||
}
|
|
||||||
// state[id][mid].likes = currLikes;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
updateUserMsg(state, action) {
|
updateUserMsg(state, action) {
|
||||||
const { id, mid, content, time } = action.payload;
|
msgUpdate(state, action.payload);
|
||||||
console.log("update user message", id, mid);
|
|
||||||
if (state[id][mid]) {
|
|
||||||
state[id][mid].content = content;
|
|
||||||
state[id][mid].edited = time;
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
deleteUserMsg(state, action) {
|
deleteUserMsg(state, action) {
|
||||||
const { id, mid } = action.payload;
|
msgDelete(state, action.payload);
|
||||||
console.log("delete user message", id, mid);
|
|
||||||
if (state[id][mid]) {
|
|
||||||
// 添加removed标识
|
|
||||||
// state[id][mid].removed = true;
|
|
||||||
delete state[id][mid];
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
setUserMsgRead(state, action) {
|
setUserMsgRead(state, action) {
|
||||||
const { id, mid } = action.payload;
|
msgSetRead(state, action.payload);
|
||||||
console.log("set unread", id, mid);
|
|
||||||
state[id][mid].unread = false;
|
|
||||||
},
|
|
||||||
removeMsg(state, action) {
|
|
||||||
const { id, mid } = action.payload;
|
|
||||||
console.log("remove user msg", id, mid);
|
|
||||||
delete state[id][mid];
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@@ -101,6 +42,5 @@ export const {
|
|||||||
initUserMsg,
|
initUserMsg,
|
||||||
addUserMsg,
|
addUserMsg,
|
||||||
setUserMsgRead,
|
setUserMsgRead,
|
||||||
removeMsg,
|
|
||||||
} = userMsgSlice.actions;
|
} = userMsgSlice.actions;
|
||||||
export default userMsgSlice.reducer;
|
export default userMsgSlice.reducer;
|
||||||
|
|||||||
Reference in New Issue
Block a user