Skip to content

Commit

Permalink
fix trigger offline message
Browse files Browse the repository at this point in the history
fix service worker POST event
  • Loading branch information
shubhsherl committed Jul 16, 2020
1 parent 5ab2df2 commit b77b999
Show file tree
Hide file tree
Showing 11 changed files with 97 additions and 112 deletions.
2 changes: 1 addition & 1 deletion app/lib/server/methods/deleteMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Meteor.methods({
if (!originalMessage) {
return;
}

if (!canDeleteMessage(uid, originalMessage)) {
throw new Meteor.Error('error-action-not-allowed', 'Not allowed', {
method: 'deleteMessage',
Expand Down
4 changes: 2 additions & 2 deletions app/message-attachments/client/messageAttachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ Template.messageAttachment.helpers({
return true;
},
uploading() {
return Template.parentData(1).msg.uploads;
},
return Template.parentData(1).msg.uploads;
},
getImageHeight(height = 200) {
return height;
},
Expand Down
88 changes: 44 additions & 44 deletions app/message-attachments/client/stylesheets/messageAttachments.css
Original file line number Diff line number Diff line change
Expand Up @@ -160,52 +160,52 @@ html.rtl .attachment {
& .actions-container {
margin-top: 6px;
}

& .attachment-upload-progress {
& .progress {
overflow: hidden;
height: 20px;
margin-bottom: 20px;
border-radius: 4px;
background-color: #f5f5f5;
-webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
& .progress-bar {
float: left;
width: 0%;
height: 100%;
-webkit-transition: width 0.6s ease;
-o-transition: width 0.6s ease;
transition: width 0.6s ease;
text-align: center;
color: #ffffff;
background-color: #337ab7;
-webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);
font-size: 12px;
line-height: 20px;
}
& .progress-bar-success {
background-color: #5cb85c;
}
}
}
& .progress {
overflow: hidden;

height: 20px;
margin-bottom: 20px;

border-radius: 4px;
background-color: #f5f5f5;

-webkit-box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);

& .progress-bar {
float: left;

width: 0%;
height: 100%;

-webkit-transition: width 0.6s ease;
-o-transition: width 0.6s ease;
transition: width 0.6s ease;
text-align: center;

color: #ffffff;

background-color: #337ab7;
-webkit-box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);

box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.15);

font-size: 12px;

line-height: 20px;
}

& .progress-bar-success {
background-color: #5cb85c;
}
}
}
}

.attachment-temp {
& .attachment-image {
opacity: 0.5;
& .attachment-image {
opacity: 0.5;
}
}

6 changes: 3 additions & 3 deletions app/models/client/models/ChatMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +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 { cleanMessagesAtStartup, triggerOfflineMsgs } from '../../../utils';
import { renderMessageBody } from '../../../ui-utils/client/lib/renderMessageBody';
import { promises } from '../../../promises/client';
import { callbacks } from '../../../callbacks';
Expand Down Expand Up @@ -104,6 +104,8 @@ const messagePreFetch = () => {
if (status.status !== 'connected') {
return;
}
clearTimeout(timeout);
triggerOfflineMsgs();
messagesFetched = true;
const subscriptions = ChatSubscription.find(
{
Expand Down Expand Up @@ -131,8 +133,6 @@ const messagePreFetch = () => {
});
});
});
clearTimeout(timeout);
cleanMessagesAtStartup(false);
}
});
};
Expand Down
6 changes: 5 additions & 1 deletion app/reactions/client/methods/setReaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import { emoji } from '../../../emoji';
import { roomTypes } from '../../../utils/client';

Meteor.methods({
setReaction(reaction, messageId) {
setReaction(reaction, messageId, shouldReact, offlineTrigerred = false) {
if (!Meteor.userId()) {
throw new Meteor.Error(203, 'User_logged_out');
}

if (offlineTrigerred) {
return;
}

const user = Meteor.user();

const message = Messages.findOne({ _id: messageId });
Expand Down
10 changes: 5 additions & 5 deletions app/ui-sidenav/client/sidebarItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ Template.sidebarItem.helpers({
return this.lastMessage && Template.instance().lastMessageTs.get();
},
lastMessageDelivered() {
return this.lastMessage && !this.lastMessage.temp;
},
lastMessageSentByUser() {
return this.lastMessage && this.lastMessage.u && (this.lastMessage.u._id === Meteor.userId());
},
return this.lastMessage && !this.lastMessage.temp;
},
lastMessageSentByUser() {
return this.lastMessage && this.lastMessage.u && (this.lastMessage.u._id === Meteor.userId());
},
mySelf() {
return this.t === 'd' && this.name === Template.instance().user.username;
},
Expand Down
45 changes: 10 additions & 35 deletions app/ui/client/lib/fileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,52 +22,38 @@ export const uploadFileWithMessage = async (rid, tmid, { id, description, fileNa
let offlineFile;
const data = new FormData();
id = id || Random.id();

description && data.append('description', description);
console.log(id);
id && data.append('id', id);
msg && data.append('msg', msg);
tmid && data.append('tmid', tmid);
data.append('file', file.file, fileName);

// const uploads = Session.get('uploading') || [];

const upload = {
id: file.file._id,
name: fileName,
percentage: 0,
};

// uploads.push(upload);
// Session.set('uploading', uploads);

sendOfflineFileMessage(rid, {id, msg, tmid}, file.file, { name: fileName, description }, (file) => {
sendOfflineFileMessage(rid, { id, msg, tmid }, file.file, { name: fileName, description }, (file) => {
offlineFile = file;
});

const { xhr, promise } = APIClient.upload(`v1/rooms.upload/${ rid }`, {}, data, {
progress(progress) {
// const uploads = Session.get('uploading') || [];

if (progress === 100) {
return;
}

const uploads = upload;
uploads.percentage = Math.round(progress * 100) || 0;
uploads.percentage = Math.round(progress) || 0;
ChatMessage.setProgress(id, uploads);
// uploads.filter((u) => u.id === upload.id).forEach((u) => {
// u.percentage = Math.round(progress) || 0;
// });
// Session.set('uploading', uploads);
},
error(error) {
ChatMessage.setProgress(id, upload);
// const uploads = Session.get('uploading') || [];
// uploads.filter((u) => u.id === upload.id).forEach((u) => {
// u.error = error.message;
// u.percentage = 0;
// });
// Session.set('uploading', uploads);
const uploads = upload;
uploads.error = (error.xhr && error.xhr.responseJSON && error.xhr.responseJSON.error) || error.message;
uploads.percentage = 0;
ChatMessage.setProgress(id, uploads);
},
});

Expand All @@ -80,28 +66,17 @@ export const uploadFileWithMessage = async (rid, tmid, { id, description, fileNa
Session.delete(`uploading-cancel-${ upload.id }`);

xhr.abort();

// const uploads = Session.get('uploading') || {};
// Session.set('uploading', uploads.filter((u) => u.id !== upload.id));
});

try {
await promise;
offlineFile && SWCache.removeFromCache(offlineFile);
// const uploads = Session.get('uploading') || [];
// return Session.set('uploading', uploads.filter((u) => u.id !== upload.id));
const res = await promise;

if (typeof res === 'object' && res.success && offlineFile) { SWCache.removeFromCache(offlineFile); }
} catch (error) {
// const uploads = Session.get('uploading') || [];
// uploads.filter((u) => u.id === upload.id).forEach((u) => {
// u.error = (error.xhr && error.xhr.responseJSON && error.xhr.responseJSON.error) || error.message;
// u.percentage = 0;
// });
// Session.set('uploading', uploads);
const uploads = upload;
uploads.error = (error.xhr && error.xhr.responseJSON && error.xhr.responseJSON.error) || error.message;
uploads.percentage = 0;
ChatMessage.setProgress(id, uploads);

}
};

Expand Down
2 changes: 1 addition & 1 deletion app/utils/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ export { templateVarHandler } from '../lib/templateVarHandler';
export { APIClient, mountArrayQueryParameters } from './lib/RestApiClient';
export { canDeleteMessage } from './lib/canDeleteMessage';
export { SWCache } from './lib/swCache';
export { cleanMessagesAtStartup } from './lib/offlineMessages';
export { cleanMessagesAtStartup, triggerOfflineMsgs } from './lib/offlineMessages';
export { mime } from '../lib/mimeTypes';
export { secondsToHHMMSS } from '../lib/timeConverter';
38 changes: 22 additions & 16 deletions app/utils/client/lib/offlineMessages.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,20 @@ const action = {
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;
uploads.percentage = Math.round(progress) || 0;
ChatMessage.setProgress(msg._id, uploads);
},
error() {
ChatMessage.setProgress(msg._id, upload);
error(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);
},
});

Expand All @@ -68,8 +70,9 @@ const action = {
});

try {
await promise;
SWCache.removeFromCache(msg.file);
const res = await promise;

if (typeof res === 'object' && res.success) { SWCache.removeFromCache(msg.file); }
} catch (error) {
const uploads = upload;
uploads.error = (error.xhr && error.xhr.responseJSON && error.xhr.responseJSON.error) || error.message;
Expand All @@ -80,14 +83,14 @@ const action = {

update: (msg) => {
msg.editedAt = new Date();
call('updateMessage', msg);
call('updateMessage', msg, true);
},

react: ({ _id }, reaction) => {
call('setReaction', reaction, _id);
call('setReaction', reaction, _id, undefined, true);
},

delete: ({ _id }) => call('deleteMessage', { _id }),
delete: ({ _id }) => call('deleteMessage', { _id }, true),
};

function trigger(msg) {
Expand Down Expand Up @@ -115,10 +118,14 @@ function trigger(msg) {
}
}

function triggerOfflineMsgs(messages) {
const tempMsgs = messages.filter((msg) => msg.temp);
tempMsgs.forEach((msg) => trigger(msg));
}
export const triggerOfflineMsgs = () => {
localforage.getItem('chatMessage').then((value) => {
if (value && value.records) {
const tempMsgs = value.records.filter((msg) => msg.temp);
tempMsgs.forEach((msg) => trigger(msg));
}
});
};

const retainMessages = (rid, messages) => {
const roomMsgs = messages.filter((msg) => rid === msg.rid);
Expand All @@ -138,14 +145,13 @@ function clearOldMessages({ records: messages, ...value }) {
value.updatedAt = new Date();
localforage.setItem('chatMessage', value).then(() => {
CachedChatMessage.loadFromCache();
triggerOfflineMsgs(retain);
});
}

export const cleanMessagesAtStartup = (offline = true) => {
export const cleanMessagesAtStartup = () => {
localforage.getItem('chatMessage').then((value) => {
if (value && value.records) {
offline ? clearOldMessages(value) : triggerOfflineMsgs(value.records);
clearOldMessages(value);
}
});
};
4 changes: 2 additions & 2 deletions client/methods/deleteMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { ChatMessage } from '../../app/models/client';
import { canDeleteMessage, SWCache } from '../../app/utils/client';

Meteor.methods({
deleteMessage(msg) {
if (!Meteor.userId()) {
deleteMessage(msg, offlineTriggered = false) {
if (!Meteor.userId() || offlineTriggered) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions client/methods/updateMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { settings } from '../../app/settings';
import { callbacks } from '../../app/callbacks';

Meteor.methods({
updateMessage(message) {
if (!Meteor.userId()) {
updateMessage(message, offlineTriggered = false) {
if (!Meteor.userId() || offlineTriggered) {
return false;
}

Expand Down

0 comments on commit b77b999

Please sign in to comment.