Skip to content
This repository has been archived by the owner on Sep 17, 2024. It is now read-only.

Commit

Permalink
Merge pull request #7 from clem/stw-issue-6
Browse files Browse the repository at this point in the history
[Configuration] Add 'APP_IMPORT_ONLY_CHANNELS' option
  • Loading branch information
clem authored Feb 20, 2018
2 parents 99771e5 + 10441cc commit a9871ab
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 20 deletions.
1 change: 1 addition & 0 deletions .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ DATABASE_URL=postgres://db_user:[email protected]:3306/db_name

###> slacktowallabag/app ###
APP_LOCALE='en'
APP_IMPORT_ONLY_CHANNELS=''
APP_EXCLUDED_CHANNELS='random'
APP_IMPORT_ONLY_USER_LINKS=''
SLACK_OAUTH_ACCESS_TOKEN=''
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Here are the different configurations variables used in the `.env.dist` file.

### Slack-to-Wallabag

- `APP_IMPORT_ONLY_CHANNELS`: List of channels (separated by comma) that will ONLY be crawled (and not the whole list)
- `APP_EXCLUDED_CHANNELS`: List of channels (separated by comma - 'general,random') that will NOT be crawled
- `APP_IMPORT_ONLY_USER_LINKS`: Import only one user's links (and not all users links)
- `HOME_SLACK_LINK`: Link to Slack homepage (could be any link, we don't use this link to crawl data)
Expand Down
4 changes: 4 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
"description": "List of channels (separated by comma - 'general,random') that will NOT be crawled",
"value": "general,random"
},
"APP_IMPORT_ONLY_CHANNELS": {
"description": "List of channels (separated by comma) that will NOT be crawled",
"value": ""
},
"APP_IMPORT_ONLY_USER_LINKS": {
"description": "Import only one user's links (and not all users links)",
"value": ""
Expand Down
6 changes: 5 additions & 1 deletion config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: '%env(APP_LOCALE)%'
app.import_only_channels: '%env(APP_IMPORT_ONLY_CHANNELS)%'
app.excluded_channels: '%env(APP_EXCLUDED_CHANNELS)%'
app.import_only_user_links: '%env(APP_IMPORT_ONLY_USER_LINKS)%'
app.slack_api.oauth_token: '%env(SLACK_OAUTH_ACCESS_TOKEN)%'
Expand Down Expand Up @@ -71,7 +72,10 @@ services:
stw.crawl_helper.slack:
class: App\Services\Slack\CrawlHelper
public: true
arguments: ['%app.slack_api.oauth_token%', '%app.excluded_channels%']
arguments:
- '%app.slack_api.oauth_token%'
- '%app.excluded_channels%'
- '%app.import_only_channels%'

stw.twitter.links_update_helper:
class: App\Services\Twitter\LinksUpdateHelper
Expand Down
22 changes: 15 additions & 7 deletions src/Command/ImportSlackFullExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,16 @@ protected function configure()
"Only import given user's links"
)
->addOption(
'exclude-app-channels',
'only-channels',
'c',
InputOption::VALUE_REQUIRED,
'Import only these channels and not app import only channels'
)
->addOption(
'exclude-app-channels-configuration',
'X',
InputOption::VALUE_NONE,
"Don't import app excluded channels"
"Don't use app channels configuration"
)
;
}
Expand Down Expand Up @@ -89,12 +95,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

// Initialize
$container = $this->getContainer();
$defaultExcludedChannels = $container->getParameter('app.excluded_channels');
$excludedChannels = $container->getParameter('app.excluded_channels');
$onlyChannels = $container->getParameter('app.import_only_channels');

