Skip to content

Commit

Permalink
Merge pull request #1050 from nextcloud/add-support-for-room-shares
Browse files Browse the repository at this point in the history
Add support for room shares
  • Loading branch information
nickvergessen authored Aug 24, 2018
2 parents dc88a77 + 57bf6e6 commit 74b90c1
Show file tree
Hide file tree
Showing 19 changed files with 7,054 additions and 3 deletions.
2 changes: 1 addition & 1 deletion js/richobjectstringparser.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
_userLocalTemplate: '<span class="mention-user {{#if isCurrentUser}}current-user{{/if}}" data-user="{{id}}">@{{name}}</span>',

_unknownTemplate: '<strong>{{name}}</strong>',
_unknownLinkTemplate: '<a href="{{link}}">{{name}}</a>',
_unknownLinkTemplate: '<a href="{{link}}" class="external" target="_blank" rel="noopener noreferrer"><strong>{{name}}</strong></a>',

/**
* @param {string} subject
Expand Down
36 changes: 36 additions & 0 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use OCA\Spreed\GuestManager;
use OCA\Spreed\HookListener;
use OCA\Spreed\Notification\Notifier;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCA\Spreed\Settings\Personal;
use OCA\Spreed\Signaling\BackendNotifier;
Expand Down Expand Up @@ -70,6 +71,7 @@ public function register() {
$this->registerRoomInvitationHook($dispatcher);
$this->registerCallNotificationHook($dispatcher);
$this->registerChatHooks($dispatcher);
$this->registerRoomHooks($dispatcher);
$this->registerClientLinks($server);

/** @var Listener $systemMessageListener */
Expand Down Expand Up @@ -321,4 +323,38 @@ protected function registerChatHooks(EventDispatcherInterface $dispatcher) {
};
$dispatcher->addListener(Room::class . '::postDeleteRoom', $listener);
}

protected function registerRoomHooks(EventDispatcherInterface $dispatcher) {
$listener = function(GenericEvent $event) {
/** @var Room $room */
$room = $event->getSubject();

if ($event->getArgument('selfJoin')) {
/** @var \OCA\Spreed\Share\RoomShareProvider $roomShareProvider */
$roomShareProvider = $this->getContainer()->query(\OCA\Spreed\Share\RoomShareProvider::class);
$roomShareProvider->deleteInRoom($room->getToken(), $event->getArgument('userId'));
}
};
$dispatcher->addListener(Room::class . '::postUserDisconnectRoom', $listener);

$listener = function(GenericEvent $event) {
/** @var Room $room */
$room = $event->getSubject();

/** @var \OCA\Spreed\Share\RoomShareProvider $roomShareProvider */
$roomShareProvider = $this->getContainer()->query(\OCA\Spreed\Share\RoomShareProvider::class);
$roomShareProvider->deleteInRoom($room->getToken(), $event->getArgument('user')->getUID());
};
$dispatcher->addListener(Room::class . '::postRemoveUser', $listener);

$listener = function(GenericEvent $event) {
/** @var Room $room */
$room = $event->getSubject();

/** @var \OCA\Spreed\Share\RoomShareProvider $roomShareProvider */
$roomShareProvider = $this->getContainer()->query(\OCA\Spreed\Share\RoomShareProvider::class);
$roomShareProvider->deleteInRoom($room->getToken());
};
$dispatcher->addListener(Room::class . '::postDeleteRoom', $listener);
}
}
19 changes: 18 additions & 1 deletion lib/Chat/SystemMessage/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@


use OCA\Spreed\Chat\ChatManager;
use OCA\Spreed\Manager;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCA\Spreed\TalkSession;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share;
use OCP\Share\IShare;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;

Expand All @@ -37,14 +40,17 @@ class Listener {
protected $dispatcher;
/** @var ChatManager */
protected $chatManager;
/** @var Manager */
protected $roomManager;
/** @var TalkSession */
protected $talkSession;
/** @var IUserSession */
protected $userSession;

public function __construct(EventDispatcherInterface $dispatcher, ChatManager $chatManager, TalkSession $talkSession, IUserSession $userSession) {
public function __construct(EventDispatcherInterface $dispatcher, ChatManager $chatManager, Manager $roomManager, TalkSession $talkSession, IUserSession $userSession) {
$this->dispatcher = $dispatcher;
$this->chatManager = $chatManager;
$this->roomManager = $roomManager;
$this->talkSession = $talkSession;
$this->userSession = $userSession;
}
Expand Down Expand Up @@ -129,6 +135,17 @@ public function register() {
$this->sendSystemMessage($room, 'moderator_demoted', ['user' => $event->getArgument('user')]);
}
});
$this->dispatcher->addListener('OCP\Share::postShare', function(GenericEvent $event) {
/** @var IShare $share */
$share = $event->getSubject();

if ($share->getShareType() !== Share::SHARE_TYPE_ROOM) {
return;
}

$room = $this->roomManager->getRoomByToken($share->getSharedWith());
$this->sendSystemMessage($room, 'file_shared', ['share' => $share->getId()]);
});
}

