Commit 8b24464a by Sixong.Zhu

update db

parent 00d93de0
...@@ -32,16 +32,17 @@ class ChatCacheDatabaseController { ...@@ -32,16 +32,17 @@ class ChatCacheDatabaseController {
} }
public setup(uid: string) { public setup(uid: string) {
if (this.db) {
return Promise.resolve();
}
if (this.setuping) { if (this.setuping) {
return this.waitSetupCompleted(); return this.waitSetupCompleted();
} }
return new Promise<void>((resolve, reject) => { return new Promise<void>((resolve, reject) => {
if (uid && indexedDB) { if (uid && indexedDB) {
this.setuping = true; this.setuping = true;
const r = indexedDB.open( const key = "u-" + (this.uid = uid);
"u-" + (this.uid = uid), const r = indexedDB.open(key, this.listVersion);
this.listVersion
);
const setupDb = () => { const setupDb = () => {
if (this.setuping) { if (this.setuping) {
if (this.db) { if (this.db) {
...@@ -99,17 +100,17 @@ class ChatCacheDatabaseController { ...@@ -99,17 +100,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}`
); );
......
...@@ -195,6 +195,7 @@ export interface BaseChatItem extends BaseChatItemBusinessData { ...@@ -195,6 +195,7 @@ export interface BaseChatItem extends BaseChatItemBusinessData {
create_time: number; create_time: number;
update_time: number; update_time: number;
last_msg_ts: number; last_msg_ts: number;
last_msg_sender: string;
members_updated: number; members_updated: number;
user_updated: number; user_updated: number;
} }
......
...@@ -94,6 +94,10 @@ function buildChatItem(chat: RawChatItem) { ...@@ -94,6 +94,10 @@ function buildChatItem(chat: RawChatItem) {
const b = JSON.parse(chat.business_data) as BaseChatItemBusinessData; const b = JSON.parse(chat.business_data) as BaseChatItemBusinessData;
b.detail_name && (chat.detail_name = b.detail_name); b.detail_name && (chat.detail_name = b.detail_name);
} }
// 系统发的自动消息,重新校准未读消息数为0
if (!+chat.last_msg_sender && chat.msg_id === 1) {
chat.unread_msg_count = 0;
}
return { ...chat, chat_id: chat.id } as ChatType; return { ...chat, chat_id: chat.id } as ChatType;
} }
...@@ -553,29 +557,25 @@ export default { ...@@ -553,29 +557,25 @@ 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[ const chat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT] as ChatType;
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) return; if (!chatId) return;
let data: Message[] = []; let data: Message[] = [];
const getMessages = async () => { const getMessages = async () =>
const o = { await xim.queryLastPageMsg(chatType, chatId, 20, {
model: chat.model_name, model: chat.model_name,
obj: chat.obj_id, obj: chat.obj_id,
isMember, isMember,
}; });
data = await xim.queryLastPageMsg(chatType, chatId, 20, o);
};
if (isMember) { if (isMember) {
const cache = await dbController.getChatMessages(chatId); const cache = await dbController.getChatMessages(chatId);
if (cache && cache.length) { if (cache && cache.length) {
data = cache; data = cache;
} else { } else {
await getMessages(); data = await getMessages();
} }
} else { } else {
await getMessages(); data = await getMessages();
} }
try { try {
...@@ -614,7 +614,7 @@ export default { ...@@ -614,7 +614,7 @@ export default {
dbController.appendMessages(chatId, data); dbController.appendMessages(chatId, data);
return data; return data;
}, },
async [ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID]( async[ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID](
{ state, commit, getters }, { state, commit, getters },
msgId: Parameters<ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID>[0] msgId: Parameters<ChatStore.ACTION_GET_CHAT_MESSAGES_AFTER_SPECIFIC_ID>[0]
) { ) {
...@@ -639,7 +639,7 @@ export default { ...@@ -639,7 +639,7 @@ export default {
dbController.appendMessages(chatId, data); dbController.appendMessages(chatId, data);
return data; return data;
}, },
async [ChatStore.ACTION_SEND_MESSAGE]( async[ChatStore.ACTION_SEND_MESSAGE](
{ state, dispatch, getters, commit }, { state, dispatch, getters, commit },
params: Parameters<ChatStore.ACTION_SEND_MESSAGE>[0] params: Parameters<ChatStore.ACTION_SEND_MESSAGE>[0]
) { ) {
...@@ -667,7 +667,7 @@ export default { ...@@ -667,7 +667,7 @@ export default {
return Promise.reject(error); return Promise.reject(error);
} }
}, },
async [ChatStore.ACTION_GET_FRESH_MESSAGE]({ async[ChatStore.ACTION_GET_FRESH_MESSAGE]({
state, state,
dispatch, dispatch,
commit, commit,
...@@ -687,7 +687,7 @@ export default { ...@@ -687,7 +687,7 @@ export default {
await preCacheImgs(newMsgsArr); await preCacheImgs(newMsgsArr);
commit(ChatStore.MUTATION_SCROLL_TO_BOTTOM); commit(ChatStore.MUTATION_SCROLL_TO_BOTTOM);
}, },
async [ChatStore.ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN]( async[ChatStore.ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN](
{ commit, dispatch }, { commit, dispatch },
params: Parameters<ChatStore.ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN>[0] params: Parameters<ChatStore.ACTION_CREATE_NEW_CHAT_BY_SERVICE_MAN>[0]
) { ) {
...@@ -704,7 +704,7 @@ export default { ...@@ -704,7 +704,7 @@ export default {
); );
return { chatId, catalog }; return { chatId, catalog };
}, },
async [ChatStore.ACTION_CREATE_NEW_CHAT_BY_CLIENT]( async[ChatStore.ACTION_CREATE_NEW_CHAT_BY_CLIENT](
{ commit, dispatch }, { commit, dispatch },
params: Parameters<ChatStore.ACTION_CREATE_NEW_CHAT_BY_CLIENT>[0] params: Parameters<ChatStore.ACTION_CREATE_NEW_CHAT_BY_CLIENT>[0]
) { ) {
...@@ -722,7 +722,7 @@ export default { ...@@ -722,7 +722,7 @@ export default {
dispatch(ChatStore.ACTION_GET_MY_CHAT_LIST); dispatch(ChatStore.ACTION_GET_MY_CHAT_LIST);
return chatId; return chatId;
}, },
async [ChatStore.ACTION_REGISTER_EVENT]({ async[ChatStore.ACTION_REGISTER_EVENT]({
dispatch, dispatch,
commit, commit,
state, state,
...@@ -732,7 +732,7 @@ export default { ...@@ -732,7 +732,7 @@ export default {
const onNewMsg = (e: Message) => { const onNewMsg = (e: Message) => {
const thenAction = () => { const thenAction = () => {
if (e.type === MessageType.Withdraw) { if (e.type === MessageType.Withdraw) {
commit(ChatStore.MUTATION_WITHDRAW, [+e.msg]); commit(ChatStore.MUTATION_WITHDRAW, e.msg.startsWith('[') ? JSON.parse(e.msg) : [+e.msg]);
} }
const scroll = () => const scroll = () =>
state[ChatStore.STATE_FUNC_ON_NEW_MSG](e); state[ChatStore.STATE_FUNC_ON_NEW_MSG](e);
...@@ -813,7 +813,7 @@ export default { ...@@ -813,7 +813,7 @@ export default {
xim.on("chat_notify", "read", onMsgRead); xim.on("chat_notify", "read", onMsgRead);
xim.on("chat_notify", "user.input", onInputing); xim.on("chat_notify", "user.input", onInputing);
}, },
async [ChatStore.ACTION_SAVE_CURRENT_CHAT_ID_VERSION]( async[ChatStore.ACTION_SAVE_CURRENT_CHAT_ID_VERSION](
{ state, commit, dispatch }, { state, commit, dispatch },
chatId: ChatStore.STATE_CHAT_CURRENT_CHAT_ID chatId: ChatStore.STATE_CHAT_CURRENT_CHAT_ID
) { ) {
...@@ -879,7 +879,7 @@ export default { ...@@ -879,7 +879,7 @@ export default {
commit(ChatStore.MUTATION_SCROLL_TO_BOTTOM); commit(ChatStore.MUTATION_SCROLL_TO_BOTTOM);
(<any>state)[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_ERROR] = null; (<any>state)[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_ERROR] = null;
}, },
async [ChatStore.ACTION_CLEAR_CURRENT_CHAT_DATA]({ commit, state }) { async[ChatStore.ACTION_CLEAR_CURRENT_CHAT_DATA]({ commit, state }) {
commit(ChatStore.MUTATION_CLEAR_CURRENT_CHAT_ID); commit(ChatStore.MUTATION_CLEAR_CURRENT_CHAT_ID);
commit(ChatStore.MUTATION_CLEAR_MYSELF_ID); commit(ChatStore.MUTATION_CLEAR_MYSELF_ID);
commit(ChatStore.MUTATION_CLEAR_CHAT_MSG_HISTORY); commit(ChatStore.MUTATION_CLEAR_CHAT_MSG_HISTORY);
...@@ -887,7 +887,7 @@ export default { ...@@ -887,7 +887,7 @@ export default {
commit(ChatStore.MUTATION_CLEAR_CURRENT_CHAT_MEMBERS); commit(ChatStore.MUTATION_CLEAR_CURRENT_CHAT_MEMBERS);
(<any>state)[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_ERROR] = null; (<any>state)[ChatStore.STATE_CHAT_CURRENT_IS_CHAT_ERROR] = null;
}, },
async [ChatStore.ACTION_GET_CHAT_MEMBERS]({ commit, state }) { async[ChatStore.ACTION_GET_CHAT_MEMBERS]({ commit, state }) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID]; const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (!chatId) return; if (!chatId) return;
const getChatMembersResult = await xim.fetchChatMembers(chatId); const getChatMembersResult = await xim.fetchChatMembers(chatId);
...@@ -930,7 +930,7 @@ export default { ...@@ -930,7 +930,7 @@ export default {
}) })
); );
}, },
async [ChatStore.ACTION_TERINATE_CHAT]({ state, dispatch }) { async[ChatStore.ACTION_TERINATE_CHAT]({ state, dispatch }) {
const v = state[ChatStore.STATE_CHAT_CURRENT_CHAT_VERSION]; const v = state[ChatStore.STATE_CHAT_CURRENT_CHAT_VERSION];
if (v == null) return; if (v == null) return;
const id = Number( const id = Number(
...@@ -958,7 +958,7 @@ export default { ...@@ -958,7 +958,7 @@ export default {
firstChat.chat_id firstChat.chat_id
); );
}, },
async [ChatStore.ACTION_CHAT_START_RECEPTION]({ getters, dispatch }) { async[ChatStore.ACTION_CHAT_START_RECEPTION]({ getters, dispatch }) {
const currentChat = getters[ const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType; ] as ChatType;
...@@ -982,7 +982,7 @@ export default { ...@@ -982,7 +982,7 @@ export default {
); );
}); });
}, },
async [ChatStore.ACTION_CHAT_FINISH_RECEPTION]({ getters, dispatch }) { async[ChatStore.ACTION_CHAT_FINISH_RECEPTION]({ getters, dispatch }) {
const currentChat = getters[ const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType; ] as ChatType;
...@@ -999,7 +999,7 @@ export default { ...@@ -999,7 +999,7 @@ export default {
.finishChat() .finishChat()
.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[ const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType; ] as ChatType;
...@@ -1017,7 +1017,7 @@ export default { ...@@ -1017,7 +1017,7 @@ export default {
.userExitChat() .userExitChat()
.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[ const currentChat = getters[
ChatStore.GETTER_CURRENT_CURRENT_CHAT ChatStore.GETTER_CURRENT_CURRENT_CHAT
] as ChatType; ] as ChatType;
...@@ -1035,7 +1035,7 @@ export default { ...@@ -1035,7 +1035,7 @@ export default {
.csExitChat() .csExitChat()
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS)); .finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
}, },
async [ChatStore.ACTION_CHAT_ADD_MEMBERS]( async[ChatStore.ACTION_CHAT_ADD_MEMBERS](
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0] uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0]
) { ) {
...@@ -1055,7 +1055,7 @@ export default { ...@@ -1055,7 +1055,7 @@ export default {
.addMember(uids.map((id) => +id)) .addMember(uids.map((id) => +id))
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS)); .finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
}, },
async [ChatStore.ACTION_CHAT_REMOVE_MEMBER]( async[ChatStore.ACTION_CHAT_REMOVE_MEMBER](
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_MEMBER>[0] uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_MEMBER>[0]
) { ) {
...@@ -1075,7 +1075,7 @@ export default { ...@@ -1075,7 +1075,7 @@ export default {
.removeMember(uids.map((id) => +id)) .removeMember(uids.map((id) => +id))
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS)); .finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
}, },
async [ChatStore.ACTION_CHAT_ADD_CS]( async[ChatStore.ACTION_CHAT_ADD_CS](
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0] uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0]
) { ) {
...@@ -1095,7 +1095,7 @@ export default { ...@@ -1095,7 +1095,7 @@ export default {
.addCs(uids.map((id) => Number(id))) .addCs(uids.map((id) => Number(id)))
.finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS)); .finally(() => dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS));
}, },
async [ChatStore.ACTION_CHAT_REMOVE_CS]( async[ChatStore.ACTION_CHAT_REMOVE_CS](
{ getters, dispatch }, { getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_CS>[0] uids: Parameters<ChatStore.ACTION_CHAT_REMOVE_CS>[0]
) { ) {
......
...@@ -200,7 +200,9 @@ export class Xim { ...@@ -200,7 +200,9 @@ export class Xim {
} }
private setMessagesRead(chatId: number, msg: Message[]) { private setMessagesRead(chatId: number, msg: Message[]) {
if (msg.length === 0) return; if (!msg.length) {
return this.setRead(chatId, 1, 1);
}
return this.setRead(chatId, msg[0].id, msg[msg.length - 1].id); return this.setRead(chatId, msg[0].id, msg[msg.length - 1].id);
} }
......
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