Commit 7abbf4ee by Sixong.Zhu

Merge branch 'master' into pre

parents 257f730d 8b24464a
......@@ -191,9 +191,10 @@
this.handleScrollWrapper();
this.onNewMessage((e) => {
if (e.type === MessageType.Withdraw) {
this.executeWithDraw(e.ref_id);
const ids = e.ref_id ? [e.ref_id] : JSON.parse(e.msg);
this.executeWithDraw(ids);
dbController
.removeMessage(e.chat_id, e.ref_id)
.removeMessage(e.chat_id, ids)
.finally(() => this.refresh());
}
});
......@@ -339,6 +340,23 @@
// eslint-disable-next-line no-console
console.log("没有更多新消息了");
}
const removingIds: number[] = [];
for (const item of data) {
if (item.type === MessageType.Withdraw) {
item.msg && removingIds.push(...JSON.parse(item.msg));
}
}
if (removingIds.length) {
dbController
.removeMessage(this.chatId, removingIds)
.then(() => {
this.refresh();
this.executeWithDraw(removingIds);
});
}
this.$emit("next-page", msgId);
this.endLoadingNew();
})
......
......@@ -486,15 +486,14 @@
}
private withdraw() {
if (new Date().valueOf() - this.data.ts * 1000 > twoMinutes) {
Xim.error("超过两分钟的消息不能撤回");
return;
if (!this.hoverWithdraw()) {
return Xim.error("不能撤回");
}
ximInstance.withdraw(this.chatId!, this.data.id).finally(() => {
ximInstance.withdraw(this.chatId, this.data.id).finally(() => {
dbController
.removeMessage(this.chatId!, this.data.id)
.removeMessage(this.chatId, [this.data.id])
.finally(() => {
this.executeWithDraw(this.data.id);
this.executeWithDraw([this.data.id]);
this.$emit("withdraw", this.data.id);
});
});
......@@ -502,11 +501,11 @@
private hoverWithdraw() {
if (!this.isWithdraw || !this.isMyMessage) {
return;
return false;
}
this.isWithdraw = this.needReadTip
return (this.isWithdraw = this.needReadTip
? new Date().valueOf() - this.data.ts * 1000 < twoMinutes
: new Date().valueOf() - this.data.ts * 1000 < twoHours;
: new Date().valueOf() - this.data.ts * 1000 < twoHours);
}
private openReaderList(e: MouseEvent) {
......
......@@ -20,7 +20,9 @@ class ChatCacheDatabaseController {
return new Promise<void>((resolve, reject) => {
const checker = () => {
if (!this.setuping) {
this.setupError ? reject(new Error(`IM index database setup failed`)) : resolve();
this.setupError
? reject(new Error(`IM index database setup failed`))
: resolve();
} else {
setTimeout(() => checker(), 200);
}
......@@ -30,16 +32,17 @@ class ChatCacheDatabaseController {
}
public setup(uid: string) {
if (this.db) {
return Promise.resolve();
}
if (this.setuping) {
return this.waitSetupCompleted();
}
return new Promise<void>((resolve, reject) => {
if (uid && indexedDB) {
this.setuping = true;
const r = indexedDB.open(
"u-" + (this.uid = uid),
this.listVersion
);
const key = "u-" + (this.uid = uid);
const r = indexedDB.open(key, this.listVersion);
const setupDb = () => {
if (this.setuping) {
if (this.db) {
......@@ -131,8 +134,9 @@ class ChatCacheDatabaseController {
}
private buildTransaction(key: string) {
try { return this.db.transaction(key, "readwrite"); }
catch{
try {
return this.db.transaction(key, "readwrite");
} catch {
window.location.reload();
throw new Error(`transition failed`);
}
......@@ -329,7 +333,7 @@ class ChatCacheDatabaseController {
const r = store.getAll();
r.onsuccess = (o) => {
const items = (o.target as any).result as Chat[];
resolve(items.find(i => i.biz_type_code === code) as Chat);
resolve(items.find((i) => i.biz_type_code === code) as Chat);
};
r.onerror = () => resolve(null);
});
......@@ -379,13 +383,22 @@ class ChatCacheDatabaseController {
});
}
public removeMessage(chat: number, msg: number) {
public removeMessage(chat: number, msgs: number[]) {
return new Promise<void>((resolve, reject) => {
if (this.db) {
const store = this.buildChatMessageStore(chat);
const d = store.delete(msg);
d.onsuccess = () => setTimeout(() => resolve(), 100);
d.onerror = () => reject();
let count = 0;
const action = () => {
count++;
if (count === msgs.length) {
resolve();
}
};
for (const item of msgs) {
const d = store.delete(item);
d.onsuccess = () => action();
d.onerror = () => action();
}
} else {
resolve();
}
......@@ -396,7 +409,10 @@ class ChatCacheDatabaseController {
for (const item of source2) {
const t = source1.find((i) => i.id === item.id);
if (t) {
item.unread_msg_count = Math.max(item.unread_msg_count, t.unread_msg_count);
item.unread_msg_count = Math.max(
item.unread_msg_count,
t.unread_msg_count
);
const index = source1.indexOf(t);
source1[index] = item;
} else {
......
import type { UniplatSdk } from "uniplat-sdk";
export * from "./order";
export * from "./order-product"
export * from "./order-product";
export const enum ChatRole {
Default = 25,
......@@ -115,12 +115,12 @@ export const enum MessageType {
Action = "action",
Notify = "notify",
MpNavigate = "mp-navigate",
PayV1 = 'gpay',
PayV1 = "gpay",
Pay = "gpay2",
PayResult = "gresult",
RefundV1 = 'grefund',
RefundV1 = "grefund",
Refund = "grefund2",
Card = 'card'
Card = "card",
}
export const enum MessageHandled {
......@@ -195,6 +195,7 @@ export interface BaseChatItem extends BaseChatItemBusinessData {
create_time: number;
update_time: number;
last_msg_ts: number;
last_msg_sender: string;
members_updated: number;
user_updated: number;
}
......
......@@ -31,7 +31,7 @@ export namespace ChatStore {
export type STATE_CHAT_SENDING_MESSAGE = dto.Message;
export const STATE_CHAT_CURRENT_CHAT_ID = "当前chat-id";
export type STATE_CHAT_CURRENT_CHAT_ID = number | null;
export type STATE_CHAT_CURRENT_CHAT_ID = number;
export const STATE_CHAT_CURRENT_CHAT_VERSION = "当前chat的Uniplat version";
export type STATE_CHAT_CURRENT_CHAT_VERSION = number | null;
export const STATE_CHAT_CURRENT_IS_CHAT_MEMBER = "是否是当前chat的成员";
......@@ -169,7 +169,7 @@ export namespace ChatStore {
export type MUTATION_CLEAR_CURRENT_CHAT_UNIPLAT_ID = () => void;
export const MUTATION_WITHDRAW = "撤回";
export type MUTATION_WITHDRAW = (id: number) => void;
export type MUTATION_WITHDRAW = (ids: number[]) => void;
export const MUTATION_SAVE_MYSELF_ID =
"保存我的id:聊天窗口显示在右边那个人的id";
......
......@@ -200,7 +200,9 @@ export class Xim {
}
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);
}
......
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