// Get excluded channels
$excludedChannels = $input->getOption('excluded-channels') ?? '';
if ($input->getOption('exclude-app-channels')) {
$excludedChannels = $defaultExcludedChannels;
// Get channels configuration from input
if ($input->getOption('exclude-app-channels-configuration')) {
$excludedChannels = $input->getOption('excluded-channels') ?? '';
$onlyChannels = $input->getOption('only-channels') ?? '';
}

// Check for "only user links"
Expand All @@ -108,6 +115,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$importStatus = $fullImportHelper->importAllFromFolder($folder, [
'excluded_channels' => $excludedChannels,
'only_user' => $onlyUser,
'only_channels' => $onlyChannels
]);

// Check status
Expand Down
61 changes: 51 additions & 10 deletions src/Services/Slack/CrawlHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,25 @@ class CrawlHelper
*/
private $excludedChannels;

/**
* @var array
*/
private $importOnlyChannels;

/**
* Main constructor
*
* @param string $slackOauthToken - Slack OAuth Token
* @param string $excludedChannels - List of excluded channels (separated with comma)
* @param string $importOnlyChannels - List of channels (separated with comma) to import only
* This option won't override excluded channels
*/
public function __construct($slackOauthToken, $excludedChannels)
public function __construct($slackOauthToken, $excludedChannels, $importOnlyChannels)
{
// Initialize
$this->slack = new slack($slackOauthToken);
$this->excludedChannels = explode(',', $excludedChannels);
$this->importOnlyChannels = explode(',', $importOnlyChannels);
}

/**
Expand Down Expand Up @@ -62,20 +70,53 @@ public function getChannelsList() : array
return $channelsList;
}

// Check for excluded channels
if (!empty($this->excludedChannels)) {
// Loop on channels to excluded unwanted channels
foreach ($channelsListResponse['channels'] as $channel) {
if (!in_array($channel['name'], $this->excludedChannels)) {
$channelsList[] = $channel;
}
}
}
// Filter channels list
$channelsList = $this->filterUnwantedChannels(
$channelsListResponse['channels'],
$this->excludedChannels,
false
);
$channelsList = $this->filterUnwantedChannels(
$channelsList,
$this->importOnlyChannels,
true
);

// Return channels list
return $channelsList;
}

/**
* Filter unwanted channels from a given channels list
*
* @param array $channels - Channels list to filter
* @param array $filter - Filter list
* @param bool $inArrayMustReturn - To filter, in_array must return this value
*
* @return array - Filtered channels list
*/
private function filterUnwantedChannels(array $channels, array $filter, $inArrayMustReturn = false) : array
{
// Check channels list
if (empty($channels) || empty($filter)) {
return $channels;
}

// Initialize
$filteredChannels = [];

// Loop on channels list
/* @var array $channel */
foreach ($channels as $channel) {
if (in_array($channel['name'], $filter) === $inArrayMustReturn) {
$filteredChannels[] = $channel;
}
}

// Return filtered channels
return $filteredChannels;
}

/**
* Get channel last messages
*
Expand Down
17 changes: 15 additions & 2 deletions src/Services/Slack/FullImportHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,21 @@ public function importAllChannelsFromFolder($importFolder, array $options = [])

// Get options
$doImportArchivedChannels = $options['import_archived_channels'] ?? false;

// Check for excluded channels
$excludedChannels = $options['excluded_channels'] ?? '';
$channelsToExclude = explode(',', $excludedChannels);

// Check for no excluded channels
if (empty($channelsToExclude[0])) {
$channelsToExclude = [];
}

// Check for included channels
$importOnlyChannels = $options['only_channels'] ?? '';
$channelsToInclude = explode(',', $importOnlyChannels);
if (empty($channelsToInclude[0])) {
$channelsToInclude = [];
}

// Loop on channels and import wanted contents
foreach ($channels as $channel) {
// Check for archived channel
Expand All @@ -125,6 +132,12 @@ public function importAllChannelsFromFolder($importFolder, array $options = [])
continue;
}

// Check for included channels
if (!empty($channelsToInclude) && !in_array($channel->name, $channelsToInclude)) {
// Don't import unwanted channel
continue;
}

// Import channel
try {
$importStatus = $this->slackLinksImportHelper
Expand Down

0 comments on commit a9871ab

Please sign in to comment.