Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed reactivity of "Tweet Stream" helper, and some minor cleanup #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/profile.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ <h3 class="username">{{user.profile.name}}</h3>
<h4 class="list-group-item-heading">My Latest Tweets</h4>
</li>

{{> tweets}}
{{> tweets tweets=profileTweets}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, so you are overridding the helper call to tweets and it runs profileTweets instead? Didn't even realize you could do that with spacebars/blaze

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually saw it in your "class 4" repo, so I stole the idea from there.


<li class="list-group-item load-more">
<a href="#">Load more tweets</a>
Expand Down
3 changes: 2 additions & 1 deletion client/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Template.profile.events({

Template.profile.helpers({
following: function() {
return _(Meteor.user().profile.followingIds).contains(this.user._id);
if (this.user)
return _(Meteor.user().profile.followingIds).contains(this.user._id);
}
});
37 changes: 18 additions & 19 deletions client/tweets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Template.tweets.rendered = function() {
Template.tweets.created = function() {
Session.set('lastSeenTweets', new Date());
};

Expand All @@ -10,25 +10,24 @@ Template.tweets.helpers({
}
});
},
profileTweets: function() {
return Tweets.find({
userId: this.user._id,
tweetedAt: {
$lt: Session.get('lastSeenTweets')
}
}, {
sort: {tweetedAt: -1}
});
},
tweets: function() {
if (this.user) {
return Tweets.find({
userId: this.user._id,
tweetedAt: {
$lt: Session.get('lastSeenTweets')
}
}, {
sort: {tweetedAt: -1}
});
} else {
return Tweets.find({
tweetedAt: {
$lt: Session.get('lastSeenTweets')
}
}, {
sort: {tweetedAt: -1}
});
}
return Tweets.find({
tweetedAt: {
$lt: Session.get('lastSeenTweets')
}
}, {
sort: {tweetedAt: -1}
});
}
});

Expand Down
4 changes: 2 additions & 2 deletions collections/tweets.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Tweets = new Mongo.Collection('tweets');

processTweet = function(text) {
var processTweet = function(text) {
if (Meteor.isServer) {
var ids = [];
if (_.contains(text, "@")) {
Expand All @@ -17,7 +17,7 @@ processTweet = function(text) {
}
};

linkTweet = function(text) {
var linkTweet = function(text) {
if (Meteor.isServer) {
if (_.contains(text, '@')) {
mentions = _.select(text.split(" "), function(string) { return _.contains(string, "@");});
Expand Down
17 changes: 15 additions & 2 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,27 @@ Router.map(function() {
path: '/',
waitOn: function() {
return Meteor.subscribe('tweets');
}
}/*,
data: function() {
return {
tweets: Tweets.find({tweetedAt: {$lt: Session.get('lastSeenTweets')}}, {sort: {tweetedAt: -1}}),
newTweets: Tweets.find({tweetedAt: {$gt: Session.get('lastSeenTweets')}}),
user: Meteor.user()
};
}*/
});
this.route('notifications', {
path: '/notifications',
name: 'notification',
waitOn: function() {
return Meteor.subscribe('notifications');
}
}/*,
data: function() {
return {
tweets: Tweets.find({tweetedAt: {$lt: Session.get('lastSeenTweets')}}, {sort: {tweetedAt: -1}}),
newTweets: Tweets.find({tweetedAt: {$gt: Session.get('lastSeenTweets')}})
};
}*/
});
this.route('profile', {
path: '/:username',
Expand Down
48 changes: 34 additions & 14 deletions server/publications.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ Meteor.publish('tweets', function() {
var cursors = [];
var ids = [];
var self = this;
ids.push(user.profile.followingIds);
ids.push(this.userId);
followingIds = _.flatten(ids);
if (this.userId) {
ids.push(user.profile.followingIds);
ids.push(this.userId);
}
var followingIds = _.flatten(ids);

cursors.push(Tweets.find({userId: {$in: followingIds}}));
cursors.push(Users.find({
_id: {$in: followingIds}
Expand All @@ -15,29 +18,46 @@ Meteor.publish('tweets', function() {
}));

userCursor.observeChanges({
changed: function(id, user) {
ids = user.profile.followingIds;
ids.push(self.userId);
console.log(followingIds);
flatIds = _.flatten(ids);
changed: function(id, fields) {
var flatIds = fields.profile.followingIds;
flatIds.push(self.userId);

addedFollowingIds = _.difference(flatIds, followingIds);
removedFollowingIds = _.difference(followingIds, flatIds);
followingIds = user.profile.followingIds;
// console.log(removedFollowingIds);

// Update the followingIds for the next run through this
followingIds = fields.profile.followingIds;
followingIds.push(self.userId);

if (addedFollowingIds) {
users = Users.find({_id: {$in: addedFollowingIds}}, {fields: {username: 1, "profile.name": 1}});
users = Users.find({
_id: {$in: addedFollowingIds}
}, {
fields: {username: 1, "profile.name": 1}
});

console.log(users.count());

_.each(users.fetch(), function(user) {
console.log(user);
self.added('users', user._id, user);
console.log('added - user', user);

tweets = Tweets.find({userId: user._id});
console.log('Tweets found: ', tweets.count());

tweets.forEach(function(tweet) {
console.log(tweet);
self.added('tweets', tweet._id, tweet);
console.log('added - tweet', tweet);
});
});
}
if (removedFollowingIds) {
users = Users.find({_id: {$in: addedFollowingIds}}, {fields: {username: 1, "profile.name": 1}});
users = Users.find({
_id: {$in: removedFollowingIds}
}, {
fields: {username: 1, "profile.name": 1}
});

_.each(users.fetch(), function(user) {
self.removed('users', user._id);
tweets = Tweets.find({userId: user._id});
Expand Down