-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulling out twitter rule code into it's own thing.
- Loading branch information
Mathew Hartley
committed
Oct 18, 2013
1 parent
515bd94
commit c78c78e
Showing
16 changed files
with
282 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,5 +11,6 @@ accounts-password | |
accounts-ui | ||
twitter-bots | ||
scss | ||
twitter-api | ||
browser-policy | ||
twitter-api | ||
twitter-rules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.6.6 | ||
0.6.6.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
Package.describe({ | ||
summary: "Twitter official npm package" | ||
summary: "Twit npm package" | ||
}); | ||
// file: package.js | ||
Npm.depends({ | ||
'twitter': '0.1.18' | ||
'twit': '1.1.11' | ||
}); | ||
|
||
Package.on_use(function(api) { | ||
api.add_files('twitter-api.js', 'server'); | ||
api.add_files('twitter-api.js', 'server'); | ||
|
||
api.export('twitApi'); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
// file: twitter.js | ||
twitter = Npm.require("twitter"); | ||
twitterApi = { | ||
var twit = Npm.require("twit"); | ||
var options; | ||
twitApi = { | ||
get: function(options) { | ||
return new twitter(options); | ||
return new twit(options); | ||
}, | ||
config: function (_options) { | ||
options = _options; | ||
} | ||
}; | ||
|
||
if (Meteor.isServer) { | ||
Meteor.startup(function() { | ||
var Future = Npm.require('fibers/future'); | ||
Meteor.methods({ | ||
searchTwitterUser: function (username, currentBotId) { | ||
var fut = new Future(); | ||
var bot = TwitterBots.findOne({_id: currentBotId}); | ||
var config = { | ||
access_token: bot.accessToken, | ||
access_token_secret: bot.accessTokenSecret | ||
}; | ||
var twitterClient = twitApi.get(_.extend(config, options)); | ||
twitterClient.get('users/search', {q: username}, function (error, reply) { | ||
fut['return'](reply); | ||
}); | ||
return fut.wait(); | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.build* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Package.describe({ | ||
summary: "/* fill me in */" | ||
}); | ||
|
||
Package.on_use(function (api, where) { | ||
api.use(['underscore'], ['client', 'server']); | ||
api.use(['templating'], 'client'); | ||
api.add_files('twitter-rules.js', ['client', 'server']); | ||
|
||
api.add_files('twitter-rules.html', 'client'); | ||
|
||
api.export('TwitterRules'); | ||
|
||
}); | ||
|
||
Package.on_test(function (api) { | ||
api.use('twitter-rules'); | ||
|
||
api.add_files('twitter-rules_tests.js', ['client', 'server']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "twitter-rules", | ||
"description": "", | ||
"homepage": "", | ||
"author": "", | ||
"version": "0.0.1", | ||
"git": "", | ||
"packages": {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<template name="ruleRepeat"> | ||
<form class="rule"><input type="checkbox" id="rule-repeat" data-collapse {{checkedHelper 'active'}}/><label for="rule-repeat">Repeat another user's tweets</label> | ||
<div class="collapse"> | ||
<p>I want to copy @<input type="text" name="repeatSource" value="{{repeatSource}}" /></p> | ||
<p> and include <label for="repeatMentions"><input type="checkbox" name="repeatMentions" id="repeatMentions" {{checkedHelper 'repeatMentions'}} />mentions</label></p> | ||
<input type="submit" class="pure-button" value="Update" /> | ||
</div> | ||
</form> | ||
</template> | ||
|
||
<template name="twitterRules"> | ||
<div class="content"> | ||
<h1>Your rules for {{botName}}</h1> | ||
{{> ruleRepeat}} | ||
<form class="rule"> | ||
<input type="checkbox" id="rule-retweet" data-collapse /><label for="rule-retweet">Retweet another user's favourites</label> | ||
<div class="collapse">Collapse | ||
<input type="submit" class="pure-button" value="Update" /> | ||
</div> | ||
|
||
</form> | ||
<form class="rule"> | ||
<input type="checkbox" id="rule-respond" data-collapse /><label for="rule-respond">Automatically respond to interactions with bot</label> | ||
<div class="collapse">Collapse | ||
<input type="submit" class="pure-button" value="Update" /> | ||
</div> | ||
|
||
</form> | ||
<form class="rule"> | ||
<input type="checkbox" id="rule-keywords" data-collapse /><label for="rule-keywords">Automatically respond to keywords</label> | ||
<div class="collapse">Collapse | ||
<input type="submit" class="pure-button" value="Update" /> | ||
</div> | ||
|
||
</form> | ||
<form class="rule"> | ||
<input type="checkbox" id="rule-keywords-rt" data-collapse /><label for="rule-keywords-rt">Automatically retweet keywords</label> | ||
<div class="collapse">Collapse | ||
<input type="submit" class="pure-button" value="Update" /> | ||
</div> | ||
|
||
</form> | ||
<form class="rule"> | ||
<input type="checkbox" id="rule-tweet" data-collapse /><label for="rule-tweet">Tweet these things</label> | ||
<div class="collapse">Collapse | ||
<input type="submit" class="pure-button" value="Update" /> | ||
</div> | ||
|
||
</form> | ||
</div> | ||
</template> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
TwitterRule = function (doc) { | ||
_.extend(this, doc); | ||
}; | ||
|
||
TwitterRepeatRule = function (doc) { | ||
TwitterRule.call(this); | ||
}; | ||
|
||
// inherit TwitterRule | ||
TwitterRepeatRule.prototype = new TwitterRule(); | ||
|
||
// correct the constructor pointer because it points to Person | ||
TwitterRepeatRule.prototype.constructor = TwitterRepeatRule; | ||
TwitterRepeatRule.prototype.start = function () { | ||
console.log("starting repeat rule"); | ||
|
||
}; | ||
|
||
|
||
_.extend(TwitterRule.prototype, { | ||
start: function () { | ||
console.log("starting rule"); | ||
}, | ||
stop: function () { | ||
console.log("stopping rule"); | ||
} | ||
}); | ||
|
||
TwitterRules = new Meteor.Collection('twitter-rules', { | ||
transform: function (doc) { | ||
switch (doc.type) { | ||
case 'repeat': | ||
return new TwitterRepeatRule(doc); | ||
default: | ||
return new TwitterRule(doc); | ||
} | ||
} | ||
}); | ||
|
||
TwitterRules.allow({ | ||
insert: function (userId, doc) { | ||
if (TwitterRules.findOne({}, {type: doc.type, botId: doc.botId})) return false; | ||
return (userId && doc.ownerId === userId); | ||
}, | ||
update: function (userId, doc, fields, modifier) { | ||
// can only change your own documents | ||
return doc.ownerId === userId; | ||
}, | ||
remove: function (userId, doc) { | ||
// can only remove your own documents | ||
return doc.ownerId === userId; | ||
} | ||
}); | ||
|
||
if (Meteor.isClient) { | ||
Meteor.subscribe('twitter-rules'); | ||
|
||
var searchTwitterUser = _.debounce(function(value, callback){ | ||
var currentBotId = Session.get('currentBotId'); | ||
resultsArray = []; | ||
Meteor.call('searchTwitterUser', value, currentBotId, function (error, result){ | ||
resultsArray = result.map(function(item) { | ||
return item.screen_name; | ||
}); | ||
callback(resultsArray); | ||
}); | ||
}, 1000); | ||
Meteor.startup(function() { | ||
var setRuleToSession = function (type) { | ||
var rules = Session.get('rules') || {}; | ||
var selectedRule = TwitterRules.findOne({type: type, botId: Session.get('currentBotId')}); | ||
rules[type] = (selectedRule) ? selectedRule._id : undefined; | ||
Session.set('rules', rules); | ||
}; | ||
|
||
var getRuleFromSession = function(type) { | ||
var rules = Session.get('rules') || {}; | ||
return TwitterRules.findOne({_id: rules[type]}); | ||
}; | ||
|
||
Template.ruleRepeat.created = function () { | ||
setRuleToSession('repeat'); | ||
}; | ||
|
||
Template.twitterRules.botName = function() { | ||
var bot = TwitterBots.findOne({_id: Session.get('currentBotId')}); | ||
if (!bot) return ""; | ||
return "@" + bot.screenName; | ||
}; | ||
|
||
Template.ruleRepeat.repeatSource = function () { | ||
var rule = getRuleFromSession('repeat'); | ||
return rule ? rule.repeatSource : ''; | ||
}; | ||
|
||
Template.ruleRepeat.helpers({ | ||
checkedHelper: function (objectName) { | ||
var rule = getRuleFromSession('repeat'); | ||
return (rule && rule[objectName]) ? "checked='checked'" : undefined; | ||
} | ||
}); | ||
|
||
Template.ruleRepeat.events({ | ||
'click [data-collapse]': function (event) { | ||
var value = !!$(event.target).attr('checked'); | ||
var currentBotId = Session.get('currentBotId'); | ||
Meteor.call('updateRule', currentBotId, 'repeat', {active: value}); | ||
}, | ||
'submit': function (event) { | ||
event.preventDefault(); | ||
var currentBotId = Session.get('currentBotId'); | ||
var temp = $(event.target).serializeArray(); | ||
var data = {}; | ||
temp.map(function(x){data[x.name] = x.value;}); | ||
Meteor.call('updateRule', currentBotId, 'repeat', data); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
if (Meteor.isServer) { | ||
Meteor.publish("twitter-rules", function () { | ||
return TwitterRules.find({ownerId: this.userId}); | ||
}); | ||
Meteor.startup(function() { | ||
// var Future = Npm.require('fibers/future'); | ||
Meteor.methods({ | ||
updateRule: function (currentBotId, type, values) { | ||
console.log("running update for " + type + " with values: " + console.dir(values)); | ||
TwitterRules.upsert({ownerId: Meteor.userId(), botId: currentBotId, type: 'repeat'}, {$set: values}); | ||
}, | ||
clearRules: function() { | ||
TwitterRules.remove({}); | ||
} | ||
}); | ||
}); | ||
|
||
var repeatRules = TwitterRules.find({type: 'repeat'}); | ||
var handle = repeatRules.observeChanges({ | ||
added: function (id, fields) { | ||
var rule = TwitterRules.findOne({_id: id}); | ||
rule.start(); | ||
}, | ||
changed: function (id, fields) { | ||
if (_.has(fields, 'active')) { | ||
var rule = TwitterRules.findOne({_id: id}); | ||
(fields.active) ? rule.start() : rule.stop(); | ||
} | ||
} | ||
}); | ||
} |
Empty file.
Oops, something went wrong.