Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

View more comments on mobile #187

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/mobile.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,64 @@ def group(group_id='public', action='group', page=1):
request=request,
view='group')


@app.route("/feed/<int:feed_id>/comments", methods=["OPTIONS"])
def feed_actions(feed_id=None, action=None,
message_id=None, domain=None, comment_id=None):
if session and session.get('session_id'):
data = session
else:
authorization = request.headers.get('Authorization')
if not authorization or not authorization.startswith('session '):
abort(401)

data = SecureCookie.unserialize(authorization.split()[-1],
settings.SECRET_KEY)
if not data:
abort(401)

session_id = data.get('session_id')
network = data.get('network')
db_name = '%s_%s' % (network.replace('.', '_'),
settings.PRIMARY_DOMAIN.replace('.', '_'))

user_id = api.get_user_id(session_id, db_name=db_name)
if not user_id:
abort(401)

owner = api.get_user_info(user_id, db_name=db_name)
if request.path.endswith('/comments'):
limit = int(request.args.get('limit', 5))
last_comment_id = int(request.args.get('last'))

post = api.get_feed(session_id, feed_id, db_name=db_name)
if not post.id:
abort(400)

comments = []
for comment in post.comments:
if comment.id == last_comment_id:
break
else:
comments.append(comment)

if len(comments) > limit:
comments = comments[-limit:]

html = render(comments, 'comment',
owner, None, None,
item=post, hidden=True)
resp = {'html': html,
'length': len(comments),
'comments_count': post.comments_count}

if comments[0].id != post.comments[0].id:
resp['next_url'] = '/feed/%s/comments?last=%s' \
% (feed_id, comments[0].id)

return Response(dumps(resp), mimetype='application/json')


@app.route('/networks', methods=['GET', 'POST'])
@app.route('/network/<domain>', methods=['GET', 'POST'])
def network(domain=None):
Expand Down
48 changes: 48 additions & 0 deletions src/public/scripts/mobile/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,51 @@ $(document).ready(function() {
return false;
});

$('ul.stream').on("tap", 'a.view-previous-comments', function(e) {
e.preventDefault();

var _this = $(this);
var post_id = _this.parents('li.feed').attr('id');

// Show preloaded comments
$('#' + post_id + ' ul.comments li.comment.hidden').removeClass('hidden');

var displayed_count = $('#' + post_id + ' ul.comments li.comment:not(.hidden)').length;
$('.displayed-count', _this).html(displayed_count);

var undisplayed_count = $('.comment-count', _this).html() - displayed_count;
if (undisplayed_count <= 0) {
_this.parent().remove();
}
else if (undisplayed_count > 5) {
$('text', _this).html('View previous comments');
} else {
$('text', _this).html('View ' + undisplayed_count + ' more comments');
}
if (_this.attr('rel') != undefined) {
$.ajax({
type: "OPTIONS",
url: _this.attr('rel'),
dataType: "json",
success: function(data) {

if (data.next_url < 5) {
_this.attr('rel', '#');
} else {
_this.attr('rel', data.next_url);
}
var post_id = _this.parents('li.feed').attr('id');
$('#' + post_id + ' ul.comments li.comment:first').before(data.html);
refresh('#' + post_id + ' ul.comments');
return false;
}
});
}
e.stopPropagation();
return false;
});


$('body').on("click", 'a', function(e) {
e.preventDefault();
e.stopPropagation();
Expand All @@ -209,5 +254,8 @@ $(document).ready(function() {

return false;
});




});
2 changes: 1 addition & 1 deletion src/templates/comments.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<li class="action">
<span class="comment-icon"></span>
<a class="view-previous-comments" {% if item.comments_count > (comments_preview_count + 5) %}href='/feed/{{ item.id }}/comments?last={{ item.comments[-comments_preview_count-5].id }}'{% endif %}>
<a class="view-previous-comments" {% if mobile %}rel='/feed/{{ item.id }}/comments?last={{ item.comments[-comments_preview_count-5].id }}'{% else %}{% if item.comments_count > (comments_preview_count + 5) %}href='/feed/{{ item.id }}/comments?last={{ item.comments[-comments_preview_count-5].id }}'{% endif %}{% endif %}>
{% if item.comments_count > (5 + comments_preview_count) %}
<span class='rfloat'>
Showing <span class='displayed-count'>{{ comments_preview_count }}</span> of <span class='comment-count'>{{ item.comments_count }}</span> comments
Expand Down
1 change: 0 additions & 1 deletion src/templates/mobile/news_feed.html
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
<div class='loading'>
<img src='/public/images/mobile/spinner.gif'>
</div>

<script src='/public/scripts/zepto.min.js'></script>
<script src='/public/scripts/touch.js'></script>
<script src='/public/scripts/mobile/core.js'></script>
Expand Down