Commit 87ad0f2f by Sixong.Zhu

update

parent d30772c1
Showing with 100 additions and 29 deletions
......@@ -349,9 +349,10 @@ export default {
} else {
arr.push(eid);
}
setTimeoutId[eid] = setTimeout(() => {
arr.splice(arr.indexOf(eid), 1);
}, 4000);
setTimeoutId[eid] = setTimeout(
() => arr.splice(arr.indexOf(eid), 1),
4000
);
};
})(),
[ChatStore.MUTATION_CLEAR_CURRENT_CHAT_INPUTING]: (state) => {
......@@ -477,21 +478,31 @@ export default {
items.forEach((i) => (sum += i.unread_msg_count));
state[ChatStore.STATE_CURRENT_UNREAD_MESSAGE_COUNT] = sum;
},
async [ChatStore.ACTION_GET_CHAT_MESSAGES]({ state, commit }) {
async [ChatStore.ACTION_GET_CHAT_MESSAGES]({ state, commit, getters }) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
const isMember = state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER];
if (chatId == null) return;
let data: Message[] = [];
const cache = await dbController.getChatMessages(chatId);
if (cache && cache.length) {
data = cache;
const getMessages = async () => {
const o = {
model: chat.model_name,
obj: chat.obj_id,
isMember,
};
data = await xim.queryLastPageMsg(chatType, chatId, 20, o);
};
if (isMember) {
const cache = await dbController.getChatMessages(chatId);
if (cache && cache.length) {
data = cache;
} else {
await getMessages();
}
} else {
data = await xim.queryLastPageMsg(
chatType,
chatId,
20,
!state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER]
);
await getMessages();
}
try {
commit(ChatStore.MUTATION_PUSH_CHAT_MSG_HISTORY, data);
dbController.saveChatMessages(chatId, data);
......@@ -504,34 +515,46 @@ export default {
}
},
async [ChatStore.ACTION_GET_CHAT_MESSAGES_BEFORE_SPECIFIC_ID](
{ state, commit },
{ state, commit, getters },
msgId: Parameters<ChatStore.ACTION_GET_CHAT_MESSAGES_BEFORE_SPECIFIC_ID>[0]
) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) return;
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
const o = {
model: chat.model_name,
obj: chat.obj_id,
isMember: state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER],
};
const data = await xim.queryPrevPageMsg(
chatType,
chatId,
msgId,
10,
!state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER]
o
);
commit(ChatStore.MUTATION_UNSHIFT_CHAT_MSG_HISTORY, data);
dbController.appendMessages(chatId, data);
return data;
},
async [ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID](
{ state, commit },
{ state, commit, getters },
msgId: Parameters<ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID>[0]
) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) return;
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
const o = {
model: chat.model_name,
obj: chat.obj_id,
isMember: state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER],
};
const data = await xim.queryNextPageMsg(
chatType,
chatId,
msgId,
10,
!state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER]
o
);
commit(ChatStore.MUTATION_PUSH_CHAT_MSG_HISTORY, data);
dbController.appendMessages(chatId, data);
......@@ -618,7 +641,12 @@ export default {
dispatch(ChatStore.ACTION_GET_MY_CHAT_LIST);
return chatId;
},
async [ChatStore.ACTION_REGISTER_EVENT]({ dispatch, commit, state }) {
async [ChatStore.ACTION_REGISTER_EVENT]({
dispatch,
commit,
state,
getters,
}) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const onNewMsg = (e: Message) => {
if (e.chat_id === chatId) {
......@@ -637,11 +665,21 @@ export default {
if (msgs == null) return;
const oldestMsgId = msgs[0].id - 1;
const lastMsgId = msgs[msgs.length - 1].id + 1;
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
const o = {
model: chat.model_name,
obj: chat.obj_id,
isMember:
state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER],
};
const freshMsgs = await xim.queryMsgs(
chatType,
chatId,
oldestMsgId < 1 ? 1 : oldestMsgId,
lastMsgId
lastMsgId,
20,
true,
o
);
commit(ChatStore.MUTATION_CLEAR_CHAT_MSG_HISTORY);
commit(
......
......@@ -165,12 +165,16 @@ export class Xim {
lid = 0,
rid = 0,
limit = DefaultMsgPageSize,
desc = true
desc: boolean,
p: { isMember: boolean; model: string; obj: string }
): Promise<Message[]> {
this.checkConnected();
if (this.client == null) {
throw new Error("client shouldn't undefined");
}
if (!p.isMember && p.model && p.obj) {
return this.queryMessageWhenIsNotMember(p, limit, lid, rid, desc);
}
const res = await this.client.fetchChatMsgs(chatType, chatId, {
lid,
rid,
......@@ -180,6 +184,25 @@ export class Xim {
return res.args[0];
}
private queryMessageWhenIsNotMember(
p: { isMember: boolean; model: string; obj: string },
limit: number,
lid: number,
rid: number,
desc: boolean
) {
return chat
.getSdk()
.getAxios()
.get(
`/general/xim/model/${p.model}/${
p.obj
}/msgs?lid=${lid}&rid=${rid}&limit=${limit}&desc=${
desc ? 0 : 1
}`
);
}
private setMessagesRead(chatId: number, msg: Message[]) {
if (msg.length === 0) return;
return this.setRead(chatId, msg[0].id, msg[msg.length - 1].id);
......@@ -190,10 +213,18 @@ export class Xim {
chatType: string,
chatId: number,
limit: number,
notToRead?: boolean
p: { isMember: boolean; model: string; obj: string }
) {
const data = await this.queryMsgs(chatType, chatId, 0, 0, limit, true);
if (!notToRead) {
const data = await this.queryMsgs(
chatType,
chatId,
0,
0,
limit,
true,
p
);
if (p && p.isMember) {
this.setMessagesRead(chatId, data);
}
return data;
......@@ -205,7 +236,7 @@ export class Xim {
chatId: number,
msgId: number,
limit: number,
notToRead?: boolean
p: { isMember: boolean; model: string; obj: string }
) {
const data = await this.queryMsgs(
chatType,
......@@ -213,9 +244,10 @@ export class Xim {
0,
msgId,
limit,
true
true,
p
);
if (!notToRead) {
if (p && p.isMember) {
this.setMessagesRead(chatId, data);
}
return data;
......@@ -227,7 +259,7 @@ export class Xim {
chatId: number,
msgId: number,
limit: number,
notToRead?: boolean
p: { isMember: boolean; model: string; obj: string }
) {
const data = await this.queryMsgs(
chatType,
......@@ -235,9 +267,10 @@ export class Xim {
msgId,
0,
limit,
false
false,
p
);
if (!notToRead) {
if (p && p.isMember) {
this.setMessagesRead(chatId, data);
}
return data;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment