Created
February 7, 2017 11:47
-
-
Save ronau/dd8e628215e2f767e01976cb87c498c1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>Cleanup files shared on Slack</title> | |
<style type="text/css"> | |
body { | |
font-family: Arial, sans-serif; | |
max-width: 50em; | |
margin: 0 auto; | |
padding: 10px; | |
} | |
input { | |
margin-bottom: 5px; | |
min-width: 150px; | |
} | |
#log { | |
min-height: 30em; | |
font-family: monospace; | |
font-size: 14px; | |
background-color: #ddd; | |
padding: 5px; | |
} | |
</style> | |
</head> | |
<body> | |
<h1>Cleanup files shared on Slack</h1> | |
<div id="info"> | |
This script will delete files that you posted to Slack more than 30 days ago.<br> | |
</div> | |
<h3>Step 1: Retrieve Slack API test token</h2> | |
Go to <a href="https://api.slack.com/docs/oauth-test-tokens" target="_blank">https://api.slack.com/docs/oauth-test-tokens</a> and retrieve a test token for the Slack API. | |
<h3>Step 2: Paste API token + run the script</h2> | |
Paste the token here:<br> | |
<input type="text" id="token" name="token" placeholder="Slack API token" size="70" /> | |
<input type="button" id="run" name="run" value="Run" /> | |
<h3>Output</h2> | |
<div id="log"></div> | |
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> | |
<script> | |
var token = ""; | |
var files = []; | |
var page = 1; | |
var pageCount = 1; | |
function divlog(text, linebreak) { | |
if (linebreak === undefined) | |
linebreak = true; // default value | |
$("#log").append(text); | |
if (linebreak === true) | |
$("#log").append("<br>"); | |
} | |
function getFiles() { | |
var requestUrl = "https://slack.com/api/files.list"; | |
var ts_to = parseInt(Date.now() / 1000) - 30 * 24 * 60 * 60; // older than 30 days | |
var params = { | |
"token": token, | |
"ts_to": ts_to, | |
"count": 1000, | |
"page": page | |
}; | |
console.log(params); | |
divlog("Querying list of all files visible to you ... ", false); | |
var jqxhr = $.get(requestUrl, params) | |
.done(function(data) { | |
divlog("Done"); | |
if (data.ok === true) { | |
pageCount = data.paging.pages; | |
files = files.concat(data.files); | |
if (page < pageCount) { | |
divlog("Response is paged. Repeating to fetch next page."); | |
page = page + 1; | |
getFiles(); | |
} | |
else { | |
deleteFiles(); | |
} | |
} | |
else { | |
divlog("ERROR: Slack API returned error <strong>" + data.error + "</strong>"); | |
} | |
}) | |
.fail(function() { | |
divlog("<strong>XHR to URL " + requestUrl + " FAILED</strong>"); | |
}); | |
} | |
function deleteFiles() { | |
var requestUrl = "https://slack.com/api/files.delete"; | |
divlog("<br>Now request deletion of each file. Will succeed only for files shared by you.<br>"); | |
// run through files array | |
for (var i = 0; i < files.length; i++) { | |
(function(i) { // closure | |
var params = { | |
"token": token, | |
"file": files[i].id | |
}; | |
var spanHTML = '<span id="file' + i + '">File ' + parseInt(i+1) + ' of ' + files.length + ': </span>'; | |
var spanId = "#file" + i; | |
divlog(spanHTML); | |
var jqxhr = $.get(requestUrl, params) | |
.done(function(data) { | |
if (data.ok === true) { | |
$(spanId).append("<strong>Success</strong>"); | |
} | |
else { | |
$(spanId).append("failed (<strong>" + data.error + "</strong>)"); | |
} | |
}) | |
.fail(function() { | |
$(spanId).append("<strong>XHR to URL " + requestUrl + " FAILED</strong>"); | |
}); | |
})(i); | |
} // for loop | |
} | |
$("#run").click(function() { | |
token = $("#token").val(); | |
getFiles(); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment