Skip to content

Commit

Permalink
Add a local channels clear which will delete no longer valid channels…
Browse files Browse the repository at this point in the history
… to the command `slack:channels:update` if there is an issue with the daemon.

Ensure slack_user id is unique and drop duplicate values
  • Loading branch information
elfaus committed Sep 1, 2016
1 parent 23461b5 commit a2949c8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# Version 0.7.3
Add a local channels clear which will delete no longer valid channels to the command `slack:channels:update` if there is an issue with the daemon.

# Version 0.7.2
remove MPM channels from private channels list

Expand Down
27 changes: 26 additions & 1 deletion src/Commands/SlackChannelsUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,26 @@ public function handle()
throw new SlackSettingException("missing slack_token in settings");
}

// init Slack Api using token
$api = new SlackApi($token);

// make a call in order to fetch both public and private channels
$channels = array_merge($api->channels(false), $api->channels(true));

$slackChannelIds = [];

// iterate over each slack channel and create or update information from SeAT
foreach ($channels as $channel) {
$slackChannel = SlackChannel::find($channel['id']);
// init channels ids array which will be used later in order to remove outdate channels
$slackChannelIds[] = $channel['id'];

// init flags to default value
$isGroup = true;
$isGeneral = false;

// try to get channel object from SeAT
$slackChannel = SlackChannel::find($channel['id']);

// Determine if this is a group (private channel) or a channel
if (substr($channel['id'], 0, 1) === 'C') {
$isGroup = false;
Expand All @@ -51,6 +62,7 @@ public function handle()
$isGeneral = (boolean) $channel['is_general'];
}

// create the channel if it doesn't exist
if ($slackChannel == null) {
SlackChannel::create([
'id' => $channel['id'],
Expand All @@ -62,10 +74,23 @@ public function handle()
continue;
}

// update the channel if it is already known by SeAT
$slackChannel->update([
'name' => $channel['name'],
'is_general' => $isGeneral
]);
}

// get all known channels from SeAT
SlackChannel::whereNotIn('id', $slackChannelIds)->delete();
/*
// iterate over each of them and check if they are still valid
// if not, we will remove them from the database since they are no longer usable
foreach ($seatChannels as $channel) {
if (in_array($channel->id, $slackChannelIds) == false) {
$channel->delete();
}
}
*/
}
}
5 changes: 4 additions & 1 deletion src/Config/slackbot.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Time: 18:39
*/

/*
* TODO: lazy coder, remember to edit this file before TAG ! DAMMIT !
*/
return [
'version' => '0.7.1'
'version' => '0.7.3'
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Schema;
use Seat\Slackbot\Models\SlackUser;

class AlterSlackbotUsersUnique extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$last_user = '';
$users = SlackUser::all();

foreach ($users as $user) {
if ($user->slack_id == $last_user) {
$user->delete();

}
$last_user = $user->slack_id;
}

Schema::table('slack_users', function (Blueprint $table) {
$table->unique('slack_id');
});
}
}

0 comments on commit a2949c8

Please sign in to comment.