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
1e31c03a
Commit
1e31c03a
authored
Aug 15, 2012
by
Rocky Duan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
user recent active threads
parent
d62ac7eb
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
84 additions
and
32 deletions
+84
-32
api/comment_threads.rb
+4
-0
api/commentables.rb
+1
-30
api/users.rb
+29
-0
lib/helpers.rb
+49
-0
models/comment_thread.rb
+1
-2
No files found.
api/comment_threads.rb
View file @
1e31c03a
get
"
#{
APIPREFIX
}
/threads"
do
# retrieve threads by course
handle_threads_query
(
CommentThread
.
where
(
course_id:
params
[
"course_id"
]))
end
get
"
#{
APIPREFIX
}
/threads/:thread_id"
do
|
thread_id
|
CommentThread
.
find
(
thread_id
).
to_hash
(
recursive:
bool_recursive
).
to_json
end
...
...
api/commentables.rb
View file @
1e31c03a
...
...
@@ -5,37 +5,8 @@ end
get
"
#{
APIPREFIX
}
/:commentable_id/threads"
do
|
commentable_id
|
sort_key_mapper
=
{
"date"
=>
:created_at
,
"activity"
=>
:last_activity_at
,
"votes"
=>
:"votes.point"
,
"comments"
=>
:comment_count
,
}
sort_order_mapper
=
{
"desc"
=>
:desc
,
"asc"
=>
:asc
,
}
handle_threads_query
(
commentable
.
comment_threads
)
sort_key
=
sort_key_mapper
[
params
[
"sort_key"
]]
sort_order
=
sort_order_mapper
[
params
[
"sort_order"
]]
sort_keyword_valid
=
(
!
params
[
"sort_key"
]
&&
!
params
[
"sort_order"
]
||
sort_key
&&
sort_order
)
if
not
sort_keyword_valid
{}.
to_json
else
page
=
(
params
[
"page"
]
||
DEFAULT_PAGE
).
to_i
per_page
=
(
params
[
"per_page"
]
||
DEFAULT_PER_PAGE
).
to_i
comment_threads
=
commentable
.
comment_threads
comment_threads
=
comment_threads
.
order_by
(
"
#{
sort_key
}
#{
sort_order
}
"
)
if
sort_key
&&
sort_order
num_pages
=
[
1
,
(
comment_threads
.
count
/
per_page
.
to_f
).
ceil
].
max
page
=
[
num_pages
,
[
1
,
page
].
max
].
min
paged_comment_threads
=
comment_threads
.
page
(
page
).
per
(
per_page
)
{
collection:
paged_comment_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
bool_recursive
)},
num_pages:
num_pages
,
page:
page
,
}.
to_json
end
end
post
"
#{
APIPREFIX
}
/:commentable_id/threads"
do
|
commentable_id
|
...
...
api/users.rb
View file @
1e31c03a
...
...
@@ -14,6 +14,35 @@ get "#{APIPREFIX}/users/:user_id" do |user_id|
user
.
to_hash
(
complete:
bool_complete
,
course_id:
params
[
"course_id"
]).
to_json
end
get
"
#{
APIPREFIX
}
/users/:user_id/active_threads"
do
|
user_id
|
return
{}.
to_json
if
not
params
[
"course_id"
]
get_thread_id
=
lambda
{
|
c
|
c
.
_type
==
"Comment"
?
c
.
comment_thread_id
:
c
.
id
}
get_thread
=
lambda
{
|
thread_id
|
CommentThread
.
find
(
thread_id
)}
page
=
(
params
[
"page"
]
||
DEFAULT_PAGE
).
to_i
per_page
=
(
params
[
"per_page"
]
||
DEFAULT_PER_PAGE
).
to_i
active_contents
=
Content
.
where
(
author_id:
user_id
,
anonymous:
false
,
course_id:
params
[
"course_id"
])
.
order_by
(
updated_at: :desc
)
num_pages
=
[
1
,
(
active_contents
.
count
/
per_page
.
to_f
).
ceil
].
max
page
=
[
num_pages
,
[
1
,
page
].
max
].
min
paged_active_contents
=
active_contents
.
page
(
page
).
per
(
per_page
)
paged_active_threads
=
paged_active_contents
.
map
(
&
get_thread_id
)
.
uniq
.
map
(
&
get_thread
)
collection
=
paged_active_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
true
)}
collection
=
author_contents_only
(
collection
,
user_id
)
{
collection:
collection
,
num_pages:
num_pages
,
page:
page
,
}.
to_json
end
put
"
#{
APIPREFIX
}
/users/:user_id"
do
|
user_id
|
user
=
User
.
where
(
external_id:
user_id
).
first
if
not
user
...
...
lib/helpers.rb
View file @
1e31c03a
...
...
@@ -63,4 +63,53 @@ helpers do
value_to_boolean
params
[
"anonymous"
]
end
def
handle_paged_threads_query
(
paged_comment_threads
)
end
def
handle_threads_query
(
comment_threads
)
sort_key_mapper
=
{
"date"
=>
:created_at
,
"activity"
=>
:last_activity_at
,
"votes"
=>
:"votes.point"
,
"comments"
=>
:comment_count
,
}
sort_order_mapper
=
{
"desc"
=>
:desc
,
"asc"
=>
:asc
,
}
sort_key
=
sort_key_mapper
[
params
[
"sort_key"
]]
sort_order
=
sort_order_mapper
[
params
[
"sort_order"
]]
sort_keyword_valid
=
(
!
params
[
"sort_key"
]
&&
!
params
[
"sort_order"
]
||
sort_key
&&
sort_order
)
if
not
sort_keyword_valid
{}.
to_json
else
page
=
(
params
[
"page"
]
||
DEFAULT_PAGE
).
to_i
per_page
=
(
params
[
"per_page"
]
||
DEFAULT_PER_PAGE
).
to_i
comment_threads
=
comment_threads
.
order_by
(
"
#{
sort_key
}
#{
sort_order
}
"
)
if
sort_key
&&
sort_order
num_pages
=
[
1
,
(
comment_threads
.
count
/
per_page
.
to_f
).
ceil
].
max
page
=
[
num_pages
,
[
1
,
page
].
max
].
min
paged_comment_threads
=
comment_threads
.
page
(
page
).
per
(
per_page
)
{
collection:
paged_comment_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
bool_recursive
)},
num_pages:
num_pages
,
page:
page
,
}.
to_json
end
end
def
author_contents_only
(
contents
,
author_id
)
contents
.
map
do
|
content
|
content
[
'children'
]
=
author_contents_only
(
content
[
'children'
],
author_id
)
if
content
[
'children'
].
length
>
0
or
\
(
content
[
'user_id'
]
==
author_id
and
not
content
[
'anonymous'
])
content
else
nil
end
end
.
compact
end
end
models/comment_thread.rb
View file @
1e31c03a
...
...
@@ -142,9 +142,8 @@ class CommentThread < Content
if
params
[
:recursive
]
doc
=
doc
.
merge
(
"children"
=>
root_comments
.
map
{
|
c
|
c
.
to_hash
(
recursive:
true
)})
else
doc
=
doc
.
merge
(
"comments_count"
=>
comments
.
count
)
end
doc
=
doc
.
merge
(
"comments_count"
=>
comments
.
count
)
doc
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