From 5e2a8cca1b79d7ab5574bcc4d7309bcf0f7a2522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Calvi=C3=B1o=20S=C3=A1nchez?= Date: Mon, 19 Mar 2018 17:16:11 +0100 Subject: [PATCH] Return also exact matches besides all suggestions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit "_getSuggestions" returned all the suggestions from the server, which are composed by exact matches and partial matches. Now the exact matches are also returned on their own parameter. This will be used by the button to confirm a share. Note that until now the order of the suggestions was "exact users, partial users, exact groups, partial groups, exact..."; this commit also changes that order to become "exact users, exact groups, exact..., partial users, partial groups, partial...". This is not a problem, as the suggestions were used in the autocomplete dropdown, and this new order is arguably better than the old one, as all exact matches appear now at the beginning. Signed-off-by: Daniel Calviño Sánchez --- core/js/sharedialogview.js | 67 ++++-- core/js/tests/specs/sharedialogviewSpec.js | 248 +++++++++++++++++++++ 2 files changed, 299 insertions(+), 16 deletions(-) diff --git a/core/js/sharedialogview.js b/core/js/sharedialogview.js index bfa63a63bd..d67ff89e28 100644 --- a/core/js/sharedialogview.js +++ b/core/js/sharedialogview.js @@ -147,20 +147,14 @@ }, function (result) { if (result.ocs.meta.statuscode === 100) { - var users = result.ocs.data.exact.users.concat(result.ocs.data.users); - var groups = result.ocs.data.exact.groups.concat(result.ocs.data.groups); - var remotes = result.ocs.data.exact.remotes.concat(result.ocs.data.remotes); - var lookup = result.ocs.data.lookup; - var emails = [], - circles = []; - if (typeof(result.ocs.data.emails) !== 'undefined') { - emails = result.ocs.data.exact.emails.concat(result.ocs.data.emails); - } - if (typeof(result.ocs.data.circles) !== 'undefined') { - circles = result.ocs.data.exact.circles.concat(result.ocs.data.circles); - } - var filter = function(users, groups, remotes, emails, circles) { + if (typeof(emails) === 'undefined') { + emails = []; + } + if (typeof(circles) === 'undefined') { + circles = []; + } + var usersLength; var groupsLength; var remotesLength; @@ -240,11 +234,52 @@ } }; - filter(users, groups, remotes, emails, circles); + filter( + result.ocs.data.exact.users, + result.ocs.data.exact.groups, + result.ocs.data.exact.remotes, + result.ocs.data.exact.emails, + result.ocs.data.exact.circles + ); - var suggestions = users.concat(groups).concat(remotes).concat(emails).concat(circles).concat(lookup); + var exactUsers = result.ocs.data.exact.users; + var exactGroups = result.ocs.data.exact.groups; + var exactRemotes = result.ocs.data.exact.remotes; + var exactEmails = []; + if (typeof(result.ocs.data.emails) !== 'undefined') { + exactEmails = result.ocs.data.exact.emails; + } + var exactCircles = []; + if (typeof(result.ocs.data.circles) !== 'undefined') { + exactCircles = result.ocs.data.exact.circles; + } - deferred.resolve(suggestions); + var exactMatches = exactUsers.concat(exactGroups).concat(exactRemotes).concat(exactEmails).concat(exactCircles); + + filter( + result.ocs.data.users, + result.ocs.data.groups, + result.ocs.data.remotes, + result.ocs.data.emails, + result.ocs.data.circles + ); + + var users = result.ocs.data.users; + var groups = result.ocs.data.groups; + var remotes = result.ocs.data.remotes; + var lookup = result.ocs.data.lookup; + var emails = []; + if (typeof(result.ocs.data.emails) !== 'undefined') { + emails = result.ocs.data.emails; + } + var circles = []; + if (typeof(result.ocs.data.circles) !== 'undefined') { + circles = result.ocs.data.circles; + } + + var suggestions = exactMatches.concat(users).concat(groups).concat(remotes).concat(emails).concat(circles).concat(lookup); + + deferred.resolve(suggestions, exactMatches); } else { deferred.reject(result.ocs.meta.message); } diff --git a/core/js/tests/specs/sharedialogviewSpec.js b/core/js/tests/specs/sharedialogviewSpec.js index be1aa4c9a8..e35e8df15a 100644 --- a/core/js/tests/specs/sharedialogviewSpec.js +++ b/core/js/tests/specs/sharedialogviewSpec.js @@ -471,6 +471,254 @@ describe('OC.Share.ShareDialogView', function() { }); }); }); + describe('get suggestions', function() { + it('no matches', function() { + var doneStub = sinon.stub(); + var failStub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(doneStub).fail(failStub); + + var jsonData = JSON.stringify({ + 'ocs': { + 'meta': { + 'status': 'success', + 'statuscode': 100, + 'message': null + }, + 'data': { + 'exact': { + 'users': [], + 'groups': [], + 'remotes': [] + }, + 'users': [], + 'groups': [], + 'remotes': [], + 'lookup': [] + } + } + }); + + expect(doneStub.called).toEqual(false); + expect(failStub.called).toEqual(false); + + fakeServer.requests[0].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(doneStub.calledOnce).toEqual(true); + expect(doneStub.calledWithExactly([], [])).toEqual(true); + expect(failStub.called).toEqual(false); + }); + it('single partial match', function() { + var doneStub = sinon.stub(); + var failStub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(doneStub).fail(failStub); + + var jsonData = JSON.stringify({ + 'ocs': { + 'meta': { + 'status': 'success', + 'statuscode': 100, + 'message': null + }, + 'data': { + 'exact': { + 'users': [], + 'groups': [], + 'remotes': [] + }, + 'users': [ + { + 'label': 'bobby', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_USER, + 'shareWith': 'imbob' + } + } + ], + 'groups': [], + 'remotes': [], + 'lookup': [] + } + } + }); + + expect(doneStub.called).toEqual(false); + expect(failStub.called).toEqual(false); + + fakeServer.requests[0].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(doneStub.calledOnce).toEqual(true); + expect(doneStub.calledWithExactly([{ + 'label': 'bobby', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'imbob'} + }], [ + ])).toEqual(true); + expect(failStub.called).toEqual(false); + }); + it('single exact match', function() { + var doneStub = sinon.stub(); + var failStub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(doneStub).fail(failStub); + + var jsonData = JSON.stringify({ + 'ocs': { + 'meta': { + 'status': 'success', + 'statuscode': 100, + 'message': null + }, + 'data': { + 'exact': { + 'users': [ + { + 'label': 'bob', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_USER, + 'shareWith': 'user1' + } + } + ], + 'groups': [], + 'remotes': [] + }, + 'users': [], + 'groups': [], + 'remotes': [], + 'lookup': [] + } + } + }); + + expect(doneStub.called).toEqual(false); + expect(failStub.called).toEqual(false); + + fakeServer.requests[0].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(doneStub.calledOnce).toEqual(true); + expect(doneStub.calledWithExactly([{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }], [{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }])).toEqual(true); + expect(failStub.called).toEqual(false); + }); + it('mixed matches', function() { + var doneStub = sinon.stub(); + var failStub = sinon.stub(); + + dialog._getSuggestions('bob', 42, shareModel).done(doneStub).fail(failStub); + + var jsonData = JSON.stringify({ + 'ocs': { + 'meta': { + 'status': 'success', + 'statuscode': 100, + 'message': null + }, + 'data': { + 'exact': { + 'users': [ + { + 'label': 'bob', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_USER, + 'shareWith': 'user1' + } + } + ], + 'groups': [ + { + 'label': 'bob', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_GROUP, + 'shareWith': 'group1' + } + } + ], + 'remotes': [] + }, + 'users': [ + { + 'label': 'bobby', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_USER, + 'shareWith': 'imbob' + } + }, + { + 'label': 'bob the second', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_USER, + 'shareWith': 'user2' + } + } + ], + 'groups': [ + { + 'label': 'bobfans', + 'value': { + 'shareType': OC.Share.SHARE_TYPE_GROUP, + 'shareWith': 'fans' + } + } + ], + 'remotes': [], + 'lookup': [] + } + } + }); + + expect(doneStub.called).toEqual(false); + expect(failStub.called).toEqual(false); + + fakeServer.requests[0].respond( + 200, + {'Content-Type': 'application/json'}, + jsonData + ); + + expect(doneStub.calledOnce).toEqual(true); + expect(doneStub.calledWithExactly([{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }, { + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_GROUP, 'shareWith': 'group1'} + }, { + 'label': 'bobby', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'imbob'} + }, { + 'label': 'bob the second', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user2'} + }, { + 'label': 'bobfans', + 'value': {'shareType': OC.Share.SHARE_TYPE_GROUP, 'shareWith': 'fans'} + }], [{ + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_USER, 'shareWith': 'user1'} + }, { + 'label': 'bob', + 'value': {'shareType': OC.Share.SHARE_TYPE_GROUP, 'shareWith': 'group1'} + }])).toEqual(true); + expect(failStub.called).toEqual(false); + }); + }); describe('autocompletion of users', function() { var showTemporaryNotificationStub;