|
1
|
|
|
<?php |
|
2
|
|
|
namespace App; |
|
3
|
|
|
|
|
4
|
|
|
use Carbon\Carbon; |
|
5
|
|
|
use Gitter\Models\Message as GitterMessage; |
|
6
|
|
|
use Illuminate\Database\Eloquent\ { Builder, Collection }; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Message |
|
10
|
|
|
* @package App |
|
11
|
|
|
* |
|
12
|
|
|
* @property int $id |
|
13
|
|
|
* @property string $gitter_id |
|
14
|
|
|
* @property string $user_id |
|
15
|
|
|
* @property string $room_id |
|
16
|
|
|
* @property Text $text |
|
17
|
|
|
* @property array $sentences |
|
18
|
|
|
* @property string $html |
|
19
|
|
|
* @property array $urls |
|
20
|
|
|
* @property Carbon $created_at |
|
21
|
|
|
* @property Carbon $updated_at |
|
22
|
|
|
* |
|
23
|
|
|
* @property-read Mention[]|Collection $mentions |
|
24
|
|
|
* @property-read User $user |
|
25
|
|
|
* |
|
26
|
|
|
* @method Message forRoom(Room $room) |
|
27
|
|
|
*/ |
|
28
|
|
|
class Message extends \Eloquent |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var string |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $primaryKey = 'id'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $table = 'messages'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $fillable = ['gitter_id', 'user_id', 'room_id', 'text', 'html', 'urls', 'created_at', 'updated_at']; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param GitterMessage $gitter |
|
47
|
|
|
* @return Message |
|
48
|
|
|
*/ |
|
49
|
|
|
public static function createFromGitterMessage(GitterMessage $gitter) |
|
50
|
|
|
{ |
|
51
|
|
|
// Mentions |
|
52
|
|
|
foreach ($gitter->mentions as $mention) { |
|
53
|
|
|
Mention::firstOrCreate([ |
|
54
|
|
|
'user_id' => $mention, |
|
55
|
|
|
'message_id' => $gitter->id, |
|
56
|
|
|
]); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** @var Message $message */ |
|
60
|
|
|
$message = Message::where(['gitter_id' => $gitter->id])->first(); |
|
61
|
|
|
if (!$message) { |
|
62
|
|
|
$message = Message::create([ |
|
63
|
|
|
'gitter_id' => $gitter->id, |
|
64
|
|
|
'user_id' => $gitter->fromUser->id, |
|
65
|
|
|
'room_id' => $gitter->room->id, |
|
66
|
|
|
'text' => $gitter->text, |
|
67
|
|
|
'html' => $gitter->html, |
|
68
|
|
|
'urls' => json_encode($gitter->urls), |
|
69
|
|
|
|
|
70
|
|
|
'created_at' => (new Carbon($gitter->sent ?? date('Y-m-d H:i:s', 0))) |
|
71
|
|
|
->setTimezone('Europe/Moscow') |
|
72
|
|
|
->timestamp, |
|
73
|
|
|
|
|
74
|
|
|
'updated_at' => (new Carbon($gitter->editedAt ?? date('Y-m-d H:i:s', 0))) |
|
|
|
|
|
|
75
|
|
|
->setTimezone('Europe/Moscow') |
|
76
|
|
|
->timestamp, |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return $message; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param Builder $builder |
|
85
|
|
|
* @return $this |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function scopeOwn(Builder $builder) |
|
88
|
|
|
{ |
|
89
|
|
|
/** @var User $user */ |
|
90
|
|
|
$user = app(Client::class)->getUser(); |
|
91
|
|
|
|
|
92
|
|
|
return $builder->where('user_id', $user->gitter_id); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param Builder $builder |
|
97
|
|
|
* @param Room|string $room |
|
98
|
|
|
* @return $this |
|
99
|
|
|
*/ |
|
100
|
|
|
public static function scopeForRoom(Builder $builder, $room) |
|
101
|
|
|
{ |
|
102
|
|
|
$roomId = $room; |
|
103
|
|
|
if ($room instanceof Room) { |
|
104
|
|
|
$roomId = $room->gitter_id; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $builder->where('room_id', $roomId); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
|
112
|
|
|
*/ |
|
113
|
|
|
public function mentions() |
|
114
|
|
|
{ |
|
115
|
|
|
return $this->hasMany(Mention::class, 'message_id', 'gitter_id'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
|
120
|
|
|
*/ |
|
121
|
|
|
public function user() |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->belongsTo(User::class, 'user_id', 'gitter_id'); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* @outputs |
|
128
|
|
|
* @param $message |
|
129
|
|
|
* @return mixed |
|
130
|
|
|
*/ |
|
131
|
|
|
public function answer($message) |
|
132
|
|
|
{ |
|
133
|
|
|
// Break while output breaks |
|
134
|
|
|
if (!app('config')->get('gitter.output')) { |
|
135
|
|
|
return null; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** @var Client $client */ |
|
139
|
|
|
$client = app(Client::class); |
|
140
|
|
|
|
|
141
|
|
|
return await($client |
|
|
|
|
|
|
142
|
|
|
->request('message.send') |
|
143
|
|
|
->where('roomId', $this->room_id) |
|
144
|
|
|
->fetch([ |
|
145
|
|
|
'method' => 'POST', |
|
146
|
|
|
'body' => ['text' => $message], |
|
147
|
|
|
])); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @param $content |
|
152
|
|
|
* @return Text |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getTextAttribute($content) |
|
155
|
|
|
{ |
|
156
|
|
|
return new Text($content); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param array $urls |
|
161
|
|
|
*/ |
|
162
|
|
|
public function setUrlsAttribute($urls = []) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->attributes['urls'] = json_encode($urls); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param $urls |
|
169
|
|
|
* @return array|mixed |
|
170
|
|
|
*/ |
|
171
|
|
|
public function getUrlsAttribute($urls) |
|
172
|
|
|
{ |
|
173
|
|
|
$result = json_decode($urls); |
|
174
|
|
|
if (json_last_error() !== JSON_ERROR_NONE) { |
|
175
|
|
|
return []; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
return $result; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.