Skip to content

Commit 98849c1

Browse files
chore: add real values in the activity trigger
Signed-off-by: Samuelson Brito <samuelsonma@gmail.com>
1 parent 6450c45 commit 98849c1

File tree

10 files changed

+206
-73
lines changed

10 files changed

+206
-73
lines changed

appinfo/info.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Developed with ❤️ by [LibreCode](https://librecode.coop). Help us transform
7979
</filters>
8080
<providers>
8181
<provider>OCA\Libresign\Activity\Provider\SignRequest</provider>
82+
<provider>OCA\Libresign\Activity\Provider\Signed</provider>
8283
</providers>
8384
</activity>
8485
<navigations>

lib/Activity/Filter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public function getIcon() {
4040

4141
public function filterTypes(array $types) {
4242
return array_intersect([
43-
'file_to_sign',
44-
'file_signed',
45-
], $types);
43+
'file_to_sign',
44+
'file_signed',
45+
], $types);
4646
}
4747

4848
public function allowedApps() {

lib/Activity/Listener.php

Lines changed: 47 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use OCA\Libresign\Db\SignRequest;
1414
use OCA\Libresign\Db\SignRequestMapper;
1515
use OCA\Libresign\Events\SendSignNotificationEvent;
16-
use OCA\Libresign\Events\SignedCallbackEvent;
16+
use OCA\Libresign\Events\SignedEvent;
1717
use OCA\Libresign\Service\AccountService;
1818
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
1919
use OCP\Activity\Exceptions\UnknownActivityException;
@@ -42,14 +42,18 @@ public function __construct(
4242
}
4343

4444
public function handle(Event $event): void {
45-
/** @var SendSignNotificationEvent|SignedCallbackEvent $event */
45+
/** @var SendSignNotificationEvent|SignedEvent $event */
4646
match (get_class($event)) {
4747
SendSignNotificationEvent::class => $this->generateNewSignNotificationActivity(
4848
$event->getSignRequest(),
4949
$event->getLibreSignFile(),
5050
$event->getIdentifyMethod(),
5151
),
52-
SignedCallbackEvent::class => $this->generateSignedCallbackEventActivity($event),
52+
SignedEvent::class => $this->generateSignedEventActivity(
53+
$event->getSignRequest(),
54+
$event->getLibreSignFile(),
55+
$event->getIdentifyMethod(),
56+
),
5357
};
5458
}
5559

@@ -112,34 +116,42 @@ protected function generateNewSignNotificationActivity(
112116
}
113117
}
114118

