posts.coffee 1.23 KB
################################################################################
#
# Set Mastodon posts example.
#
# Displays the global timeline from App.net.
#
# Copyright © Aral Balkan. 
# Released with ♥ by Ind.ie under the MIT License. 
# Portions released under the Apache License.
#
################################################################################

superagent = require 'superagent'

exports.route = (request, response) ->

    superagent.get('https://mastodon.ar.al/api/v1/timelines/public')

        .end (error, globalTimelineResponse) ->

            globalTimeline = globalTimelineResponse.body

            # Option to render a static template.
            globalTimeline.__set = { renderStatic: yes }

            # Handle network and Mastodon errors gracefully.
            if error
            	# There was a network error
            	globalTimeline.errorType = 'Network'
            	globalTimeline.error = error

            else if not globalTimeline

            	# There was an Mastodon error
            	globalTimeline.errorType = 'Mastodon'
            	globalTimeline.error = "Could not get the public timeline."

            # Render the response
            response.render 'posts', { data: globalTimeline }