Skip to content
This repository has been archived by the owner on Aug 21, 2020. It is now read-only.

Commit

Permalink
Updated widget logic to accepts either username or hashtag
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumnersmith committed Sep 18, 2014
1 parent a936389 commit 5ac174c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
24 changes: 17 additions & 7 deletions index.js
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
Expand Down
13 changes: 12 additions & 1 deletion public/js/content.js
Expand Up @@ -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;
Expand Down Expand Up @@ -71,8 +81,9 @@ apos.widgetPlayers.twitter = function($widget) {
};

// Linkify URLs
text = text.replace(/(https?\:\/\/[^ ]\S+)/g, '<a href="$1">$1</a>');
text = text.replace(/(https?\:\/\/[^ ]\S+)/g, '<a href="$1" target="blank">$1</a>');
// Tweets are pre-escaped for some reason in the JSON responses
text = text.replace(/(^|[^@\w])@(\w{1,15})\b/g, '$1<a href="http://twitter.com/$2" target="blank">@$2</a>');
$li.find('.apos-tweet-text, [data-apos-tweet-text]').html(text);
$li.removeClass('apos-template');
$tweets.append($li);
Expand Down
8 changes: 4 additions & 4 deletions public/js/editor.js
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 5ac174c

Please sign in to comment.