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
|
<?php
namespace JsonApi\Routes\Messages;
use JsonApi\Errors\AuthorizationFailedException;
use JsonApi\Errors\InternalServerError;
use JsonApi\Errors\RecordNotFoundException;
use JsonApi\JsonApiController;
use JsonApi\Routes\ValidationTrait;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
class MessageCreate extends JsonApiController
{
use ValidationTrait;
public function __invoke(Request $request, Response $response, $args)
{
if (!MessageAuthority::canCreateMessage($user = $this->getUser($request))) {
throw new AuthorizationFailedException();
}
if (!$message = $this->createMessageFromJSON($user, $this->validate($request))) {
throw new InternalServerError('Could not create message.');
}
return $this->getCreatedResponse($message);
}
protected function validateResourceDocument($json, $data)
{
if (!self::arrayHas($json, 'data')) {
return 'Missing `data` member at document´s top level.';
}
if (self::arrayGet($json, 'data.type')
!== \JsonApi\Schemas\Message::TYPE
) {
return 'Missing `type` member of document´s `data`.';
}
if (!self::arrayHas($json, 'data.attributes')) {
return 'Missing `attributes` member of document´s `data`.';
}
// Attribute: subject
$subject = self::arrayGet($json, 'data.attributes.subject', '');
if (!$subject || !mb_strlen(trim($subject))) {
return '`subject` must not be empty.';
}
// Attribute: message
$message = self::arrayGet($json, 'data.attributes.message', '');
if (!$message || !mb_strlen(trim($message))) {
return '`message` must not be empty.';
}
// Attribute: tags (optional)
if ($tags = self::arrayGet($json, 'data.attributes.tags')) {
if (!is_array($tags)) {
return '`tags` must be an array of strings.';
}
foreach ($tags as $tag) {
if (!is_string($tag)) {
return '`tags` must be an array of strings.';
}
}
}
// Relation: recipients
if (!self::arrayGet($json, 'data.relationships.recipients')) {
return 'Relationship `recipients` must be present.';
}
$recipients = self::arrayGet($json, 'data.relationships.recipients.data', []);
if (empty($recipients)) {
return 'Relationship `recipients` must not be empty.';
}
foreach ($recipients as $recipient) {
if (self::arrayGet($recipient, 'type') !== \JsonApi\Schemas\User::TYPE) {
return 'Relationship `recipients` must only contain users.';
}
}
// Relation: originator
// TODO
}
protected function createMessageFromJSON(\User $user, array $json)
{
$subject = self::arrayGet($json, 'data.attributes.subject');
$body = self::arrayGet($json, 'data.attributes.message');
$tags = self::arrayGet($json, 'data.attributes.tags', []);
$recipients = $this->getRecipientsFromJSON($json);
// TODO:
$originatorMessage = null;
return $this->createMessage($user, $recipients, $subject, $body, $tags, $originatorMessage);
}
private function getRecipientsFromJSON($json)
{
return array_map(
function ($jsonRecipient) {
if (!$user = \User::find($jsonRecipient['id'])) {
throw new RecordNotFoundException('Recipient not found.');
}
return $user;
},
self::arrayGet($json, 'data.relationships.recipients.data')
);
}
protected function createMessage(
\User $sender,
array $recipients,
string $subject,
string $body,
?array $tags = null,
?string $answerTo = null
) {
$messageBody = \Studip\Markup::purifyHtml($body);
$messaging = new \messaging();
$messaging->send_as_email = 1;
$messaging->insert_message(
$messageBody,
array_map(
function (\User $recipient) {
return $recipient->username;
},
$recipients
),
$sender->id,
'',
$messageId = md5(uniqid('message', true)),
'',
null,
$subject,
'',
'normal',
$tags ?: null
);
if ($answerTo) {
$oldMessage = \Message::find($answerTo);
if ($oldMessage) {
$oldMessage->originator->answered = 1;
$oldMessage->store();
}
}
return \Message::find($messageId);
}
}
|