1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nopolabs\Yabot\Message; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Nopolabs\Yabot\Helpers\SlackTrait; |
7
|
|
|
use Nopolabs\Yabot\Slack\Client; |
8
|
|
|
use Slack\Channel; |
9
|
|
|
|
10
|
|
|
class MessageFactory |
11
|
|
|
{ |
12
|
|
|
use SlackTrait; |
13
|
|
|
|
14
|
|
|
public function __construct(Client $slackClient) |
15
|
|
|
{ |
16
|
|
|
$this->setSlack($slackClient); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function create(array $data) : Message |
20
|
|
|
{ |
21
|
|
|
$formattedText = $this->assembleFormattedText($data); |
22
|
|
|
$user = $this->getUser($data); |
23
|
|
|
$channel = $this->getChannel($data); |
24
|
|
|
|
25
|
|
|
return new Message($this->getSlack(), $data, $formattedText, $user, $channel); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getUser(array $data) |
29
|
|
|
{ |
30
|
|
|
if ($this->isMessageChanged($data) && isset($data['message']['user'])) { |
31
|
|
|
return $this->getUserById($data['message']['user'] ?? null); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->getUserById($data['user'] ?? null); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getChannel(array $data) : Channel |
38
|
|
|
{ |
39
|
|
|
$channel = $this->getChannelById($data['channel']); |
40
|
|
|
|
41
|
|
|
if ($channel instanceof Channel) { |
42
|
|
|
return $channel; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
throw new Exception('Message data does not have a channel.'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function assembleFormattedText(array $data) : string |
49
|
|
|
{ |
50
|
|
|
$formatted = [$this->formatMessageText($data)]; |
51
|
|
|
|
52
|
|
|
$formatted = array_merge($formatted, $this->formatAttachmentsText($data)); |
53
|
|
|
|
54
|
|
|
return trim(implode("\n", array_filter($formatted))); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function formatMessageText(array $data) : string |
58
|
|
|
{ |
59
|
|
|
if ($this->isMessageChanged($data) && isset($data['message']['text'])) { |
60
|
|
|
return $this->formatText($data['message']['text']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if (isset($data['text'])) { |
64
|
|
|
return $this->formatText($data['text']); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return ''; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function formatAttachmentsText(array $data) : array |
71
|
|
|
{ |
72
|
|
|
$formatted = []; |
73
|
|
|
if (isset($data['attachments'])) { |
74
|
|
|
foreach ($data['attachments'] as $attachment) { |
75
|
|
|
if (isset($attachment['pretext'])) { |
76
|
|
|
$formatted[] = $this->formatText($attachment['pretext']); |
77
|
|
|
} |
78
|
|
|
if (isset($attachment['fallback'])) { |
79
|
|
|
$formatted[] = $this->formatText($attachment['fallback']); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
return $formatted; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function formatText(string $text) : string |
87
|
|
|
{ |
88
|
|
|
$pattern = '/<([^>]*)>/'; |
89
|
|
|
|
90
|
|
|
preg_match_all($pattern, $text, $matches); |
91
|
|
|
|
92
|
|
|
$split = preg_split($pattern, $text); |
93
|
|
|
|
94
|
|
|
$index = 0; |
95
|
|
|
$formatted = []; |
96
|
|
|
|
97
|
|
|
foreach ($matches[1] as $match) { |
98
|
|
|
$formatted[] = $split[$index++]; |
99
|
|
|
$formatted[] = $this->formatEntity($match); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($index < count($split)) { |
103
|
|
|
$formatted[] = $split[$index]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return trim(implode('', array_filter($formatted))); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function formatEntity(string $entity) : string |
110
|
|
|
{ |
111
|
|
|
$pipe = strrpos($entity, '|'); |
112
|
|
|
|
113
|
|
|
$readable = ($pipe !== false) ? substr($entity, $pipe + 1) : null; |
114
|
|
|
|
115
|
|
|
if ($entity && $entity[0] === '@') { |
116
|
|
|
return $this->formatUser($entity, $readable); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if ($entity && $entity[0] === '#') { |
120
|
|
|
return $this->formatChannel($entity, $readable); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $readable ? $readable : $entity; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function formatUser(string $entity, $readable = null) : string |
127
|
|
|
{ |
128
|
|
|
if ($readable) { |
129
|
|
|
return "@$readable"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$userId = substr($entity, 1); |
133
|
|
|
if ($user = $this->getUserById($userId)) { |
134
|
|
|
return '@'.$user->getUsername(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return $entity; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function formatChannel(string $entity, $readable = null) : string |
141
|
|
|
{ |
142
|
|
|
if ($readable) { |
143
|
|
|
return "#$readable"; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$channelId = substr($entity, 1); |
147
|
|
|
if ($channel = $this->getChannelById($channelId)) { |
148
|
|
|
return '#'.$channel->getName(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return $entity; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
protected function isMessageChanged(array $data): bool |
155
|
|
|
{ |
156
|
|
|
return isset($data['subtype']) && $data['subtype'] === 'message_changed'; |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|