diff --git a/app/models/client/models/ChatMessage.js b/app/models/client/models/ChatMessage.js index cfa62729b74b..a5c32b1244c2 100644 --- a/app/models/client/models/ChatMessage.js +++ b/app/models/client/models/ChatMessage.js @@ -6,6 +6,7 @@ import { CachedCollection } from '../../../ui-cached-collection'; import { CachedChatSubscription } from './CachedChatSubscription'; import { ChatSubscription } from './ChatSubscription'; import { getConfig } from '../../../ui-utils/client/config'; +import { cleanMessagesAtStartup } from '../../../utils'; import { renderMessageBody } from '../../../ui-utils/client/lib/renderMessageBody'; import { promises } from '../../../promises/client'; import { callbacks } from '../../../callbacks'; @@ -91,6 +92,7 @@ const messagePreFetch = () => { if (!messagesFetched && CachedChatSubscription.ready.get()) { const status = Meteor.status(); if (status.status !== 'connected') { + cleanMessagesAtStartup(); return; } messagesFetched = true; @@ -120,6 +122,7 @@ const messagePreFetch = () => { }); }); }); + cleanMessagesAtStartup(false); } }); }; diff --git a/app/utils/client/index.js b/app/utils/client/index.js index f40b8a8dc5ef..c5d012ee9cf3 100644 --- a/app/utils/client/index.js +++ b/app/utils/client/index.js @@ -20,6 +20,7 @@ export { templateVarHandler } from '../lib/templateVarHandler'; export { APIClient } from './lib/RestApiClient'; export { canDeleteMessage } from './lib/canDeleteMessage'; export { SWCache } from './lib/swCache'; +export { cleanMessagesAtStartup } from './lib/offlineMessages'; export { mime } from '../lib/mimeTypes'; export { secondsToHHMMSS } from '../lib/timeConverter'; export { isMobile } from './lib/isMobile'; diff --git a/client/startup/offlineMessages.js b/app/utils/client/lib/offlineMessages.js similarity index 61% rename from client/startup/offlineMessages.js rename to app/utils/client/lib/offlineMessages.js index 3ea449400691..e86ebf6dc384 100644 --- a/client/startup/offlineMessages.js +++ b/app/utils/client/lib/offlineMessages.js @@ -1,15 +1,13 @@ -import { Meteor } from 'meteor/meteor'; import { Tracker } from 'meteor/tracker'; import { Session } from 'meteor/session'; import { sortBy } from 'underscore'; import localforage from 'localforage'; -import { call } from '../../app/ui-utils/client'; -import { getConfig } from '../../app/ui-utils/client/config'; -import { SWCache } from '../../app/utils/client'; -import { fileUploadHandler } from '../../app/file-upload'; -import { ChatMessage, CachedChatMessage } from '../../app/models/client'; -import { callbacks } from '../../app/callbacks'; +import { call } from '../../../ui-utils/client'; +import { getConfig } from '../../../ui-utils/client/config'; +import { ChatMessage, CachedChatMessage } from '../../../models/client'; + +import { SWCache, APIClient } from '..'; const action = { clean: (msg) => { @@ -28,7 +26,7 @@ const action = { sendFile: async (msg) => { const file = await SWCache.getFileFromCache(msg.file); - const uploading = { + const upload = { id: msg.file._id, name: msg.file.name, percentage: 0, @@ -36,45 +34,48 @@ const action = { if (!file) { return; } - const upload = fileUploadHandler('Uploads', msg.meta, file); - - // Session.set(`uploading-${msg.file._id}`, uploading); - - upload.onProgress = (progress) => { - const uploads = uploading; - uploads.percentage = Math.round(progress * 100) || 0; - ChatMessage.setProgress(msg._id, uploads); - }; - - upload.start((error, file, storage) => { - if (error) { - ChatMessage.setProgress(msg._id, uploading); - return; - } - - if (!file) { - return; - } - - const msgData = { id: msg._id, msg: msg.msg, tmid: msg.tmid }; - - Meteor.call('sendFileMessage', msg.rid, storage, file, msgData, () => { - SWCache.removeFromCache(msg.file); - }); + const data = new FormData(); + msg.meta.description && data.append('description', msg.meta.description); + data.append('id', msg._id); + msg.msg && data.append('msg', msg.msg); + msg.tmid && data.append('tmid', msg.tmid); + data.append('file', file, msg.file.name); + + const { xhr, promise } = APIClient.upload(`v1/rooms.upload/${ msg.rid }`, {}, data, { + progress(progress) { + if (progress === 100) { + return; + } + const uploads = upload; + uploads.percentage = Math.round(progress * 100) || 0; + ChatMessage.setProgress(msg._id, uploads); + }, + error() { + ChatMessage.setProgress(msg._id, upload); + }, }); Tracker.autorun((computation) => { - // using file._id as initial upload id, as messsageAttachment have access to file._id - const isCanceling = Session.get(`uploading-cancel-${ msg.file._id }`); + const isCanceling = Session.get(`uploading-cancel-${ upload.id }`); + if (!isCanceling) { return; } - computation.stop(); - upload.stop(); + Session.delete(`uploading-cancel-${ upload.id }`); - ChatMessage.setProgress(msg._id, uploading); + xhr.abort(); }); + + try { + await promise; + SWCache.removeFromCache(msg.file); + } catch (error) { + const uploads = upload; + uploads.error = (error.xhr && error.xhr.responseJSON && error.xhr.responseJSON.error) || error.message; + uploads.percentage = 0; + ChatMessage.setProgress(msg._id, uploads); + } }, update: (msg) => { @@ -137,16 +138,14 @@ function clearOldMessages({ records: messages, ...value }) { value.updatedAt = new Date(); localforage.setItem('chatMessage', value).then(() => { CachedChatMessage.loadFromCache(); - triggerOfflineMsgs(retain); }); } -const cleanMessagesAtStartup = () => { +export const cleanMessagesAtStartup = (offline = true) => { localforage.getItem('chatMessage').then((value) => { if (value && value.records) { - clearOldMessages(value); + triggerOfflineMsgs(value.records); + offline && clearOldMessages(value); } }); }; - -callbacks.add('afterMainReady', cleanMessagesAtStartup, callbacks.priority.MEDIUM, 'cleanMessagesAtStartup'); diff --git a/client/main.js b/client/main.js index ee82fd2aaf29..c855143be58e 100644 --- a/client/main.js +++ b/client/main.js @@ -24,7 +24,6 @@ import './routes'; import './startup/emailVerification'; import './startup/i18n'; import './startup/loginViaQuery'; -import './startup/offlineMessages'; import './startup/roomObserve'; import './startup/startup'; import './startup/unread';