{"size": 7065, "ext": "js", "max_stars_repo_path": "api.js", "max_stars_repo_name": "thescouser89/web", "max_stars_repo_stars_event_min_datetime": "2015-01-02T00:05:15.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-16T04:07:36.000Z", "max_issues_repo_path": "api.js", "max_issues_repo_name": "thescouser89/web", "max_issues_repo_issues_event_min_datetime": "2015-10-26T06:02:07.000Z", "max_issues_repo_issues_event_max_datetime": "2022-03-16T23:12:06.000Z", "max_forks_repo_path": "api.js", "max_forks_repo_name": "thescouser89/web", "max_forks_repo_forks_event_min_datetime": "2015-01-08T20:10:16.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-14T12:02:39.000Z"}
{"max_stars_count": 121, "max_issues_count": 79, "max_forks_count": 59, "avg_line_length": 35.5025125628, "max_line_length": 134, "alphanum_fraction": 0.5408351026}
var express = require('express');
var router = express.Router();

var Christmas = require("./public/js/christmas"),
    zones = require("./public/js/zones"),
    labels = require("./public/js/labels")
    moment = require("moment-timezone");

module.exports = function(app, config, findCountry) {

    var ERROR = function(message) {
        return {
            "errors": [
                {"message": message}
            ]
        }
    };

    var UNAUTHORIZED = "No thank you.",
        BAD_TRIGGER = "Invalid trigger.",
        BAD_TRIGGER_FIELD = "Invalid trigger field.",
        BAD_TIMEZONE = "Invalid timezone.",
        BAD_COUNTRY = "Invalid country code.",
        INVALID_TIMEZONE = "Not a valid timezone.",
        MISSING_TRIGGER_FIELDS = "Missing trigger fields.",
        EVERYTHING_IS_FINE = "Everything is fine."


    // if a client secret is set, gate access to some methods
    var authed = function(req) {
        var secret = config.ifttt;
        if (!secret) {
            console.log("No client secret set, not checking auth.");
            return true;
        }

        return req.get("IFTTT-Channel-Key") == secret;
    };

    // Every Christmas since 2007, in the given time zone,
    // to honor the site's history.
    var christmasesFor = function(country, timezone) {
        var firstYear = 2007; // nostalgia

        var now = new Date();

        var thisYear = 1900 + now.getYear();
        var years = thisYear - firstYear;

        var christmases = [];

        for (var i=0; i<=years; i++) {
            var year = thisYear - i; // reverse chronological order
            var christmasDay = Christmas.forYear(moment, year, timezone);

            // only show christmases past
            if (now > christmasDay) {
                christmases.push({
                    answer: Christmas.yes(country),
                    christmas: true,
                    christmas_day: christmasDay.toISOString(),
                    christmas_time: (christmasDay.getTime() / 1000),
                    year: year,
                    id: "christmas-" + year,
                    timezone: timezone,
                    country: country,
                    country_names: Christmas.countries[country].names
                });
            }
        }

        return christmases;
    };

    // given a christmas object above, prepare it for IFTTT format
    var iftttFor = function(christmas) {
        return {
            answer: christmas.answer,
            year: christmas.year,
            created_at: christmas.christmas_day,
            meta: {
                // should only ever fire once a year
                id: christmas.id,
                timestamp: christmas.christmas_time,
            }
        }
    };

    // isitchristmas API.
    // Default to Esperanto and Greenwich Mean Time.
    router.get('/', function(req, res) {
        var country = findCountry(req) || "EO";
        if (!Christmas.countries[country]) return res.status(400).json(ERROR(BAD_COUNTRY));

        var timezone = (req.query.timezone || "UTC");
        if (moment.tz.zone(timezone) == null) {
            return res.status(400).json(ERROR(BAD_TIMEZONE));
        }

        res.json({christmases: christmasesFor(country, timezone)})
    });

    router.get('/test', function(req, res) {
        res.send("The API is alive!");
    });

    router.post('/ifttt/v1/triggers/:trigger', function(req, res) {
        if (config.ifttt_debug) console.log("trigger: " + req.params.trigger);
        if (!authed(req)) return res.status(401).json(ERROR(UNAUTHORIZED));

        if (req.params.trigger != "christmas") return res.status(400).json(ERROR(BAD_TRIGGER));

        if (!req.body.triggerFields || !req.body.triggerFields.timezone) return res.status(400).json(ERROR(MISSING_TRIGGER_FIELDS));
        if (!zones[req.body.triggerFields.timezone]) return res.status(400).json(ERROR(INVALID_TIMEZONE));

        if (req.body.limit === 0)
            res.json({"data": []});
        else {

            // map IFTTT zone name to standard zone name
            // we've validated that this field is here
            var timezone = zones[req.body.triggerFields.timezone];

            // every christmas since 2007 in the user's time zone
            // for IFTTT, sadly I must just assume English
            var christmases = christmasesFor("US", timezone);

            var items = [];
            for (var i=0; i<christmases.length; i++)
                items.push(iftttFor(christmases[i]))

            // no more than 20 years of christmas data, tops
            items = items.slice(0, (req.body.limit || 20));

            res.json({"data": items});
        }
    });

    router.post('/ifttt/v1/triggers/:trigger/fields/:field/options', function(req, res) {
        if (!authed(req)) return res.status(401).json(ERROR(UNAUTHORIZED));
        if (config.ifttt_debug) console.log('triggerfield, trigger: ' + req.params.trigger + ', field: ' + req.params.field);
        if (req.params.trigger != "christmas") return res.status(400).json(ERROR(BAD_TRIGGER));
        if (req.params.field != "timezone") return res.status(400).json(ERROR(BAD_TRIGGER_FIELD));

        res.json({"data": labels});
    });

    router.post('/ifttt/v1/triggers/:trigger/fields/:field/validate', function(req, res) {
        if (!authed(req)) return res.status(401).json(ERROR(UNAUTHORIZED));
        if (config.ifttt_debug) console.log('triggerfield validate, trigger: ' + req.params.trigger + ', field: ' + req.params.field);
        if (req.params.trigger != "christmas") return res.status(400).json(ERROR(BAD_TRIGGER));
        if (req.params.field != "timezone") return res.status(400).json(ERROR(BAD_TRIGGER_FIELD));

        if (zones[req.body.value])
            res.json({"data": {valid: true}});
        else
            res.json({"data": {valid: false, message: INVALID_TIMEZONE}});

    });

    router.get('/ifttt/v1/status', function(req, res) {
        if (!authed(req)) return res.status(401).json(ERROR(UNAUTHORIZED));

        res.json({
            "data": {
                "status": "OK",
                "time": new Date().toISOString(),
                "message": EVERYTHING_IS_FINE
            }
        });
    });

    router.post('/ifttt/v1/test/setup', function(req, res) {
        if (!authed(req)) return res.status(401).json(ERROR(UNAUTHORIZED));
        res.json({
            "data": {
                "samples": {
                    "triggers": {
                        "christmas": {
                            "timezone": "Pacific Time (US & Canada)"
                        }
                    },
                    "triggerFieldValidations": {
                        "christmas": {
                            "timezone": {
                                "valid": "Pacific Time (US & Canada)",
                                "invalid": "America/Los_Angeles"
                            }
                        }
                    }
                }
            }
        });
    });

    return router;

};
