Created
April 3, 2016 22:46
-
-
Save boxofrox/9cf351660c29b8e94265e172d425ec6c to your computer and use it in GitHub Desktop.
Alternative solution to issue-83. Tested simply to verify code works. Not tested or benchmarked under load.
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
commit 360527e99cbae6fc702615e9f03485210bccbc89 | |
Author: Justin Charette <[email protected]> | |
Date: Sun Apr 3 18:23:45 2016 -0400 | |
track review counts per profile and show in developer profile boxes | |
Signed-off-by: Justin Charette <[email protected]> | |
diff --git a/both/collections/reviewCounts.js b/both/collections/reviewCounts.js | |
new file mode 100644 | |
index 0000000..0ccb1bb | |
--- /dev/null | |
+++ b/both/collections/reviewCounts.js | |
@@ -0,0 +1,43 @@ | |
+if (Meteor.isClient) { | |
+ ReviewCounts = new Mongo.Collection('review.counts'); | |
+} | |
+else if (Meteor.isServer) { | |
+ // create a memory-backed collection for review counts. | |
+ ReviewCounts = new Mongo.Collection(null); | |
+ | |
+ var comments = Comments.getCollection(); | |
+ | |
+ // populate the review counts collection after startup, otherwise Meteor's | |
+ // goofy loading order may run this before Profiles is defined. | |
+ Meteor.startup(function () { | |
+ // create a ReviewCount doc for each Profile over time. this does not | |
+ // react to changes in comments. | |
+ // server startup may slow from initial processing of all comments (see observe.added docs) | |
+ Profiles.find({}, { fields: { _id: true } }).observe({ | |
+ added: function (doc) { | |
+ console.log('review count: added profile:', doc._id); | |
+ ReviewCounts.insert({ profileId: doc._id, count: 0 }); | |
+ }, | |
+ removed: function (doc) { | |
+ console.log('review count: removed profile:', doc._id); | |
+ ReviewCounts.remove({ profileId: doc._id }); | |
+ }, | |
+ }); | |
+ | |
+ // update ReviewCounts "reactively" on server for Comments over time. | |
+ // server startup may slow from initial processing of all comments (see observe.added docs) | |
+ comments.find({}, { fields: { referenceId: true } }).observe({ | |
+ added: function (doc) { | |
+ console.log('review count: +1 comment for', doc.referenceId); | |
+ ReviewCounts.update({ profileId: doc.referenceId }, | |
+ { $inc: { count: 1 } }); | |
+ }, | |
+ // when comments change, count remains same. | |
+ removed: function (doc) { | |
+ console.log('review count: -1 comment for', doc.referenceId); | |
+ ReviewCounts.update({ profileId: doc.referenceId }, | |
+ { $inc: { count: -1 } }); | |
+ }, | |
+ }); | |
+ }); | |
+} | |
diff --git a/client/subscriptions.js b/client/subscriptions.js | |
index dd8905e..345061b 100644 | |
--- a/client/subscriptions.js | |
+++ b/client/subscriptions.js | |
@@ -1,4 +1,4 @@ | |
Meteor.subscribe("userData"); | |
Meteor.subscribe("jobCount"); | |
Meteor.subscribe("developerCount"); | |
-Meteor.subscribe('postCommentCount', "referenceId"); | |
+Meteor.subscribe('review.counts'); | |
diff --git a/client/views/profiles/profileSmall.html b/client/views/profiles/profileSmall.html | |
index aa56f3b..bd8006e 100644 | |
--- a/client/views/profiles/profileSmall.html | |
+++ b/client/views/profiles/profileSmall.html | |
@@ -21,7 +21,7 @@ | |
{{#if $eq type 'Company'}} | |
<span class="label label-danger"><i class="fa fa-users"></i> Company</span> | |
{{/if}} | |
- <span class="label label-primary"><i class="fa fa-rocket"></i> {{getPublishedCount 'comments'}} Reviews</span> | |
+ <span class="label label-primary"><i class="fa fa-rocket"></i> {{reviewCount _id}} Reviews</span> | |
</div> | |
<div class="title"> | |
<small>{{title}}</small> | |
diff --git a/client/views/profiles/profileSmall.js b/client/views/profiles/profileSmall.js | |
new file mode 100644 | |
index 0000000..086e4c3 | |
--- /dev/null | |
+++ b/client/views/profiles/profileSmall.js | |
@@ -0,0 +1,5 @@ | |
+Template.profileSmall.helpers({ | |
+ reviewCount: function (profileId) { | |
+ return ReviewCounts.findOne({ profileId: profileId }).count; | |
+ }, | |
+}); | |
diff --git a/server/publications.js b/server/publications.js | |
index 1a823b8..60981d8 100644 | |
--- a/server/publications.js | |
+++ b/server/publications.js | |
@@ -28,13 +28,6 @@ Meteor.publish('developerCount', function() { | |
})); | |
}); | |
-Meteor.publish('postCommentCount', function(referenceId) { | |
- check(referenceId, String); | |
- Counts.publish(this, 'comments', Comments.getCollection().find({ | |
- referenceId: referenceId | |
- })); | |
-}); | |
- | |
Meteor.publish("homeJobs", function() { | |
check(arguments, [Match.Any]); | |
return [ | |
@@ -239,3 +232,25 @@ Meteor.publish('profiles', function(limit) { | |
} | |
}); | |
}); | |
+ | |
+Meteor.publish('review.counts', function () { | |
+ // this publication is one-way only (server -> client). | |
+ var pub = this; | |
+ | |
+ ReviewCounts.find({}).observe({ | |
+ added: function (doc) { | |
+ console.log('review count: add count', doc); | |
+ pub.added('review.counts', doc._id, doc); | |
+ }, | |
+ changed: function (newDoc, oldDoc) { | |
+ console.log('review count: update count', newDoc); | |
+ pub.changed('review.counts', newDoc._id, { count: newDoc.count }); | |
+ }, | |
+ removed: function (doc) { | |
+ console.log('review count: remove count', doc); | |
+ pub.removed('review.counts', doc._id); | |
+ }, | |
+ }); | |
+ | |
+ this.ready(); | |
+}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment