Permalink
Browse files

creating and displaying activities corresponding to up and donw votin…

…g actions
  • Loading branch information...
isstaif committed Aug 10, 2012
1 parent 0371652 commit 4ee7b70b73b12786e70fa119043329a9c852c5dd
@@ -1,10 +1,12 @@
_ = require('underscore')
database = require('./db')
+activities_provider = require './activities-provider'
db = null
exports.setup = (database) ->
db = database
+ activities_provider.setup db
exports.vote = (vote_type, user_id, object_id, collection, callback)->
@@ -25,11 +27,24 @@ exports.vote = (vote_type, user_id, object_id, collection, callback)->
db.collection collection, (err, collection)->
if vote_type is 'up'
push = { up_votes : vote }
+ verb = 'upvote'
else
push = { down_votes : vote }
+ verb = 'downvote'
collection.update { _id : object_id }, { $push : push }, (err, vote)->
- callback(vote)
+
+
+ activity =
+ actor: user_id
+ verb: verb
+ object: object_id
+ object_type: 'Revision'
+ created_at : new Date()
+
+ activities_provider.createActivity activity, (err, new_activity)->
+ callback(vote)
+
else
callback
error : "Already voted"
@@ -57,14 +72,26 @@ exports.remove_vote = (vote_type, user_id, object_id, collection, callback)->
if vote_type is 'up'
pull = { up_votes : { user : user_id } }
+ verb = 'upvote'
else
pull = { down_votes : { user : user_id } }
+ verb = 'downvote'
updating = { $pull : pull }
db.collection collection, (err, collection)->
- collection.update { _id : object_id }, updating, (vote)->
- callback(vote)
+ collection.update { _id : object_id }, updating, (err)->
+ unless err
+ db.collection 'activities', (err, activities)->
+ match =
+ object : object_id
+ actor : user_id
+ verb : verb
+ activities.remove match, (err, object)->
+ callback(err)
+ else
+ callback err
+
exports.remove_up_vote = (user_id, object_id, collection, callback)->
exports.remove_vote('up', user_id, object_id, collection, callback)
@@ -60,10 +60,8 @@ define [
#injecting views
injectActivity: (activity)=>
- collection = new Activity.Collection
- collection.add activity
activityView = new Activity.View
- collection: collection
+ model: activity
@injectView activityView
appendActivity: (activity)=>
@@ -17,10 +17,14 @@ define [
initialize: ->
- if @collection.length is 1
+ if @collection? and (@collection.length is 1)
@singleMode = true
- @model = @collection.at(0)
+ unless @model?
+ @model = @collection.at(0)
+ else
+ console.debug @model.toJSON()
+ @singleMode = true
@objectClass = @model.get('object').constructor.name
@@ -32,7 +36,7 @@ define [
model: @model.get('object')
if @singleMode
- if @objectClass == 'Revision' && @model.get('verb') == 'edit'
+ if @objectClass == 'Revision' && ((@model.get('verb') == 'edit') or (@model.get('verb') == 'upvote') or (@model.get('verb') == 'downvote'))
@diffView = new DiffView
model : @model.get 'object'
else
@@ -42,9 +46,7 @@ define [
diffView = new DiffView
model : model.get 'object'
@diffViews.push diffView
-
-
-
+
_.bindAll @
render: ->
@@ -55,7 +57,7 @@ define [
#$(@el).find('.comments-thread').html @commentsThread.render().el
$(@el).find('.embeded-content').html @view.render().el
if @singleMode
- if @objectClass == 'Revision' && @model.get('verb') == 'edit'
+ if @objectClass == 'Revision' && ((@model.get('verb') == 'edit') or (@model.get('verb') == 'upvote') or (@model.get('verb') == 'downvote'))
$(@el).find('.attachements').append @diffView.render().el
$(@el).find('.diff-content').hide()
else
@@ -74,6 +76,8 @@ define [
edit: "#{name} edited a wikipage titled #{@model.get('object').get('page').get('title')}"
aggr_edit : "#{name} made several edits on the wikipage titled #{@model.get('object').get('page').get('title')}"
create: "#{name} created a wikipage titled #{@model.get('object').get('page').get('title')}"
+ upvote: "#{name} upvoted a revision"
+ downvote: "#{name} downvoted a revision"
Post:
comment: "#{name} commented a post"
create: "#{name} created a new post"
@@ -64,6 +64,16 @@ describe 'Votes Provider', ()->
assert.equal new_revision.up_votes[0].user.toString(), user._id.toString()
done()
+ it 'should create the corresponding activity object', (done)->
+ db.collection 'activities', (err, activities)->
+ match =
+ object : revision._id
+ actor : user._id
+ verb : 'upvote'
+ activities.find(match).count (err, count)->
+ assert count, 1
+ done()
+
it 'should not vote it, if the user has voted before', (done)->
votes_provider.up_vote user._id, revision._id, 'revisions', (votes_err)->
db.collection 'revisions', (err, revisions)->
@@ -85,6 +95,16 @@ describe 'Votes Provider', ()->
assert.equal new_revision.down_votes[0].user.toString(), user._id.toString()
done()
+ it 'should create the corresponding activity object', (done)->
+ db.collection 'activities', (err, activities)->
+ match =
+ object : revision._id
+ actor : user._id
+ verb : 'downvote'
+ activities.find(match).count (err, count)->
+ assert count, 1
+ done()
+
it 'should not vote it, if the user has voted before', (done)->
votes_provider.down_vote user._id, revision._id, 'revisions', (votes_err)->
db.collection 'revisions', (err, revisions)->
@@ -118,22 +138,44 @@ describe 'Votes Provider', ()->
describe 'Removing votes', ()->
describe 'remove up votes', ()->
- it 'should remove the up vote', (done)->
+ it 'should remove the up vote', (done)->
votes_provider.remove_vote 'up', user._id, revision._id, 'revisions', (votes_err)->
db.collection 'revisions', (err, revisions)->
revisions.findOne { _id : revision._id }, (err, new_revision)->
assert.ok new_revision.up_votes
assert.equal new_revision.up_votes.length, 0
done()
+ it 'should remove the corresponding activity object', (done)->
+ db.collection 'activities', (err, activities)->
+ match =
+ object : revision._id
+ actor : user._id
+ verb : 'upvote'
+ activities.find(match).count (err, count)->
+ assert.equal count, 0
+ done()
+
+
describe 'remove down votes', ()->
it 'should remove the down vote', (done)->
votes_provider.remove_vote 'down', user._id, revision._id, 'revisions', (votes_err)->
db.collection 'revisions', (err, revisions)->
revisions.findOne { _id : revision._id }, (err, new_revision)->
assert.ok new_revision.down_votes
assert.equal new_revision.down_votes.length, 0
- done()
+ done()
+
+ it 'should remove the corresponding activity object', (done)->
+ db.collection 'activities', (err, activities)->
+ match =
+ object : revision._id
+ actor : user._id
+ verb : 'downvote'
+ activities.find(match).count (err, count)->
+ assert.equal count, 0
+ done()
+
describe 'Reverse voting', ()->

0 comments on commit 4ee7b70

Please sign in to comment.