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
5f4bd140
Commit
5f4bd140
authored
Jul 16, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adjust naming to notification / subscription
parent
81b54db0
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
39 additions
and
41 deletions
+39
-41
app.rb
+4
-4
config/application.yml
+1
-1
models/comment.rb
+6
-6
models/comment_thread.rb
+7
-7
models/commentable.rb
+1
-1
models/user.rb
+11
-13
spec/app_spec.rb
+9
-9
No files found.
app.rb
View file @
5f4bd140
...
@@ -119,9 +119,9 @@ post '/api/v1/users/:user_id/subscriptions' do |user_id|
...
@@ -119,9 +119,9 @@ post '/api/v1/users/:user_id/subscriptions' do |user_id|
when
"user"
when
"user"
user
.
follow
(
User
.
find_or_create_by
(
external_id:
params
[
"subscribed_id"
]))
user
.
follow
(
User
.
find_or_create_by
(
external_id:
params
[
"subscribed_id"
]))
when
"thread"
when
"thread"
user
.
watch
_comment_thread
(
CommentThread
.
find
(
params
[
"subscribed_id"
]))
user
.
subscribe
_comment_thread
(
CommentThread
.
find
(
params
[
"subscribed_id"
]))
else
else
user
.
watch
_commentable
(
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"subscribed_type"
],
commentable_id:
params
[
"subscribed_id"
]))
user
.
subscribe
_commentable
(
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"subscribed_type"
],
commentable_id:
params
[
"subscribed_id"
]))
end
end
user
.
reload
.
to_hash
.
to_json
user
.
reload
.
to_hash
.
to_json
end
end
...
@@ -132,9 +132,9 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id|
...
@@ -132,9 +132,9 @@ delete '/api/v1/users/:user_id/subscriptions' do |user_id|
when
"user"
when
"user"
user
.
unfollow
(
User
.
find_or_create_by
(
external_id:
params
[
"subscribed_id"
]))
user
.
unfollow
(
User
.
find_or_create_by
(
external_id:
params
[
"subscribed_id"
]))
when
"thread"
when
"thread"
user
.
un
watch
_comment_thread
(
CommentThread
.
find
(
params
[
"subscribed_id"
]))
user
.
un
subscribe
_comment_thread
(
CommentThread
.
find
(
params
[
"subscribed_id"
]))
else
else
user
.
un
watch
_commentable
(
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"subscribed_type"
],
commentable_id:
params
[
"subscribed_id"
]))
user
.
un
subscribe
_commentable
(
Commentable
.
find_or_create_by
(
commentable_type:
params
[
"subscribed_type"
],
commentable_id:
params
[
"subscribed_id"
]))
end
end
user
.
reload
.
to_hash
.
to_json
user
.
reload
.
to_hash
.
to_json
end
end
...
...
config/application.yml
View file @
5f4bd140
level_limit
:
3
level_limit
:
3
send_notifications_to_author
:
false
send_notifications_to_author
:
false
auto_
watch
_comment_threads
:
true
auto_
subscribe
_comment_threads
:
true
allow_anonymity
:
false
allow_anonymity
:
false
models/comment.rb
View file @
5f4bd140
...
@@ -54,7 +54,7 @@ class Comment
...
@@ -54,7 +54,7 @@ class Comment
private
private
def
generate_notifications
def
generate_notifications
if
get_comment_thread
.
watch
ers
or
(
author
.
followers
if
author
)
if
get_comment_thread
.
subscrib
ers
or
(
author
.
followers
if
author
)
notification
=
Notification
.
new
(
notification
=
Notification
.
new
(
notification_type:
"post_reply"
,
notification_type:
"post_reply"
,
info:
{
info:
{
...
@@ -65,21 +65,21 @@ private
...
@@ -65,21 +65,21 @@ private
)
)
notification
.
actor
=
author
notification
.
actor
=
author
notification
.
target
=
self
notification
.
target
=
self
notification
.
receivers
<<
(
get_comment_thread
.
watch
ers
+
author
.
followers
).
uniq_by
(
&
:id
)
notification
.
receivers
<<
(
get_comment_thread
.
subscrib
ers
+
author
.
followers
).
uniq_by
(
&
:id
)
notification
.
receivers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
notification
.
receivers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
notification
.
save!
notification
.
save!
end
end
end
end
def
auto_
watch
_comment_thread
def
auto_
subscribe
_comment_thread
if
CommentService
.
config
[
"auto_
watch
_comment_threads"
]
and
author
if
CommentService
.
config
[
"auto_
subscribe
_comment_threads"
]
and
author
author
.
watch
_comment_thread
(
get_comment_thread
)
author
.
subscribe
_comment_thread
(
get_comment_thread
)
end
end
end
end
def
handle_after_create
def
handle_after_create
generate_notifications
generate_notifications
auto_
watch
_comment_thread
auto_
subscribe
_comment_thread
end
end
handle_asynchronously
:handle_after_create
handle_asynchronously
:handle_after_create
...
...
models/comment_thread.rb
View file @
5f4bd140
...
@@ -12,7 +12,7 @@ class CommentThread
...
@@ -12,7 +12,7 @@ class CommentThread
belongs_to
:author
,
class_name:
"User"
,
inverse_of: :comment_threads
,
index:
true
,
autosave:
true
belongs_to
:author
,
class_name:
"User"
,
inverse_of: :comment_threads
,
index:
true
,
autosave:
true
belongs_to
:commentable
,
index:
true
,
autosave:
true
belongs_to
:commentable
,
index:
true
,
autosave:
true
has_many
:comments
,
dependent: :destroy
,
autosave:
true
# Use destroy to envoke callback on the top-level comments TODO async
has_many
:comments
,
dependent: :destroy
,
autosave:
true
# Use destroy to envoke callback on the top-level comments TODO async
has_and_belongs_to_many
:
watchers
,
class_name:
"User"
,
inverse_of: :watch
ed_comment_threads
,
autosave:
true
has_and_belongs_to_many
:
subscribers
,
class_name:
"User"
,
inverse_of: :subscrib
ed_comment_threads
,
autosave:
true
attr_accessible
:title
,
:body
,
:course_id
attr_accessible
:title
,
:body
,
:course_id
...
@@ -35,7 +35,7 @@ class CommentThread
...
@@ -35,7 +35,7 @@ class CommentThread
private
private
def
generate_notifications
def
generate_notifications
if
watch
ers
or
(
author
.
followers
if
author
)
if
subscrib
ers
or
(
author
.
followers
if
author
)
notification
=
Notification
.
new
(
notification
=
Notification
.
new
(
notification_type:
"post_topic"
,
notification_type:
"post_topic"
,
info:
{
info:
{
...
@@ -47,21 +47,21 @@ private
...
@@ -47,21 +47,21 @@ private
)
)
notification
.
actor
=
author
notification
.
actor
=
author
notification
.
target
=
self
notification
.
target
=
self
notification
.
receivers
<<
(
commentable
.
watch
ers
+
(
author
.
followers
if
author
).
to_a
).
uniq_by
(
&
:id
)
notification
.
receivers
<<
(
commentable
.
subscrib
ers
+
(
author
.
followers
if
author
).
to_a
).
uniq_by
(
&
:id
)
notification
.
receivers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
and
author
notification
.
receivers
.
delete
(
author
)
if
not
CommentService
.
config
[
"send_notifications_to_author"
]
and
author
notification
.
save!
notification
.
save!
end
end
end
end
def
auto_
watch
_comment_thread
def
auto_
subscribe
_comment_thread
if
CommentService
.
config
[
"auto_
watch
_comment_threads"
]
and
author
if
CommentService
.
config
[
"auto_
subscribe
_comment_threads"
]
and
author
author
.
watch
_comment_thread
(
self
)
author
.
subscribe
_comment_thread
(
self
)
end
end
end
end
def
handle_after_create
def
handle_after_create
generate_notifications
generate_notifications
auto_
watch
_comment_thread
auto_
subscribe
_comment_thread
end
end
handle_asynchronously
:handle_after_create
handle_asynchronously
:handle_after_create
...
...
models/commentable.rb
View file @
5f4bd140
...
@@ -5,7 +5,7 @@ class Commentable
...
@@ -5,7 +5,7 @@ class Commentable
field
:commentable_id
,
type:
String
field
:commentable_id
,
type:
String
has_many
:comment_threads
,
dependent: :destroy
has_many
:comment_threads
,
dependent: :destroy
has_and_belongs_to_many
:
watchers
,
class_name:
"User"
,
inverse_of: :watch
ed_commentables
,
autosave:
true
has_and_belongs_to_many
:
subscribers
,
class_name:
"User"
,
inverse_of: :subscrib
ed_commentables
,
autosave:
true
attr_accessible
:commentable_type
,
:commentable_id
attr_accessible
:commentable_type
,
:commentable_id
...
...
models/user.rb
View file @
5f4bd140
...
@@ -9,7 +9,7 @@ class User
...
@@ -9,7 +9,7 @@ class User
has_many
:activities
,
class_name:
"Notification"
,
inverse_of: :actor
has_many
:activities
,
class_name:
"Notification"
,
inverse_of: :actor
has_and_belongs_to_many
:notifications
,
inverse_of: :receivers
has_and_belongs_to_many
:notifications
,
inverse_of: :receivers
has_and_belongs_to_many
:followers
,
class_name:
"User"
,
inverse_of: :followings
,
autosave:
true
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
has_and_belongs_to_many
:followings
,
class_name:
"User"
,
inverse_of: :followers
validates_presence_of
:external_id
validates_presence_of
:external_id
validates_uniqueness_of
:external_id
validates_uniqueness_of
:external_id
...
@@ -28,30 +28,28 @@ class User
...
@@ -28,30 +28,28 @@ class User
followings
.
delete
(
user
)
followings
.
delete
(
user
)
end
end
def
self
.
watch
ing
(
class_plural_sym
)
def
self
.
subscrib
ing
(
class_plural_sym
)
class_plural
=
class_plural_sym
.
to_s
class_plural
=
class_plural_sym
.
to_s
class_single
=
class_plural
.
singularize
class_single
=
class_plural
.
singularize
class_name
=
class_single
.
camelize
class_name
=
class_single
.
camelize
watched_symbol
=
"watch
ed_
#{
class_plural
}
"
.
intern
subscribed_symbol
=
"subscrib
ed_
#{
class_plural
}
"
.
intern
has_and_belongs_to_many
watched_symbol
,
class_name:
class_name
,
inverse_of: :watchers
#, autosave: true
has_and_belongs_to_many
subscribed_symbol
,
class_name:
class_name
,
inverse_of: :subscribers
self
.
class_eval
<<-
END
self
.
class_eval
<<-
END
def watch_
#{
class_single
}
(watching_object)
def subscribe_
#{
class_single
}
(subscribing_object)
if not watched_
#{
class_plural
}
.include? watching_object
if not subscribed_
#{
class_plural
}
.include? subscribing_object
watched_
#{
class_plural
}
<< watching_object
subscribed_
#{
class_plural
}
<< subscribing_object
#save!
end
end
end
end
def unwatch_
#{
class_single
}
(watching_object)
def unsubscribe_
#{
class_single
}
(subscribing_object)
watched_
#{
class_plural
}
.delete(watching_object)
subscribed_
#{
class_plural
}
.delete(subscribing_object)
#save!
end
end
END
END
end
end
watch
ing
:comment_threads
subscrib
ing
:comment_threads
watch
ing
:commentables
subscrib
ing
:commentables
end
end
spec/app_spec.rb
View file @
5f4bd140
...
@@ -85,12 +85,12 @@ def init_with_subscriptions
...
@@ -85,12 +85,12 @@ def init_with_subscriptions
user2
.
followings
<<
user1
user2
.
followings
<<
user1
commentable
=
Commentable
.
new
(
commentable_type:
"questions"
,
commentable_id:
"1"
)
commentable
=
Commentable
.
new
(
commentable_type:
"questions"
,
commentable_id:
"1"
)
commentable
.
watch
ers
<<
[
user1
,
user2
]
commentable
.
subscrib
ers
<<
[
user1
,
user2
]
commentable
.
save!
commentable
.
save!
thread
=
commentable
.
comment_threads
.
new
(
title:
"I can't solve this problem"
,
body:
"can anyone help me?"
,
course_id:
"1"
)
thread
=
commentable
.
comment_threads
.
new
(
title:
"I can't solve this problem"
,
body:
"can anyone help me?"
,
course_id:
"1"
)
thread
.
author
=
user1
thread
.
author
=
user1
thread
.
watch
ers
<<
user2
thread
.
subscrib
ers
<<
user2
thread
.
save!
thread
.
save!
comment
=
thread
.
comments
.
new
(
body:
"this problem is so easy"
,
course_id:
"1"
)
comment
=
thread
.
comments
.
new
(
body:
"this problem is so easy"
,
course_id:
"1"
)
...
@@ -375,15 +375,15 @@ describe "app" do
...
@@ -375,15 +375,15 @@ describe "app" do
user3
=
User
.
find_or_create_by
(
external_id:
"3"
)
user3
=
User
.
find_or_create_by
(
external_id:
"3"
)
post
"/api/v1/users/
#{
user3
.
external_id
}
/subscriptions"
,
subscribed_type:
"questions"
,
subscribed_id:
"1"
post
"/api/v1/users/
#{
user3
.
external_id
}
/subscriptions"
,
subscribed_type:
"questions"
,
subscribed_id:
"1"
last_response
.
should
be_ok
last_response
.
should
be_ok
Commentable
.
first
.
watch
ers
.
length
.
should
==
3
Commentable
.
first
.
subscrib
ers
.
length
.
should
==
3
Commentable
.
first
.
watch
ers
.
should
include
user3
Commentable
.
first
.
subscrib
ers
.
should
include
user3
end
end
it
"unsubscribe a commentable"
do
it
"unsubscribe a commentable"
do
user2
=
User
.
find_or_create_by
(
external_id:
"2"
)
user2
=
User
.
find_or_create_by
(
external_id:
"2"
)
delete
"/api/v1/users/
#{
user2
.
external_id
}
/subscriptions"
,
subscribed_type:
"questions"
,
subscribed_id:
"1"
delete
"/api/v1/users/
#{
user2
.
external_id
}
/subscriptions"
,
subscribed_type:
"questions"
,
subscribed_id:
"1"
last_response
.
should
be_ok
last_response
.
should
be_ok
Commentable
.
first
.
watch
ers
.
length
.
should
==
1
Commentable
.
first
.
subscrib
ers
.
length
.
should
==
1
Commentable
.
first
.
watch
ers
.
should_not
include
user2
Commentable
.
first
.
subscrib
ers
.
should_not
include
user2
end
end
it
"subscribe a comment thread"
do
it
"subscribe a comment thread"
do
user1
=
User
.
find_or_create_by
(
external_id:
"1"
)
user1
=
User
.
find_or_create_by
(
external_id:
"1"
)
...
@@ -391,8 +391,8 @@ describe "app" do
...
@@ -391,8 +391,8 @@ describe "app" do
post
"/api/v1/users/
#{
user1
.
external_id
}
/subscriptions"
,
subscribed_type:
"thread"
,
subscribed_id:
thread
.
id
post
"/api/v1/users/
#{
user1
.
external_id
}
/subscriptions"
,
subscribed_type:
"thread"
,
subscribed_id:
thread
.
id
last_response
.
should
be_ok
last_response
.
should
be_ok
thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
thread
.
watch
ers
.
length
.
should
==
2
thread
.
subscrib
ers
.
length
.
should
==
2
thread
.
watch
ers
.
should
include
user1
thread
.
subscrib
ers
.
should
include
user1
end
end
it
"unsubscribe a comment thread"
do
it
"unsubscribe a comment thread"
do
user2
=
User
.
find_or_create_by
(
external_id:
"2"
)
user2
=
User
.
find_or_create_by
(
external_id:
"2"
)
...
@@ -400,7 +400,7 @@ describe "app" do
...
@@ -400,7 +400,7 @@ describe "app" do
delete
"/api/v1/users/
#{
user2
.
external_id
}
/subscriptions"
,
subscribed_type:
"thread"
,
subscribed_id:
thread
.
id
delete
"/api/v1/users/
#{
user2
.
external_id
}
/subscriptions"
,
subscribed_type:
"thread"
,
subscribed_id:
thread
.
id
last_response
.
should
be_ok
last_response
.
should
be_ok
thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
thread
=
CommentThread
.
where
(
body:
"it is unsolvable"
).
first
thread
.
watch
ers
.
length
.
should
==
0
thread
.
subscrib
ers
.
length
.
should
==
0
end
end
end
end
end
end
...
...
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