message.sync.ts 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. import { Q } from '@nozbe/watermelondb';
  2. import { database } from 'src/watermelondb';
  3. import Message from 'src/watermelondb/models/Message';
  4. import { chatApi } from '@api/chat';
  5. import NetInfo from '@react-native-community/netinfo';
  6. import { testConnectionSpeed } from 'src/database/speedService';
  7. export function makeChatKey(params: { chatUid?: number | null; groupChatToken?: string | null }) {
  8. if (params.chatUid) return `u:${params.chatUid}`;
  9. if (params.groupChatToken) return `g:${params.groupChatToken}`;
  10. throw new Error('Invalid chat identity');
  11. }
  12. function now() {
  13. return Date.now();
  14. }
  15. export type MessageDirtyAction =
  16. | {
  17. type: 'send';
  18. value: {
  19. text: string;
  20. currentUid: number;
  21. attachment?: any;
  22. reply_to_id?: number;
  23. replyMessage?: any;
  24. };
  25. ts: number;
  26. }
  27. | { type: 'edit'; value: { text: string }; ts: number }
  28. | { type: 'delete'; value?: any; ts: number }
  29. | { type: 'read'; value: { messagesIds: number[] }; ts: number }
  30. | { type: 'reaction'; value: string; ts: number }
  31. | { type: 'unreaction'; value: string; ts: number };
  32. export function addMessageDirtyAction(msg: Message, action: Omit<MessageDirtyAction, 'ts'>) {
  33. const list: MessageDirtyAction[] = msg.dirtyActions ? JSON.parse(msg.dirtyActions) : [];
  34. list.push({ ...(action as MessageDirtyAction), ts: now() });
  35. msg.isDirty = true;
  36. msg.dirtyActions = JSON.stringify(list);
  37. (msg as any)._raw._status = 'synced';
  38. (msg as any)._raw._changed = '';
  39. }
  40. export function compactMessageActions(actions: MessageDirtyAction[]): MessageDirtyAction[] {
  41. if (!actions.length) return [];
  42. const sorted = [...actions].sort((a, b) => a.ts - b.ts);
  43. const res: MessageDirtyAction[] = [];
  44. for (const a of sorted) {
  45. const last = res[res.length - 1];
  46. if (a.type === 'delete') {
  47. return [a];
  48. }
  49. if (a.type === 'edit' && last?.type === 'edit') {
  50. last.value = a.value;
  51. last.ts = a.ts;
  52. continue;
  53. }
  54. if (
  55. (last?.type === 'reaction' && a.type === 'unreaction') ||
  56. (last?.type === 'unreaction' && a.type === 'reaction')
  57. ) {
  58. res.pop();
  59. continue;
  60. }
  61. if (a.type === 'reaction' && last?.type === 'reaction') {
  62. last.value = a.value;
  63. last.ts = a.ts;
  64. continue;
  65. }
  66. if (a.type === 'read' && last?.type === 'read') {
  67. last.ts = a.ts;
  68. continue;
  69. }
  70. res.push(a);
  71. }
  72. return res;
  73. }
  74. async function performMessageAction(token: string, msg: Message, action: MessageDirtyAction) {
  75. const isGroup = Boolean(msg.isGroup);
  76. const chatKey = msg.chatKey;
  77. if (action.type !== 'send' && msg.messageId == null) {
  78. throw new Error('Message has no server id yet');
  79. }
  80. switch (action.type) {
  81. case 'send': {
  82. if (isGroup) {
  83. const res = await chatApi.sendGroupMessage({
  84. token,
  85. to_group_token: chatKey.slice(2),
  86. text: action.value.text,
  87. attachment: action.value.attachment,
  88. reply_to_id: action.value.reply_to_id
  89. });
  90. return {
  91. newId: res.data.message_id,
  92. attachment: res.data.attachment,
  93. wsEvent: {
  94. action: 'new_message',
  95. payload: {
  96. message: {
  97. _id: res.data.message_id,
  98. text: action.value.text,
  99. replyMessage: action.value.replyMessage,
  100. attachment: res.data.attachment ? res.data.attachment : -1
  101. }
  102. }
  103. }
  104. };
  105. } else {
  106. const res = await chatApi.sendMessage({
  107. token,
  108. to_uid: Number(chatKey.slice(2)),
  109. text: action.value.text,
  110. attachment: action.value.attachment,
  111. reply_to_id: action.value.reply_to_id
  112. });
  113. return {
  114. newId: res.data.message_id,
  115. attachment: res.data.attachment,
  116. wsEvent: {
  117. action: 'new_message',
  118. payload: {
  119. message: {
  120. _id: res.data.message_id,
  121. text: action.value.text,
  122. replyMessage: action.value.replyMessage,
  123. attachment: res.data.attachment ? res.data.attachment : -1
  124. }
  125. }
  126. }
  127. };
  128. }
  129. }
  130. case 'edit': {
  131. if (isGroup) {
  132. await chatApi.editGroupMessage({
  133. token,
  134. group_token: chatKey.slice(2),
  135. message_id: msg.messageId!,
  136. text: action.value.text
  137. });
  138. } else {
  139. await chatApi.editMessage({
  140. token,
  141. to_uid: Number(chatKey.slice(2)),
  142. message_id: msg.messageId!,
  143. text: action.value.text
  144. });
  145. }
  146. return {};
  147. }
  148. case 'delete': {
  149. if (isGroup) {
  150. await chatApi.deleteGroupMessage({
  151. token,
  152. group_token: chatKey.slice(2),
  153. message_id: msg.messageId!
  154. });
  155. } else {
  156. await chatApi.deleteMessage({
  157. token,
  158. conversation_with_user: Number(chatKey.slice(2)),
  159. message_id: msg.messageId!
  160. });
  161. }
  162. return { deleted: true };
  163. }
  164. case 'reaction': {
  165. if (isGroup) {
  166. await chatApi.reactToGroupMessage({
  167. token,
  168. group_token: chatKey.slice(2),
  169. message_id: msg.messageId!,
  170. reaction: action.value
  171. });
  172. } else {
  173. await chatApi.reactToMessage({
  174. token,
  175. conversation_with_user: Number(chatKey.slice(2)),
  176. message_id: msg.messageId!,
  177. reaction: action.value
  178. });
  179. }
  180. return {};
  181. }
  182. case 'unreaction': {
  183. if (isGroup) {
  184. await chatApi.unreactToGroupMessage({
  185. token,
  186. group_token: chatKey.slice(2),
  187. message_id: msg.messageId!
  188. });
  189. } else {
  190. await chatApi.unreactToMessage({
  191. token,
  192. conversation_with_user: Number(chatKey.slice(2)),
  193. message_id: msg.messageId!
  194. });
  195. }
  196. return {};
  197. }
  198. case 'read': {
  199. if (isGroup) {
  200. await chatApi.groupMessagesRead({
  201. token,
  202. group_token: chatKey.slice(2),
  203. messages_id: action.value.messagesIds
  204. });
  205. } else {
  206. await chatApi.messagesRead({
  207. token,
  208. from_user: Number(chatKey.slice(2)),
  209. messages_id: action.value.messagesIds
  210. });
  211. }
  212. return {};
  213. }
  214. }
  215. }
  216. export async function upsertMessagesIntoDB({
  217. chatUid,
  218. groupToken,
  219. apiMessages,
  220. avatar = null,
  221. name = ''
  222. }: {
  223. chatUid?: number;
  224. groupToken?: string;
  225. apiMessages: any[];
  226. avatar?: string | null;
  227. name?: string | null;
  228. }) {
  229. if (!apiMessages?.length) return;
  230. const chatKey = makeChatKey({ chatUid, groupChatToken: groupToken });
  231. const isGroup = Boolean(groupToken);
  232. const col = database.get<Message>('messages');
  233. await database.write(async () => {
  234. const batch: any[] = [];
  235. for (const msg of apiMessages) {
  236. const compositeId = `${chatKey}:${msg.id}`;
  237. const existing = await col
  238. .query(Q.where('chat_key', chatKey), Q.where('message_id', msg.id))
  239. .fetch();
  240. if (existing.length) {
  241. const record = existing[0];
  242. const hasDirty = Boolean(msg.dirtyActions);
  243. try {
  244. batch.push(
  245. record.prepareUpdate((r) => {
  246. r.messageId = msg.id;
  247. r.sentAt = msg.sent_datetime;
  248. r.receivedAt = msg.received_datetime ?? r.receivedAt;
  249. r.readAt = msg.read_datetime ?? r.readAt;
  250. if (!hasDirty) {
  251. r.text = msg.text;
  252. }
  253. if (msg.attachement && msg.attachement !== -1) {
  254. const prev = r.attachment ? JSON.parse(r.attachment) : {};
  255. r.attachment = JSON.stringify({
  256. ...msg.attachement,
  257. local_uri: prev?.local_uri ?? null
  258. });
  259. } else {
  260. r.attachment = null;
  261. }
  262. r.status = msg.status;
  263. r.isSending = false;
  264. r.replyToId = msg.reply_to_id;
  265. r.replyTo = msg.reply_to ? JSON.stringify(msg.reply_to) : null;
  266. if (avatar && groupToken) {
  267. r.senderAvatar = avatar;
  268. } else {
  269. r.senderAvatar = msg.sender_avatar ?? null;
  270. }
  271. if (name && groupToken) {
  272. r.senderName = name;
  273. } else {
  274. r.senderName = msg.sender_name ?? '';
  275. }
  276. r.reactions = msg.reactions ?? '{}';
  277. r.edits = msg.edits ?? '{}';
  278. (r as any)._raw._status = 'synced';
  279. (r as any)._raw._changed = '';
  280. })
  281. );
  282. } catch (err) {}
  283. } else {
  284. try {
  285. batch.push(
  286. col.prepareCreate((r) => {
  287. r.chatKey = chatKey;
  288. r.isGroup = isGroup;
  289. r.messageId = msg.id;
  290. r.compositeId = compositeId;
  291. r.sentAt = msg.sent_datetime;
  292. r.receivedAt = msg.received_datetime ?? null;
  293. r.readAt = msg.read_datetime ?? null;
  294. r.senderId = msg.sender;
  295. r.recipientId = msg.recipient;
  296. r.text = msg.text;
  297. r.status = msg.status;
  298. r.isSending = false;
  299. r.reactions = msg.reactions ?? '{}';
  300. r.edits = msg.edits ?? '{}';
  301. r.attachment =
  302. msg.attachement && msg.attachement !== -1 ? JSON.stringify(msg.attachement) : null;
  303. r.encrypted = msg.encrypted ?? 0;
  304. r.replyToId = msg.reply_to_id ?? -1;
  305. r.replyTo = msg.reply_to ? JSON.stringify(msg.reply_to) : null;
  306. if (avatar && groupToken) {
  307. r.senderAvatar = avatar;
  308. } else {
  309. r.senderAvatar = msg.sender_avatar ?? null;
  310. }
  311. if (name && groupToken) {
  312. r.senderName = name;
  313. } else {
  314. r.senderName = msg.sender_name ?? '';
  315. }
  316. r.isDirty = false;
  317. r.dirtyActions = null;
  318. (r as any)._raw._status = 'synced';
  319. (r as any)._raw._changed = '';
  320. })
  321. );
  322. } catch (err) {}
  323. }
  324. }
  325. if (batch.length) {
  326. await database.batch(batch);
  327. }
  328. });
  329. }
  330. export async function reconcileChatRange(
  331. chatKey: string,
  332. serverMessages: any[],
  333. isLatest: boolean
  334. ) {
  335. if (!serverMessages.length) return;
  336. const col = database.get<Message>('messages');
  337. if (serverMessages.length === 1 && serverMessages[0].status === 4 && isLatest) {
  338. const keepCompositeId = `${chatKey}:${serverMessages[0].id}`;
  339. const local = await col.query(Q.where('chat_key', chatKey)).fetch();
  340. await database.write(async () => {
  341. for (const msg of local) {
  342. if (msg.compositeId !== keepCompositeId) {
  343. await msg.destroyPermanently();
  344. }
  345. }
  346. });
  347. return;
  348. }
  349. const serverIds = new Set(serverMessages.map((m) => m.id));
  350. const minId = Math.min(...serverMessages.map((m) => m.id));
  351. const maxId = Math.max(...serverMessages.map((m) => m.id));
  352. const local = await col
  353. .query(Q.where('chat_key', chatKey), Q.where('message_id', Q.between(minId, maxId)))
  354. .fetch();
  355. await database.write(async () => {
  356. for (const msg of local) {
  357. if (msg.messageId && msg.messageId > 0 && !serverIds.has(msg.messageId) && !msg.isDirty) {
  358. await msg.destroyPermanently();
  359. }
  360. }
  361. });
  362. }
  363. export type OutgoingWsEvent = {
  364. action: string;
  365. payload: Record<string, any>;
  366. };
  367. export async function pushMessageChanges(
  368. token: string,
  369. onWsEvent?: (event: OutgoingWsEvent) => void
  370. ) {
  371. const col = database.get<Message>('messages');
  372. const dirty = await col.query(Q.where('is_dirty', true)).fetch();
  373. if (!dirty.length) return;
  374. for (const msg of dirty) {
  375. const actions: MessageDirtyAction[] = msg.dirtyActions ? JSON.parse(msg.dirtyActions) : [];
  376. const compacted = compactMessageActions(actions);
  377. for (const a of compacted) {
  378. const res = await performMessageAction(token, msg, a);
  379. await database.write(async () => {
  380. if (res?.newId) {
  381. const duplicates = await col
  382. .query(Q.where('chat_key', msg.chatKey), Q.where('message_id', res.newId))
  383. .fetch();
  384. for (const d of duplicates) {
  385. if (d.id !== msg.id) {
  386. await d.destroyPermanently();
  387. }
  388. }
  389. }
  390. msg.update((m) => {
  391. if (res?.newId) {
  392. m.messageId = res.newId;
  393. m.compositeId = `${msg.chatKey}:${res.newId}`;
  394. m.status = 1;
  395. m.isSending = false;
  396. }
  397. if (res?.attachment) {
  398. const prev = m.attachment ? JSON.parse(m.attachment) : {};
  399. m.attachment = JSON.stringify({
  400. ...res.attachment,
  401. local_uri: prev?.local_uri ?? null
  402. });
  403. }
  404. m.isDirty = false;
  405. m.dirtyActions = null;
  406. (m as any)._raw._status = 'synced';
  407. (m as any)._raw._changed = '';
  408. if (res?.wsEvent && onWsEvent) {
  409. onWsEvent(res.wsEvent);
  410. }
  411. });
  412. });
  413. }
  414. }
  415. }
  416. let pushInFlight = false;
  417. let needsAnotherRun = false;
  418. export async function triggerMessagePush(
  419. token: string,
  420. onWsEvent?: (event: OutgoingWsEvent) => void
  421. ) {
  422. if (pushInFlight) {
  423. needsAnotherRun = true;
  424. return;
  425. }
  426. pushInFlight = true;
  427. try {
  428. do {
  429. needsAnotherRun = false;
  430. await pushMessageChanges(token, onWsEvent);
  431. } while (needsAnotherRun);
  432. } finally {
  433. pushInFlight = false;
  434. }
  435. }
  436. export function normalizeServerMessage(s: any, chatKey: string, isGroup: boolean) {
  437. return {
  438. messageId: s.id,
  439. compositeId: `${chatKey}:${s.id}`,
  440. chatKey,
  441. isGroup,
  442. senderId: s.sender,
  443. recipientId: s.recipient,
  444. text: s.text,
  445. sentAt: s.sent_datetime,
  446. receivedAt: s.received_datetime,
  447. readAt: s.read_datetime,
  448. status: s.status,
  449. reactions: s.reactions,
  450. edits: s.edits,
  451. attachment: s.attachement !== -1 ? JSON.stringify(s.attachement) : null,
  452. replyToId: s.reply_to_id ?? null,
  453. replyTo: s.reply_to_id && s.reply_to_id !== -1 ? JSON.stringify(s.reply_to) : null,
  454. encrypted: s.encrypted,
  455. senderName: s.sender_name ?? null,
  456. senderAvatar: s.sender_avatar ?? null,
  457. isSending: false
  458. };
  459. }
  460. export async function syncMessagesIncremental(token: string) {
  461. const net = await NetInfo.fetch();
  462. if (!net.isConnected) return;
  463. try {
  464. const speed = await testConnectionSpeed();
  465. if ((speed?.downloadSpeed && speed.downloadSpeed < 0.2) || (speed?.ping && speed.ping > 1500)) {
  466. console.warn('Internet too slow for sync');
  467. return;
  468. }
  469. } catch {}
  470. await pushMessageChanges(token);
  471. }