Skip to content

Commit

Permalink
fix message retain issue
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhsherl committed Apr 7, 2020
1 parent df02d81 commit e0fcd75
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 44 deletions.
3 changes: 3 additions & 0 deletions app/models/client/models/ChatMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -91,6 +92,7 @@ const messagePreFetch = () => {
if (!messagesFetched && CachedChatSubscription.ready.get()) {
const status = Meteor.status();
if (status.status !== 'connected') {
cleanMessagesAtStartup();
return;
}
messagesFetched = true;
Expand Down Expand Up @@ -120,6 +122,7 @@ const messagePreFetch = () => {
});
});
});
cleanMessagesAtStartup(false);
}
});
};
Expand Down
1 change: 1 addition & 0 deletions app/utils/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -28,53 +26,56 @@ 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,
};

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) => {
Expand Down Expand Up @@ -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');
1 change: 0 additions & 1 deletion client/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down

0 comments on commit e0fcd75

Please sign in to comment.