More pagination
This commit is contained in:
parent
acafc1cc85
commit
10f26d0350
5 changed files with 81 additions and 73 deletions
109
app.py
109
app.py
|
@ -588,33 +588,27 @@ def tmp_migrate3():
|
||||||
return "Done"
|
return "Done"
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
def paginated_query(db, q, limit=50, sort_key="_id"):
|
||||||
def index():
|
|
||||||
if is_api_request():
|
|
||||||
return jsonify(**ME)
|
|
||||||
|
|
||||||
older_than = newer_than = None
|
older_than = newer_than = None
|
||||||
query_sort = -1
|
query_sort = -1
|
||||||
first_page = not request.args.get('older_than') and not request.args.get('newer_than')
|
first_page = not request.args.get("older_than") and not request.args.get(
|
||||||
limit = 5
|
"newer_than"
|
||||||
q = {
|
)
|
||||||
"box": Box.OUTBOX.value,
|
|
||||||
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]},
|
|
||||||
"activity.object.inReplyTo": None,
|
|
||||||
"meta.deleted": False,
|
|
||||||
"meta.undo": False,
|
|
||||||
}
|
|
||||||
query_older_than = request.args.get("older_than")
|
query_older_than = request.args.get("older_than")
|
||||||
query_newer_than = request.args.get("newer_than")
|
query_newer_than = request.args.get("newer_than")
|
||||||
|
|
||||||
if query_older_than:
|
if query_older_than:
|
||||||
q["_id"] = {"$lt": ObjectId(query_older_than)}
|
q["_id"] = {"$lt": ObjectId(query_older_than)}
|
||||||
elif query_newer_than:
|
elif query_newer_than:
|
||||||
q["_id"] = {"$gt": ObjectId(query_newer_than)}
|
q["_id"] = {"$gt": ObjectId(query_newer_than)}
|
||||||
query_sort = 1
|
query_sort = 1
|
||||||
|
|
||||||
outbox_data = list(DB.activities.find(q, limit=limit+1).sort("_id", query_sort))
|
outbox_data = list(db.find(q, limit=limit + 1).sort(sort_key, query_sort))
|
||||||
outbox_len = len(outbox_data)
|
outbox_len = len(outbox_data)
|
||||||
outbox_data = sorted(outbox_data[:limit], key=lambda x: str(x["_id"]), reverse=True)
|
outbox_data = sorted(
|
||||||
|
outbox_data[:limit], key=lambda x: str(x[sort_key]), reverse=True
|
||||||
|
)
|
||||||
|
|
||||||
if query_older_than:
|
if query_older_than:
|
||||||
newer_than = str(outbox_data[0]["_id"])
|
newer_than = str(outbox_data[0]["_id"])
|
||||||
|
@ -627,6 +621,23 @@ def index():
|
||||||
elif first_page and outbox_len == limit + 1:
|
elif first_page and outbox_len == limit + 1:
|
||||||
older_than = str(outbox_data[-1]["_id"])
|
older_than = str(outbox_data[-1]["_id"])
|
||||||
|
|
||||||
|
return outbox_data, older_than, newer_than
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/")
|
||||||
|
def index():
|
||||||
|
if is_api_request():
|
||||||
|
return jsonify(**ME)
|
||||||
|
|
||||||
|
q = {
|
||||||
|
"box": Box.OUTBOX.value,
|
||||||
|
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]},
|
||||||
|
"activity.object.inReplyTo": None,
|
||||||
|
"meta.deleted": False,
|
||||||
|
"meta.undo": False,
|
||||||
|
}
|
||||||
|
outbox_data, older_than, newer_than = paginated_query(DB.activities, q)
|
||||||
|
|
||||||
return render_template(
|
return render_template(
|
||||||
"index.html",
|
"index.html",
|
||||||
outbox_data=outbox_data,
|
outbox_data=outbox_data,
|
||||||
|
@ -637,24 +648,20 @@ def index():
|
||||||
|
|
||||||
@app.route("/with_replies")
|
@app.route("/with_replies")
|
||||||
def with_replies():
|
def with_replies():
|
||||||
# FIXME(tsileo): implements pagination, also for the followers/following page
|
|
||||||
limit = 50
|
|
||||||
q = {
|
q = {
|
||||||
"box": Box.OUTBOX.value,
|
"box": Box.OUTBOX.value,
|
||||||
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]},
|
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]},
|
||||||
"meta.deleted": False,
|
"meta.deleted": False,
|
||||||
"meta.undo": False,
|
"meta.undo": False,
|
||||||
}
|
}
|
||||||
c = request.args.get("cursor")
|
outbox_data, older_than, newer_than = paginated_query(DB.activities, q)
|
||||||
if c:
|
|
||||||
q["_id"] = {"$lt": ObjectId(c)}
|
|
||||||
|
|
||||||
outbox_data = list(DB.activities.find(q, limit=limit).sort("_id", -1))
|
return render_template(
|
||||||
cursor = None
|
"index.html",
|
||||||
if outbox_data and len(outbox_data) == limit:
|
outbox_data=outbox_data,
|
||||||
cursor = str(outbox_data[-1]["_id"])
|
older_than=older_than,
|
||||||
|
newer_than=newer_than,
|
||||||
return render_template("index.html", outbox_data=outbox_data, cursor=cursor)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _build_thread(data, include_children=True):
|
def _build_thread(data, include_children=True):
|
||||||
|
@ -1107,8 +1114,6 @@ def new():
|
||||||
@app.route("/notifications")
|
@app.route("/notifications")
|
||||||
@login_required
|
@login_required
|
||||||
def notifications():
|
def notifications():
|
||||||
# FIXME(tsileo): implements pagination, also for the followers/following page
|
|
||||||
limit = 50
|
|
||||||
# FIXME(tsileo): show unfollow (performed by the current actor) and liked???
|
# FIXME(tsileo): show unfollow (performed by the current actor) and liked???
|
||||||
mentions_query = {
|
mentions_query = {
|
||||||
"type": ActivityType.CREATE.value,
|
"type": ActivityType.CREATE.value,
|
||||||
|
@ -1141,16 +1146,14 @@ def notifications():
|
||||||
unfollow_query,
|
unfollow_query,
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
c = request.args.get("cursor")
|
inbox_data, older_than, newer_than = paginated_query(DB.activities, q)
|
||||||
if c:
|
|
||||||
q["_id"] = {"$lt": ObjectId(c)}
|
|
||||||
|
|
||||||
outbox_data = list(DB.activities.find(q, limit=limit).sort("_id", -1))
|
return render_template(
|
||||||
cursor = None
|
"stream.html",
|
||||||
if outbox_data and len(outbox_data) == limit:
|
inbox_data=inbox_data,
|
||||||
cursor = str(outbox_data[-1]["_id"])
|
older_than=older_than,
|
||||||
|
newer_than=newer_than,
|
||||||
return render_template("stream.html", inbox_data=outbox_data, cursor=cursor)
|
)
|
||||||
|
|
||||||
|
|
||||||
@app.route("/api/key")
|
@app.route("/api/key")
|
||||||
|
@ -1254,26 +1257,19 @@ def api_undo():
|
||||||
@app.route("/stream")
|
@app.route("/stream")
|
||||||
@login_required
|
@login_required
|
||||||
def stream():
|
def stream():
|
||||||
# FIXME(tsileo): implements pagination, also for the followers/following page
|
|
||||||
limit = 100
|
|
||||||
c = request.args.get("cursor")
|
|
||||||
q = {
|
q = {
|
||||||
"box": Box.INBOX.value,
|
"box": Box.INBOX.value,
|
||||||
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]},
|
"type": {"$in": [ActivityType.CREATE.value, ActivityType.ANNOUNCE.value]},
|
||||||
"meta.deleted": False,
|
"meta.deleted": False,
|
||||||
}
|
}
|
||||||
if c:
|
inbox_data, older_than, newer_than = paginated_query(DB.activities, q)
|
||||||
q["_id"] = {"$lt": ObjectId(c)}
|
|
||||||
|
|
||||||
inbox_data = list(
|
return render_template(
|
||||||
# FIXME(tsileo): reshape using meta.cached_object
|
"stream.html",
|
||||||
DB.activities.find(q, limit=limit).sort("_id", -1)
|
inbox_data=inbox_data,
|
||||||
|
older_than=older_than,
|
||||||
|
newer_than=newer_than,
|
||||||
)
|
)
|
||||||
cursor = None
|
|
||||||
if inbox_data and len(inbox_data) == limit:
|
|
||||||
cursor = str(inbox_data[-1]["_id"])
|
|
||||||
|
|
||||||
return render_template("stream.html", inbox_data=inbox_data, cursor=cursor)
|
|
||||||
|
|
||||||
|
|
||||||
@app.route("/inbox", methods=["GET", "POST"])
|
@app.route("/inbox", methods=["GET", "POST"])
|
||||||
|
@ -1541,16 +1537,17 @@ def tags(tag):
|
||||||
@app.route("/liked")
|
@app.route("/liked")
|
||||||
def liked():
|
def liked():
|
||||||
if not is_api_request():
|
if not is_api_request():
|
||||||
return render_template(
|
q = {
|
||||||
"liked.html",
|
|
||||||
liked=DB.activities.find(
|
|
||||||
{
|
|
||||||
"box": Box.OUTBOX.value,
|
"box": Box.OUTBOX.value,
|
||||||
"type": ActivityType.LIKE.value,
|
"type": ActivityType.LIKE.value,
|
||||||
"meta.deleted": False,
|
"meta.deleted": False,
|
||||||
"meta.undo": False,
|
"meta.undo": False,
|
||||||
}
|
}
|
||||||
),
|
|
||||||
|
liked, older_than, newer_than = paginated_query(DB.activities, q)
|
||||||
|
|
||||||
|
return render_template(
|
||||||
|
"liked.html", liked=liked, older_than=older_than, newer_than=newer_than
|
||||||
)
|
)
|
||||||
|
|
||||||
q = {"meta.deleted": False, "meta.undo": False, "type": ActivityType.LIKE.value}
|
q = {"meta.deleted": False, "meta.undo": False, "type": ActivityType.LIKE.value}
|
||||||
|
|
|
@ -34,20 +34,10 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<div class="clear">
|
|
||||||
{% if older_than %}
|
{{ utils.display_pagination(older_than, newer_than) }}
|
||||||
<a href="{{ config.BASE_URL }}{{ request.path }}?older_than={{older_than}}" rel="next" class="older-link lcolor"><span class="pcolor">🡨</span> Older</a>
|
|
||||||
{% endif %}
|
|
||||||
{% if newer_than %}
|
|
||||||
<a href="{{ config.BASE_URL }}{{ request.path }}?newer_than={{newer_than}}" rel="prev" class="newer-link lcolor">Newer <span class="pcolor">🡪</span></a>
|
|
||||||
{% endif %}
|
|
||||||
</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block links %}
|
{% block links %}{{ utils.display_pagination_links(older_than, newer_than) }}{% endblock %}
|
||||||
{% if older_than %}<link rel="next" href="{{ config.BASE_URL }}{{ request.path }}?older_than={{older_than}}">{% endif %}
|
|
||||||
{% if newer_than %}<link rel="prev" href="{{ config.BASE_URL }}{{ request.path }}?newer_than={{newer_than}}">{% endif %}
|
|
||||||
{% endblock %}
|
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,10 @@
|
||||||
{{ utils.display_note(item.meta.object) }}
|
{{ utils.display_note(item.meta.object) }}
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
{{ utils.display_pagination(older_than, newer_than) }}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
{% block links %}{{ utils.display_pagination_links(older_than, newer_than) }}{% endblock %}
|
||||||
|
|
|
@ -45,6 +45,8 @@
|
||||||
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
|
||||||
|
{{ utils.display_pagination(older_than, newer_than) }}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|
|
@ -168,3 +168,19 @@
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
{% endmacro -%}
|
{% endmacro -%}
|
||||||
|
|
||||||
|
{% macro display_pagination(older_than, newer_than) -%}
|
||||||
|
<div class="clear">
|
||||||
|
{% if older_than %}
|
||||||
|
<a href="{{ config.BASE_URL }}{{ request.path }}?older_than={{older_than}}" rel="next" class="older-link lcolor"><span class="pcolor">🡨</span> Older</a>
|
||||||
|
{% endif %}
|
||||||
|
{% if newer_than %}
|
||||||
|
<a href="{{ config.BASE_URL }}{{ request.path }}?newer_than={{newer_than}}" rel="prev" class="newer-link lcolor">Newer <span class="pcolor">🡪</span></a>
|
||||||
|
{% endif %}
|
||||||
|
</div>
|
||||||
|
{% endmacro -%}
|
||||||
|
|
||||||
|
{% macro display_pagination_links(older_than, newer_than) -%}
|
||||||
|
{% if older_than %}<link rel="next" href="{{ config.BASE_URL }}{{ request.path }}?older_than={{older_than}}">{% endif %}
|
||||||
|
{% if newer_than %}<link rel="prev" href="{{ config.BASE_URL }}{{ request.path }}?newer_than={{newer_than}}">{% endif %}
|
||||||
|
{% endmacro -%}
|
||||||
|
|
Loading…
Reference in a new issue