Commit c28d3aa0 by Sixong.Zhu

eslint

parent 5d95f6cd
import { Component, Vue } from "vue-property-decorator";
import { chatStore, ChatStore } from "@/customer-service/store/model";
import { parserMessage } from ".";
import { Chat as ChatItem } from "@/customer-service/xim/models/chat";
import { chatStore, ChatStore } from "@/customer-service/store/model";
import { formatTime, TimeFormatRule } from "@/customer-service/utils/time";
import { Chat as ChatItem } from "@/customer-service/xim/models/chat";
@Component({ components: {} })
export default class ChatList extends Vue {
......@@ -40,7 +42,7 @@ export default class ChatList extends Vue {
protected readonly hideChat: ChatStore.MUTATION_HIDE_CHAT;
protected parseMesage(data: ChatItem) {
if (data.last_msg_sender && data.last_msg_sender != "0") {
if (data.last_msg_sender && data.last_msg_sender !== "0") {
if (this.userNames[data.last_msg_sender] === undefined) {
this.updateUserName({ id: data.last_msg_sender, name: "" });
this.sdk
......
......@@ -3,7 +3,7 @@ import { Chat, Message } from "./../xim/models/chat";
class ChatCacheDatabaseController {
private db: IDBDatabase;
private readonly version = new Date().valueOf();
private readonly version = 1;
private readonly chatListKey = "chat-list";
private readonly chatMessageKey = "chat-message";
......@@ -11,6 +11,7 @@ class ChatCacheDatabaseController {
return new Promise<void>((resolve) => {
if (uid && indexedDB) {
const r = indexedDB.open(uid, this.version);
// eslint-disable-next-line @typescript-eslint/no-this-alias
const that = this;
const setupDb = () => {
if (that.db) {
......@@ -112,6 +113,29 @@ class ChatCacheDatabaseController {
r.onerror = () => resolve();
});
}
public mergeChatList(source1: Chat[], source2: Chat[]) {
for (const item of source2) {
const t = source1.find((i) => i.id === item.id);
if (t) {
const index = source1.indexOf(t);
source1[index] = item;
} else {
source1.push(item);
}
}
const source = source1.sort((x, y) => (x.last_msg_ts < y.last_msg_ts ? 1 : -1));
if (this.db) {
const store = this.buildStore(this.chatListKey);
store.clear();
for (const item of source) {
store.add(item, item.id);
}
}
return source;
}
}
export const dbController = new ChatCacheDatabaseController();
import { RootStoreState } from "@/store/model";
import { Module } from "vuex";
import Vue from "vue";
import { Module } from "vuex";
import { dbController } from "../database";
import { ChatMember } from "../model";
import { isAccessibleUrl } from "../service/tools";
import { unique } from "../utils";
......@@ -13,7 +14,8 @@ import { Chat as ChatType, Message } from "../xim/models/chat";
import xim, { ChatNotifyListener } from "../xim/xim";
import { ChatStatus, ChatStore, ChatStoreState } from "./model";
import { dbController } from "../database";
import { RootStoreState } from "@/store/model";
export const ns = ChatStore.ns;
......@@ -70,6 +72,22 @@ async function preCacheImgs(msgs: any[]) {
})
);
}
function buildChatItem(chat: any) {
let business_data;
if (chat.business_data) {
business_data = JSON.parse(chat.business_data);
}
if (business_data?.model_name == null) {
business_data = null;
}
return {
...chat,
chat_id: chat.id,
business_data,
} as ChatType;
}
export default {
namespaced: true,
state: () => ({
......@@ -332,36 +350,34 @@ export default {
},
actions: {
async [ChatStore.ACTION_GET_MY_CHAT_LIST]({
state,
commit,
}) /* ...params: Parameters<ChatStore.ACTION_GET_MY_CHAT_LIST> */ {
const cache = await dbController.getChatList();
let cache = await dbController.getChatList();
if (cache && cache.length) {
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, {
list: cache,
total: 9999,
});
const ts = cache.map((i) => i.last_msg_ts).sort();
const last = ts[ts.length - 1];
await xim.fetchChatListAfter(last).then((r) => {
const list = r.args[0] as any[];
const items = list.map((i) => buildChatItem(i));
if (items && items.length) {
cache = dbController.mergeChatList(cache, items);
}
});
return cache;
}
const data = await xim.fetchChatList();
if (data == null) return;
const chatList = data.args[0];
const items = chatList.map((chat: any) => {
let business_data;
if (chat.business_data) {
business_data = JSON.parse(chat.business_data);
}
if (business_data?.model_name == null) {
business_data = null;
}
return {
...chat,
chat_id: chat.id,
business_data,
} as ChatType;
});
const items = chatList.map((chat: any) => buildChatItem(chat));
dbController.saveChatList(items);
commit(ChatStore.MUTATION_SAVE_CHAT_LIST, {
list: items,
......@@ -512,13 +528,13 @@ export default {
);
},
async [ChatStore.ACTION_REGISTER_EVENT]({ dispatch, commit, state }) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const onNewMsg = (e: Message) => {
if (e.chat_id === chatId) {
dispatch(ChatStore.ACTION_GET_FRESH_MESSAGE);
}
state[ChatStore.STATE_FUNC_ON_NEW_MSG](e);
};
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
if (chatId == null) {
xim.off("msg", onNewMsg);
xim.on("msg", onNewMsg);
......@@ -588,7 +604,7 @@ export default {
if (!data) {
return;
}
let chat = data.args[0];
const chat = data.args[0];
let business_data;
if (chat.business_data) {
business_data = JSON.parse(chat.business_data);
......@@ -709,7 +725,7 @@ export default {
await dispatch(ChatStore.ACTION_GET_MY_CHAT_LIST);
const firstChat = state[ChatStore.STATE_MY_CHAT_ROOM_LIST]?.list[0];
if (firstChat == null) return;
const chatInfo = await getChatModelInfo(
await getChatModelInfo(
firstChat.business_data.model_name,
firstChat.business_data.obj_id
);
......@@ -718,7 +734,6 @@ export default {
firstChat.chat_id
);
},
async [ChatStore.ACTION_CHAT_START_RECEPTION]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
if (currentChat == null) return;
......@@ -739,7 +754,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
},
async [ChatStore.ACTION_CHAT_USER_EXIT]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
if (currentChat == null) return;
......@@ -750,7 +764,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
},
async [ChatStore.ACTION_CHAT_CS_EXIT]({ getters, dispatch }) {
const currentChat = getters[ChatStore.GETTER_CURRENT_CURRENT_CHAT];
if (currentChat == null) return;
......@@ -761,7 +774,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
},
async [ChatStore.ACTION_CHAT_ADD_MEMBERS](
{ getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_MEMBERS>[0]
......@@ -794,7 +806,6 @@ export default {
await new Promise((resolve) => setTimeout(resolve, 500));
await dispatch(ChatStore.ACTION_GET_CHAT_MEMBERS);
},
async [ChatStore.ACTION_CHAT_ADD_CS](
{ getters, dispatch },
uids: Parameters<ChatStore.ACTION_CHAT_ADD_CS>[0]
......@@ -852,7 +863,7 @@ export default {
[ChatStore.GETTER_CURRENT_CURRENT_CHAT](state) {
const chatId = state[ChatStore.STATE_CHAT_CURRENT_CHAT_ID];
const singleChat = state[ChatStore.STATE_SINGLE_CHAT];
if (singleChat && singleChat.chat_id == chatId) {
if (singleChat && singleChat.chat_id === chatId) {
return singleChat;
}
const chatList =
......
......@@ -6,6 +6,7 @@ import chatType from "../xim/chat-type";
import { TokenStringGetter } from "./../model";
import { ChatLoggerService } from "./logger";
import { Message, NotifyMessage } from "./models/chat";
import chat from "./index";
wampDebug(true);
......@@ -16,11 +17,11 @@ function emptyFunc() {
return null;
}
export type MsgListener = (msg: Message) => void
export type MsgListener = (msg: Message) => void;
export type ChatNotifyListener = (msg: NotifyMessage) => void
export type ChatNotifyListener = (msg: NotifyMessage) => void;
export type StatusChangeListener = (status: any, details: any) => void
export type StatusChangeListener = (status: any, details: any) => void;
export enum Events {
Msg = "msg",
......@@ -34,14 +35,14 @@ export enum Kind {
}
export class Xim {
private eventBus = new Vue()
private eventBus = new Vue();
private client?: XChatClient
private client?: XChatClient;
private paramsForReconnection?: {
url: string;
token: TokenStringGetter;
}
};
public close() {
if (this.client) {
......@@ -56,7 +57,7 @@ export class Xim {
}
}
private connectionPending = false
private connectionPending = false;
public async open(url: string, token: TokenStringGetter) {
this.connectionPending = true;
......@@ -112,6 +113,11 @@ export class Xim {
return this.client.fetchChatList({});
}
public fetchChatListAfter(lastMsgTs: number) {
if (this.client == null) return;
return this.client.fetchChatList({ last_msg_ts: lastMsgTs });
}
public fetchChat(chat_id: number) {
if (this.client == null) return;
return this.client.fetchChat(chat_id);
......@@ -160,7 +166,7 @@ export class Xim {
this.checkConnected();
if (this.client == null) {
throw new Error("client shouldn't undefined");
};
}
const res = await this.client.fetchChatMsgs(chatType, chatId, {
lid,
rid,
......@@ -233,43 +239,43 @@ export class Xim {
return data;
}
public on(event: "msg", chatId: number, listener: MsgListener): this
public on(event: "msg", listener: MsgListener): this
public on(event: "msg", chatId: number, listener: MsgListener): this;
public on(event: "msg", listener: MsgListener): this;
public on(
event: "chat_notify",
kind: "chat_change",
listener: ChatNotifyListener
): this
): this;
public on(
event: "chat_notify",
kind: string,
listener: ChatNotifyListener
): this
): this;
public on(event: "chat_notify", listener: ChatNotifyListener): this
public on(event: "status", listener: StatusChangeListener): this
public on(event: "chat_notify", listener: ChatNotifyListener): this;
public on(event: "status", listener: StatusChangeListener): this;
public on(...args: any[]): this {
this.eventBus.$on(...this.parseEventListener(...args));
return this;
}
public off(event: "msg", chatId: number, listener: MsgListener): this
public off(event: "msg", listener: MsgListener): this
public off(event: "msg", chatId: number, listener: MsgListener): this;
public off(event: "msg", listener: MsgListener): this;
public off(
event: "chat_notify",
kind: "chat_change",
listener: ChatNotifyListener
): this
): this;
public off(
event: "chat_notify",
kind: string,
listener: ChatNotifyListener
): this
): this;
public off(event: "chat_notify", listener: ChatNotifyListener): this
public off(event: "status", listener: StatusChangeListener): this
public off(event: "chat_notify", listener: ChatNotifyListener): this;
public off(event: "status", listener: StatusChangeListener): this;
public off(...args: any[]): this {
this.eventBus.$off(...this.parseEventListener(...args));
return this;
......@@ -312,7 +318,7 @@ export class Xim {
private onConnected() {
if (this.client == null) return;
// 连接成功后,需要调用pubUserInfo, 否则服务端会认为此连接无效
this.client.pubUserInfo(JSON.stringify({org_id: chat.getOrgId()}));
this.client.pubUserInfo(JSON.stringify({ org_id: chat.getOrgId() }));
this.debug("xim connected");
}
......
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