Skip to content

Commit

Permalink
fix: DI userId instead of userSession
Browse files Browse the repository at this point in the history
The `ActivityManager` only needs the user ID so we just can inject the `userId`.
Also make sure that it can be null, because the `ActivityManager` is DI in the `FormsService`
and the `FormsService` is used also for public forms where no user is logged in.

Signed-off-by: Ferdinand Thiessen <[email protected]>
  • Loading branch information
susnux committed Aug 20, 2024
1 parent e9e6701 commit a7e03d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 20 deletions.
17 changes: 7 additions & 10 deletions lib/Activity/ActivityManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,20 @@
use OCP\Activity\IManager;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession;
use OCP\Share\IShare;

use Psr\Log\LoggerInterface;

class ActivityManager {
private IUser $currentUser;

public function __construct(
protected string $appName,
private ?string $userId,
private IManager $manager,
private IGroupManager $groupManager,
private LoggerInterface $logger,
private IUserSession $userSession,
private CirclesService $circlesService,
) {
$this->currentUser = $userSession->getUser();
}

/**
Expand All @@ -58,9 +55,9 @@ public function publishNewShare(Form $form, string $shareeId) {
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSHARE)
->setAffectedUser($shareeId)
->setAuthor($this->currentUser->getUID())
->setAuthor($this->userId)
->setSubject(ActivityConstants::SUBJECT_NEWSHARE, [
'userId' => $this->currentUser->getUID(),
'userId' => $this->userId,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
])
Expand All @@ -82,9 +79,9 @@ public function publishNewGroupShare(Form $form, string $groupId) {
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSHARE)
->setAffectedUser($user->getUID())
->setAuthor($this->currentUser->getUID())
->setAuthor($this->userId)
->setSubject(ActivityConstants::SUBJECT_NEWGROUPSHARE, [
'userId' => $this->currentUser->getUID(),
'userId' => $this->userId,
'groupId' => $groupId,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
Expand All @@ -109,9 +106,9 @@ public function publishNewCircleShare(Form $form, string $circleId) {
$event->setApp($this->appName)
->setType(ActivityConstants::TYPE_NEWSHARE)
->setAffectedUser($user)
->setAuthor($this->currentUser->getUID())
->setAuthor($this->userId)
->setSubject(ActivityConstants::SUBJECT_NEWCIRCLESHARE, [
'userId' => $this->currentUser->getUID(),
'userId' => $this->userId,
'circleId' => $circleId,
'formTitle' => $form->getTitle(),
'formHash' => $form->getHash()
Expand Down
11 changes: 1 addition & 10 deletions tests/Unit/Activity/ActivityManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,8 @@ public function setUp(): void {
$this->groupManager = $this->createMock(IGroupManager::class);
$this->logger = $this->getMockBuilder('Psr\Log\LoggerInterface')->getMock();
$this->circlesService = $this->createMock(CirclesService::class);
$userSession = $this->createMock(IUserSession::class);

$user = $this->createMock(IUser::class);
$user->expects($this->any())
->method('getUID')
->willReturn('currentUser');
$userSession->expects($this->once())
->method('getUser')
->willReturn($user);

$this->activityManager = new ActivityManager('forms', $this->manager, $this->groupManager, $this->logger, $userSession, $this->circlesService);
$this->activityManager = new ActivityManager('forms', 'currentUser', $this->manager, $this->groupManager, $this->logger, $this->circlesService);
}

public function testPublishNewShare() {
Expand Down

0 comments on commit a7e03d0

Please sign in to comment.