119+
protected function generateSignedEventActivity(
120+
SignRequest $signRequest,
121+
FileEntity $libreSignFile,
122+
IIdentifyMethod $identifyMethod,
123+
): void {
115124

116-
//TODO dados mockados para testar activities
117-
protected function generateSignedCallbackEventActivity(SignedCallbackEvent $event): void {
118-
125+
$actorId = $libreSignFile->getUserId();
119126

120127
$activityEvent = $this->activityManager->generateEvent();
128+
121129
try {
122130
$activityEvent
123131
->setApp(Application::APP_ID)
124132
->setType('file_signed')
125-
->setAuthor('admin')
133+
->setAuthor($actorId)
126134
->setObject('signedFile', 10)
127135
->setTimestamp($this->timeFactory->getTime())
128-
->setAffectedUser('admin')
136+
->setAffectedUser($actorId)
129137
->setGenerateNotification(true);
130138

131-
//dados mockados por enquanto
132-
$activityEvent->setSubject('new_file_signed', [
133-
'from' => $this->getUserParameter(
134-
'admin',
135-
'admin',
139+
$activityEvent->setSubject('file_signed', [
140+
'from' => $this->getFromSignedParameter(
141+
$identifyMethod->getEntity()->getIdentifierKey(),
142+
$identifyMethod->getEntity()->getIdentifierValue(),
143+
$signRequest->getDisplayName(),
136144
),
137145
'file' => [
138146
'type' => 'file',
139-
'id' => '2151',
140-
'name' => 'teste',
141-
'path' => 'teste',
142-
'link' => $this->url->linkToRouteAbsolute('libresign.page.sign', ['uuid' => 'admin']),
147+
'id' => (string)$libreSignFile->getNodeId(),
148+
'name' => $libreSignFile->getName(),
149+
'path' => $libreSignFile->getName(),
150+
'link' => $this->url->linkToRouteAbsolute('libresign.file.validateFileId', [
151+
'apiVersion' => 'v1',
152+
'fileId' => $libreSignFile->getNodeId(),
153+
]),
154+
143155
]
144156
]);
145157

@@ -173,4 +185,22 @@ protected function getUserParameter(
173185
'name' => $displayName,
174186
];
175187
}
188+
189+
protected function getFromSignedParameter(
190+
string $type,
191+
string $identifier,
192+
string $displayName,
193+
): array {
194+
195+
if ($type === 'account') {
196+
return $this->getUserParameter(
197+
$identifier,
198+
$displayName
199+
);
200+
}
201+
202+
return [
203+
'name' => $displayName,
204+
];
205+
}
176206
}

lib/Activity/Provider/SignRequest.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ public function __construct(
3030

3131
public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
3232
if ($event->getApp() !== Application::APP_ID) {
33-
throw new UnknownActivityException('Wrong app');
33+
throw new UnknownActivityException('app');
34+
}
35+
36+
if (!in_array($event->getSubject(), ['new_sign_request', 'update_sign_request'])) {
37+
throw new UnknownActivityException('subject');
3438
}
3539

3640
$this->definitions->definitions['sign-request'] = [
@@ -58,22 +62,20 @@ public function parse($language, IEvent $event, ?IEvent $previousEvent = null):
5862
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg')));
5963
}
6064

61-
if (in_array($event->getSubject(), ['new_sign_request', 'update_sign_request', 'new_file_signed'])) {
62-
$l = $this->languageFactory->get(Application::APP_ID, $language);
63-
$parameters = $event->getSubjectParameters();
65+
$l = $this->languageFactory->get(Application::APP_ID, $language);
66+
$parameters = $event->getSubjectParameters();
6467

65-
$subject = $this->getParsedSubject($l, $event->getSubject());
66-
$event->setParsedSubject(
67-
str_replace(
68-
['{from}', '{file}'],
69-
[
70-
$parameters['from']['name'],
71-
$parameters['file']['name'],
72-
],
73-
$subject
74-
))
75-
->setRichSubject($subject, $parameters);
76-
}
68+
$subject = $this->getParsedSubject($l, $event->getSubject());
69+
$event->setParsedSubject(
70+
str_replace(
71+
['{from}', '{file}'],
72+
[
73+
$parameters['from']['name'],
74+
$parameters['file']['name'],
75+
],
76+
$subject
77+
))
78+
->setRichSubject($subject, $parameters);
7779

7880
return $event;
7981
}
@@ -83,8 +85,6 @@ private function getParsedSubject(\OCP\IL10N $l, string $subject) {
8385
return $l->t('{from} requested your signature on {file}');
8486
} elseif ($subject === 'update_sign_request') {
8587
return $l->t('{from} made changes on {file}');
86-
} elseif ($subject === 'new_file_signed') {
87-
return '{from} teste aqui foi {file}';
8888
}
8989
}
9090
}

lib/Activity/Provider/Signed.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Libresign\Activity\Provider;
10+
11+
use OCA\Libresign\AppInfo\Application;
12+
use OCP\Activity\Exceptions\UnknownActivityException;
13+
use OCP\Activity\IEvent;
14+
use OCP\Activity\IManager;
15+
use OCP\Activity\IProvider;
16+
use OCP\IURLGenerator;
17+
use OCP\IUserManager;
18+
use OCP\L10N\IFactory;
19+
use OCP\RichObjectStrings\Definitions;
20+
21+
class Signed implements IProvider {
22+
public function __construct(
23+
protected IFactory $languageFactory,
24+
protected IURLGenerator $url,
25+
protected Definitions $definitions,
26+
protected IManager $activityManager,
27+
protected IUserManager $userManager,
28+
) {
29+
}
30+
31+
public function parse($language, IEvent $event, ?IEvent $previousEvent = null): IEvent {
32+
if ($event->getApp() !== Application::APP_ID) {
33+
throw new UnknownActivityException('app');
34+
}
35+
36+
if ($event->getSubject() !== 'file_signed') {
37+
throw new UnknownActivityException('subject');
38+
}
39+
40+
if ($this->activityManager->getRequirePNG()) {
41+
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.png')));
42+
} else {
43+
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath(Application::APP_ID, 'app-dark.svg')));
44+
}
45+
46+
$l = $this->languageFactory->get(Application::APP_ID, $language);
47+
$parameters = $event->getSubjectParameters();
48+
49+
$subject = $l->t('{from} signed {file}');
50+
$event->setParsedSubject(
51+
str_replace(
52+
['{from}', '{file}'],
53+
[
54+
$parameters['from']['name'],
55+
$parameters['file']['name'],
56+
],
57+
$subject
58+
))
59+
->setRichSubject($subject, $parameters);
60+
61+
return $event;
62+
}
63+
}

lib/AppInfo/Application.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OCA\Libresign\Capabilities;
1515
use OCA\Libresign\Events\SendSignNotificationEvent;
1616
use OCA\Libresign\Events\SignedCallbackEvent;
17+
use OCA\Libresign\Events\SignedEvent;
1718
use OCA\Libresign\Files\TemplateLoader as FilesTemplateLoader;
1819
use OCA\Libresign\Listener\BeforeNodeDeletedListener;
1920
use OCA\Libresign\Listener\LoadAdditionalListener;
@@ -70,7 +71,7 @@ public function register(IRegistrationContext $context): void {
7071

7172
// Activity listeners
7273
$context->registerEventListener(SendSignNotificationEvent::class, ActivityListener::class);
73-
$context->registerEventListener(SignedCallbackEvent::class, ActivityListener::class);
74+
$context->registerEventListener(SignedEvent::class, ActivityListener::class);
7475

7576
// Notification listeners
7677
$context->registerEventListener(SendSignNotificationEvent::class, NotificationListener::class);

lib/Events/SignedEvent.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Libresign\Events;
10+
11+
use OCA\Libresign\Db\File as FileEntity;
12+
use OCA\Libresign\Db\SignRequest;
13+
use OCA\Libresign\Service\IdentifyMethod\IIdentifyMethod;
14+
use OCP\EventDispatcher\Event;
15+
16+
class SignedEvent extends Event {
17+
public function __construct(
18+
private SignRequest $signRequest,
19+
private FileEntity $libreSignFile,
20+
private IIdentifyMethod $identifyMethod,
21+
) {
22+
}
23+
24+
public function getLibreSignFile(): FileEntity {
25+
return $this->libreSignFile;
26+
}
27+
28+
public function getSignRequest(): SignRequest {
29+
return $this->signRequest;
30+
}
31+
32+
public function getIdentifyMethod(): IIdentifyMethod {
33+
return $this->identifyMethod;
34+
}
35+
}

lib/Listener/NotificationListener.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,19 +98,19 @@ private function sendSignedNotification(SignedCallbackEvent $event): void {
9898
$notification = $this->notificationManager->createNotification();
9999
$notification
100100
->setApp(AppInfoApplication::APP_ID)
101-
->setObject('signedFile', 'document_signed')
101+
->setObject('signedFile', 'document_signed')
102102
->setDateTime((new \DateTime())->setTimestamp($this->timeFactory->now()->getTimestamp()))
103-
->setUser('admin')
103+
->setUser('admin')
104104
->setSubject('new_file_signed', [
105105
'from' => $this->getUserParameter(
106106
'admin',
107107
'admin',
108108
),
109109
'file' => [
110110
'type' => 'file',
111-
'id' => 'mocked-file-id',
112-
'name' => 'Mocked File Name',
113-
'link' => 'https://example.com/file/mock',
111+
'id' => 'mocked-file-id',
112+
'name' => 'Mocked File Name',
113+
'link' => 'https://example.com/file/mock',
114114
],
115115
]);
116116

lib/Notification/Notifier.php

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -128,29 +128,29 @@ private function parseSignRequest(
128128
$notification->addParsedAction($dismissAction);
129129
}
130130

131-
if($notification->getSubject() == 'new_file_signed'){
132-
$parameters = [
133-
'actor' => [
134-
'name' => 'John Doe',
135-
],
136-
'file' => [
137-
'name' => 'Contract.pdf',
138-
'link' => 'https://example.com/file/123',
139-
],
140-
];
141-
if (isset($parameters['actor'])) {
142-
$subject = $l->t('{actor} signed the document {file}');
143-
$notification->setParsedSubject(
144-
str_replace(
145-
['{actor}', '{file}'],
146-
[
147-
$parameters['actor']['name'] ?? 'Mocked Actor',
148-
$parameters['file']['name'] ?? 'Mocked File',
149-
],
150-
$subject
151-
)
152-
)->setRichSubject($subject, $parameters);
153-
}
131+
if ($notification->getSubject() == 'new_file_signed') {
132+
$parameters = [
133+
'actor' => [
134+
'name' => 'John Doe',
135+
],
136+
'file' => [
137+
'name' => 'Contract.pdf',
138+
'link' => 'https://example.com/file/123',
139+
],
140+
];
141+
if (isset($parameters['actor'])) {
142+
$subject = $l->t('{actor} signed the document {file}');
143+
$notification->setParsedSubject(
144+
str_replace(
145+
['{actor}', '{file}'],
146+
[
147+
$parameters['actor']['name'] ?? 'Mocked Actor',
148+
$parameters['file']['name'] ?? 'Mocked File',
149+
],
150+
$subject
151+
)
152+
)->setRichSubject($subject, $parameters);
153+
}
154154
}
155155

156156
return $notification;

0 commit comments

Comments
 (0)