I think there’s a plugin now which you may find useful:
https://github.com/discourse/discourse-auto-deactivate