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
f5f27465
Commit
f5f27465
authored
Jun 22, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
depth limit
parent
5a1dcfde
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
12 additions
and
3 deletions
+12
-3
Rakefile
+8
-3
app.rb
+3
-0
config/application.yml
+1
-0
No files found.
Rakefile
View file @
f5f27465
...
@@ -21,20 +21,25 @@ namespace :db do
...
@@ -21,20 +21,25 @@ namespace :db do
require_relative
'models/comment.rb'
require_relative
'models/comment.rb'
require_relative
'models/comment_thread.rb'
require_relative
'models/comment_thread.rb'
require_relative
'models/vote.rb'
require_relative
'models/vote.rb'
require_relative
'models/user.rb'
Comment
.
delete_all
Comment
.
delete_all
CommentThread
.
delete_all
CommentThread
.
delete_all
Vote
.
delete_all
Vote
.
delete_all
User
.
delete_all
depth_limit
=
YAML
.
load_file
(
"config/application.yml"
)[
"depth_limit"
]
comment_thread
=
CommentThread
.
create!
:commentable_type
=>
"questions"
,
:commentable_id
=>
1
comment_thread
=
CommentThread
.
create!
:commentable_type
=>
"questions"
,
:commentable_id
=>
1
5
.
times
do
5
.
times
do
comment_thread
.
root_comments
.
create
:body
=>
"top comment"
,
:title
=>
"top
#{
rand
(
10
)
}
"
,
:user_id
=>
1
,
:course_id
=>
1
,
:comment_thread_id
=>
comment_thread
.
id
comment_thread
.
root_comments
.
create
:body
=>
"top comment"
,
:title
=>
"top
#{
rand
(
10
)
}
"
,
:user_id
=>
1
,
:course_id
=>
1
,
:comment_thread_id
=>
comment_thread
.
id
end
end
50
.
times
do
10
.
times
do
Comment
.
all
.
reject
{
|
c
|
c
.
is_root?
}.
sample
.
children
.
create
:body
=>
"comment body"
,
:title
=>
"comment title
#{
rand
(
50
)
}
"
,
:user_id
=>
1
,
:course_id
=>
1
,
:comment_thread_id
=>
comment_thread
.
id
comment
=
Comment
.
all
.
reject
{
|
c
|
c
.
is_root?
or
c
.
depth
-
1
>=
depth_limit
}.
sample
comment
.
children
.
create
:body
=>
"comment body"
,
:title
=>
"comment title
#{
rand
(
50
)
}
"
,
:user_id
=>
1
,
:course_id
=>
1
,
:comment_thread_id
=>
comment_thread
.
id
end
end
Comment
.
all
.
reject
{
|
c
|
c
.
is_root?
}.
each
do
|
c
|
Comment
.
all
.
reject
{
|
c
|
c
.
is_root?
}.
each
do
|
c
|
(
1
..
20
).
each
do
|
id
|
(
1
..
20
).
each
do
|
id
|
Vote
.
create!
:value
=>
[
"up"
,
"down"
].
sample
,
:comment_id
=>
c
.
id
,
:user_id
=>
id
user
=
User
.
find_or_create_by_id
(
id
)
user
.
vote
(
c
,
{
:direction
=>
[
:up
,
:down
].
sample
})
end
end
end
end
end
end
...
...
app.rb
View file @
f5f27465
...
@@ -13,6 +13,7 @@ env_index = ARGV.index("-e")
...
@@ -13,6 +13,7 @@ env_index = ARGV.index("-e")
env_arg
=
ARGV
[
env_index
+
1
]
if
env_index
env_arg
=
ARGV
[
env_index
+
1
]
if
env_index
env
=
env_arg
||
ENV
[
"SINATRA_ENV"
]
||
"development"
env
=
env_arg
||
ENV
[
"SINATRA_ENV"
]
||
"development"
databases
=
YAML
.
load_file
(
"config/database.yml"
)
databases
=
YAML
.
load_file
(
"config/database.yml"
)
config
=
YAML
.
load_file
(
"config/application.yml"
)
ActiveRecord
::
Base
.
establish_connection
(
databases
[
env
])
ActiveRecord
::
Base
.
establish_connection
(
databases
[
env
])
# retrive all comments of a commentable object
# retrive all comments of a commentable object
...
@@ -49,6 +50,8 @@ post '/api/v1/comments/:comment_id' do |comment_id|
...
@@ -49,6 +50,8 @@ post '/api/v1/comments/:comment_id' do |comment_id|
comment
=
Comment
.
find_by_id
(
comment_id
)
comment
=
Comment
.
find_by_id
(
comment_id
)
if
comment
.
nil?
or
comment
.
is_root?
if
comment
.
nil?
or
comment
.
is_root?
error
400
,
{
:error
=>
"invalid comment id"
}.
to_json
error
400
,
{
:error
=>
"invalid comment id"
}.
to_json
elsif
comment
.
depth
-
1
>=
config
[
"depth_limit"
]
# The depth should be subtracted by 1 because we are counting the super comment
error
400
,
{
:error
=>
"depth limit exceeded"
}.
to_json
else
else
comment_params
=
params
.
select
{
|
key
,
value
|
%w{body title user_id course_id}
.
include?
key
}.
merge
({
:comment_thread_id
=>
comment
.
comment_thread_id
})
comment_params
=
params
.
select
{
|
key
,
value
|
%w{body title user_id course_id}
.
include?
key
}.
merge
({
:comment_thread_id
=>
comment
.
comment_thread_id
})
sub_comment
=
comment
.
children
.
create
(
comment_params
)
sub_comment
=
comment
.
children
.
create
(
comment_params
)
...
...
config/application.yml
0 → 100644
View file @
f5f27465
depth_limit
:
1
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