diff --git a/core/js/shareitemmodel.js b/core/js/shareitemmodel.js index c9fc85c91b..a7764dd626 100644 --- a/core/js/shareitemmodel.js +++ b/core/js/shareitemmodel.js @@ -123,7 +123,7 @@ shareId = this.get('linkShare').id; // note: update can only update a single value at a time - call = this.updateShare(shareId, attributes); + call = this.updateShare(shareId, attributes, options); } else { attributes = _.defaults(attributes, { password: '', @@ -133,7 +133,7 @@ shareType: OC.Share.SHARE_TYPE_LINK }); - call = this.addShare(attributes); + call = this.addShare(attributes, options); } return call; @@ -189,8 +189,9 @@ } } }); - }).fail(function(result) { + }).fail(function(xhr) { var msg = t('core', 'Error'); + var result = xhr.responseJSON; if (result.ocs && result.ocs.meta) { msg = result.ocs.meta.message; } @@ -203,15 +204,34 @@ }); }, - updateShare: function(shareId, attrs) { + updateShare: function(shareId, attrs, options) { var self = this; + options = options || {}; return $.ajax({ type: 'PUT', url: this._getUrl('shares/' + encodeURIComponent(shareId)), data: attrs, dataType: 'json' }).done(function() { - self.fetch(); + self.fetch({ + success: function() { + if (_.isFunction(options.success)) { + options.success(self); + } + } + }); + }).fail(function(xhr) { + var msg = t('core', 'Error'); + var result = xhr.responseJSON; + if (result.ocs && result.ocs.meta) { + msg = result.ocs.meta.message; + } + + if (_.isFunction(options.error)) { + options.error(self, msg); + } else { + OC.dialogs.alert(msg, t('core', 'Error while sharing')); + } }); }, @@ -221,13 +241,32 @@ * @param {int} shareId share id * @return {jQuery} */ - removeShare: function(shareId) { + removeShare: function(shareId, options) { var self = this; + options = options || {}; return $.ajax({ type: 'DELETE', url: this._getUrl('shares/' + encodeURIComponent(shareId)), }).done(function() { - self.fetch(); + self.fetch({ + success: function() { + if (_.isFunction(options.success)) { + options.success(self); + } + } + }); + }).fail(function(xhr) { + var msg = t('core', 'Error'); + var result = xhr.responseJSON; + if (result.ocs && result.ocs.meta) { + msg = result.ocs.meta.message; + } + + if (_.isFunction(options.error)) { + options.error(self, msg); + } else { + OC.dialogs.alert(msg, t('core', 'Error removing share')); + } }); }, diff --git a/core/js/tests/specs/shareitemmodelSpec.js b/core/js/tests/specs/shareitemmodelSpec.js index e1a14bbbe7..b2480a8bea 100644 --- a/core/js/tests/specs/shareitemmodelSpec.js +++ b/core/js/tests/specs/shareitemmodelSpec.js @@ -659,6 +659,47 @@ describe('OC.Share.ShareItemModel', function() { password: 'test' }); }); + it('forwards error message on add', function() { + var errorStub = sinon.stub(); + model.set({ + linkShare: { + isLinkShare: false + } + }, { + }); + + model.saveLinkShare({ + password: 'test' + }, { + error: errorStub + }); + + addShareStub.yieldTo('error', 'Some error message'); + + expect(errorStub.calledOnce).toEqual(true); + expect(errorStub.lastCall.args[0]).toEqual('Some error message'); + }); + it('forwards error message on update', function() { + var errorStub = sinon.stub(); + model.set({ + linkShare: { + isLinkShare: true, + id: '123' + } + }, { + }); + + model.saveLinkShare({ + password: 'test' + }, { + error: errorStub + }); + + updateShareStub.yieldTo('error', 'Some error message'); + + expect(errorStub.calledOnce).toEqual(true); + expect(errorStub.lastCall.args[0]).toEqual('Some error message'); + }); }); describe('creating shares', function() { it('sends POST method to endpoint with passed values', function() { @@ -680,6 +721,31 @@ describe('OC.Share.ShareItemModel', function() { shareWith: 'group1' }); }); + it('calls error handler with error message', function() { + var errorStub = sinon.stub(); + model.addShare({ + shareType: OC.Share.SHARE_TYPE_GROUP, + shareWith: 'group1' + }, { + error: errorStub + }); + + expect(fakeServer.requests.length).toEqual(1); + fakeServer.requests[0].respond( + 400, + { 'Content-Type': 'application/json' }, + JSON.stringify({ + ocs: { + meta: { + message: 'Some error message' + } + } + }) + ); + + expect(errorStub.calledOnce).toEqual(true); + expect(errorStub.lastCall.args[1]).toEqual('Some error message'); + }); }); describe('updating shares', function() { it('sends PUT method to endpoint with passed values', function() { @@ -697,6 +763,30 @@ describe('OC.Share.ShareItemModel', function() { permissions: '' + (OC.PERMISSION_READ | OC.PERMISSION_SHARE) }); }); + it('calls error handler with error message', function() { + var errorStub = sinon.stub(); + model.updateShare(123, { + permissions: OC.PERMISSION_READ | OC.PERMISSION_SHARE + }, { + error: errorStub + }); + + expect(fakeServer.requests.length).toEqual(1); + fakeServer.requests[0].respond( + 400, + { 'Content-Type': 'application/json' }, + JSON.stringify({ + ocs: { + meta: { + message: 'Some error message' + } + } + }) + ); + + expect(errorStub.calledOnce).toEqual(true); + expect(errorStub.lastCall.args[1]).toEqual('Some error message'); + }); }); describe('removing shares', function() { it('sends DELETE method to endpoint with share id', function() { @@ -709,6 +799,28 @@ describe('OC.Share.ShareItemModel', function() { 'shares/123?format=json' ); }); + it('calls error handler with error message', function() { + var errorStub = sinon.stub(); + model.removeShare(123, { + error: errorStub + }); + + expect(fakeServer.requests.length).toEqual(1); + fakeServer.requests[0].respond( + 400, + { 'Content-Type': 'application/json' }, + JSON.stringify({ + ocs: { + meta: { + message: 'Some error message' + } + } + }) + ); + + expect(errorStub.calledOnce).toEqual(true); + expect(errorStub.lastCall.args[1]).toEqual('Some error message'); + }); }); });