Non-working implementation of ajax notification badge

This commit is contained in:
Jason McBrayer 2018-09-08 09:09:04 -04:00
parent 1523e3f229
commit 86ec6aed95
6 changed files with 54 additions and 2 deletions

View File

@ -0,0 +1,18 @@
# Generated by Django 2.1.1 on 2018-09-08 11:47
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('brutaldon', '0013_auto_20180826_1935'),
]
operations = [
migrations.AddField(
model_name='account',
name='note_seen',
field=models.IntegerField(null=True),
),
]

View File

@ -38,4 +38,5 @@ class Account(models.Model):
access_token = models.CharField(null=True, blank=True, max_length=2048)
client= models.ForeignKey(Client, models.SET_NULL, null=True)
preferences = models.ForeignKey(Preference, models.SET_NULL, null=True)
note_seen = models.IntegerField(null=True)

View File

@ -100,9 +100,12 @@
ic-on-success="afterPage('{{ own_acct.username }}', 'Notifications');"
ic-indicator="#page-load-indicator">
<span class="fa fa-bell-o"></span>
{% if notifications and not preferences.theme.is_brutalist %}
{% if not preferences.theme.is_brutalist %}
<span class="badge"
data-badge="{{ notifications }}">
data-badge="{{ notifications }}"
ic-src="{% url 'notes_count' %}"
ic-poll="60s"
ic-push-url="false">
&nbsp; Notifications
</span>
{% elif notifications %}

View File

@ -0,0 +1,7 @@
<span class="badge"
data-badge="{{ notifications }}"
ic-src="{% url 'notes_count' %}"
ic-poll="60s"
ic-push-url="false">
&nbsp; Notifications
</span>

View File

@ -38,6 +38,7 @@ urlpatterns = [
path('note', views.note, name='note'),
path('note/next<int:next>', views.note, name='note_next'),
path('note/prev/<int:prev>', views.note, name='note_prev'),
path('notes_count', views.notes_count, name='notes_count'),
path('settings', views.settings, name='settings'),
path('thread/<int:id>', views.thread, name='thread'),
path('tags/<tag>', views.tag, name='tag'),

View File

@ -38,6 +38,19 @@ def get_usercontext(request):
def is_logged_in(request):
return request.session.has_key('user')
def _notes_count(request):
try:
account, mastodon = get_usercontext(request)
except NotLoggedInException:
return ""
notes = mastodon.notifications(limit=40)
for index, item in enumerate(notes):
if item.id == account.note_seen:
break
else:
index = "40+"
return str(index)
def br_login_required(function=None, home_url=None, redirect_field_name=None):
"""Check that the user is logged in to a Mastodon instance.
@ -75,6 +88,11 @@ def br_login_required(function=None, home_url=None, redirect_field_name=None):
else:
return _dec(function)
def notes_count(request):
count = _notes_count(request)
return render(request, 'intercooler/notes.html',
{'notifications': count,})
def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_id=None):
account, mastodon = get_usercontext(request)
data = mastodon.timeline(timeline, limit=100, max_id=max_id, since_id=since_id)
@ -296,6 +314,10 @@ def note(request, next=None, prev=None):
account, mastodon = get_usercontext(request)
except NotLoggedInException:
return redirect(about)
last_seen = mastodon.notifications(limit=1)[0]
account.note_seen = last_seen.id
account.save()
notes = mastodon.notifications(limit=100, max_id=next, since_id=prev)
try:
prev = notes[0]._pagination_prev