diff --git a/pages.py b/pages.py index 6ffb3f1d..6dec02c9 100644 --- a/pages.py +++ b/pages.py @@ -374,6 +374,27 @@ def set_username(user=None): return redirect('/settings', code=302) +@app.post('/settings/toggle-notifs') +@require_login +def toggle_notifs(user=None): + """Toggles DM notifications for a given account. + + Args: + user (models.User) + """ + if user.send_notifs == 'all': + user.send_notifs = 'none' + verb = 'disabled' + else: + user.send_notifs = 'all' + verb = 'enabled' + + user.put() + + flash(f'DM notifications {verb} for {user.handle_or_id()}.') + return redirect('/settings', code=302) + + @app.get(f'//') # WARNING: this overrides the /ap/... actor URL route in activitypub.py, *only* # for handles with leading @ character. be careful when changing this route! diff --git a/templates/settings.html b/templates/settings.html index 4d66b776..7c1f57c2 100644 --- a/templates/settings.html +++ b/templates/settings.html @@ -17,7 +17,7 @@
- {% if user.enabled_protocols and not user.status %} + {% if user.enabled_protocols and not user.status %} {# Bridging: on #} Bridging: @@ -46,7 +46,27 @@

{% endif %} - {% else %} + + + {% else %} {# Bridging: off #} Bridging:
diff --git a/tests/test_pages.py b/tests/test_pages.py index 7c1bf7b2..22e5d5a7 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -912,6 +912,36 @@ class PagesTest(TestCase): get_flashed_messages()) self.assertEqual('yoozer', OtherFake.usernames['http://b.c/a']) + def test_toggle_notifs(self): + user = self.make_user('http://b.c/a', cls=ActivityPub, send_notifs='none', + obj_as2={'id': 'http://b.c/a', 'preferredUsername': 'a'}) + auth = MastodonAuth(id='@a@b.c', access_token_str='', + user_json='{"uri":"http://b.c/a"}').put() + + with self.client.session_transaction() as sess: + sess[LOGINS_SESSION_KEY] = [('MastodonAuth', '@a@b.c')] + + # toggle on + resp = self.client.post('/settings/toggle-notifs', data={ + 'key': user.key.urlsafe().decode(), + }) + self.assertEqual(302, resp.status_code) + self.assertEqual('/settings', resp.headers['Location']) + self.assertEqual(['DM notifications enabled for @a@b.c.'], + get_flashed_messages()) + self.assertEqual('all', user.key.get().send_notifs) + + # toggle off + resp = self.client.post('/settings/toggle-notifs', data={ + 'key': user.key.urlsafe().decode(), + }) + self.assertEqual(302, resp.status_code) + self.assertEqual('/settings', resp.headers['Location']) + self.assertEqual(['DM notifications enabled for @a@b.c.', + 'DM notifications disabled for @a@b.c.'], + get_flashed_messages()) + self.assertEqual('none', user.key.get().send_notifs) + def test_memcache_evict(self): # key = Fake(id='fake:foo').put() self.user.key.get()