Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide contactsmenu as ocs #29249

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions core/Controller/ContactsMenuController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php

declare(strict_types=1);
/**
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
*
Expand All @@ -25,13 +27,13 @@
namespace OC\Core\Controller;

use OC\Contacts\ContactsMenu\Manager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\DataResponse;
use OCP\Contacts\ContactsMenu\IEntry;
use OCP\IRequest;
use OCP\IUserSession;

class ContactsMenuController extends Controller {
class ContactsMenuController extends \OCP\AppFramework\OCSController {
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved

/** @var Manager */
private $manager;
Expand All @@ -54,25 +56,33 @@ public function __construct(IRequest $request, IUserSession $userSession, Manage
* @NoAdminRequired
*
* @param string|null filter
* @return \JsonSerializable[]
* @return DataResponse
*/
public function index($filter = null) {
return $this->manager->getEntries($this->userSession->getUser(), $filter);
public function index(?string $filter = null): DataResponse {
$data = $this->manager->getEntries($this->userSession->getUser(), $filter);

$response = new DataResponse(array_map([$this, 'entryToArray'], $data['contacts']));
$response->addHeader('X-Contacts-App-Enabled', $data['contactsAppEnabled'] ? 'yes' : 'no');
return $response;
}

/**
* @NoAdminRequired
*
* @param integer $shareType
* @param string $shareWith
* @return JSONResponse|\JsonSerializable
* @return DataResponse
*/
public function findOne($shareType, $shareWith) {
public function findOne(int $shareType, string $shareWith): DataResponse {
$contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);

if ($contact) {
return $contact;
return new DataResponse($this->entryToArray($contact));
}
return new JSONResponse([], Http::STATUS_NOT_FOUND);
return new DataResponse([], Http::STATUS_NOT_FOUND);
}

protected function entryToArray(IEntry $entry): array {
return json_decode(json_encode($entry), true);
}
}
78 changes: 78 additions & 0 deletions core/Controller/ContactsMenuLegacyController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* @copyright 2017 Christoph Wurst <christoph@winzerhof-wurst.at>
*
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
* @author Georg Ehrke <oc.list@georgehrke.com>
* @author Roeland Jago Douma <roeland@famdouma.nl>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
namespace OC\Core\Controller;

use OC\Contacts\ContactsMenu\Manager;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\JSONResponse;
use OCP\IRequest;
use OCP\IUserSession;

class ContactsMenuLegacyController extends Controller {

/** @var Manager */
private $manager;

/** @var IUserSession */
private $userSession;

/**
* @param IRequest $request
* @param IUserSession $userSession
* @param Manager $manager
*/
public function __construct(IRequest $request, IUserSession $userSession, Manager $manager) {
parent::__construct('core', $request);
$this->userSession = $userSession;
$this->manager = $manager;
}

/**
* @NoAdminRequired
*
* @param string|null filter
* @return \JsonSerializable[]
*/
public function index($filter = null) {
return $this->manager->getEntries($this->userSession->getUser(), $filter);
}

/**
* @NoAdminRequired
*
* @param integer $shareType
* @param string $shareWith
* @return JSONResponse|\JsonSerializable
*/
public function findOne($shareType, $shareWith) {
$contact = $this->manager->findOne($this->userSession->getUser(), $shareType, $shareWith);

if ($contact) {
return $contact;
}
return new JSONResponse([], Http::STATUS_NOT_FOUND);
}
}
6 changes: 3 additions & 3 deletions core/js/dist/install.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/install.js.map

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions core/js/dist/login.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/login.js.map

Large diffs are not rendered by default.

35 changes: 17 additions & 18 deletions core/js/dist/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/maintenance.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/maintenance.js.map

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions core/js/dist/recommendedapps.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/recommendedapps.js.map

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions core/js/dist/unified-search.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion core/js/dist/unified-search.js.map

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions core/js/tests/specs/contactsmenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
*
*/

import { generateOcsUrl } from '@nextcloud/router'

describe('Contacts menu', function() {
var $triggerEl,
$menuEl,
Expand All @@ -47,7 +49,7 @@ describe('Contacts menu', function() {
});

it('shows a loading message while data is being fetched', function() {
fakeServer.respondWith('GET', OC.generateUrl('/contactsmenu/contacts'), [
fakeServer.respondWith('GET', generateOcsUrl('/contactsmenu/contacts'), [
200,
{},
''
Expand All @@ -60,7 +62,7 @@ describe('Contacts menu', function() {

it('shows an error message when loading the contacts data fails', function(done) {
spyOn(console, 'error');
fakeServer.respondWith('GET', OC.generateUrl('/contactsmenu/contacts'), [
fakeServer.respondWith('GET', generateOcsUrl('/contactsmenu/contacts'), [
500,
{},
''
Expand Down
10 changes: 5 additions & 5 deletions core/js/tests/specs/jquery.contactsmenuSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('jquery.contactsMenu tests', function() {
$selector1.click();

expect(fakeServer.requests[0].method).toEqual('POST');
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
expect(fakeServer.requests[0].url).toEqual('http://localhost/ocs/v2.php/contactsmenu/find-one?format=json');
fakeServer.requests[0].respond(
200,
{ 'Content-Type': 'application/json; charset=utf-8' },
Expand Down Expand Up @@ -151,7 +151,7 @@ describe('jquery.contactsMenu tests', function() {
})
);
expect(fakeServer.requests[0].method).toEqual('POST');
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
expect(fakeServer.requests[0].url).toEqual('http://localhost/ocs/v2.php/contactsmenu/find-one?format=json');

$selector1.on('load', function() {
expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="mailto:bar%40baz.wtf"><img src="foo.svg"><span>bar@baz.wtf</span></a></li><li><a href="http://localhost/index.php/apps/contacts"><img src="details.svg"><span>Details</span></a></li></ul></div>');
Expand All @@ -175,7 +175,7 @@ describe('jquery.contactsMenu tests', function() {
})
);
expect(fakeServer.requests[0].method).toEqual('POST');
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
expect(fakeServer.requests[0].url).toEqual('http://localhost/ocs/v2.php/contactsmenu/find-one?format=json');

$selector1.on('load', function() {
expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="#"><span>No action available</span></a></li></ul></div>');
Expand All @@ -194,7 +194,7 @@ describe('jquery.contactsMenu tests', function() {
JSON.stringify([])
);
expect(fakeServer.requests[0].method).toEqual('POST');
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
expect(fakeServer.requests[0].url).toEqual('http://localhost/ocs/v2.php/contactsmenu/find-one?format=json');

$selector1.on('loaderror', function() {
expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="#"><span>Error fetching contact actions</span></a></li></ul></div>');
Expand All @@ -213,7 +213,7 @@ describe('jquery.contactsMenu tests', function() {
JSON.stringify([])
);
expect(fakeServer.requests[0].method).toEqual('POST');
expect(fakeServer.requests[0].url).toEqual('http://localhost/index.php/contactsmenu/findOne');
expect(fakeServer.requests[0].url).toEqual('http://localhost/ocs/v2.php/contactsmenu/find-one?format=json');

$selector1.on('loaderror', function() {
expect($appendTo.html().replace(/[\r\n\t]?(\ \ +)?/g, '')).toEqual('<div class="menu popovermenu menu-left contactsmenu-popover loaded" style="display: block;"><ul><li class="hidden"><a><span class="icon-loading-small"></span></a></li><li><a href="#"><span>No action available</span></a></li></ul></div>');
Expand Down
7 changes: 5 additions & 2 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@
['name' => 'Svg#getSvgFromApp', 'url' => '/svg/{app}/{fileName}', 'verb' => 'GET'],
['name' => 'Css#getCss', 'url' => '/css/{appName}/{fileName}', 'verb' => 'GET'],
['name' => 'Js#getJs', 'url' => '/js/{appName}/{fileName}', 'verb' => 'GET'],
['name' => 'contactsMenu#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'],
['name' => 'contactsMenu#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'],
['name' => 'contactsMenuLegacy#index', 'url' => '/contactsmenu/contacts', 'verb' => 'POST'],
['name' => 'contactsMenuLegacy#findOne', 'url' => '/contactsmenu/findOne', 'verb' => 'POST'],
['name' => 'WalledGarden#get', 'url' => '/204', 'verb' => 'GET'],
['name' => 'Search#search', 'url' => '/core/search', 'verb' => 'GET'],
['name' => 'Wipe#checkWipe', 'url' => '/core/wipe/check', 'verb' => 'POST'],
Expand All @@ -108,6 +108,9 @@
['root' => '/core', 'name' => 'AppPassword#rotateAppPassword', 'url' => '/apppassword/rotate', 'verb' => 'POST'],
['root' => '/core', 'name' => 'AppPassword#deleteAppPassword', 'url' => '/apppassword', 'verb' => 'DELETE'],

['root' => '/contactsmenu', 'name' => 'ContactsMenu#index', 'url' => '/contacts', 'verb' => 'POST'],
['root' => '/contactsmenu', 'name' => 'ContactsMenu#findOne', 'url' => '/find-one', 'verb' => 'POST'],
skjnldsv marked this conversation as resolved.
Show resolved Hide resolved

['root' => '/collaboration', 'name' => 'CollaborationResources#searchCollections', 'url' => '/resources/collections/search/{filter}', 'verb' => 'GET'],
['root' => '/collaboration', 'name' => 'CollaborationResources#listCollection', 'url' => '/resources/collections/{collectionId}', 'verb' => 'GET'],
['root' => '/collaboration', 'name' => 'CollaborationResources#renameCollection', 'url' => '/resources/collections/{collectionId}', 'verb' => 'PUT'],
Expand Down
27 changes: 20 additions & 7 deletions core/src/OC/contactsmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import $ from 'jquery'
import { Collection, Model, View } from 'backbone'

import OC from './index'
import { generateOcsUrl } from '@nextcloud/router'

/**
* @class Contact
Expand Down Expand Up @@ -447,13 +448,25 @@ ContactsMenu.prototype = {
* @returns {Promise}
*/
_getContacts: function(searchTerm) {
var url = OC.generateUrl('/contactsmenu/contacts')
return Promise.resolve($.ajax(url, {
method: 'POST',
data: {
filter: searchTerm
}
}))
return new Promise((resolve, reject) => {
$.ajax({
url: generateOcsUrl('/contactsmenu/contacts'),
type: 'POST',
data: {
format: 'json',
filter: searchTerm
},
success: function (data, status, request) {
resolve({
contacts: data.ocs.data,
contactsAppEnabled: request.getResponseHeader('X-Contacts-App-Enabled') === 'yes',
})
},
error: function (error) {
reject(error)
},
})
})
},

/**
Expand Down
11 changes: 7 additions & 4 deletions core/src/jquery/contactsmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import $ from 'jquery'

import OC from '../OC'
import { generateOcsUrl } from '@nextcloud/router'

const LIST = ''
+ '<div class="menu popovermenu menu-left hidden contactsmenu-popover">'
Expand Down Expand Up @@ -65,23 +65,26 @@ $.fn.contactsMenu = function(shareWith, shareType, appendTo) {
}

$list.addClass('loaded')
$.ajax(OC.generateUrl('/contactsmenu/findOne'), {
$.ajax(generateOcsUrl('/contactsmenu/find-one'), {
method: 'POST',
data: {
format: 'json',
shareType,
shareWith,
},
}).then(function(data) {
const contact = data.ocs.data

$list.find('ul').find('li').addClass('hidden')

let actions
if (!data.topAction) {
if (!contact.topAction) {
actions = [{
hyperlink: '#',
title: t('core', 'No action available'),
}]
} else {
actions = [data.topAction].concat(data.actions)
actions = [contact.topAction].concat(contact.actions)
}

actions.forEach(function(action) {
Expand Down
2 changes: 2 additions & 0 deletions lib/private/Contacts/ContactsMenu/ContactsStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,8 @@ private function contactArrayToEntry(array $contact) {

if (isset($contact['id'])) {
$entry->setId($contact['id']);
} elseif (isset($contact['UID'])) {
$entry->setId($contact['UID']);
}

if (isset($contact['FN'])) {
Expand Down