Last active
August 29, 2015 14:11
-
-
Save topicus/ce1d818e38c1ca380fbd to your computer and use it in GitHub Desktop.
Publishing a custom collection in meteorjs
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
///CLIENT/// | |
Accounts = new Mongo.Collection('accounts'); | |
Tracker.autorun(function () { | |
Meteor.subscribe('accounts-stats'); | |
}); | |
console.log(Accounts.find().fetch()); | |
///END CLIENT/// | |
///SERVER/// | |
Meteor.publish('taxonomies', function () { | |
return Taxonomies.find(); | |
}); | |
Meteor.publish('accounts-stats', function () { | |
var self = this; | |
var handleAccounts = Taxonomies.find({}).observeChanges({ | |
added: function (id) { | |
var account = getAccountStats(id); | |
self.added('accounts', id, account); | |
}, | |
removed: function (id) { | |
self.removed('accounts', id); | |
} | |
// In my case, update an account doesn't change anything. | |
}); | |
var handleTweets = Tweets.find({}).observeChanges({ | |
added: function(id){ | |
var mentions = _.pluck(Tweets.findOne(id).entities.user_mentions,'screen_name'); | |
var accounts = Taxonomies.find().fetch(); | |
var accountNames = _.pluck(accounts, 'account'); | |
var account = _.intersection(accountNames, mentions); | |
if(account.length){ | |
var changedAccount = _.findWhere(accounts, {account: account[0]}); | |
//Everytime I get a tweet, I need to update the stats. | |
self.changed('accounts', changedAccount._id, getAccountStats(changedAccount._id)); | |
} | |
} | |
}) | |
self.ready(); | |
self.onStop(function () { | |
handleAccounts.stop(); | |
handleTweets.stop(); | |
}); | |
}); | |
function getAccountStats(id){ | |
var account = getAccount(id); | |
var re = new RegExp(account.account.split(',').join('|'), 'i'); | |
var positive = Tweets.find({text:re, sentiment: 'positive'}).count(); | |
var negative = Tweets.find({text:re, sentiment: 'negative'}).count(); | |
var neutral = Tweets.find({text:re, sentiment: 'neutral'}).count(); | |
var accountWithStats = _.extend(account, {positive: positive, negative:negative, neutral: neutral}); | |
return accountWithStats; | |
} | |
function getAccountFromTweet(text){ | |
//var accounts = Taxonomies.find({}, ); | |
} | |
function getAccount(id){ | |
return Taxonomies.findOne(id); | |
} | |
///END SERVER/// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment