Last active
September 8, 2017 21:03
-
-
Save cayleyh/038a1b21601493c5e942390a65e1173c to your computer and use it in GitHub Desktop.
Bookmarklet: Delete Old Files From Slack
This file contains hidden or 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
javascript:(function()%7B(function()%20%7B%2F*%20check%20for%20slack%20*%2Fif(!window.TS)%20return%20alert('Not%20on%20slack.com..%3F')%3B%2F*%20files%20older%20than%2060%20days%20*%2Fconst%20days%20%3D%2060%3Bfunction%20getFiles()%20%7Blet%20types%20%3D%20'all'%3Blet%20page%20%3D%201%3B%2F*%20unix%20timestamp%20*%2Flet%20ts_to%20%3D%20Math.round(%20(Date.now()%20-%20(days%20*%2024%20*%2060%20*%2060%20*%201000))%20%2F%201000%20)%3Breturn%20new%20Promise((res%2C%20err)%20%3D%3E%20%7BTS.api.call('files.list'%2C%20%7Btypes%2Cuser%3A%20boot_data.user_id%2Cts_to%3A%20ts_to%2Cpage%7D%2C%20(a%2C%20data)%20%3D%3E%20%7Bres(data)%3B%7D)%3B%7D)%3B%7D%3B%2F*%20promise%20wrapper%20for%20delete%20api%20*%2Ffunction%20deleteFile(file)%20%7Breturn%20new%20Promise((res%2C%20err)%20%3D%3E%20%7BTS.api.call('files.delete'%2C%20%7Bfile%3A%20file.id%7D%2C%20()%20%3D%3E%20%7B%2F*%20ignoring%20errors%20left%20%26%20right%20*%2Fres()%3B%7D)%3B%7D)%3B%7D%3Bfunction%20deleteBatches(files)%20%7Bif(files.length%20%3D%3D%3D%200)%20return%20alert(%60Done!%60)%3Blet%20deleting%20%3D%20files.splice(0%2C%2010).map(file%20%3D%3E%20%7B%20return%20deleteFile(file)%3B%20%7D)%3BPromise.all(deleting).then(()%20%3D%3E%20%7B%2F*%20shitty%20rate%20limit%20handler%20*%2FsetTimeout(()%20%3D%3E%20%7B%20deleteBatches(files)%3B%20%7D%2C%20500)%3B%7D)%3B%7D%3BgetFiles().then(data%20%3D%3E%20%7Blet%20files%20%3D%20%5B%5D%3Bfiles%20%3D%20%5B...files%2C%20...data.files%5D%3Bif(!files%20%7C%7C%20!files.length)%20return%20alert(%60No%20files%20older%20than%20%24%7Bdays%7D%20days%20to%20delete%60)%3Bif(confirm(%60Deleting%20%24%7Bfiles.length%7D%20files%20from%20Slack...%60))%20deleteBatches(files)%3B%7D)%3B%7D)()%7D)() |
This file contains hidden or 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
(function() { | |
/* check for slack */ | |
if(!window.TS) return alert('Not on slack.com..?'); | |
/* files older than 60 days */ | |
const days = 60; | |
function getFiles() { | |
let types = 'all'; | |
let page = 1; | |
/* unix timestamp */ | |
let ts_to = Math.round( (Date.now() - (days * 24 * 60 * 60 * 1000)) / 1000 ); | |
return new Promise((res, err) => { | |
TS.api.call('files.list', { | |
types, | |
user: boot_data.user_id, | |
ts_to: ts_to, | |
page | |
}, (a, data) => { | |
res(data); | |
}); | |
}); | |
}; | |
/* promise wrapper for delete api */ | |
function deleteFile(file) { | |
return new Promise((res, err) => { | |
TS.api.call('files.delete', { | |
file: file.id | |
}, () => { | |
/* ignoring errors left & right */ | |
res(); | |
}); | |
}); | |
}; | |
function deleteBatches(files) { | |
if(files.length === 0) return alert(`Done!`); | |
let deleting = files.splice(0, 10).map(file => { return deleteFile(file); }); | |
Promise.all(deleting).then(() => { | |
/* shitty rate limit handler */ | |
setTimeout(() => { deleteBatches(files); }, 500); | |
}); | |
}; | |
getFiles() | |
.then(data => { | |
let files = []; | |
files = [...files, ...data.files]; | |
if(!files || !files.length) return alert(`No files older than ${days} days to delete`); | |
if(confirm(`Deleting ${files.length} files from Slack...`)) deleteBatches(files); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A bookmarklet for deleting all files older than 60 days from Slack.
delete from slack
Based on https://gist.github.com/felixexter/97ddb80782c42ba433b2
Made with http://mrcoles.com/bookmarklet/