Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purge from all channels #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ slack-cleaner --token <TOKEN> --message --group hr --user "*"
slack-cleaner --token <TOKEN> --message --direct sherry --user johndoe

# Delete all messages from a multiparty direct message channel
slack-cleaner --token <TOKEN> --message --mpdirect sherry,james --user "*"
slack-cleaner --token <TOKEN> --message --mpdirect sherry,james --user "*"

# Delete all messages from certain user
slack-cleaner --token <TOKEN> --message --channel gossip --user johndoe
Expand All @@ -40,6 +40,9 @@ slack-cleaner --token <TOKEN> --message --channel auto-build --bot
# Delete all messages older than 2015/09/19
slack-cleaner --token <TOKEN> --message --channel general --user "*" --before 20150919

# Purge messages from all channels for a user
slack-cleaner --token <TOKEN> --message --purge yes --user johndoe

# Always have a look at help message
slack-cleaner --help
```
Expand Down
5 changes: 4 additions & 1 deletion slack_cleaner/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ def __init__(self):

# Channel, DM or group
g_chan = p.add_mutually_exclusive_group(required=True)
g_chan.add_argument('--purge',
help='Purge messages from all channels')
g_chan.add_argument('--channel',
help='Channel name\'s, e.g., general')
g_chan.add_argument('--direct',
Expand Down Expand Up @@ -61,11 +63,12 @@ def __init__(self):
self.delete_message = args.message
self.delete_file = args.file

self.purge_name = args.purge
self.channel_name = args.channel
self.direct_name = args.direct
self.group_name = args.group
self.mpdirect_name = args.mpdirect

self.user_name = args.user
self.bot = args.bot
self.start_time = args.after
Expand Down
81 changes: 53 additions & 28 deletions slack_cleaner/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
# User dict
user_dict = {}

# Channel dict
channel_dict = {}


# Construct a local user dict for further usage
def init_user_dict():
Expand All @@ -60,12 +63,29 @@ def init_user_dict():
# Init user dict
init_user_dict()

# Construct a local channel dict for further usage
def init_channel_dict():
res = slack.channels.list().body
if not res['ok']:
return
channels = res['channels']

for c in channels:
channel_dict[c['id']] = c['name']

# Init channel dict
init_channel_dict()

def get_id_by_name(list_dict, key_name):
for d in list_dict:
if d['name'] == key_name:
return d['id']

def purge_channels(time_range, user_id=None, bot=False):

for c in channel_dict:
print "Cleaning Channel: ",channel_dict[c]
clean_channel(c, time_range, user_id, args.bot)

def clean_channel(channel_id, time_range, user_id=None, bot=False):
# Setup time range for query
Expand All @@ -74,7 +94,9 @@ def clean_channel(channel_id, time_range, user_id=None, bot=False):

_api_end_point = None
# Set to the right API end point
if args.channel_name:
if args.purge_name:
_api_end_point = slack.channels.history
elif args.channel_name:
_api_end_point = slack.channels.history
elif args.direct_name:
_api_end_point = slack.im.history
Expand All @@ -83,6 +105,7 @@ def clean_channel(channel_id, time_range, user_id=None, bot=False):
elif args.mpdirect_name:
_api_end_point = slack.mpim.history


has_more = True
while has_more:
res = _api_end_point(channel_id, latest, oldest).body
Expand Down Expand Up @@ -169,12 +192,9 @@ def get_user_id_by_name(name):


def get_channel_id_by_name(name):
res = slack.channels.list().body
if not res['ok']:
return
channels = res['channels']
if len(channels) > 0:
return get_id_by_name(channels, name)
for k, v in channel_dict.iteritems():
if v == name:
return k


def get_direct_id_by_name(name):
Expand All @@ -201,7 +221,7 @@ def get_mpdirect_id_by_name(name):

if len(mpims) > 0:
for mpim in mpims:
# match the mpdirect user ids
# match the mpdirect user ids
if set(mpim['members']) == members:
return mpim['id']

Expand All @@ -218,24 +238,6 @@ def message_cleaner():
_channel_id = None
_user_id = None

# If channel's name is supplied
if args.channel_name:
_channel_id = get_channel_id_by_name(args.channel_name)

# If DM's name is supplied
if args.direct_name:
_channel_id = get_direct_id_by_name(args.direct_name)

# If channel's name is supplied
if args.group_name:
_channel_id = get_group_id_by_name(args.group_name)

# If group DM's name is supplied
if args.mpdirect_name:
_channel_id = get_mpdirect_id_by_name(args.mpdirect_name)

if _channel_id is None:
sys.exit('Channel, direct message or private group not found')

# If user's name is also supplied
if args.user_name:
Expand All @@ -248,8 +250,31 @@ def message_cleaner():
if _user_id is None:
sys.exit('User not found')

# Delete messages on certain channel
clean_channel(_channel_id, time_range, _user_id, args.bot)
if args.purge_name:
purge_channels(time_range, _user_id, args.bot)
else:
# If channel's name is supplied
if args.channel_name:
_channel_id = get_channel_id_by_name(args.channel_name)

# If DM's name is supplied
if args.direct_name:
_channel_id = get_direct_id_by_name(args.direct_name)

# If channel's name is supplied
if args.group_name:
_channel_id = get_group_id_by_name(args.group_name)

# If group DM's name is supplied
if args.mpdirect_name:
_channel_id = get_mpdirect_id_by_name(args.mpdirect_name)

if _channel_id is None:
sys.exit('Channel, direct message or private group not found')


# Delete messages on certain channel
clean_channel(_channel_id, time_range, _user_id, args.bot)


def file_cleaner():
Expand Down