Skip to content

Instantly share code, notes, and snippets.

@abrahamjuliot
Last active August 1, 2021 22:43
Show Gist options
  • Save abrahamjuliot/1904b532680a2669a43bff245d2a296f to your computer and use it in GitHub Desktop.
Save abrahamjuliot/1904b532680a2669a43bff245d2a296f to your computer and use it in GitHub Desktop.
silence unread workaholics - gmail API
// https://stackoverflow.com/a/63964522
// requires Gmail API
const getLabelMap = () => {
const allLabels = Gmail.Users.Labels.list('me')
const labelMap = allLabels.labels.reduce((acc, label) => {
acc[label.name] = label.id
return acc
}, {})
return labelMap
}
const labelMap = getLabelMap()
const labelMessage = (messageID, labels = []) => {
const labelIds = labels.map(name => labelMap[name])
return Gmail.Users.Messages.modify({ addLabelIds: labelIds }, 'me', messageID)
}
// get after hours workaholic
const workaholic = d => {
const hour = d.getHours()
const min = d.getMinutes()
const day = d.getDay()
const weekend = /^6|7$/.test(day)
const beforeStartTime = hour == 7 && min < 30
const closed = hour <= 6 || hour >= 16
return weekend || closed || beforeStartTime
}
// silence the noise
const silenceUnreadWorkaholics = () => {
const threads = GmailApp.search('is:unread', 0, 100)
const messages = threads
.map(thread => thread.getMessages())
.flat()
.filter(message => message.isUnread())
// identify the stress
const stressMessageIds = messages.reduce((acc,message) => {
const timestamp = message.getDate()
if (workaholic(timestamp)) {
acc = [...acc, message]
}
return acc
}, [])
// silence the stress
return stressMessageIds.forEach(message => {
try {
labelMessage(message.getId(), ['After Hours', 'Marked as Read'])
return message.markRead()
} catch(error) {
return console.log(error)
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment