diff --git a/apps/files_sharing/js/app.js b/apps/files_sharing/js/app.js index 447aaaf8ec28..7499399a7fb3 100644 --- a/apps/files_sharing/js/app.js +++ b/apps/files_sharing/js/app.js @@ -118,10 +118,16 @@ OCA.Sharing.App = { } }, + registerNotificationHandler: function() { + this._onNotificationEvent = _.bind(this._onNotificationEvent, this); + $('body').on('OCA.Notification.Action', this._onNotificationEvent); + }, + /** * Destroy the app */ destroy: function() { + $('body').off('OCA.Notification.Action', this._onNotificationEvent); OCA.Files.fileActions.off('setDefault.app-sharing', this._onActionsUpdated); OCA.Files.fileActions.off('registerAction.app-sharing', this._onActionsUpdated); this.removeSharingIn(); @@ -133,6 +139,19 @@ OCA.Sharing.App = { delete this._globalActionsInitialized; }, + _onNotificationEvent: function(e) { + if (e.notification.app === 'files_sharing') { + if (this._inFileList) { + this._inFileList.reload(); + } + if (e.action.type === 'POST' && OCA.Files && OCA.Files.App && OCA.Files.App.fileList) { + // reload the file list only if the share is accepted + // both internal sharing and remote sharing + OCA.Files.App.fileList.reload(); + } + } + }, + _createFileActions: function() { // inherit file actions from the files app var fileActions = new OCA.Files.FileActions(); @@ -319,5 +338,6 @@ $(document).ready(function() { $('#app-content-sharinglinks').on('hide', function() { OCA.Sharing.App.removeSharingLinks(); }); + OCA.Sharing.App.registerNotificationHandler(); }); diff --git a/apps/files_sharing/tests/js/appSpec.js b/apps/files_sharing/tests/js/appSpec.js index b4a5b31b6804..2ce38b0a1e84 100644 --- a/apps/files_sharing/tests/js/appSpec.js +++ b/apps/files_sharing/tests/js/appSpec.js @@ -161,7 +161,7 @@ describe('OCA.Sharing.App tests', function() { shareOwner: 'user2' }; }); - afterEach(function() { + afterEach(function() { showMenuStub.restore(); }); it('provides accept and reject actions for pending shares', function() { @@ -275,4 +275,90 @@ describe('OCA.Sharing.App tests', function() { }); }); }); + describe('Action events', function() { + var oldList; + var appReloadStub; + var fileListInReloadStub; + + beforeEach(function() { + oldList = OCA.Files.App.fileList; + // dummy new list to make sure it exists + OCA.Files.App.fileList = new OCA.Files.FileList($('
')); + + appReloadStub = sinon.stub(OCA.Files.App.fileList, 'reload'); + fileListInReloadStub = sinon.stub(fileListIn, 'reload'); + + App.registerNotificationHandler(); + }); + afterEach(function() { + appReloadStub.restore(); + fileListInReloadStub.restore(); + + // restore old list + OCA.Files.App.fileList = oldList; + App.destroy(); + }); + it('accept share triggers reload of sharein and files fileLists', function() { + var ev = new $.Event('OCA.Notification.Action', { + notification: { + app: 'files_sharing', + }, + action: { + url: 'https://randomurl', + type: 'POST' + } + }); + $('body').trigger(ev); + + expect(fileListInReloadStub.callCount).toEqual(1); + expect(appReloadStub.callCount).toEqual(1); + }); + it('reject share only triggers reload of sharein fileList', function() { + var ev = new $.Event('OCA.Notification.Action', { + notification: { + app: 'files_sharing', + }, + action: { + url: 'https://randomurl', + type: 'DELETE' + } + }); + $('body').trigger(ev); + + expect(fileListInReloadStub.callCount).toEqual(1); + expect(appReloadStub.notCalled).toEqual(true); + }); + it('ignore events from notifications not related to files_sharing', function() { + var ev = new $.Event('OCA.Notification.Action', { + notification: { + app: 'notifications', + }, + action: { + url: 'https://randomurl', + type: 'DELETE' + } + }); + $('body').trigger(ev); + + expect(fileListInReloadStub.notCalled).toEqual(true); + expect(appReloadStub.notCalled).toEqual(true); + }); + it('after destroyed no events will be processed', function() { + var ev = new $.Event('OCA.Notification.Action', { + notification: { + app: 'files_sharing', + }, + action: { + url: 'https://randomurl', + type: 'POST' + } + }); + + App.destroy(); + $('body').trigger(ev); + + expect(fileListInReloadStub.notCalled).toEqual(true); + expect(appReloadStub.notCalled).toEqual(true); + }); + }); });