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