1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the slack-cli package. |
5
|
|
|
* |
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
13
|
|
|
|
14
|
|
|
use CL\Slack\Payload\SearchMessagesPayload; |
15
|
|
|
use CL\Slack\Payload\SearchMessagesPayloadResponse; |
16
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @author Cas Leentfaar <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class SearchMessagesCommand extends AbstractApiCommand |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* {@inheritDoc} |
26
|
|
|
*/ |
27
|
|
|
protected function configure() |
28
|
|
|
{ |
29
|
|
|
parent::configure(); |
30
|
|
|
|
31
|
|
|
$this->setName('search:messages'); |
32
|
|
|
$this->setDescription('Searches messages and files within your Slack team'); |
33
|
|
|
$this->addArgument('query', InputArgument::REQUIRED, 'Search query. May contains booleans, etc.'); |
34
|
|
|
$this->addOption('sort', null, InputOption::VALUE_REQUIRED, 'Return matches sorted by either score or timestamp'); |
35
|
|
|
$this->addOption('sort-dir', null, InputOption::VALUE_REQUIRED, 'Change sort direction to ascending (asc) or descending (desc)'); |
36
|
|
|
$this->addOption('highlight', null, InputOption::VALUE_REQUIRED, 'Pass a value of 1 to enable query highlight markers'); |
37
|
|
|
$this->addOption('count', 'c', InputOption::VALUE_REQUIRED, 'Number of items to return per page'); |
38
|
|
|
$this->addOption('page', 'p', InputOption::VALUE_REQUIRED, 'Page number of results to return'); |
39
|
|
|
$this->setHelp(<<<EOT |
40
|
|
|
The <info>search:messages</info> command allows you to search for messages matching a given query |
41
|
|
|
|
42
|
|
|
If the `--highlight` option is specified, the matching query terms will be marked up in the results so that clients may |
43
|
|
|
replace them with appropriate highlighting markers (e.g. <span class="highlight"></span>). |
44
|
|
|
|
45
|
|
|
The UTF-8 markers used are: |
46
|
|
|
- start: "\xEE\x80\x80"; # U+E000 (private-use) |
47
|
|
|
- end : "\xEE\x80\x81"; # U+E001 (private-use) |
48
|
|
|
|
49
|
|
|
For more information about the related API method, check out the official documentation: |
50
|
|
|
<comment>https://api.slack.com/methods/search.messages</comment> |
51
|
|
|
EOT |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return SearchMessagesPayload |
57
|
|
|
*/ |
58
|
|
|
protected function createPayload() |
59
|
|
|
{ |
60
|
|
|
$payload = new SearchMessagesPayload(); |
61
|
|
|
$payload->setQuery($this->input->getArgument('query')); |
62
|
|
|
$payload->setSort($this->input->getOption('sort')); |
63
|
|
|
$payload->setSortDir($this->input->getOption('sort-dir')); |
64
|
|
|
$payload->setPage($this->input->getOption('page')); |
65
|
|
|
$payload->setCount($this->input->getOption('count')); |
66
|
|
|
$payload->setHighlight($this->input->getOption('highlight')); |
67
|
|
|
|
68
|
|
|
return $payload; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
|
|
* |
74
|
|
|
* @param SearchMessagesPayloadResponse $payloadResponse |
75
|
|
|
*/ |
76
|
|
|
protected function handleResponse($payloadResponse) |
77
|
|
|
{ |
78
|
|
|
if ($payloadResponse->isOk()) { |
79
|
|
|
$total = 0; |
80
|
|
|
if ($messageSearchResult = $payloadResponse->getResult()) { |
81
|
|
|
$total += $messageSearchResult->getTotal(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->writeComment(sprintf('Got %d results...', $total)); |
85
|
|
|
|
86
|
|
|
if ($total > 0) { |
87
|
|
|
$this->writeComment('Listing messages...'); |
88
|
|
|
if ($messageSearchResult->getTotal() > 1) { |
89
|
|
|
$this->renderTable($messageSearchResult->getMatches()); |
90
|
|
|
} else { |
91
|
|
|
$this->writeComment('No messages matched the query'); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} else { |
95
|
|
|
$this->writeError(sprintf('Failed to search. %s', lcfirst($payloadResponse->getErrorExplanation()))); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|