"use strict";

var components = require("server-components");
var domino = require("domino");
var Twit = require("twit");
var cache = require("memory-cache");
var moment = require("moment");

var Twitter = components.newElement();
Twitter.createdCallback = function () {
    var twitter = new Twit({
        consumer_key: process.env.TWITTER_CONSUMER_KEY,
        consumer_secret: process.env.TWITTER_CONSUMER_SECRET,
        access_token: process.env.TWITTER_ACCESS_TOKEN_KEY,
        access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET
    });

    var username = this.getAttribute("username");
    var cacheId = "twitter-statuses-" + username;

    var cachedTweets = cache.get(cacheId);

    var getTweetData;
    if (cachedTweets) getTweetData = Promise.resolve(cachedTweets);
    else {
        getTweetData = twitter.get("statuses/user_timeline", {
            screen_name: username,
            exclude_replies: true,
            include_rts: false,
            count: 200
        }).then((response) => {
            var tweets = response.data;
            cache.put(cacheId, tweets, 1000 * 60 * 1);
            return tweets;
        });
    }

    return getTweetData.then((tweets) => {
        this.dispatchEvent(new domino.impl.CustomEvent('items-ready', {
            items: tweets.map((tweet) => { return {
                icon: "/icons/twitter",
                details: tweet.text,
                timestamp: moment(tweet.created_at, 'dd MMM DD HH:mm:ss ZZ YYYY', 'en').unix()
            }}),
            bubbles: true
        }));
    });
};

components.registerElement("twitter-source", { prototype: Twitter });
@@ -1211,114 +1211,230 @@
     
-this.dispatchEvent(new domino.impl.CustomEvent('items-ready', %7B%0A            items: tweets.map((tweet) =%3E %7B
+var items = tweets.map((tweet) =%3E %7B%0A            var tweetText = tweet.entities.urls.reduce((tweetText, url) =%3E %7B%0A                return tweetText.replace(url.url, url.expanded_url);%0A            %7D, tweet.text);%0A%0A           
  ret
@@ -1509,18 +1509,17 @@
 s: tweet
-.t
+T
 ext,%0A   
@@ -1623,18 +1623,126 @@
        %7D
-%7D)
+%0A        %7D);%0A%0A        this.dispatchEvent(new domino.impl.CustomEvent('items-ready', %7B%0A            items: items
 ,%0A      

Replace out tweet urls with the originals