Skip to content
Merged
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
6 changes: 6 additions & 0 deletions appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,10 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
<contactsmenu>
<provider>OCA\Spreed\ContactsMenu\Providers\CallProvider</provider>
</contactsmenu>

<collaboration>
<plugins>
<plugin type="collaborator-search" share-type="SHARE_TYPE_ROOM">OCA\Spreed\Collaboration\Collaborators\RoomPlugin</plugin>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apart from the name this PR looks good

</plugins>
</collaboration>
</info>
96 changes: 96 additions & 0 deletions lib/Collaboration/Collaborators/RoomPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
declare(strict_types=1);

/**
*
* @copyright Copyright (c) 2018, Daniel Calviño Sánchez ([email protected])
*
* @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 OCA\Spreed\Collaboration\Collaborators;

use OCA\Spreed\Manager;
use OCA\Spreed\Room;
use OCP\Collaboration\Collaborators\ISearchPlugin;
use OCP\Collaboration\Collaborators\ISearchResult;
use OCP\Collaboration\Collaborators\SearchResultType;
use OCP\IUserSession;
use OCP\Share;

class RoomPlugin implements ISearchPlugin {

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

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

/**
* @param Manager manager
* @param IUserSession userSession
*/
public function __construct(Manager $manager, IUserSession $userSession) {
$this->manager = $manager;
$this->userSession = $userSession;
}

/**
* {@inheritdoc}
*/
public function search($search, $limit, $offset, ISearchResult $searchResult) {
if (empty($search)) {
return false;
}

$result = ['wide' => [], 'exact' => []];

$rooms = $this->manager->getRoomsForParticipant($this->userSession->getUser()->getUID());
foreach ($rooms as $room) {
if (stripos($room->getName(), $search) !== false) {
$item = $this->roomToSearchResultItem($room);

if (strtolower($room->getName()) === strtolower($search)) {
$result['exact'][] = $item;
} else {
$result['wide'][] = $item;
}
}
}

$type = new SearchResultType('rooms');
$searchResult->addResultSet($type, $result['wide'], $result['exact']);

return false;
}

/**
* @param Room $room
* @return array
*/
private function roomToSearchResultItem(Room $room): array {
return
[
'label' => $room->getName(),
'value' => [
'shareType' => Share::SHARE_TYPE_ROOM,
'shareWith' => $room->getToken()
]
];
}

}
102 changes: 102 additions & 0 deletions tests/integration/features/bootstrap/SharingContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,31 @@ public function userGetsDeletedShares(string $user) {
$this->sendingTo('GET', $url);
}

/**
* @When /^user "([^"]*)" gets sharees for$/
*
* @param string $user
* @param TableNode $body
*/
public function userGetsShareesFor(string $user, TableNode $body) {
$this->currentUser = $user;

$url = '/apps/files_sharing/api/v1/sharees';

$parameters = [];
$parameters[] = 'shareType=10'; // Share::SHARE_TYPE_ROOM,
$parameters[] = 'itemType=file';
foreach ($body->getRowsHash() as $key => $value) {
$parameters[] = $key . '=' . $value;
}

$url .= '?' . implode('&', $parameters);

$this->sendingTo('GET', $url);

PHPUnit_Framework_Assert::assertEquals(200, $this->response->getStatusCode());
}

/**
* @When user :user gets the share-type DAV property for :path
*
Expand Down Expand Up @@ -490,6 +515,36 @@ public function shareXIsReturnedWith(int $number, TableNode $body) {
}
}

/**
* @Then /^"([^"]*)" sharees returned (are|is empty)$/
*
* Each sharee is specified as "| room name | room test identifier |"; the
* name is checked against the returned "label" value, and the room test
* identifier is used to get the room token, which is checked against the
* returned "shareWith" value. The returned "shareType" value is expected to
* always be "Share::SHARE_TYPE_ROOM", so there is no need to specify it.
*
* @param string $shareeType
* @param string $isEmpty
* @param TableNode|null $shareesList
*/
public function shareesReturnedAreIsEmpty(string $shareeType, string $isEmpty, TableNode $shareesList = null) {
if ($isEmpty !== 'is empty') {
$sharees = [];
foreach ($shareesList->getRows() as $row) {
$expectedSharee = [$row[0]];
$expectedSharee[] = 10; // Share::SHARE_TYPE_ROOM
$expectedSharee[] = FeatureContext::getTokenForIdentifier($row[1]);
$sharees[] = $expectedSharee;
}
$respondedArray = $this->getArrayOfShareesResponded($this->response, $shareeType);
PHPUnit_Framework_Assert::assertEquals($sharees, $respondedArray);
} else {
$respondedArray = $this->getArrayOfShareesResponded($this->response, $shareeType);
PHPUnit_Framework_Assert::assertEmpty($respondedArray);
}
}

/**
* @Then the response contains a share-types DAV property with
*
Expand Down Expand Up @@ -764,4 +819,51 @@ private function assertFieldIsInReturnedShare(string $field, string $contentExpe
}
}

private function getArrayOfShareesResponded(ResponseInterface $response, string $shareeType) {
$elements = simplexml_load_string($response->getBody())->data;
$elements = json_decode(json_encode($elements), 1);
if (strpos($shareeType, 'exact ') === 0) {
$elements = $elements['exact'];
$shareeType = substr($shareeType, 6);
}

// "simplexml_load_string" creates a SimpleXMLElement object for each
// XML element with child elements. In turn, each child is indexed by
// its tag in the SimpleXMLElement object. However, when there are
// several child XML elements with the same tag, an array with all the
// children with the same tag is indexed instead. Therefore, when the
// XML contains
// <rooms>
// <element>
// <label>...</label>
// <value>...</value>
// </element>
// </rooms>
// the "$elements[$shareeType]" variable contains an "element" key which
// in turn contains "label" and "value" keys, but when the XML contains
// <rooms>
// <element>
// <label>...</label>
// <value>...</value>
// </element>
// <element>
// <label>...</label>
// <value>...</value>
// </element>
// </rooms>
// the "$elements[$shareeType]" variable contains an "element" key which
// in turn contains "0" and "1" keys, and in turn each one contains
// "label" and "value" keys.
$elements = $elements[$shareeType];
if (array_key_exists('element', $elements) && is_int(array_keys($elements['element'])[0])) {
$elements = $elements['element'];
}

$sharees = [];
foreach ($elements as $element) {
$sharees[] = [$element['label'], $element['value']['shareType'], $element['value']['shareWith']];
}
return $sharees;
}

}
Loading