1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of GitterBot package. |
4
|
|
|
* |
5
|
|
|
* @author Serafim <[email protected]> |
6
|
|
|
* @date 26.01.2016 4:57 |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
namespace App\Console\Commands; |
12
|
|
|
|
13
|
|
|
use App\Gitter\Extensions\MiddlewareBuilder; |
14
|
|
|
use App\Message; |
15
|
|
|
use App\Room; |
16
|
|
|
use App\User; |
17
|
|
|
use Carbon\Carbon; |
18
|
|
|
use Gitter\Client; |
19
|
|
|
use Gitter\Iterators\PromiseIterator\Controls; |
20
|
|
|
use Gitter\Models\Message as GitterMessage; |
21
|
|
|
use Gitter\Models\Room as GitterRoom; |
22
|
|
|
use Gitter\Models\User as GitterUser; |
23
|
|
|
use Illuminate\Contracts\Config\Repository; |
24
|
|
|
use Illuminate\Contracts\Container\Container; |
25
|
|
|
use React\EventLoop\LoopInterface; |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Class GitterRoomListenCommand |
30
|
|
|
* @package App\Console\Commands |
31
|
|
|
* |
32
|
|
|
* Прослушивание сообщений требуемой комнаты |
33
|
|
|
*/ |
34
|
|
|
class GitterRoomListenCommand extends AbstractCommand |
35
|
|
|
{ |
36
|
|
|
/** |
37
|
|
|
* @var string |
38
|
|
|
*/ |
39
|
|
|
protected $signature = 'gitter:listen {room}'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $description = 'Listen gitter room'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Execute the console command. |
48
|
|
|
* |
49
|
|
|
* @param Repository $config |
50
|
|
|
* @param Container $container |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
public function handle(Repository $config, Container $container) |
55
|
|
|
{ |
56
|
|
|
/** @var Client $client */ |
57
|
|
|
$client = $this->createClient($container, $config->get('gitter.token')); |
58
|
|
|
|
59
|
|
|
$this->auth($client, function (User $user) use ($client) { |
|
|
|
|
60
|
|
|
|
61
|
|
|
$this->findRoom($client, $this->argument('room'), function (GitterRoom $room) { |
|
|
|
|
62
|
|
|
try { |
63
|
|
|
$this->listen($room, true); |
64
|
|
|
|
65
|
|
|
} catch (\Throwable $e) { |
66
|
|
|
|
67
|
|
|
$this->error($e->getMessage()); |
68
|
|
|
} |
69
|
|
|
}); |
70
|
|
|
|
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
/** @var LoopInterface $loop */ |
74
|
|
|
$loop = $container->make(LoopInterface::class); |
75
|
|
|
$loop->run(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param Client $client |
80
|
|
|
* @param \Closure $callback |
81
|
|
|
*/ |
82
|
|
|
protected function auth(Client $client, \Closure $callback) |
83
|
|
|
{ |
84
|
|
|
GitterUser::current($client) |
85
|
|
|
->then(function (GitterUser $gitterUser) use ($callback) { |
86
|
|
|
$user = User::make($gitterUser); |
87
|
|
|
\Auth::setUser($user); |
88
|
|
|
|
89
|
|
|
$callback($user); |
90
|
|
|
}, function(\Throwable $e) { |
91
|
|
|
$this->onError($e); |
92
|
|
|
}); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param GitterRoom $gitter |
97
|
|
|
* @param bool $startup |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
protected function listen(GitterRoom $gitter, $startup = false) |
101
|
|
|
{ |
102
|
|
|
/** @var Room $room */ |
103
|
|
|
$room = Room::make($gitter, function (Room $room) { |
104
|
|
|
$this->call('gitter:sync', [ |
105
|
|
|
'room' => $room->gitter_id, |
106
|
|
|
'--users' => true, |
107
|
|
|
'--messages' => true, |
108
|
|
|
]); |
109
|
|
|
}); |
110
|
|
|
|
111
|
|
|
/** @var LoopInterface $loop */ |
112
|
|
|
$loop = app(LoopInterface::class); |
113
|
|
|
|
114
|
|
|
if ($startup) { |
115
|
|
|
$this->info('Fetching last messages...'); |
116
|
|
|
$this->onMessageFallback($loop, $gitter, 1); |
117
|
|
|
|
118
|
|
|
return $this; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->info(sprintf( |
122
|
|
|
'Startup gitter listener for %s at [%s]', |
123
|
|
|
$room->url, |
124
|
|
|
Carbon::now()->toDateTimeString() |
125
|
|
|
)); |
126
|
|
|
|
127
|
|
|
$gitter->onMessage(function (GitterMessage $msg) { |
128
|
|
|
app(Client::class)->setFallbackMode(false); |
129
|
|
|
$this->onMessage($msg); |
130
|
|
|
|
131
|
|
|
}, function (\Throwable $e) use ($loop, $gitter) { |
132
|
|
|
$this->onError($e); |
133
|
|
|
$this->warn('Stream not available. Use message fallback fetch mode.'); |
134
|
|
|
|
135
|
|
|
app(Client::class)->setFallbackMode(true); |
136
|
|
|
$this->onMessageFallback($loop, $gitter); |
137
|
|
|
}); |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param LoopInterface $loop |
144
|
|
|
* @param GitterRoom $gitter |
145
|
|
|
* @param int $timeout |
146
|
|
|
*/ |
147
|
|
|
protected function onMessageFallback(LoopInterface $loop, GitterRoom $gitter, $timeout = 10) |
148
|
|
|
{ |
149
|
|
|
/** @var Message $lastMessage */ |
150
|
|
|
$lastMessage = Message::where('room_id', $gitter->id) |
151
|
|
|
->latest('created_at') |
152
|
|
|
->first(); |
153
|
|
|
|
154
|
|
|
if ($lastMessage) { |
155
|
|
|
$loop->addTimer($timeout, function () use ($gitter, $lastMessage) { |
|
|
|
|
156
|
|
|
$gitter->getMessages($lastMessage->gitter_id, GitterRoom::MESSAGE_FETCH_ASC) |
157
|
|
|
->fetch(function (GitterMessage $message, Controls $controls) { |
158
|
|
|
$this->onMessage($message); |
159
|
|
|
$controls->next(); |
160
|
|
|
}) |
161
|
|
|
->then(function () use ($gitter) { |
162
|
|
|
$this->warn('Connection...'); |
163
|
|
|
$this->listen($gitter); |
164
|
|
|
}, function(\Throwable $e) { |
165
|
|
|
$this->onError($e); |
166
|
|
|
}); |
167
|
|
|
}); |
168
|
|
|
} else { |
169
|
|
|
$this->listen($gitter); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* @param GitterMessage $gitter |
175
|
|
|
*/ |
176
|
|
|
protected function onMessage(GitterMessage $gitter) |
177
|
|
|
{ |
178
|
|
|
try { |
179
|
|
|
/** @var MiddlewareBuilder $builder */ |
180
|
|
|
$builder = app(MiddlewareBuilder::class); |
181
|
|
|
$builder->fire($gitter->room, Message::make($gitter)); |
182
|
|
|
|
183
|
|
|
} catch (\Throwable $e) { |
184
|
|
|
$this->onError($e); |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param \Throwable $e |
190
|
|
|
*/ |
191
|
|
|
protected function onError(\Throwable $e) |
192
|
|
|
{ |
193
|
|
|
$this->line(''); |
194
|
|
|
$this->error('> ' . get_class($e)); |
195
|
|
|
$this->error('> ' . $e->getMessage()); |
196
|
|
|
$this->error('> ' . $e->getFile() . ':' . $e->getLine()); |
197
|
|
|
$this->line(''); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
|
202
|
|
|
|
203
|
|
|
|
204
|
|
|
|
205
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.