Last active
April 14, 2023 08:05
-
-
Save O5ten/830d6939bd6194501d36df3b2fcc7972 to your computer and use it in GitHub Desktop.
Mattermost-readonly-apps-script
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
/** | |
* A simple Google Apps Script that makes a channel "read-only" by | |
* intercepting all messages and removing them if a user isn't in the allowedUsers list | |
* latest version here: https://gist.github.com/O5ten/830d6939bd6194501d36df3b2fcc7972 | |
*/ | |
const token = '<TOKEN>'; //Bot token | |
const mattermostUrl = '<Mattermost-URL>'; | |
const allowedUsers = ['<username>'] | |
const getOptions = { | |
'method': 'get', | |
'headers': { | |
'Authorization': 'Bearer ' + token, | |
'Content-Type': 'application/json' | |
}, | |
'muteHttpExceptions': true | |
}; | |
const deleteOptions = { | |
'method': 'delete', | |
'headers': { | |
'Authorization': 'Bearer ' + token, | |
'Content-Type': 'application/json' | |
}, | |
'muteHttpExceptions': true | |
}; | |
const doPost = (request) => { | |
const { parameter, postData: { contents, type } = {} } = request; | |
const incomingWebhook = JSON.parse(contents); | |
if(!incomingWebhook){ | |
return; | |
} | |
const post = JSON.parse(UrlFetchApp.fetch(mattermostUrl + `/posts/${incomingWebhook.post_id}`, getOptions)); | |
if(post.root_id != '') { | |
Logger.log('This message is part of a thread and therefore allowed'); | |
return; | |
} | |
const user = JSON.parse(UrlFetchApp.fetch(mattermostUrl + `/users/${post.user_id}`, getOptions)); | |
Logger.log(user.username); | |
if(allowedUsers.indexOf(user.username) == -1) { | |
Logger.log("User is not allowed to post and the message is removed") | |
UrlFetchApp.fetch(mattermostUrl + `/posts/${incomingWebhook.post_id}`, deleteOptions); | |
} else { | |
Logger.log("User is allowed to post and the message is kept") | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment