Skip to content

Instantly share code, notes, and snippets.

@O5ten
Last active April 14, 2023 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save O5ten/830d6939bd6194501d36df3b2fcc7972 to your computer and use it in GitHub Desktop.
Save O5ten/830d6939bd6194501d36df3b2fcc7972 to your computer and use it in GitHub Desktop.
Mattermost-readonly-apps-script
/**
* 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