|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
namespace OCA\Mood\Activity; |
|
5
|
|
|
|
|
6
|
|
|
use OCA\Circles\Api\v1\Circles; |
|
7
|
|
|
use OCA\Circles\Model\SharingFrame; |
|
8
|
|
|
use OCP\Activity\IEvent; |
|
9
|
|
|
use OCP\Activity\IManager; |
|
10
|
|
|
use OCP\Activity\IProvider; |
|
11
|
|
|
use OCP\IL10N; |
|
12
|
|
|
use OCP\IURLGenerator; |
|
13
|
|
|
|
|
14
|
|
|
class Provider implements IProvider { |
|
15
|
|
|
|
|
16
|
|
|
/** @var IL10N */ |
|
17
|
|
|
protected $l10n; |
|
18
|
|
|
|
|
19
|
|
|
/** @var IURLGenerator */ |
|
20
|
|
|
protected $url; |
|
21
|
|
|
|
|
22
|
|
|
/** @var IManager */ |
|
23
|
|
|
protected $activityManager; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(IURLGenerator $url, IManager $activityManager, IL10N $l10n) { |
|
26
|
|
|
$this->url = $url; |
|
27
|
|
|
$this->activityManager = $activityManager; |
|
28
|
|
|
$this->l10n = $l10n; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param string $lang |
|
34
|
|
|
* @param IEvent $event |
|
35
|
|
|
* @param IEvent|null $previousEvent |
|
36
|
|
|
* |
|
37
|
|
|
* @return IEvent |
|
38
|
|
|
* @since 11.0.0 |
|
39
|
|
|
*/ |
|
40
|
|
|
public function parse($lang, IEvent $event, IEvent $previousEvent = null) { |
|
41
|
|
|
|
|
42
|
|
|
if ($event->getApp() !== 'mood') { |
|
43
|
|
|
throw new \InvalidArgumentException(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$event->setIcon( |
|
47
|
|
|
$this->url->getAbsoluteURL($this->url->imagePath('mood', 'mood_black.svg')) |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
switch ($event->getSubject()) { |
|
51
|
|
|
case 'mood_item': |
|
52
|
|
|
$this->parseMoodItem($event); |
|
53
|
|
|
|
|
54
|
|
|
return $event; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
throw new \InvalidArgumentException(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param IEvent $event |
|
63
|
|
|
*/ |
|
64
|
|
|
private function parseMoodItem(IEvent &$event) { |
|
65
|
|
|
$params = $event->getSubjectParameters(); |
|
66
|
|
|
if (!key_exists('share', $params)) { |
|
67
|
|
|
throw new \InvalidArgumentException(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$frame = SharingFrame::fromJSON($params['share']); |
|
71
|
|
|
if ($frame === null) { |
|
72
|
|
|
throw new \InvalidArgumentException(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$this->parseActivityHeader($event, $frame); |
|
76
|
|
|
$this->parseMoodPayload($event, $frame->getPayload()); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param IEvent $event |
|
82
|
|
|
* @param $mood |
|
83
|
|
|
*/ |
|
84
|
|
|
private function parseMoodPayload(IEvent &$event, $mood) { |
|
85
|
|
|
|
|
86
|
|
|
if (key_exists('website', $mood)) { |
|
87
|
|
|
$event->setRichMessage( |
|
88
|
|
|
$mood['text'] . '{opengraph}', |
|
89
|
|
|
['opengraph' => $this->generateOpenGraphParameter('_id_', $mood['website'])] |
|
90
|
|
|
); |
|
91
|
|
|
} else { |
|
92
|
|
|
$event->setRichMessage(htmlspecialchars($mood['text'])); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param IEvent $event |
|
100
|
|
|
* @param SharingFrame $frame |
|
101
|
|
|
*/ |
|
102
|
|
|
private function parseActivityHeader(IEvent &$event, SharingFrame $frame) { |
|
103
|
|
|
|
|
104
|
|
|
$data = [ |
|
105
|
|
|
'author' => $this->generateUserParameter($frame), |
|
106
|
|
|
'circles' => $this->generateCircleParameter($frame) |
|
107
|
|
|
]; |
|
108
|
|
|
|
|
109
|
|
|
if ($frame->getAuthor() === $this->activityManager->getCurrentUserId() |
|
110
|
|
|
&& $frame->getCloudId() === null |
|
111
|
|
|
) { |
|
112
|
|
|
$event->setRichSubject($this->l10n->t('You shared a mood with {circles}'), $data); |
|
113
|
|
|
|
|
114
|
|
|
return; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
$event->setRichSubject($this->l10n->t('{author} shared a mood with {circles}'), $data); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param $id |
|
123
|
|
|
* @param $website |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
private function generateOpenGraphParameter($id, $website) { |
|
128
|
|
|
return [ |
|
129
|
|
|
'type' => 'open-graph', |
|
130
|
|
|
'id' => $id, |
|
131
|
|
|
'name' => $website['title'], |
|
132
|
|
|
'description' => $website['description'], |
|
133
|
|
|
'website' => $website['website'], |
|
134
|
|
|
'thumb' => \OC::$server->getURLGenerator() |
|
135
|
|
|
->linkToRoute('mood.Tools.binFromExternalImage') . '?url=' |
|
136
|
|
|
. rawurlencode($website['thumb']), |
|
137
|
|
|
'link' => $website['url'] |
|
138
|
|
|
]; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param SharingFrame $frame |
|
144
|
|
|
* |
|
145
|
|
|
* @return array |
|
146
|
|
|
*/ |
|
147
|
|
|
private function generateCircleParameter(SharingFrame $frame) { |
|
148
|
|
|
return [ |
|
149
|
|
|
'type' => 'circle', |
|
150
|
|
|
'id' => $frame->getCircleId(), |
|
151
|
|
|
'name' => $frame->getCircleName(), |
|
152
|
|
|
'link' => Circles::generateLink($frame->getCircleId()) |
|
153
|
|
|
]; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @param SharingFrame $frame |
|
159
|
|
|
* |
|
160
|
|
|
* @return array |
|
161
|
|
|
*/ |
|
162
|
|
|
private function generateUserParameter(SharingFrame $frame) { |
|
163
|
|
|
|
|
164
|
|
|
if ($frame->getCloudId() !== null) { |
|
165
|
|
|
$name = $frame->getAuthor() . '@' . $frame->getCloudId(); |
|
166
|
|
|
} else { |
|
167
|
|
|
$name = \OC::$server->getUserManager() |
|
168
|
|
|
->get($frame->getAuthor()) |
|
169
|
|
|
->getDisplayName(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
return [ |
|
173
|
|
|
'type' => 'user', |
|
174
|
|
|
'id' => $frame->getAuthor(), |
|
175
|
|
|
'name' => $name |
|
176
|
|
|
]; |
|
177
|
|
|
} |
|
178
|
|
|
} |
|
179
|
|
|
|