diff --git a/index.js b/index.js index 98e90d1..256475a 100644 --- a/index.js +++ b/index.js @@ -51,18 +51,26 @@ function Construct(options, callback) { var url; app.get('/apos-twitter/feed', function(req, res) { - var username = apos.sanitizeString(req.query.username); - var hashtag = apos.sanitizeString(req.query.hashtag); + var username, + hashtag; + if (req.query.username) { + username = apos.sanitizeString(req.query.username); + } + if (req.query.hashtag){ + hashtag = apos.sanitizeString(req.query.hashtag); + } - if (!username.length) { + if (username && !username.length) { res.statusCode = 404; return res.send('not found'); } - if (!hashtag.length) { + if (username && !hashtag) { url = 'statuses/user_timeline.json?' + qs.stringify({ screen_name: username, count: req.query.count || 5 }); - } else { + } else if (username && hashtag) { url = 'search/tweets.json?' + qs.stringify({ q: 'from:' + username + ' #' + hashtag, count: req.query.count || 5 }); + } else if (hashtag && !username) { + url = 'search/tweets.json?' + qs.stringify({ q: ' #' + hashtag, count: req.query.count || 5 }); } if (_.has(tweetCache, url)) { @@ -94,8 +102,10 @@ function Construct(options, callback) { self.css = 'twitter'; self.icon = 'icon-twitter'; self.sanitize = function(item) { - var matches = item.account.match(/\w+/); - item.account = matches[0]; + if (item.account) { + var matches = item.account.match(/\w+/); + item.account = matches[0]; + } }; self.renderWidget = function(data) { return self.render('twitter', data); diff --git a/public/js/content.js b/public/js/content.js index 95317fa..4c6dfac 100644 --- a/public/js/content.js +++ b/public/js/content.js @@ -28,8 +28,18 @@ apos.widgetPlayers.twitter = function($widget) { } $tweets.find('.apos-tweet:not(.apos-template), [data-apos-tweet-place-holer]').remove(); _.each(tweets, function(tweet) { + console.log(tweet); var text = tweet.text; var $li = $tweets.find('.apos-tweet.apos-template, [data-apos-tweet].apos-template').clone(); + + var username = tweet.user.screen_name; + var $username = $li.find('[data-apos-tweet-username]'); + $username.text("@"+username); + + var profileUrl = "http://twitter.com/"+ username; + var $profileLink = $li.find('[data-apos-tweet-profile-link]'); + $profileLink.attr('href', profileUrl); + var when = parseTwitterDate(tweet.created_at); // Months start at zero for use in arrays. var month = when.getMonth() + 1; @@ -71,8 +81,9 @@ apos.widgetPlayers.twitter = function($widget) { }; // Linkify URLs - text = text.replace(/(https?\:\/\/[^ ]\S+)/g, '$1'); + text = text.replace(/(https?\:\/\/[^ ]\S+)/g, '$1'); // Tweets are pre-escaped for some reason in the JSON responses + text = text.replace(/(^|[^@\w])@(\w{1,15})\b/g, '$1@$2'); $li.find('.apos-tweet-text, [data-apos-tweet-text]').html(text); $li.removeClass('apos-template'); $tweets.append($li); diff --git a/public/js/editor.js b/public/js/editor.js index 71c6c21..5f1194a 100644 --- a/public/js/editor.js +++ b/public/js/editor.js @@ -6,9 +6,9 @@ function AposTwitterWidgetEditor(options) { if (!options.messages) { options.messages = {}; } - if (!options.messages.missing) { - options.messages.missing = 'Type in a Twitter account name first.'; - } + // if (!options.messages.missing) { + // options.messages.missing = 'Type in a Twitter account name first.'; + // } self.type = 'twitter'; options.template = '.apos-twitter-editor'; @@ -36,7 +36,7 @@ function AposTwitterWidgetEditor(options) { self.preSave = getAccount; function getAccount(callback) { - self.exists = !!self.$account.val(); + self.exists = (!!self.$account.val()) || (!!self.$hashtag.val()); if (self.exists) { self.data.account = self.$account.val(); self.data.hashtag = self.$hashtag.val();