Commit d64555ed by Sixong.Zhu

修正未读消息机制

parent ffb56bb7
...@@ -195,9 +195,6 @@ ...@@ -195,9 +195,6 @@
@chatStore.Getter(ChatStore.GETTER_CURRENT_CHAT_PRESENT_MEMBERS) @chatStore.Getter(ChatStore.GETTER_CURRENT_CHAT_PRESENT_MEMBERS)
private readonly chatMembers!: ChatStore.GETTER_CURRENT_CHAT_PRESENT_MEMBERS; private readonly chatMembers!: ChatStore.GETTER_CURRENT_CHAT_PRESENT_MEMBERS;
@chatStore.State(ChatStore.STATE_CHAT_SOURCE)
private readonly chatSource!: ChatStore.STATE_CHAT_SOURCE;
@chatStore.State(ChatStore.STATE_CHAT_CURRENT_CHAT_ID) @chatStore.State(ChatStore.STATE_CHAT_CURRENT_CHAT_ID)
private readonly chatId!: ChatStore.STATE_CHAT_CURRENT_CHAT_ID; private readonly chatId!: ChatStore.STATE_CHAT_CURRENT_CHAT_ID;
...@@ -294,10 +291,6 @@ ...@@ -294,10 +291,6 @@
return senderEid!.toString() === this.chatMyId!.toString(); return senderEid!.toString() === this.chatMyId!.toString();
} }
created() {
this.messageComponent = messageMapping.get(this.messageType) as string;
}
private get userName() { private get userName() {
if (this.chatMembers) { if (this.chatMembers) {
const t = this.chatMembers.find((i) => i.eid === this.data.eid); const t = this.chatMembers.find((i) => i.eid === this.data.eid);
...@@ -395,6 +388,10 @@ ...@@ -395,6 +388,10 @@
return false; return false;
} }
created() {
this.messageComponent = messageMapping.get(this.messageType) as string;
}
private isCustomer() { private isCustomer() {
return !this.showReadSummary; return !this.showReadSummary;
} }
......
...@@ -70,6 +70,9 @@ ...@@ -70,6 +70,9 @@
@chatStoreNamespace.State(ChatStore.STATE_CHAT_MY_ID) @chatStoreNamespace.State(ChatStore.STATE_CHAT_MY_ID)
private readonly chatMyId!: ChatStore.STATE_CHAT_MY_ID; private readonly chatMyId!: ChatStore.STATE_CHAT_MY_ID;
@chatStoreNamespace.Action(ChatStore.ACTION_UPDATE_MESSAGE_READ_STATUS)
private readonly updateMessage!: ChatStore.ACTION_UPDATE_MESSAGE_READ_STATUS;
@Prop({ type: Number }) @Prop({ type: Number })
private msgId!: number; private msgId!: number;
...@@ -153,6 +156,14 @@ ...@@ -153,6 +156,14 @@
}; };
}) })
); );
if (!this.unreadlist.length) {
this.updateMessage({
chat: this.chatId,
start: this.msgId,
all: true,
});
}
} }
private uniqueReaderList(data: dto.OneWhoReadMessage[]) { private uniqueReaderList(data: dto.OneWhoReadMessage[]) {
......
...@@ -57,17 +57,17 @@ class ChatCacheDatabaseController { ...@@ -57,17 +57,17 @@ class ChatCacheDatabaseController {
} }
resolve(); resolve();
}; };
r.onsuccess = function(e) { r.onsuccess = function (e) {
that.db = (e.target as any).result; that.db = (e.target as any).result;
console.log(`index database init comepleted`); console.log(`index database init comepleted`);
setupDb(); setupDb();
}; };
r.onupgradeneeded = function(e) { r.onupgradeneeded = function (e) {
that.db = (e.target as any).result; that.db = (e.target as any).result;
console.log(`upgrade database comepleted`); console.log(`upgrade database comepleted`);
setupDb(); setupDb();
}; };
r.onerror = function(e) { r.onerror = function (e) {
console.log(`index database init failed, ${e}`); console.log(`index database init failed, ${e}`);
}; };
} else { } else {
...@@ -94,17 +94,17 @@ class ChatCacheDatabaseController { ...@@ -94,17 +94,17 @@ class ChatCacheDatabaseController {
} }
setTimeout(() => resolve(), 200); setTimeout(() => resolve(), 200);
}; };
r.onsuccess = function(e) { r.onsuccess = function (e) {
const db = (e.target as any).result; const db = (e.target as any).result;
that.messageDatabases.set(k, db); that.messageDatabases.set(k, db);
setupDb(); setupDb();
}; };
r.onupgradeneeded = function(e) { r.onupgradeneeded = function (e) {
const db = (e.target as any).result; const db = (e.target as any).result;
that.messageDatabases.set(k, db); that.messageDatabases.set(k, db);
setupDb(); setupDb();
}; };
r.onerror = function(e) { r.onerror = function (e) {
console.log( console.log(
`chat message index database init failed, ${e}` `chat message index database init failed, ${e}`
); );
...@@ -196,6 +196,75 @@ class ChatCacheDatabaseController { ...@@ -196,6 +196,75 @@ class ChatCacheDatabaseController {
}); });
} }
public setMessageReaded(
chat: number,
option: {
/**
* 要更新已读状态的消息Id
*/
start: number;
/**
* 如果此值存在且大于 start ,则表示需要更新从 [start ~ end] 这个区间内所有消息的已读状态
*/
end?: number;
/**
* 如果此值为true,则标记此条消息被所有人已读,否则只会在现有已读数上+1
*/
allRead?: boolean;
}
) {
return new Promise<void>((resolve) => {
if (this.db) {
const store = this.buildChatMessageStore(chat);
if (option.end && option.end > option.start) {
const total = option.end - option.start;
let finished = 0;
const onCompleted = () => {
finished++;
if (finished >= total) {
resolve();
}
};
for (let i = option.start; i <= option.end; i++) {
const r = store.get(i);
r.onsuccess = (m) => {
const p = (m.target as any).result as Message;
if (p) {
if (option.allRead) {
p.read_count = p.total_read_count;
} else {
p.read_count++;
}
store.put(p).onsuccess = () => onCompleted();
} else {
onCompleted();
}
};
r.onerror = () => onCompleted();
}
} else {
const r = store.get(option.start);
r.onsuccess = (m) => {
const p = (m.target as any).result as Message;
if (p) {
if (option.allRead) {
p.read_count = p.total_read_count;
} else {
p.read_count++;
}
store.put(p).onsuccess = () => resolve();
} else {
resolve();
}
};
r.onerror = () => resolve();
}
} else {
resolve();
}
});
}
public removeChatFromList(chat: number) { public removeChatFromList(chat: number) {
return new Promise<void>((resolve) => { return new Promise<void>((resolve) => {
if (this.db) { if (this.db) {
......
...@@ -31,7 +31,7 @@ function uniqueMessages( ...@@ -31,7 +31,7 @@ function uniqueMessages(
messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY> messages: NonNullable<ChatStore.STATE_CHAT_MSG_HISTORY>
) { ) {
const arr = [...messages]; const arr = [...messages];
return unique(arr, function(item, all) { return unique(arr, function (item, all) {
return all.findIndex((k) => k.id === item.id); return all.findIndex((k) => k.id === item.id);
}); });
} }
...@@ -332,7 +332,7 @@ export default { ...@@ -332,7 +332,7 @@ export default {
state[ChatStore.STATE_CHAT_SENDING_MESSAGES] = [...current]; state[ChatStore.STATE_CHAT_SENDING_MESSAGES] = [...current];
} }
}, },
[ChatStore.MUTATION_SAVE_CURRENT_CHAT_INPUTING]: (function() { [ChatStore.MUTATION_SAVE_CURRENT_CHAT_INPUTING]: (function () {
const setTimeoutId: { [key: string]: number } = {}; const setTimeoutId: { [key: string]: number } = {};
return ( return (
state: ChatStoreState, state: ChatStoreState,
...@@ -480,7 +480,9 @@ export default { ...@@ -480,7 +480,9 @@ export default {
}, },
async [ChatStore.ACTION_GET_CHAT_MESSAGES]({ state, commit, getters }) { async [ChatStore.ACTION_GET_CHAT_MESSAGES]({ state, commit, getters }) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]; const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
const isMember = state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER]; const isMember = state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER];
if (chatId == null) return; if (chatId == null) return;
let data: Message[] = []; let data: Message[] = [];
...@@ -520,7 +522,9 @@ export default { ...@@ -520,7 +522,9 @@ export default {
) { ) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]; const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) return; if (chatId == null) return;
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
const o = { const o = {
model: chat.model_name, model: chat.model_name,
obj: chat.obj_id, obj: chat.obj_id,
...@@ -543,7 +547,9 @@ export default { ...@@ -543,7 +547,9 @@ export default {
) { ) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]; const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) return; if (chatId == null) return;
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
const o = { const o = {
model: chat.model_name, model: chat.model_name,
obj: chat.obj_id, obj: chat.obj_id,
...@@ -570,7 +576,9 @@ export default { ...@@ -570,7 +576,9 @@ export default {
} }
} }
try { try {
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
const data = await Chat.getSdk() const data = await Chat.getSdk()
.model(chat.model_name) .model(chat.model_name)
.chat(chat.obj_id, orgId()) .chat(chat.obj_id, orgId())
...@@ -665,22 +673,29 @@ export default { ...@@ -665,22 +673,29 @@ export default {
if (msgs == null) return; if (msgs == null) return;
const oldestMsgId = msgs[0].id - 1; const oldestMsgId = msgs[0].id - 1;
const lastMsgId = msgs[msgs.length - 1].id + 1; const lastMsgId = msgs[msgs.length - 1].id + 1;
const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const chat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
const o = { const o = {
model: chat.model_name, model: chat.model_name,
obj: chat.obj_id, obj: chat.obj_id,
isMember: isMember:
state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER], state[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_MEMBER],
}; };
const start = oldestMsgId < 1 ? 1 : oldestMsgId;
const freshMsgs = await xim.queryMsgs( const freshMsgs = await xim.queryMsgs(
chatType, chatType,
chatId, chatId,
oldestMsgId < 1 ? 1 : oldestMsgId, start,
lastMsgId, lastMsgId,
20, 20,
true, true,
o o
); );
dbController.setMessageReaded(chatId, {
start,
end: lastMsgId,
});
commit(ChatStore.MUTATION_CLEAR_CHAT_MSG_HISTORY); commit(ChatStore.MUTATION_CLEAR_CHAT_MSG_HISTORY);
commit( commit(
ChatStore.MUTATION_PUSH_CHAT_MSG_HISTORY, ChatStore.MUTATION_PUSH_CHAT_MSG_HISTORY,
...@@ -820,7 +835,7 @@ export default { ...@@ -820,7 +835,7 @@ export default {
} }
commit( commit(
ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS, ChatStore.MUTATION_SAVE_CURRENT_CHAT_MEMBERS,
unique(newChatMembers, function(item, all) { unique(newChatMembers, function (item, all) {
return all.findIndex((k) => k.eid === item.eid); return all.findIndex((k) => k.eid === item.eid);
}) })
); );
...@@ -854,7 +869,9 @@ export default { ...@@ -854,7 +869,9 @@ export default {
); );
}, },
async [ChatStore.ACTION_CHAT_START_RECEPTION]({ getters, dispatch }) { async [ChatStore.ACTION_CHAT_START_RECEPTION]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -869,7 +886,9 @@ export default { ...@@ -869,7 +886,9 @@ export default {
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS)); .finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
}, },
async [ChatStore.ACTION_CHAT_FINISH_RECEPTION]({ getters, dispatch }) { async [ChatStore.ACTION_CHAT_FINISH_RECEPTION]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -884,7 +903,9 @@ export default { ...@@ -884,7 +903,9 @@ export default {
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS)); .finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
}, },
async [ChatStore.ACTION_CHAT_USER_EXIT]({ getters, dispatch }) { async [ChatStore.ACTION_CHAT_USER_EXIT]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -900,7 +921,9 @@ export default { ...@@ -900,7 +921,9 @@ export default {
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS)); .finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
}, },
async [ChatStore.ACTION_CHAT_CS_EXIT]({ getters, dispatch }) { async [ChatStore.ACTION_CHAT_CS_EXIT]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -918,7 +941,9 @@ export default { ...@@ -918,7 +941,9 @@ export default {
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0] uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0]
) { ) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -936,7 +961,9 @@ export default { ...@@ -936,7 +961,9 @@ export default {
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_MEMBER>[0] uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_MEMBER>[0]
) { ) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -954,7 +981,9 @@ export default { ...@@ -954,7 +981,9 @@ export default {
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0] uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0]
) { ) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -972,7 +1001,9 @@ export default { ...@@ -972,7 +1001,9 @@ export default {
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_CS>[0] uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_CS>[0]
) { ) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType; const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType;
if ( if (
!currentChat || !currentChat ||
!currentChat.model_name || !currentChat.model_name ||
...@@ -1034,6 +1065,42 @@ export default { ...@@ -1034,6 +1065,42 @@ export default {
.catch(reject) .catch(reject)
); );
}, },
[ChatStore.ACTION_UPDATE_MESSAGE_READ_STATUS]: (
{ state },
option: {
chat: number;
start: number;
end?: number;
all?: boolean;
}
) => {
const items = state[ChatStore.STATE_CHAT_MSG_HISTORY] as Message[];
if (items) {
if (option.end && option.end > option.start) {
for (let i = option.start; i <= option.end; i++) {
const p = items.find((m) => m.id === i);
if (p) {
p.read_count = option.all
? p.total_read_count
: p.read_count + 1;
}
}
} else {
const p = items.find((i) => i.id === option.start);
if (p) {
p.read_count = option.all
? p.total_read_count
: p.read_count + 1;
}
}
}
return dbController.setMessageReaded(option.chat, {
start: option.start,
end: option.end,
allRead: option.all,
});
},
}, },
getters: { getters: {
[ChatStore.STATE_CHAT_MSG_HISTORY](state) { [ChatStore.STATE_CHAT_MSG_HISTORY](state) {
......
...@@ -358,6 +358,14 @@ export namespace ChatStore { ...@@ -358,6 +358,14 @@ export namespace ChatStore {
id: string id: string
) => Promise<{ id: string; name: string }>; ) => Promise<{ id: string; name: string }>;
export const ACTION_UPDATE_MESSAGE_READ_STATUS = "更新消息已读状态";
export type ACTION_UPDATE_MESSAGE_READ_STATUS = (option: {
chat: number;
start: number;
end?: number;
all?: boolean;
}) => Promise<void>;
export interface ChatUpdateParameter { export interface ChatUpdateParameter {
chat: number; chat: number;
type?: dto.MessageType; type?: dto.MessageType;
......
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