I spent some time this morning looking at the source code that actually runs Mastodon. It's a great resource if you're trying to figure out how things work under the hood with any similar app, really. https://github.com/mastodon
The documentation is also just fantastic for something like this, I've seen billion dollar companies that can't put something like this together https://docs.joinmastodon.org/