|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\Files_Sharing\Activity\Providers; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\Activity\IEvent; |
|
25
|
|
|
use OCP\Activity\IManager; |
|
26
|
|
|
use OCP\Activity\IProvider; |
|
27
|
|
|
use OCP\Federation\ICloudIdManager; |
|
28
|
|
|
use OCP\IL10N; |
|
29
|
|
|
use OCP\IURLGenerator; |
|
30
|
|
|
use OCP\IUserManager; |
|
31
|
|
|
use OCP\L10N\IFactory; |
|
32
|
|
|
|
|
33
|
|
|
class RemoteShares extends Base { |
|
34
|
|
|
|
|
35
|
|
|
protected $cloudIdManager; |
|
36
|
|
|
|
|
37
|
|
|
const SUBJECT_REMOTE_SHARE_ACCEPTED = 'remote_share_accepted'; |
|
38
|
|
|
const SUBJECT_REMOTE_SHARE_DECLINED = 'remote_share_declined'; |
|
39
|
|
|
const SUBJECT_REMOTE_SHARE_RECEIVED = 'remote_share_received'; |
|
40
|
|
|
const SUBJECT_REMOTE_SHARE_UNSHARED = 'remote_share_unshared'; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param IFactory $languageFactory |
|
44
|
|
|
* @param IURLGenerator $url |
|
45
|
|
|
* @param IManager $activityManager |
|
46
|
|
|
* @param IUserManager $userManager |
|
47
|
|
|
* @param ICloudIdManager $cloudIdManager |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(IFactory $languageFactory, |
|
50
|
|
|
IURLGenerator $url, |
|
51
|
|
|
IManager $activityManager, |
|
52
|
|
|
IUserManager $userManager, |
|
53
|
|
|
ICloudIdManager $cloudIdManager |
|
54
|
|
|
) { |
|
55
|
|
|
parent::__construct($languageFactory, $url, $activityManager, $userManager); |
|
56
|
|
|
$this->cloudIdManager = $cloudIdManager; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param IEvent $event |
|
61
|
|
|
* @return IEvent |
|
62
|
|
|
* @throws \InvalidArgumentException |
|
63
|
|
|
* @since 11.0.0 |
|
64
|
|
|
*/ |
|
65
|
|
|
public function parseShortVersion(IEvent $event) { |
|
66
|
|
|
$parsedParameters = $this->getParsedParameters($event); |
|
67
|
|
|
|
|
68
|
|
|
if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) { |
|
69
|
|
|
$subject = $this->l->t('{user} accepted the remote share'); |
|
70
|
|
|
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) { |
|
71
|
|
|
$subject = $this->l->t('{user} declined the remote share'); |
|
72
|
|
|
} else { |
|
73
|
|
|
throw new \InvalidArgumentException(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
77
|
|
|
$this->setSubjects($event, $subject, $parsedParameters); |
|
78
|
|
|
|
|
79
|
|
|
return $event; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param IEvent $event |
|
84
|
|
|
* @return IEvent |
|
85
|
|
|
* @throws \InvalidArgumentException |
|
86
|
|
|
* @since 11.0.0 |
|
87
|
|
|
*/ |
|
88
|
|
View Code Duplication |
public function parseLongVersion(IEvent $event) { |
|
|
|
|
|
|
89
|
|
|
$parsedParameters = $this->getParsedParameters($event); |
|
90
|
|
|
|
|
91
|
|
|
if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_RECEIVED) { |
|
92
|
|
|
$subject = $this->l->t('You received a new remote share {file} from {user}'); |
|
93
|
|
|
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_ACCEPTED) { |
|
94
|
|
|
$subject = $this->l->t('{user} accepted the remote share of {file}'); |
|
95
|
|
|
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_DECLINED) { |
|
96
|
|
|
$subject = $this->l->t('{user} declined the remote share of {file}'); |
|
97
|
|
|
} else if ($event->getSubject() === self::SUBJECT_REMOTE_SHARE_UNSHARED) { |
|
98
|
|
|
$subject = $this->l->t('{user} unshared {file} from you'); |
|
99
|
|
|
} else { |
|
100
|
|
|
throw new \InvalidArgumentException(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
104
|
|
|
$this->setSubjects($event, $subject, $parsedParameters); |
|
105
|
|
|
|
|
106
|
|
|
return $event; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
protected function getParsedParameters(IEvent $event) { |
|
110
|
|
|
$subject = $event->getSubject(); |
|
111
|
|
|
$parameters = $event->getSubjectParameters(); |
|
112
|
|
|
|
|
113
|
|
|
switch ($subject) { |
|
114
|
|
|
case self::SUBJECT_REMOTE_SHARE_RECEIVED: |
|
115
|
|
|
case self::SUBJECT_REMOTE_SHARE_UNSHARED: |
|
116
|
|
|
return [ |
|
117
|
|
|
'file' => [ |
|
118
|
|
|
'type' => 'pending-federated-share', |
|
119
|
|
|
'id' => $parameters[1], |
|
120
|
|
|
'name' => $parameters[1], |
|
121
|
|
|
], |
|
122
|
|
|
'user' => $this->getFederatedUser($parameters[0]), |
|
123
|
|
|
]; |
|
124
|
|
|
case self::SUBJECT_REMOTE_SHARE_ACCEPTED: |
|
125
|
|
|
case self::SUBJECT_REMOTE_SHARE_DECLINED: |
|
126
|
|
|
return [ |
|
127
|
|
|
'file' => $this->getFile([$event->getObjectId() => $event->getObjectName()]), |
|
128
|
|
|
'user' => $this->getFederatedUser($parameters[0]), |
|
129
|
|
|
]; |
|
130
|
|
|
} |
|
131
|
|
|
throw new \InvalidArgumentException(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $cloudId |
|
136
|
|
|
* @return array |
|
137
|
|
|
*/ |
|
138
|
|
|
protected function getFederatedUser($cloudId) { |
|
139
|
|
|
$remoteUser = $this->cloudIdManager->resolveCloudId($cloudId); |
|
140
|
|
|
return [ |
|
141
|
|
|
'type' => 'user', |
|
142
|
|
|
'id' => $remoteUser->getUser(), |
|
143
|
|
|
'name' => $cloudId,// Todo display name from contacts |
|
144
|
|
|
'server' => $remoteUser->getRemote(), |
|
145
|
|
|
]; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.