protected function sendSystemMessage(Room $room, string $message, array $parameters = []) {
Expand Down
58 changes: 57 additions & 1 deletion lib/Chat/SystemMessage/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@

use OCA\Spreed\Exceptions\ParticipantNotFoundException;
use OCA\Spreed\GuestManager;
use OCA\Spreed\Share\RoomShareProvider;
use OCP\Comments\IComment;
use OCP\IL10N;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
Expand All @@ -38,6 +40,10 @@ class Parser {
protected $guestManager;
/** @var IUserSession */
protected $userSession;
/** @var RoomShareProvider */
protected $shareProvider;
/** @var IURLGenerator */
protected $url;
/** @var IL10N */
protected $l;

Expand All @@ -46,10 +52,17 @@ class Parser {
/** @var string[] */
protected $guestNames = [];

public function __construct(IUserManager $userManager, GuestManager $guestManager, IUserSession $userSession, IL10N $l) {
public function __construct(IUserManager $userManager,
GuestManager $guestManager,
IUserSession $userSession,
RoomShareProvider $shareProvider,
IURLGenerator $url,
IL10N $l) {
$this->userManager = $userManager;
$this->guestManager = $guestManager;
$this->userSession = $userSession;
$this->shareProvider = $shareProvider;
$this->url = $url;
$this->l = $l;
}

Expand Down Expand Up @@ -146,6 +159,19 @@ public function parseMessage(IComment $comment, string $displayName): array {
} else if ($currentUser instanceof IUser && $currentUser->getUID() === $parsedParameters['user']['id']) {
$parsedMessage = $this->l->t('{actor} demoted you from moderator');
}
} else if ($message === 'file_shared') {
try {
$parsedParameters['file'] = $this->getFileFromShare($parameters['share']);
$parsedMessage = $this->l->t('{actor} shared {file} into the conversation');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You shared {file} into the conversation');
}
} catch (\Exception $e) {
$parsedMessage = $this->l->t('{actor} shared a file which is no longer available');
if ($currentUserIsActor) {
$parsedMessage = $this->l->t('You shared a file which is no longer available');
}
}
}

$comment->setMessage($message);
Expand All @@ -161,6 +187,36 @@ protected function getActor(IComment $comment): array {
return $this->getUser($comment->getActorId());
}

/**
* @param string $shareId
* @return array
* @throws \OCP\Files\InvalidPathException
* @throws \OCP\Files\NotFoundException
* @throws \OCP\Share\Exceptions\ShareNotFound
*/
protected function getFileFromShare(string $shareId): array {
$share = $this->shareProvider->getShareById($shareId);
$node = $share->getNode();


if ($this->userSession->isLoggedIn()) {
$url = $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', [
'fileid' => $node->getId(),
]);
} else {
$url = $this->url->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', [
'token' => $share->getToken(),
]);
}

return [
'type' => 'file',
'id' => $shareId,
'name' => $node->getName(),
'link' => $url,
];
}

protected function getUser(string $uid): array {
if (!isset($this->displayNames[$uid])) {
$this->displayNames[$uid] = $this->getDisplayName($uid);
Expand Down
101 changes: 101 additions & 0 deletions lib/Share/Helper/DeletedShareAPIController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);

/**
*
* @copyright Copyright (c) 2018, Daniel Calviño Sánchez (danxuliu@gmail.com)
*
* @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\Share\Helper;

use OCA\Spreed\Exceptions\RoomNotFoundException;
use OCA\Spreed\Manager;
use OCA\Spreed\Room;
use OCP\IUserManager;
use OCP\Share\IShare;

/**
* Helper of OCA\Files_Sharing\Controller\DeletedShareAPIController for room
* shares.
*
* The methods of this class are called from the DeletedShareAPIController to
* perform actions or checks specific to room shares.
*/
class DeletedShareAPIController {

/** @var string */
private $userId;

/** @var IUserManager */
private $userManager;

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

/**
* DeletedShareAPIController constructor.
*
* @param string $UserId
* @param IUserManager $userManager
* @param Manager $manager
*/
public function __construct(
string $UserId,
IUserManager $userManager,
Manager $manager
) {
$this->userId = $UserId;
$this->userManager = $userManager;
$this->manager = $manager;
}

/**
* Formats the specific fields of a room share for OCS output.
*
* The returned fields override those set by the main
* DeletedShareAPIController.
*
* @param IShare $share
* @return array
*/
public function formatShare(IShare $share): array {
$result = [];

try {
$room = $this->manager->getRoomByToken($share->getSharedWith());
} catch (RoomNotFoundException $e) {
return $result;
}

// The display name of one-to-one rooms is set to the display name of
// the other participant.
$roomName = $room->getName();
if ($room->getType() === Room::ONE_TO_ONE_CALL) {
$participantsList = $room->getParticipants()['users'];
unset($participantsList[$this->userId]);

$roomName = $this->userManager->get(key($participantsList))->getDisplayName();
}

$result['share_with_displayname'] = $roomName;

return $result;
}

}
Loading

0 comments on commit 74b90c1

Please sign in to comment.