Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cs_comments_service
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
cs_comments_service
Commits
81b54db0
Commit
81b54db0
authored
Jul 16, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
major change in urls and naming
parent
bc407596
Expand all
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
72 additions
and
153 deletions
+72
-153
Rakefile
+2
-2
app.rb
+44
-125
models/comment.rb
+9
-9
models/comment_thread.rb
+9
-9
models/notification.rb
+6
-6
models/user.rb
+2
-2
spec/app_spec.rb
+0
-0
No files found.
Rakefile
View file @
81b54db0
...
...
@@ -27,7 +27,7 @@ namespace :test do
CommentThread
.
delete_all
Commentable
.
delete_all
User
.
delete_all
Feed
.
delete_all
Notification
.
delete_all
commentable
=
Commentable
.
create!
(
commentable_type:
"questions"
,
commentable_id:
"1"
)
...
...
@@ -67,7 +67,7 @@ namespace :db do
CommentThread
.
create_indexes
User
.
create_indexes
Commentable
.
create_indexes
Feed
.
create_indexes
Notification
.
create_indexes
Delayed
::
Backend
::
Mongoid
::
Job
.
create_indexes
puts
"finished"
end
...
...
app.rb
View file @
81b54db0
...
...
@@ -19,26 +19,17 @@ Mongoid.logger.level = Logger::INFO
Dir
[
File
.
dirname
(
__FILE__
)
+
'/models/*.rb'
].
each
{
|
file
|
require
file
}
# DELETE /api/v1/commentables/:commentable_type/:commentable_id
# delete the commentable object and all of its associated comment threads and comments
delete
'/api/v1/:commentable_type/:commentable_id/comments'
do
|
commentable_type
,
commentable_id
|
delete
'/api/v1/:commentable_type/:commentable_id/threads'
do
|
commentable_type
,
commentable_id
|
commentable
=
Commentable
.
find_or_initialize_by
(
commentable_type:
commentable_type
,
commentable_id:
commentable_id
)
commentable
.
destroy
commentable
.
to_hash
.
to_json
end
# GET /api/v1/commentables/:commentable_type/:commentable_id/threads
# get all comment threads associated with a commentable object
# additional parameters accepted: recursive
get
'/api/v1/:commentable_type/:commentable_id/threads'
do
|
commentable_type
,
commentable_id
|
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
commentable_type
,
commentable_id:
commentable_id
)
commentable
.
comment_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
params
[
"recursive"
])}.
to_json
end
# POST /api/v1/commentables/:commentable_type/:commentable_id/threads
# create a new comment thread for the commentable object
post
'/api/v1/:commentable_type/:commentable_id/threads'
do
|
commentable_type
,
commentable_id
|
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
commentable_type
,
commentable_id:
commentable_id
)
thread
=
commentable
.
comment_threads
.
new
(
params
.
slice
(
*
%w[title body course_id]
))
...
...
@@ -47,26 +38,17 @@ post '/api/v1/:commentable_type/:commentable_id/threads' do |commentable_type, c
thread
.
to_hash
.
to_json
end
# GET /api/v1/threads/:thread_id
# get information of a single comment thread
# additional parameters accepted: recursive
get
'/api/v1/threads/:thread_id'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
thread
.
to_hash
(
recursive:
params
[
"recursive"
]).
to_json
end
# PUT /api/v1/threads/:thread_id
# update information of comment thread
put
'/api/v1/threads/:thread_id'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
thread
.
update_attributes!
(
params
.
slice
(
*
%w[title body]
))
thread
.
to_hash
.
to_json
end
# POST /api/v1/threads/:thread_id/comments
# create a comment to the comment thread
post
'/api/v1/threads/:thread_id/comments'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
comment
=
thread
.
comments
.
new
(
params
.
slice
(
*
%w[body course_id]
))
...
...
@@ -75,36 +57,23 @@ post '/api/v1/threads/:thread_id/comments' do |thread_id|
comment
.
to_hash
.
to_json
end
# DELETE /api/v1/threads/:thread_id
# delete the comment thread and its comments
delete
'/api/v1/threads/:thread_id'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
thread
.
destroy
thread
.
to_hash
.
to_json
end
# GET /api/v1/comments/:comment_id
# retrieve information of a single comment
# additional parameters accepted: recursive
get
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
comment
.
to_hash
(
recursive:
params
[
"recursive"
]).
to_json
end
# PUT /api/v1/comments/:comment_id
# update information of the comment
put
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
comment
.
update_attributes!
(
params
.
slice
(
*
%w[body endorsed]
))
comment
.
to_hash
.
to_json
end
# POST /api/v1/comments/:comment_id
# create a sub comment to the comment
post
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
sub_comment
=
comment
.
children
.
new
(
params
.
slice
(
*
%w[body course_id]
))
...
...
@@ -113,123 +82,61 @@ post '/api/v1/comments/:comment_id' do |comment_id|
sub_comment
.
to_hash
.
to_json
end
# DELETE /api/v1/comments/:comment_id
# delete the comment and its sub comments
delete
'/api/v1/comments/:comment_id'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
comment
.
destroy
comment
.
to_hash
.
to_json
end
# PUT /api/v1/votes/comments/:comment_id/users/:user_id
# create or update the vote on the comment
put
'/api/v1/votes/comments/:comment_id/users/:user_id'
do
|
comment_id
,
user_id
|
put
'/api/v1/comments/:comment_id/votes'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
.
vote
(
comment
,
params
[
"value"
].
intern
)
Comment
.
find
(
comment_id
).
to_hash
.
to_json
handle_vote_for
comment
end
# DELETE /api/v1/votes/comments/:comment_id/users/:user_id
# unvote on the comment
delete
'/api/v1/votes/comments/:comment_id/users/:user_id'
do
|
comment_id
,
user_id
|
delete
'/api/v1/comments/:comment_id/votes'
do
|
comment_id
|
comment
=
Comment
.
find
(
comment_id
)
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
.
unvote
(
comment
)
Comment
.
find
(
comment_id
).
to_hash
.
to_json
handle_unvote_for
comment
end
# PUT /api/v1/votes/threads/:thread_id/users/:user_id
# create or update the vote on the comment thread
put
'/api/v1/votes/threads/:thread_id/users/:user_id'
do
|
thread_id
,
user_id
|
put
'/api/v1/threads/:thread_id/votes'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
.
vote
(
thread
,
params
[
"value"
].
intern
)
CommentThread
.
find
(
thread_id
).
to_hash
.
to_json
handle_vote_for
thread
end
# DELETE /api/v1/votes/threads/:thread_id/users/:user_id
# unvote on the comment thread
delete
'/api/v1/votes/threads/:thread_id/users/:user_id'
do
|
thread_id
,
user_id
|
delete
'/api/v1/threads/:thread_id/votes'
do
|
thread_id
|
thread
=
CommentThread
.
find
(
thread_id
)
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
.
unvote
(
thread
)
CommentThread
.
find
(
thread_id
).
to_hash
.
to_json
end
# GET /api/v1/users/:user_id/feeds
# get all subscribed feeds for the user
get
'/api/v1/users/:user_id/feeds'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
user
.
subscribed_feeds
.
map
(
&
:to_hash
).
to_json
handle_unvote_for
thread
end
# POST /api/v1/users/:user_id/follow
# follow user
post
'/api/v1/users/:user_id/follow'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
followed_user
=
User
.
find_or_create_by
(
external_id:
params
[
"follow_user_id"
])
user
.
follow
(
followed_user
)
user
.
to_hash
.
to_json
end
# POST /api/v1/users/:user_id/unfollow
# unfollow user
post
'/api/v1/users/:user_id/unfollow'
do
|
user_id
|
get
'/api/v1/users/:user_id/notifications'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
followed_user
=
User
.
find_or_create_by
(
external_id:
params
[
"follow_user_id"
])
user
.
unfollow
(
followed_user
)
user
.
to_hash
.
to_json
user
.
notifications
.
map
(
&
:to_hash
).
to_json
end
# POST /api/v1/users/:user_id/watch/commentable
# watch a commentable
post
'/api/v1/users/:user_id/watch/commentable'
do
|
user_id
|
post
'/api/v1/users/:user_id/subscriptions'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
params
[
:commentable_type
],
commentable_id:
params
[
:commentable_id
])
user
.
watch_commentable
(
commentable
)
user
.
to_hash
.
to_json
end
# POST /api/v1/users/:user_id/unwatch/commentable
# unwatch a commentable
post
'/api/v1/users/:user_id/unwatch/commentable'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
commentable
=
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"commentable_type"
],
commentable_id:
params
[
"commentable_id"
])
user
.
unwatch_commentable
(
commentable
)
user
.
to_hash
.
to_json
end
# POST /api/v1/users/:user_id/watch/thread
# watch a comment thread
post
'/api/v1/users/:user_id/watch/thread'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
thread
=
CommentThread
.
find
(
params
[
"thread_id"
])
user
.
watch_comment_thread
(
thread
)
user
.
to_hash
.
to_json
case
params
[
"subscribed_type"
]
when
"user"
user
.
follow
(
User
.
find_or_create_by
(
external_id:
params
[
"subscribed_id"
]))
when
"thread"
user
.
watch_comment_thread
(
CommentThread
.
find
(
params
[
"subscribed_id"
]))
else
user
.
watch_commentable
(
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"subscribed_type"
],
commentable_id:
params
[
"subscribed_id"
]))
end
user
.
reload
.
to_hash
.
to_json
end
# POST /api/v1/users/:user_id/unwatch/thread
# unwatch a comment thread
post
'/api/v1/users/:user_id/unwatch/thread'
do
|
user_id
|
delete
'/api/v1/users/:user_id/subscriptions'
do
|
user_id
|
user
=
User
.
find_or_create_by
(
external_id:
user_id
)
thread
=
CommentThread
.
find
(
params
[
"thread_id"
])
user
.
unwatch_comment_thread
(
thread
)
user
.
to_hash
.
to_json
case
params
[
"subscribed_type"
]
when
"user"
user
.
unfollow
(
User
.
find_or_create_by
(
external_id:
params
[
"subscribed_id"
]))
when
"thread"
user
.
unwatch_comment_thread
(
CommentThread
.
find
(
params
[
"subscribed_id"
]))
else
user
.
unwatch_commentable
(
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"subscribed_type"
],
commentable_id:
params
[
"subscribed_id"
]))
end
user
.
reload
.
to_hash
.
to_json
end
if
env
.
to_s
==
"development"
...
...
@@ -238,7 +145,19 @@ if env.to_s == "development"
CommentThread
.
delete_all
Commentable
.
delete_all
User
.
delete_all
Feed
.
delete_all
Notification
.
delete_all
{}.
to_json
end
end
def
handle_vote_for
(
obj
)
user
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
user
.
vote
(
obj
,
params
[
"value"
].
to_sym
)
obj
.
reload
.
to_hash
.
to_json
end
def
handle_unvote_for
(
obj
)
user
=
User
.
find_or_create_by
(
external_id:
params
[
"user_id"
])
user
.
unvote
(
obj
)
obj
.
reload
.
to_hash
.
to_json
end
models/comment.rb
View file @
81b54db0
...
...
@@ -53,21 +53,21 @@ class Comment
end
private
def
generate_
feed
s
def
generate_
notification
s
if
get_comment_thread
.
watchers
or
(
author
.
followers
if
author
)
feed
=
Feed
.
new
(
feed
_type:
"post_reply"
,
notification
=
Notification
.
new
(
notification
_type:
"post_reply"
,
info:
{
thread_id:
get_comment_thread
.
id
,
thread_title:
get_comment_thread
.
title
,
comment_id:
id
,
},
)
feed
.
actor
=
author
feed
.
target
=
self
feed
.
subscrib
ers
<<
(
get_comment_thread
.
watchers
+
author
.
followers
).
uniq_by
(
&
:id
)
feed
.
subscrib
ers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
feed
.
save!
notification
.
actor
=
author
notification
.
target
=
self
notification
.
receiv
ers
<<
(
get_comment_thread
.
watchers
+
author
.
followers
).
uniq_by
(
&
:id
)
notification
.
receiv
ers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
notification
.
save!
end
end
...
...
@@ -78,7 +78,7 @@ private
end
def
handle_after_create
generate_
feed
s
generate_
notification
s
auto_watch_comment_thread
end
...
...
models/comment_thread.rb
View file @
81b54db0
...
...
@@ -34,10 +34,10 @@ class CommentThread
end
private
def
generate_
feed
s
def
generate_
notification
s
if
watchers
or
(
author
.
followers
if
author
)
feed
=
Feed
.
new
(
feed
_type:
"post_topic"
,
notification
=
Notification
.
new
(
notification
_type:
"post_topic"
,
info:
{
commentable_id:
commentable
.
commentable_id
,
commentable_type:
commentable
.
commentable_type
,
...
...
@@ -45,11 +45,11 @@ private
thread_title:
title
,
},
)
feed
.
actor
=
author
feed
.
target
=
self
feed
.
subscrib
ers
<<
(
commentable
.
watchers
+
(
author
.
followers
if
author
).
to_a
).
uniq_by
(
&
:id
)
feed
.
subscrib
ers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
and
author
feed
.
save!
notification
.
actor
=
author
notification
.
target
=
self
notification
.
receiv
ers
<<
(
commentable
.
watchers
+
(
author
.
followers
if
author
).
to_a
).
uniq_by
(
&
:id
)
notification
.
receiv
ers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
and
author
notification
.
save!
end
end
...
...
@@ -60,7 +60,7 @@ private
end
def
handle_after_create
generate_
feed
s
generate_
notification
s
auto_watch_comment_thread
end
...
...
models/
feed
.rb
→
models/
notification
.rb
View file @
81b54db0
class
Feed
class
Notification
include
Mongoid
::
Document
include
Mongoid
::
Timestamps
field
:
feed
_type
,
type:
String
field
:
notification
_type
,
type:
String
field
:info
,
type:
Hash
belongs_to
:actor
,
class_name:
"User"
,
inverse_of: :activities
,
index:
true
,
autosave:
true
belongs_to
:target
,
inverse_of: :activities
,
polymorphic:
true
,
autosave:
true
attr_accessible
:
feed
_type
,
:info
attr_accessible
:
notification
_type
,
:info
validates_presence_of
:
feed
_type
validates_presence_of
:
notification
_type
if
not
CommentService
.
config
[
"allow_anonymity"
]
validates_presence_of
:actor
end
validates_presence_of
:target
has_and_belongs_to_many
:
subscribers
,
class_name:
"User"
,
inverse_of: :subscribed_feed
s
,
autosave:
true
has_and_belongs_to_many
:
receivers
,
class_name:
"User"
,
inverse_of: :notification
s
,
autosave:
true
def
to_hash
(
params
=
{})
as_document
.
slice
(
*
%w[_id
feed
_type info actor target]
)
as_document
.
slice
(
*
%w[_id
notification
_type info actor target]
)
end
end
models/user.rb
View file @
81b54db0
...
...
@@ -6,8 +6,8 @@ class User
has_many
:comments
has_many
:comment_threads
,
inverse_of: :author
has_many
:activities
,
class_name:
"
Feed
"
,
inverse_of: :actor
has_and_belongs_to_many
:
subscribed_feeds
,
class_name:
"Feed"
,
inverse_of: :subscrib
ers
has_many
:activities
,
class_name:
"
Notification
"
,
inverse_of: :actor
has_and_belongs_to_many
:
notifications
,
inverse_of: :receiv
ers
has_and_belongs_to_many
:followers
,
class_name:
"User"
,
inverse_of: :followings
,
autosave:
true
has_and_belongs_to_many
:followings
,
class_name:
"User"
,
inverse_of: :followers
#, autosave: true
...
...
spec/app_spec.rb
View file @
81b54db0
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment