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
88b0cd88
Commit
88b0cd88
authored
Apr 01, 2013
by
Kevin Chugh
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
flagged search started
parent
6a27f7aa
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
50 deletions
+56
-50
lib/helpers.rb
+56
-50
No files found.
lib/helpers.rb
View file @
88b0cd88
...
...
@@ -107,62 +107,68 @@ helpers do
end
def
handle_threads_query
(
comment_threads
)
if
params
[
:course_id
]
comment_threads
=
comment_threads
.
where
(
:course_id
=>
params
[
:course_id
])
end
if
CommentService
.
config
[
:cache_enabled
]
query_params
=
params
.
slice
(
*
%w[course_id commentable_id sort_key sort_order page per_page user_id]
)
memcached_key
=
"threads_query_
#{
query_params
.
hash
}
"
cached_results
=
Sinatra
::
Application
.
cache
.
get
(
memcached_key
)
if
cached_results
return
{
collection:
cached_results
[
:collection_ids
].
map
{
|
id
|
CommentThread
.
find
(
id
).
to_hash
(
recursive:
bool_recursive
,
user_id:
params
[
"user_id"
])},
num_pages:
cached_results
[
:num_pages
],
page:
cached_results
[
:page
],
}.
to_json
if
params
[
:flagged
]
#get flagged threads and threads containing flagged responses
comment_threads
=
comment_threads
.
where
(
:course_id
=>
params
[
:course_id
]).
where
(
:abuse_flaggers
)
else
if
params
[
:course_id
]
comment_threads
=
comment_threads
.
where
(
:course_id
=>
params
[
:course_id
])
end
if
CommentService
.
config
[
:cache_enabled
]
query_params
=
params
.
slice
(
*
%w[course_id commentable_id sort_key sort_order page per_page user_id]
)
memcached_key
=
"threads_query_
#{
query_params
.
hash
}
"
cached_results
=
Sinatra
::
Application
.
cache
.
get
(
memcached_key
)
if
cached_results
return
{
collection:
cached_results
[
:collection_ids
].
map
{
|
id
|
CommentThread
.
find
(
id
).
to_hash
(
recursive:
bool_recursive
,
user_id:
params
[
"user_id"
])},
num_pages:
cached_results
[
:num_pages
],
page:
cached_results
[
:page
],
}.
to_json
end
end
end
sort_key_mapper
=
{
"date"
=>
:created_at
,
"activity"
=>
:last_activity_at
,
"votes"
=>
:"votes.point"
,
"comments"
=>
:comment_count
,
}
sort_key_mapper
=
{
"date"
=>
:created_at
,
"activity"
=>
:last_activity_at
,
"votes"
=>
:"votes.point"
,
"comments"
=>
:comment_count
,
}
sort_order_mapper
=
{
"desc"
=>
:desc
,
"asc"
=>
:asc
,
}
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
)
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
#KChugh turns out we don't need to go through all the extra work on the back end because the client is resorting anyway
#KChugh boy was I wrong, we need to sort for pagination
comment_threads
=
comment_threads
.
order_by
(
"pinned DESC,
#{
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
)
if
CommentService
.
config
[
:cache_enabled
]
cached_results
=
{
collection_ids:
paged_comment_threads
.
map
(
&
:id
),
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
#KChugh turns out we don't need to go through all the extra work on the back end because the client is resorting anyway
#KChugh boy was I wrong, we need to sort for pagination
comment_threads
=
comment_threads
.
order_by
(
"pinned DESC,
#{
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
)
if
CommentService
.
config
[
:cache_enabled
]
cached_results
=
{
collection_ids:
paged_comment_threads
.
map
(
&
:id
),
num_pages:
num_pages
,
page:
page
,
}
Sinatra
::
Application
.
cache
.
set
(
memcached_key
,
cached_results
,
CommentService
.
config
[
:cache_timeout
][
:threads_query
].
to_i
)
end
{
collection:
paged_comment_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
bool_recursive
,
user_id:
params
[
"user_id"
])},
num_pages:
num_pages
,
page:
page
,
}
Sinatra
::
Application
.
cache
.
set
(
memcached_key
,
cached_results
,
CommentService
.
config
[
:cache_timeout
][
:threads_query
].
to_i
)
}.
to_json
end
{
collection:
paged_comment_threads
.
map
{
|
t
|
t
.
to_hash
(
recursive:
bool_recursive
,
user_id:
params
[
"user_id"
])},
num_pages:
num_pages
,
page:
page
,
}.
to_json
end
end
...
...
@@ -170,8 +176,8 @@ helpers do
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'
]
and
not
content
[
'anonymous_to_peers'
])
content
(
content
[
'user_id'
]
==
author_id
and
not
content
[
'anonymous'
]
and
not
content
[
'anonymous_to_peers'
])
content
else
nil
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