Provider::parseActivityHeader()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 9.568
c 0
b 0
f 0
cc 3
nc 3
nop 2
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
		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
		if (key_exists('website', $mood)) {
86
			$event->setRichMessage(
87
				$mood['text'] . '{opengraph}',
88
				['opengraph' => $this->generateOpenGraphParameter('_id_', $mood['website'])]
89
			);
90
			$event->setParsedMessage($mood['text']);
91
		} else {
92
			$event->setRichMessage(htmlspecialchars($mood['text']));
93
			$event->setParsedMessage(htmlspecialchars($mood['text']));
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'  => Circles::generateUserParameter($frame),
106
			'circles' => Circles::generateCircleParameter($frame)
107
		];
108
109
		if ($this->parseActivityHeaderAsAuthor($event, $frame, $data)) {
110
			return;
111
		}
112
113
		if ($frame->getCircle()
114
				  ->getType() === Circle::CIRCLES_PERSONAL) {
115
			$event->setRichSubject($this->l10n->t('{author} shared a mood with you'), $data);
116
			$event->setParsedSubject($this->l10n->t('{author} shared a mood with you', $data));
117
118
			return;
119
		}
120
121
		$event->setRichSubject($this->l10n->t('{author} shared a mood with {circles}'), $data);
122
		$event->setParsedSubject($this->l10n->t('{author} shared a mood with {circles}', $data));
123
	}
124
125
	/**
126
	 * @param IEvent $event
127
	 * @param SharingFrame $frame
128
	 * @param array $data
129
	 *
130
	 * @return bool
131
	 */
132
	private function parseActivityHeaderAsAuthor(IEvent &$event, SharingFrame $frame, array $data) {
133
134
		if ($frame->getAuthor() === $this->activityManager->getCurrentUserId()
135
			&& $frame->getCloudId() === null
136
		) {
137
			$event->setRichSubject($this->l10n->t('You shared a mood with {circles}'), $data);
138
			$event->setParsedSubject($this->l10n->t('You shared a mood with {circles}', $data));
139
140
			return true;
141
		}
142
143
		return false;
144
	}
145
146
147
	/**
148
	 * @param $id
149
	 * @param $website
150
	 *
151
	 * @return array
152
	 */
153
	private function generateOpenGraphParameter($id, $website) {
154
		return [
155
			'type'        => 'open-graph',
156
			'id'          => $id,
157
			'name'        => $website['title'],
158
			'description' => $website['description'],
159
			'website'     => $website['website'],
160
			'thumb'       => \OC::$server->getURLGenerator()
161
										 ->linkToRoute('mood.Tools.binFromExternalImage') . '?url='
162
							 . rawurlencode($website['thumb']),
163
			'link'        => $website['url']
164
		];
165
	}
166
167
168
}
169