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\Message; |
14
|
|
|
use App\User; |
15
|
|
|
use Gitter\Client; |
16
|
|
|
use Gitter\Iterators\PromiseIterator\Controls; |
17
|
|
|
use Gitter\Models\Message as GitterMessage; |
18
|
|
|
use Gitter\Models\Room as GitterRoom; |
19
|
|
|
use Gitter\Models\User as GitterUser; |
20
|
|
|
use Illuminate\Container\Container; |
21
|
|
|
use Illuminate\Contracts\Config\Repository; |
22
|
|
|
use React\EventLoop\LoopInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class GitterRoomSyncCommand |
26
|
|
|
* @package App\Console\Commands |
27
|
|
|
*/ |
28
|
|
|
class GitterRoomSyncCommand extends AbstractCommand |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $signature = 'gitter:sync {room} ' . |
34
|
|
|
'{--U|users} ' . |
35
|
|
|
'{--M|messages} ' . |
36
|
|
|
'{--L|latest-messages}'; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $description = 'Sync gitter room'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Execute the console command. |
45
|
|
|
* |
46
|
|
|
* @param Repository $config |
47
|
|
|
* @param Container $container |
48
|
|
|
* |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function handle(Repository $config, Container $container) |
52
|
|
|
{ |
53
|
|
|
/** @var Client $client */ |
54
|
|
|
$client = $this->createClient($container, $config->get('gitter.token')); |
55
|
|
|
|
56
|
|
|
// Search room |
57
|
|
|
$this->findRoom($client, $this->argument('room'), function (GitterRoom $room) { |
|
|
|
|
58
|
|
|
try { |
59
|
|
|
if ($this->option('users')) { |
60
|
|
|
$this->syncUsers($room); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->option('messages')) { |
64
|
|
|
$this->syncMessages($room); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
if ($this->option('latest-messages')) { |
68
|
|
|
$this->syncLatestMessages($room); |
69
|
|
|
} |
70
|
|
|
} catch (\Throwable $e) { |
71
|
|
|
$this->error($e->getMessage()); |
72
|
|
|
} |
73
|
|
|
}); |
74
|
|
|
|
75
|
|
|
/** @var LoopInterface $loop */ |
76
|
|
|
$loop = $container->make(LoopInterface::class); |
77
|
|
|
$loop->run(); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param GitterRoom $room |
82
|
|
|
*/ |
83
|
|
|
public function syncUsers(GitterRoom $room) |
84
|
|
|
{ |
85
|
|
|
$this->output->write('Fetching...'); |
86
|
|
|
|
87
|
|
|
$room->getUsers()->fetch(function (GitterUser $gitterUser, Controls $controls) { |
88
|
|
|
/** @var User $user */ |
89
|
|
|
$user = User::make($gitterUser); |
90
|
|
|
|
91
|
|
|
$this->info(sprintf("\r[%s] %s %s", $user->created_at->toDateTimeString(), $user->login, str_repeat(' ', 20))); |
92
|
|
|
flush(); |
93
|
|
|
|
94
|
|
|
$controls->next(); |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param GitterRoom $room |
100
|
|
|
*/ |
101
|
|
View Code Duplication |
public function syncMessages(GitterRoom $room) |
|
|
|
|
102
|
|
|
{ |
103
|
|
|
/** @var CircleProgress $circle */ |
104
|
|
|
$this->output->writeln('Fetching...'); |
105
|
|
|
|
106
|
|
|
$room->getMessages() |
107
|
|
|
// Fetch all messages |
108
|
|
|
->fetch(function (GitterMessage $message, Controls $controls) { |
109
|
|
|
$this->messageRemember($message, $controls); |
110
|
|
|
}) |
111
|
|
|
->then(function () use ($room) { |
112
|
|
|
$this->syncLatestMessages($room); |
113
|
|
|
}); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param GitterMessage $gitterMessage |
118
|
|
|
* @param Controls $controls |
119
|
|
|
*/ |
120
|
|
|
protected function messageRemember(GitterMessage $gitterMessage, Controls $controls) |
121
|
|
|
{ |
122
|
|
|
User::make($gitterMessage->fromUser); |
123
|
|
|
$message = Message::make($gitterMessage); |
124
|
|
|
|
125
|
|
|
$this->info('[' . $message->created_at->toDateTimeString() . '] ' . $message->text->onlyWords()); |
126
|
|
|
flush(); |
127
|
|
|
|
128
|
|
|
$controls->next(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param GitterRoom $room |
133
|
|
|
*/ |
134
|
|
|
public function syncLatestMessages(GitterRoom $room) |
135
|
|
|
{ |
136
|
|
|
/** @var Message $lastMessage */ |
137
|
|
|
$lastMessage = Message::where('room_id', $room->id) |
138
|
|
|
->latest('created_at') |
139
|
|
|
->first(); |
140
|
|
|
|
141
|
|
|
if ($lastMessage) { |
142
|
|
|
$room->getMessages($lastMessage->gitter_id, GitterRoom::MESSAGE_FETCH_ASC) |
143
|
|
|
->fetch(function (GitterMessage $message, Controls $controls) { |
144
|
|
|
$this->messageRemember($message, $controls); |
145
|
|
|
}); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.