|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of GitterBot package. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Serafim <[email protected]> |
|
6
|
|
|
* @author butschster <[email protected]> |
|
7
|
|
|
* @date 09.10.2015 16:58 |
|
8
|
|
|
* |
|
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
10
|
|
|
* file that was distributed with this source code. |
|
11
|
|
|
*/ |
|
12
|
|
|
namespace Domains; |
|
13
|
|
|
|
|
14
|
|
|
use Carbon\Carbon; |
|
15
|
|
|
use Domains\Message\FormatterInterface; |
|
16
|
|
|
use Domains\Room\RoomInterface; |
|
17
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Class Message |
|
21
|
|
|
* @deprecated |
|
22
|
|
|
* |
|
23
|
|
|
* @property string $gitter_id |
|
24
|
|
|
* @property string $text |
|
25
|
|
|
* @property string $html |
|
26
|
|
|
* @property bool $edited |
|
27
|
|
|
* @property User $user |
|
28
|
|
|
* @property string $unread |
|
29
|
|
|
* @property int $read_by |
|
30
|
|
|
* @property array $urls |
|
31
|
|
|
* @property User[] $mentions |
|
32
|
|
|
* @property array $issues |
|
33
|
|
|
* @property array $meta |
|
34
|
|
|
* @property string $room_id |
|
35
|
|
|
* @property Carbon $created_at |
|
36
|
|
|
* @property Carbon $updated_at |
|
37
|
|
|
* |
|
38
|
|
|
* @property-read string $escaped_text |
|
39
|
|
|
* @property-read string $text_without_special_chars |
|
40
|
|
|
* |
|
41
|
|
|
*/ |
|
42
|
|
|
class Message extends Model implements FormatterInterface |
|
43
|
|
|
{ |
|
44
|
|
|
/** |
|
45
|
|
|
* @var RoomInterface |
|
46
|
|
|
*/ |
|
47
|
|
|
protected $room; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Message constructor. |
|
51
|
|
|
* |
|
52
|
|
|
* @param array $attributes |
|
53
|
|
|
* @param RoomInterface $room |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct(array $attributes, RoomInterface $room) |
|
56
|
|
|
{ |
|
57
|
|
|
parent::__construct($attributes); |
|
58
|
|
|
|
|
59
|
|
|
$this->room = $room; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $value |
|
64
|
|
|
* @return Carbon |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getUpdatedAtAttribute($value) |
|
67
|
|
|
{ |
|
68
|
|
|
if ($value === null) { |
|
69
|
|
|
return $this->created_at; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $value; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param callable $cb |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
public function hasMention(callable $cb) |
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($this->mentions as $mention) { |
|
82
|
|
|
if ($cb($mention)) { |
|
83
|
|
|
return true; |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
return false; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return mixed|string |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getEscapedTextAttribute() |
|
93
|
|
|
{ |
|
94
|
|
|
$text = $this->text; |
|
95
|
|
|
$text = mb_strtolower($text); |
|
96
|
|
|
$text = str_replace(["\n", "\r"], ' ', $text); |
|
97
|
|
|
$text = trim($text); |
|
98
|
|
|
|
|
99
|
|
|
return $text; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return mixed|string |
|
104
|
|
|
*/ |
|
105
|
|
|
public function getTextWithoutSpecialCharsAttribute() |
|
106
|
|
|
{ |
|
107
|
|
|
$escapedText = $this->escaped_text; |
|
108
|
|
|
$escapedText = preg_replace('/\@[a-z0-9\-_]+/iu', '', $escapedText); |
|
109
|
|
|
$escapedText = preg_replace('/[^\s\w]/iu', '', $escapedText); |
|
110
|
|
|
$escapedText = trim($escapedText); |
|
111
|
|
|
|
|
112
|
|
|
return $escapedText; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param $text |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
|
|
public function answer($text) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->room->sendMessage($text); |
|
122
|
|
|
|
|
123
|
|
|
return $this; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @param $text |
|
128
|
|
|
* @return Model |
|
129
|
|
|
*/ |
|
130
|
|
|
public function pre($text) |
|
131
|
|
|
{ |
|
132
|
|
|
return $this->answer("[pre]{$text}[/pre]"); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param $code |
|
137
|
|
|
* @param string $lang |
|
138
|
|
|
* @return Model |
|
139
|
|
|
*/ |
|
140
|
|
|
public function code($code, $lang = '') |
|
141
|
|
|
{ |
|
142
|
|
|
if (! empty($lang)) { |
|
143
|
|
|
return $this->answer("[code={$lang}]{$code}[/code]"); |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
return $this->answer("[code]{$code}[/code]"); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param $text |
|
151
|
|
|
* @return Model |
|
152
|
|
|
*/ |
|
153
|
|
|
public function italic($text) |
|
154
|
|
|
{ |
|
155
|
|
|
return $this->answer("[i]{$text}[/i]"); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* @param $text |
|
160
|
|
|
* @return Model |
|
161
|
|
|
*/ |
|
162
|
|
|
public function bold($text) |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->answer("[b]{$text}[/b]"); |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|