Commit bded772d by Rocky Duan

unread count tracking

parent db18ed81
......@@ -21,6 +21,8 @@ from django_comment_client.utils import merge_dict, extract, strip_none, strip_b
import django_comment_client.utils as utils
import comment_client as cc
import xml.sax.saxutils as saxutils
import datetime
from django.utils.timezone import utc
THREADS_PER_PAGE = 20
INLINE_THREADS_PER_PAGE = 20
......@@ -43,6 +45,7 @@ def get_threads(request, course_id, discussion_id=None, per_page=THREADS_PER_PAG
'tags': '',
'commentable_id': discussion_id,
'course_id': course_id,
'user_id': request.user.id,
}
if not request.GET.get('sort_key'):
......@@ -166,12 +169,21 @@ def single_thread(request, course_id, discussion_id, thread_id):
if request.is_ajax():
course = get_course_with_access(request.user, course_id, 'load')
user_info = cc.User.from_django_user(request.user).to_dict()
cc_user = cc.User.from_django_user(request.user)
user_info = cc_user.to_dict()
try:
last_read_time = datetime.datetime.utcnow().replace(tzinfo=utc).strftime('%Y-%m-%dT%H:%M:%S%z')
cc_user.update_read_states(course_id, thread_id, last_read_time)
except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err:
# TODO log error
pass
try:
thread = cc.Thread.find(thread_id).retrieve(recursive=True)
thread = cc.Thread.find(thread_id).retrieve(recursive=True, user_id=request.user.id)
except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err:
raise Http404
courseware_context = get_courseware_context(thread, course)
annotated_content_info = utils.get_annotated_content_infos(course_id, thread, request.user, user_info=user_info)
......@@ -190,9 +202,20 @@ def single_thread(request, course_id, discussion_id, thread_id):
else:
course = get_course_with_access(request.user, course_id, 'load')
category_map = utils.get_discussion_category_map(course)
cc_user = cc.User.from_django_user(request.user)
user_info = cc_user.to_dict()
try:
last_read_time = datetime.datetime.utcnow().replace(tzinfo=utc).strftime('%Y-%m-%dT%H:%M:%S%z')
cc_user.update_read_states(course_id, thread_id, last_read_time)
except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err:
# TODO log error
pass
try:
threads, query_params = get_threads(request, course_id)
thread = cc.Thread.find(thread_id).retrieve(recursive=True)
thread = cc.Thread.find(thread_id).retrieve(recursive=True, user_id=request.user.id)
threads.append(thread.to_dict())
except (cc.utils.CommentClientError, cc.utils.CommentClientUnknownError) as err:
raise Http404
......@@ -216,8 +239,7 @@ def single_thread(request, course_id, discussion_id, thread_id):
# course_id,
#)
user_info = cc.User.from_django_user(request.user).to_dict()
def infogetter(thread):
return utils.get_annotated_content_infos(course_id, thread, request.user, user_info)
......
......@@ -336,7 +336,8 @@ def safe_content(content):
'endorsed', 'parent_id', 'thread_id', 'votes', 'closed', 'created_at',
'updated_at', 'depth', 'type', 'commentable_id', 'comments_count',
'at_position_list', 'children', 'highlighted_title', 'highlighted_body',
'courseware_title', 'courseware_url', 'tags'
'courseware_title', 'courseware_url', 'tags', 'unread_comments_count',
'viewed',
]
if (content.get('anonymous') is False) and (content.get('anonymous_to_peers') is False):
......
......@@ -72,8 +72,8 @@ class Model(object):
for k, v in kwargs.items():
if k in self.accessible_fields:
self.__setattr__(k, v)
else:
raise AttributeError("Field {0} does not exist".format(k))
#else:
# raise AttributeError("Field {0} does not exist".format(k))
def updatable_attributes(self):
return extract(self.attributes, self.updatable_fields)
......
......@@ -9,7 +9,8 @@ class Thread(models.Model):
'id', 'title', 'body', 'anonymous', 'anonymous_to_peers', 'course_id',
'closed', 'tags', 'votes', 'commentable_id', 'username', 'user_id',
'created_at', 'updated_at', 'comments_count', 'at_position_list',
'children', 'type', 'highlighted_title', 'highlighted_body', 'endorsed'
'children', 'type', 'highlighted_title', 'highlighted_body', 'endorsed',
'unread_comments_count', 'viewed',
]
updatable_fields = [
......@@ -61,5 +62,5 @@ class Thread(models.Model):
def _retrieve(self, *args, **kwargs):
url = self.url(action='get', params=self.attributes)
response = perform_request('get', url, {'recursive': kwargs.get('recursive')})
response = perform_request('get', url, {'recursive': kwargs.get('recursive'), 'user_id': kwargs.get('user_id')})
self.update_attributes(**response)
......@@ -2,6 +2,7 @@ from utils import *
import models
import settings
import json
class User(models.Model):
......@@ -73,6 +74,13 @@ class User(models.Model):
response = perform_request('get', url, retrieve_params)
self.update_attributes(**response)
def update_read_states(self, course_id, thread_id, last_read_time):
url = _url_for_read_states(self.id)
response = perform_request('put', url, { "course_id": course_id,
"thread_id": thread_id,
"last_read_time": last_read_time,
})
def _url_for_vote_comment(comment_id):
return "{prefix}/comments/{comment_id}/votes".format(prefix=settings.PREFIX, comment_id=comment_id)
......@@ -82,5 +90,8 @@ def _url_for_vote_thread(thread_id):
def _url_for_subscription(user_id):
return "{prefix}/users/{user_id}/subscriptions".format(prefix=settings.PREFIX, user_id=user_id)
def _url_for_read_states(user_id):
return "{prefix}/users/{user_id}/read_states".format(prefix=settings.PREFIX, user_id=user_id)
def _url_for_user_active_threads(user_id):
return "{prefix}/users/{user_id}/active_threads".format(prefix=settings.PREFIX, user_id=user_id)
......@@ -24,6 +24,8 @@ if Backbone?
showThread: (forum_name, thread_id) ->
@thread = @discussion.get(thread_id)
@thread.set("unread_comments_count", 0)
@thread.set("viewed", true)
@setActiveThread()
if(@main)
@main.cleanup()
......
......@@ -129,6 +129,8 @@ if Backbone?
content.addClass("followed")
if thread.get('endorsed')
content.addClass("resolved")
if thread.get('viewed')
content.addClass("viewed")
@highlight(content)
......
......@@ -1018,6 +1018,7 @@ body.discussion {
text-shadow: 0 -1px 0 rgba(0, 0, 0, .3);
box-shadow: 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 1px rgba(0, 0, 0, .2) inset;
}
}
}
......@@ -1127,6 +1128,10 @@ body.discussion {
background: url(../images/following-flag.png) no-repeat;
}
&.viewed {
@include linear-gradient(top, white, #ddd);
}
&.active {
@include linear-gradient(top, #96e0fd, #61c7fc);
border-color: #4697c1;
......
......@@ -128,5 +128,5 @@
</script>
<script type="text/template" id="thread-list-item-template">
<a href="${'<%- id %>'}" data-id="${'<%- id %>'}"><span class="title">${"<%- title %>"}</span> <span class="comments-count">${"<%- comments_count %>"}</span><span class="votes-count">+${"<%- votes['up_count'] %>"}</span></a>
<a href="${'<%- id %>'}" data-id="${'<%- id %>'}"><span class="title">${"<%- title %>"}</span> <span class="comments-count">${"<%- unread_comments_count %>"}</span><span class="votes-count">+${"<%- votes['up_count'] %>"}</span></a>
</script>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment