forked from alxp/islandora
-
Notifications
You must be signed in to change notification settings - Fork 119
/
EventGenerator.php
200 lines (178 loc) · 5.44 KB
/
EventGenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Drupal\islandora\EventGenerator;
use Drupal\Core\Entity\EntityInterface;
use Drupal\islandora\IslandoraUtils;
use Drupal\islandora\MediaSource\MediaSourceService;
use Drupal\user\UserInterface;
use Drupal\Core\Site\Settings;
use Drupal\media\Entity\Media;
use Drupal\Core\Entity\EntityStorageInterface;
/**
* The default EventGenerator implementation.
*
* Provides Activity Stream 2.0 serialized events.
*/
class EventGenerator implements EventGeneratorInterface {
/**
* Islandora utils.
*
* @var \Drupal\islandora\IslandoraUtils
*/
protected $utils;
/**
* Media source service.
*
* @var \Drupal\islandora\MediaSource\MediaSourceService
*/
protected $mediaSource;
/**
* Constructor.
*
* @param \Drupal\islandora\IslandoraUtils $utils
* Islandora utils.
* @param \Drupal\islandora\MediaSource\MediaSourceService $media_source
* Media source service.
*/
public function __construct(IslandoraUtils $utils, MediaSourceService $media_source) {
$this->utils = $utils;
$this->mediaSource = $media_source;
}
/**
* {@inheritdoc}
*/
public function generateEvent(EntityInterface $entity, UserInterface $user, array $data) {
$user_url = $this->utils->getEntityUrl($user);
$entity_type = $entity->getEntityTypeId();
if ($entity_type == 'file') {
$entity_url = $this->utils->getDownloadUrl($entity);
$mimetype = $entity->getMimeType();
}
else {
$entity_url = $this->utils->getEntityUrl($entity);
$mimetype = 'text/html';
}
$event = [
"@context" => "https://www.w3.org/ns/activitystreams",
"actor" => [
"type" => "Person",
"id" => "urn:uuid:{$user->uuid()}",
"url" => [
[
"name" => "Canonical",
"type" => "Link",
"href" => "$user_url",
"mediaType" => "text/html",
"rel" => "canonical",
],
],
],
"object" => [
"id" => "urn:uuid:{$entity->uuid()}",
"url" => [
[
"name" => "Canonical",
"type" => "Link",
"href" => $entity_url,
"mediaType" => $mimetype,
"rel" => "canonical",
],
],
],
];
$flysystem_config = Settings::get('flysystem');
$fedora_url = $flysystem_config['fedora']['config']['root'];
$event["target"] = $fedora_url;
$entity_type = $entity->getEntityTypeId();
$event_type = $data["event"];
if ($data["event"] == "Generate Derivative") {
$event["type"] = "Activity";
$event["summary"] = $data["event"];
}
else {
$event["type"] = ucfirst($data["event"]);
$event["summary"] = ucfirst($data["event"]) . " a " . ucfirst($entity_type);
}
if ($data['event'] != "Generate Derivative") {
$isNewRev = FALSE;
if ($entity->getEntityType()->isRevisionable()) {
$isNewRev = $this->isNewRevision($entity);
}
$event["object"]["isNewVersion"] = $isNewRev;
}
// Add REST links for non-file entities.
if ($entity_type != 'file') {
$event['object']['url'][] = [
"name" => "JSON",
"type" => "Link",
"href" => $this->utils->getRestUrl($entity, 'json'),
"mediaType" => "application/json",
"rel" => "alternate",
];
$event['object']['url'][] = [
"name" => "JSONLD",
"type" => "Link",
"href" => $this->utils->getRestUrl($entity, 'jsonld'),
"mediaType" => "application/ld+json",
"rel" => "alternate",
];
}
// Add a link to the file described by a media.
if ($entity_type == 'media') {
$file = $this->mediaSource->getSourceFile($entity);
if ($file) {
$event['object']['url'][] = [
"name" => "Describes",
"type" => "Link",
"href" => $this->utils->getDownloadUrl($file),
"mediaType" => $file->getMimeType(),
"rel" => "describes",
];
}
}
unset($data["event"]);
unset($data["queue"]);
if (!empty($data)) {
$event["attachment"] = [
"type" => "Object",
"content" => $data,
"mediaType" => "application/json",
];
}
return json_encode($event);
}
/**
* Method to check if an entity is a new revision.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* Drupal Entity.
*
* @return bool
* Is new version.
*/
protected function isNewRevision(EntityInterface $entity) {
if ($entity->getEntityTypeId() == "node") {
$revision_ids = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId())->revisionIds($entity);
return count($revision_ids) > 1;
}
elseif ($entity->getEntityTypeId() == "media") {
$mediaStorage = \Drupal::entityTypeManager()->getStorage($entity->getEntityTypeId());
return count($this->getRevisionIds($entity, $mediaStorage)) > 1;
}
}
/**
* Method to get the revisionIds of a media object.
*
* @param \Drupal\media\Entity\Media $media
* Media object.
* @param \Drupal\Core\Entity\EntityStorageInterface $media_storage
* Media Storage.
*/
protected function getRevisionIds(Media $media, EntityStorageInterface $media_storage) {
$result = $media_storage->getQuery()
->allRevisions()
->condition($media->getEntityType()->getKey('id'), $media->id())
->sort($media->getEntityType()->getKey('revision'), 'DESC')
->execute();
return array_keys($result);
}
}