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
|
<?php
namespace JsonApi\Schemas\Forum;
use JsonApi\Schemas\SchemaProvider;
use Neomerx\JsonApi\Contracts\Schema\ContextInterface;
use Neomerx\JsonApi\Schema\Link;
class Posting extends SchemaProvider
{
const TYPE = 'forum-postings';
const REL_AUTHOR = 'author';
const REL_DISCUSSION = 'discussion';
const REL_POSTING = 'posting';
const REL_RANGE = 'range';
const REL_REACTIONS = 'reactions';
const REL_REACTIONS_USER = 'reactions.user';
const REL_OPENGRAPH_URLS = 'opengraph-urls';
const REL_EDITOR = 'editor';
/**
* @param \Forum\Posting $resource
*/
public function getId($resource): ?string
{
return $resource->posting_id;
}
/**
* @param \Forum\Posting $resource
*/
public function getAttributes($resource, ContextInterface $context): iterable
{
return [
'content' => \Studip\Markup::markupToHtml($resource->content),
'content-html' => formatReady($resource->content),
'anonymous' => (bool) $resource->anonymous,
'mkdate' => date('c', $resource->mkdate),
'chdate' => date('c', $resource->chdate)
];
}
/**
* @param \Forum\Posting $resource
*/
public function hasResourceMeta($resource): bool
{
return true;
}
/**
* @param \Forum\Posting $resource
*/
public function getResourceMeta($resource)
{
return [
self::REL_OPENGRAPH_URLS => array_map(fn($og) => [
'url' => $og['url'],
'is-opengraph' => (bool) $og['is_opengraph'],
'title' => $og['title'],
'description' => $og['description'],
'image' => $og['image'],
], $resource->getOpenGraphURLs()),
'log' => $this->getPostLog($resource->content)
];
}
/**
* @param \Forum\Posting $resource
*/
public function getRelationships($resource, ContextInterface $context): iterable
{
$relationships = [];
$relationships = $this->addAuthorRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_AUTHOR));
$relationships = $this->addDiscussionRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_DISCUSSION));
$relationships = $this->addPostingRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_POSTING));
$relationships = $this->addReactionsRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_REACTIONS));
$relationships = $this->addEditorRelationship($relationships, $resource, $this->shouldInclude($context, self::REL_EDITOR));
return $relationships;
}
private function addAuthorRelationship(array $relationships, \Forum\Posting $posting, bool $withAuthor = false)
{
$author = $posting->author;
if ($withAuthor && $author) {
$relationships[self::REL_AUTHOR] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($author)
],
self::RELATIONSHIP_DATA => $author
];
}
return $relationships;
}
private function addDiscussionRelationship(array $relationships, \Forum\Posting $posting, bool $withDiscussion = false)
{
if ($withDiscussion) {
$relationships[self::REL_DISCUSSION] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($posting->discussion)
],
self::RELATIONSHIP_DATA => $posting->discussion
];
}
return $relationships;
}
private function addPostingRelationship(array $relationships, \Forum\Posting $posting, bool $withPosting = false)
{
$posting = $posting->posting;
if ($withPosting && $posting) {
$relationships[self::REL_POSTING] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->createLinkToResource($posting)
],
self::RELATIONSHIP_DATA => $posting
];
}
return $relationships;
}
private function addReactionsRelationship(array $relationships, \Forum\Posting $posting, bool $withReactions = false)
{
if ($withReactions) {
$relationships[self::REL_REACTIONS] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($posting, self::REL_REACTIONS)
],
self::RELATIONSHIP_DATA => $posting->reactions
];
}
return $relationships;
}
private function addEditorRelationship(array $relationships, \Forum\Posting $posting, bool $withEditor = false)
{
$relationships[self::REL_EDITOR] = [
self::RELATIONSHIP_LINKS => [
Link::RELATED => $this->getRelationshipRelatedLink($posting, self::REL_REACTIONS)
],
];
if ($withEditor) {
$relationships[self::REL_EDITOR][self::RELATIONSHIP_DATA] = $posting->editor;
}
return $relationships;
}
private function getPostLog($content): array
{
$attributes = [];
if (preg_match('/(<admin_msg.*?)$/s', $content, $matches)) {
$adminTag = $matches[1];
// Extract attributes
if (preg_match_all('/(\w+)="([^"]*)"/', $adminTag, $attrMatches, PREG_SET_ORDER)) {
foreach ($attrMatches as [$full, $key, $value]) {
if ($key === 'chdate') {
$attributes[$key] = date('c', $value);
continue;
}
$attributes[$key] = $value;
}
}
}
return $attributes;
}
}
|