Skip to content

Commit

Permalink
Merge pull request RocketChat#314 from WideChat/develop_pwa
Browse files Browse the repository at this point in the history
Develop pwa catchup
  • Loading branch information
ear-dev authored Jun 15, 2020
2 parents c524249 + e011759 commit 0cd1f2f
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 41 deletions.
2 changes: 1 addition & 1 deletion app/message-attachments/client/messageAttachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ Template.messageAttachment.helpers({
return true;
},
uploading() {
return Template.parentData(2).msg.uploads;
return Template.parentData(1).msg.uploads;
},
getImageHeight(height = 200) {
return height;
Expand Down
6 changes: 4 additions & 2 deletions app/ui-flextab/client/tabs/membersList.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { getActions } from './userActions';
import { RoomManager, popover } from '../../../ui-utils';
import { ChatRoom, Subscriptions } from '../../../models';
import { settings } from '../../../settings';
import { t, isRtl, handleError, roomTypes } from '../../../utils';
import { t, isRtl, handleError, roomTypes, isMobile } from '../../../utils';
import { WebRTC } from '../../../webrtc/client';
import { hasPermission } from '../../../authorization';

Expand Down Expand Up @@ -311,7 +311,9 @@ Template.membersList.onCreated(function() {
});

Template.membersList.onRendered(function() {
this.firstNode.parentNode.querySelector('#user-search').focus();
if (!isMobile()) {
this.firstNode.parentNode.querySelector('#user-search').focus();
}
this.autorun(() => {
const showAllUsers = this.showAllUsers.get();
const statusTypeSelect = this.find('.js-type');
Expand Down
3 changes: 3 additions & 0 deletions app/ui-utils/client/lib/avatar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ export const updateAvatarOfUsername = function(username) {
$(`.sidebar-item.js-sidebar-type-d .sidebar-item__link[aria-label='${ username }'] .avatar-image`)
.attr('src', url);

// force reload of avatar on sidenav header
$(`.sidebar__header .sidebar__header-thumb .avatar-image[alt='${ username }']`).attr('src', url);

return true;
};
1 change: 1 addition & 0 deletions app/ui-utils/client/lib/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ export const menu = new class extends EventEmitter {
FlowRouter.withReplaceState(function() {
FlowRouter.go('/home');
});
Session.set('openSearchPage', false);
}));

this.list = $('.rooms-list');
Expand Down
1 change: 1 addition & 0 deletions app/ui-utils/client/lib/popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export const popover = {
if (activeElement) {
$(activeElement).removeClass('active');
}
this.renderedPopover = null;
},
};

Expand Down
2 changes: 1 addition & 1 deletion app/ui/client/components/header/headerRoom.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
{{#if $and isDirect isNotMobile}}
<div class="rc-header__block">
<div class="rc-header__image">
{{> avatar username=avatarBackground}}
{{> avatar url=avatarBackground}}
</div>
</div>
{{/if}}
Expand Down
4 changes: 3 additions & 1 deletion app/ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ const mountPopover = (e, i, outerContext) => {
onRendered: () => new Clipboard('.rc-popover__item'),
};

popover.open(config);
if (!popover.renderedPopover) {
popover.open(config);
}
};

function roomHasGlobalPurge(room) {
Expand Down
88 changes: 52 additions & 36 deletions public/sw.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,56 @@ function hasSameHash(firstUrl, secondUrl) {
}
}

function handleAvatar(request, response) {
const clonedResponse = response.clone();
const url = request.url.split('?')[0];
caches.open(version).then((cache) => cache.put(new Request(url), clonedResponse));
}

const fetchFromNetwork = (event) => {
const requestToFetch = event.request.clone();
return fetch(requestToFetch, { cache: 'reload' }).then((response) => {
const clonedResponse = response.clone();
const contentType = clonedResponse.headers.get('content-type');

if (!clonedResponse || clonedResponse.status !== 200 || clonedResponse.type !== 'basic'
|| /\/sockjs\//.test(event.request.url)) {
return response;
}

if (/html/.test(contentType)) {
caches.open(version).then((cache) => cache.put(HTMLToCache, clonedResponse));
} else {
// Delete old version of a file
if (hasHash(event.request.url)) {
caches.open(version).then((cache) => cache.keys().then((keys) => keys.forEach((asset) => {
if (new RegExp(removeHash(event.request.url)).test(removeHash(asset.url))) {
cache.delete(asset);
}
})));
}

if (/avatar\/.*\?_dc/.test(event.request.url)) {
// handle the avatar updates
handleAvatar(event.request, response);
}

caches.open(version).then((cache) => cache.put(event.request, clonedResponse));
}
return response;
}).catch(() => {
if (hasHash(event.request.url)) { return caches.match(event.request.url); }
// If the request URL hasn't been served from cache and isn't sockjs we suppose it's HTML
if (!/\/sockjs\//.test(event.request.url)) { return caches.match(HTMLToCache); }
// Only for sockjs
return new Response('No connection to the server', {
status: 503,
statusText: 'No connection to the server',
headers: new Headers({ 'Content-Type': 'text/plain' }),
});
});
};

self.addEventListener('install', (event) => {
event.waitUntil(caches.open(version).then((cache) => {
cache.add(HTMLToCache).then(self.skipWaiting());
Expand Down Expand Up @@ -42,9 +92,9 @@ self.addEventListener('fetch', (event) => {
return;
}

const requestToFetch = event.request.clone();
event.respondWith(
caches.match(event.request.clone()).then((cached) => {
const fetchEvent = fetchFromNetwork(event);
// We don't return cached HTML (except if fetch failed)
if (cached) {
const resourceType = cached.headers.get('content-type');
Expand All @@ -58,41 +108,7 @@ self.addEventListener('fetch', (event) => {
return cached;
}
}
return fetch(requestToFetch).then((response) => {
const clonedResponse = response.clone();
const contentType = clonedResponse.headers.get('content-type');

if (!clonedResponse || clonedResponse.status !== 200 || clonedResponse.type !== 'basic'
|| /\/sockjs\//.test(event.request.url)) {
return response;
}

if (/html/.test(contentType)) {
caches.open(version).then((cache) => cache.put(HTMLToCache, clonedResponse));
} else {
// Delete old version of a file
if (hasHash(event.request.url)) {
caches.open(version).then((cache) => cache.keys().then((keys) => keys.forEach((asset) => {
if (new RegExp(removeHash(event.request.url)).test(removeHash(asset.url))) {
cache.delete(asset);
}
})));
}

caches.open(version).then((cache) => cache.put(event.request, clonedResponse));
}
return response;
}).catch(() => {
if (hasHash(event.request.url)) { return caches.match(event.request.url); }
// If the request URL hasn't been served from cache and isn't sockjs we suppose it's HTML
if (!/\/sockjs\//.test(event.request.url)) { return caches.match(HTMLToCache); }
// Only for sockjs
return new Response('No connection to the server', {
status: 503,
statusText: 'No connection to the server',
headers: new Headers({ 'Content-Type': 'text/plain' }),
});
});
return fetchEvent;
}),
);
});

0 comments on commit 0cd1f2f

Please sign in to comment.