Skip to content

Commit

Permalink
feat: Store user which has downloaded file in activity feed
Browse files Browse the repository at this point in the history
Signed-off-by: Konstantin Myakshin <molodchick@gmail.com>
  • Loading branch information
Koc committed Sep 28, 2024
1 parent a0b2297 commit 4b1a335
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
37 changes: 22 additions & 15 deletions apps/files_sharing/lib/Activity/Providers/Downloads.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ public function parseLongVersion(IEvent $event, ?IEvent $previousEvent = null) {

if ($event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED ||
$event->getSubject() === self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED) {
if (!isset($parsedParameters['remote-address-hash']['type'])) {
$subject = $this->l->t('{file} downloaded via public link');
$this->setSubjects($event, $subject, $parsedParameters);
if (isset($parsedParameters['actor'])) {
$subject = $this->l->t('{file} downloaded via public link by {actor}');
} else {
$subject = $this->l->t('{file} downloaded via public link');
$this->setSubjects($event, $subject, $parsedParameters);
}

$this->setSubjects($event, $subject, $parsedParameters);
if (isset($parsedParameters['remote-address-hash']['type'])) {
$event = $this->eventMerger->mergeEvents('file', $event, $previousEvent);
}
} elseif ($event->getSubject() === self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED ||
Expand Down Expand Up @@ -92,20 +94,25 @@ protected function getParsedParameters(IEvent $event) {
switch ($subject) {
case self::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED:
case self::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED:
$parsedParameters = [
'file' => $this->getFile($parameters[0], $event),
];

if (isset($parameters[1])) {
return [
'file' => $this->getFile($parameters[0], $event),
'remote-address-hash' => [
'type' => 'highlight',
'id' => $parameters[1],
'name' => $parameters[1],
'link' => '',
],
$parsedParameters['remote-address-hash'] = [
'type' => 'highlight',
'id' => $parameters[1],
'name' => $parameters[1],
'link' => '',
];
}
return [
'file' => $this->getFile($parameters[0], $event),
];

if (isset($parameters[2])) {
$parsedParameters['actor'] = $this->getUser($parameters[2]);
}

return $parsedParameters;

case self::SUBJECT_SHARED_FILE_BY_EMAIL_DOWNLOADED:
case self::SUBJECT_SHARED_FOLDER_BY_EMAIL_DOWNLOADED:
return [
Expand Down
13 changes: 11 additions & 2 deletions apps/files_sharing/lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
use OCP\IRequest;
use OCP\ISession;
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\Events\GenerateSecurePasswordEvent;
use OCP\Security\ISecureRandom;
use OCP\Security\PasswordContext;
Expand All @@ -47,6 +49,7 @@
#[OpenAPI(scope: OpenAPI::SCOPE_IGNORE)]
class ShareController extends AuthPublicShareController {
protected ?Share\IShare $share = null;
private ?IUser $currentUser = null;

public const SHARE_ACCESS = 'access';
public const SHARE_AUTH = 'auth';
Expand All @@ -70,11 +73,17 @@ public function __construct(
protected ISecureRandom $secureRandom,
protected Defaults $defaults,
private IPublicShareTemplateFactory $publicShareTemplateFactory,
IUserSession $userSession,
) {
parent::__construct($appName, $request, $session, $urlGenerator);

$this->currentUser = $userSession->getUser();
}

/**
* @PublicPage
* @NoCSRFRequired
*
* Show the authentication page
* The form has to submit to the authenticate method route
*/
Expand Down Expand Up @@ -444,11 +453,11 @@ protected function singleFileDownloaded(Share\IShare $share, \OCP\Files\Node $no
} else {
if ($node instanceof \OCP\Files\File) {
$subject = Downloads::SUBJECT_PUBLIC_SHARED_FILE_DOWNLOADED;
$parameters[] = $remoteAddressHash;
} else {
$subject = Downloads::SUBJECT_PUBLIC_SHARED_FOLDER_DOWNLOADED;
$parameters[] = $remoteAddressHash;
}
$parameters[] = $remoteAddressHash;
$parameters[] = $this->currentUser?->getUID();
}

$this->publishActivity($subject, $parameters, $share->getSharedBy(), $fileId, $userPath);
Expand Down
7 changes: 5 additions & 2 deletions apps/files_sharing/tests/Controller/ShareControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\ISecureRandom;
use OCP\Share\Exceptions\ShareNotFound;
use OCP\Share\IPublicShareTemplateFactory;
Expand Down Expand Up @@ -71,6 +72,7 @@ class ShareControllerTest extends \Test\TestCase {
private IEventDispatcher&MockObject $eventDispatcher;
private FederatedShareProvider&MockObject $federatedShareProvider;
private IPublicShareTemplateFactory&MockObject $publicShareTemplateFactory;
private IUserSession&MockObject $userSession;

protected function setUp(): void {
parent::setUp();
Expand Down Expand Up @@ -114,6 +116,7 @@ protected function setUp(): void {
$this->appConfig,
)
);
$this->userSession = $this->createMock(IUserSession::class);

$this->shareController = new \OCA\Files_Sharing\Controller\ShareController(
$this->appName,
Expand All @@ -133,9 +136,9 @@ protected function setUp(): void {
$this->secureRandom,
$this->defaults,
$this->publicShareTemplateFactory,
$this->userSession,
);


// Store current user
$this->oldUser = \OC_User::getUser();

Expand All @@ -144,7 +147,7 @@ protected function setUp(): void {

\OC::$server->getUserManager()->createUser($this->user, $this->user);
\OC_Util::tearDownFS();
$this->loginAsUser($this->user);
self::loginAsUser($this->user);
}

protected function tearDown(): void {
Expand Down

0 comments on commit 4b1a335

Please sign in to comment.