refactor: message handler
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import { createSlice } from "@reduxjs/toolkit";
|
||||
import { isObjectEqual } from "../../common/utils";
|
||||
|
||||
import {
|
||||
msgReaction,
|
||||
msgAdd,
|
||||
msgSetRead,
|
||||
msgClearUnread,
|
||||
msgUpdate,
|
||||
msgDelete,
|
||||
} from "./message.handler";
|
||||
const initialState = {};
|
||||
|
||||
const channelMsgSlice = createSlice({
|
||||
@@ -14,85 +20,22 @@ const channelMsgSlice = createSlice({
|
||||
return action.payload;
|
||||
},
|
||||
addChannelMsg(state, action) {
|
||||
const {
|
||||
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 };
|
||||
}
|
||||
msgAdd(state, action.payload);
|
||||
},
|
||||
deleteChannelMsg(state, action) {
|
||||
const { id, mid } = 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];
|
||||
}
|
||||
msgDelete(state, action.payload);
|
||||
},
|
||||
updateChannelMsg(state, action) {
|
||||
const { id, mid, content, time } = action.payload;
|
||||
console.log("update channel message", id, mid);
|
||||
if (state[id][mid]) {
|
||||
state[id][mid].content = content;
|
||||
state[id][mid].edited = time;
|
||||
}
|
||||
msgUpdate(state, action.payload);
|
||||
},
|
||||
likeChannelMsg(state, action) {
|
||||
const { id, from_uid, mid, action: reaction } = 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;
|
||||
}
|
||||
msgReaction(state, action.payload);
|
||||
},
|
||||
setChannelMsgRead(state, action) {
|
||||
const { id, mid } = action.payload;
|
||||
console.log("set unread", id, mid);
|
||||
state[id][mid].unread = false;
|
||||
msgSetRead(state, action.payload);
|
||||
},
|
||||
clearChannelMsgUnread(state, action) {
|
||||
const gid = action.payload;
|
||||
console.log("set channel all unread", gid);
|
||||
Object.entries(state[gid]).forEach(([, obj]) => {
|
||||
obj.unread = false;
|
||||
});
|
||||
msgClearUnread(state, action.payload);
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
@@ -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 { isObjectEqual } from "../../common/utils";
|
||||
|
||||
import {
|
||||
msgReaction,
|
||||
msgAdd,
|
||||
msgSetRead,
|
||||
msgUpdate,
|
||||
msgDelete,
|
||||
} from "./message.handler";
|
||||
const initialState = {};
|
||||
const userMsgSlice = createSlice({
|
||||
name: "userMessage",
|
||||
@@ -13,83 +18,19 @@ const userMsgSlice = createSlice({
|
||||
return action.payload;
|
||||
},
|
||||
addUserMsg(state, action) {
|
||||
const {
|
||||
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 };
|
||||
}
|
||||
msgAdd(state, action.payload);
|
||||
},
|
||||
likeUserMsg(state, action) {
|
||||
const { id, from_uid, mid, action: reaction } = 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;
|
||||
}
|
||||
msgReaction(state, action.payload);
|
||||
},
|
||||
updateUserMsg(state, action) {
|
||||
const { id, mid, content, time } = action.payload;
|
||||
console.log("update user message", id, mid);
|
||||
if (state[id][mid]) {
|
||||
state[id][mid].content = content;
|
||||
state[id][mid].edited = time;
|
||||
}
|
||||
msgUpdate(state, action.payload);
|
||||
},
|
||||
deleteUserMsg(state, action) {
|
||||
const { id, mid } = action.payload;
|
||||
console.log("delete user message", id, mid);
|
||||
if (state[id][mid]) {
|
||||
// 添加removed标识
|
||||
// state[id][mid].removed = true;
|
||||
delete state[id][mid];
|
||||
}
|
||||
msgDelete(state, action.payload);
|
||||
},
|
||||
setUserMsgRead(state, action) {
|
||||
const { id, mid } = 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];
|
||||
msgSetRead(state, action.payload);
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -101,6 +42,5 @@ export const {
|
||||
initUserMsg,
|
||||
addUserMsg,
|
||||
setUserMsgRead,
|
||||
removeMsg,
|
||||
} = userMsgSlice.actions;
|
||||
export default userMsgSlice.reducer;
|
||||
|
||||
Reference in New Issue
Block a user