Commit 6342cc84 by Sixong.Zhu

chat event

parent 75a06366
......@@ -65,9 +65,16 @@ import { Component, Prop, Vue } from "vue-property-decorator";
import ChatCreator from "@/customer-service/components/create-chat.vue";
import { ChatStore, chatStore } from "@/customer-service/store/model";
import { ChatRole } from "../model";
import {
ChatChangedEvent,
ChatEventHandler,
} from "./controller/chat-event-handler";
@Component({ components: { ChatCreator } })
export default class ChatTitle extends Vue {
@chatStore.State(ChatStore.STATE_CHAT_CURRENT_CHAT_ID)
private readonly chatId!: ChatStore.STATE_CHAT_CURRENT_CHAT_ID;
@chatStore.Getter(ChatStore.GETTER_CURRENT_CHAT_PRESENT_MEMBERS)
private readonly chatMembers!: ChatStore.GETTER_CURRENT_CHAT_PRESENT_MEMBERS;
......@@ -159,7 +166,12 @@ export default class ChatTitle extends Vue {
private async startReception() {
try {
await this._startReception();
await this._startReception().then(() =>
ChatEventHandler.raiseChatChanged(
ChatChangedEvent.Start,
this.chatId
)
);
this.$emit("updateActive", "my_receiving");
} catch (error) {
console.error(error);
......@@ -176,7 +188,9 @@ export default class ChatTitle extends Vue {
type: "warning",
}
);
await this._finishReception();
await this._finishReception().then(() =>
ChatEventHandler.raiseChatChanged(ChatChangedEvent.End, this.chatId)
);
this.hideChat();
}
}
......
export const enum ChatChangedEvent {
Start = 1,
End,
}
export class ChatEventHandler {
private static actions: {
key: string;
action: (e: ChatChangedEvent, chat: number) => void;
}[] = [];
public static registerOnChatChanged(
vue: Vue,
action: (e: ChatChangedEvent, chat: number) => void
) {
const key = `${new Date().valueOf()}-${Math.random()}`;
this.actions.push({ key, action });
vue.$once("hook:beforeDestroy", () => {
const t = this.actions.find((i) => i.key === key);
if (t) {
this.actions = this.actions.filter((i) => i !== t);
}
});
}
public static raiseChatChanged(e: ChatChangedEvent, chat: number) {
for (const item of this.actions) {
item.action(e, chat);
}
}
}
......@@ -47,6 +47,9 @@ export default class ChatList extends Vue {
@chatStore.Action(ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT)
protected readonly updateUnreadMessageCount!: ChatStore.ACTION_REBUILD_UNREAD_MESSAGE_COUNT;
@chatStore.Action(ChatStore.ACTION_CLEAR_CURRENT_CHAT_DATA)
protected readonly reset!: ChatStore.ACTION_CLEAR_CURRENT_CHAT_DATA;
protected parseMesage(data: ChatItem) {
if (data.last_msg_sender && data.last_msg_sender !== "0") {
if (!this.userNames[data.last_msg_sender]) {
......
......@@ -144,6 +144,21 @@ class ChatCacheDatabaseController {
}
};
t.onerror = () => resolve();
} else {
resolve();
}
});
}
public removeChatFromList(chat: number) {
return new Promise<void>((resolve) => {
if (this.db) {
const store = this.buildStore(this.chatListKey);
const t = store.delete(chat);
t.onsuccess = () => resolve();
t.onerror = () => resolve();
} else {
resolve();
}
});
}
......
......@@ -89,6 +89,12 @@ function buildChatItem(chat: RawChatItem) {
} as ChatType;
}
const filterActiveChats = (items: RawChatItem[]) => {
return items.filter(
(i) => !i.is_finish && !i.is_exited && !i.is_remove && !i.is_deleted
);
};
export default {
namespaced: true,
state: () => ({
......@@ -378,7 +384,7 @@ export default {
const last = ts[ts.length - 1];
await xim.fetchChatListAfter(last)!.then((r) => {
const list = r.args[0] as any[];
const list = filterActiveChats(r.args[0] as RawChatItem[]);
const items = list.map((i) => buildChatItem(i));
if (items && items.length) {
cache = dbController.mergeChatList(cache, items);
......@@ -390,7 +396,7 @@ export default {
const data = await xim.fetchChatList();
if (data == null) return;
const chatList = data.args[0] as RawChatItem[];
const chatList = filterActiveChats(data.args[0] as RawChatItem[]);
const items = chatList.map((chat) => buildChatItem(chat));
dbController.saveChatList(items);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, {
......
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