From 115d85909a749b33c1f98ec19712f1fc7edc001b Mon Sep 17 00:00:00 2001 From: Ryan Barrett Date: Mon, 22 Apr 2024 20:21:56 -0700 Subject: [PATCH] abstract "update profile" button across protocols adds new /[protocol]/[id]/update-profile endpoint --- pages.py | 27 +++++++++++++++++++++++++++ templates/user_base.html | 11 ++++------- tests/test_pages.py | 21 +++++++++++++++++++++ 3 files changed, 52 insertions(+), 7 deletions(-) diff --git a/pages.py b/pages.py index bac5d18..49c27d5 100644 --- a/pages.py +++ b/pages.py @@ -19,6 +19,8 @@ from oauth_dropins.webutil.flask_util import ( flash, redirect, ) +import requests +import werkzeug.exceptions import common from common import DOMAIN_RE @@ -27,6 +29,7 @@ import ids from models import fetch_objects, fetch_page, Follower, Object, PAGE_SIZE, PROTOCOLS from protocol import Protocol + # precompute this because we get a ton of requests for non-existing users # from weird open redirect referrers: # https://github.com/snarfed/bridgy-fed/issues/422 @@ -160,6 +163,30 @@ def notifications(protocol, id): return render_template('notifications.html', **TEMPLATE_VARS, **locals()) +@app.post(f'///update-profile') +@canonicalize_request_domain(common.PROTOCOL_DOMAINS, common.PRIMARY_DOMAIN) +def update_profile(protocol, id): + user = load_user(protocol, id) + + try: + profile_obj = user.load(user.profile_id(), remote=True) + if profile_obj: + msg, status = user.receive(profile_obj) + else: + status = 400 + msg = "couldn't fetch profile" + + except (requests.RequestException, werkzeug.exceptions.HTTPException) as e: + status, msg = util.interpret_http_exception(e) + + if int(status) // 100 == 2: + flash(f'Updating profile for {user.handle_or_id()}') + else: + flash(f"Couldn't update profile for {user.handle_or_id()}: {msg}") + + return redirect(user.user_page_path(), code=302) + + @app.get(f'///') @canonicalize_request_domain(common.PROTOCOL_DOMAINS, common.PRIMARY_DOMAIN) def followers_or_following(protocol, id, collection): diff --git a/templates/user_base.html b/templates/user_base.html index 9737beb..244d610 100644 --- a/templates/user_base.html +++ b/templates/user_base.html @@ -47,13 +47,10 @@ {{ user.handle_or_id() }} - {% if user.LABEL == 'web' %} -
- - -
- {% endif %} +
+ +
diff --git a/tests/test_pages.py b/tests/test_pages.py index 0596ef0..b85c32e 100644 --- a/tests/test_pages.py +++ b/tests/test_pages.py @@ -168,6 +168,27 @@ class PagesTest(TestCase): got = self.client.get('/web/user.com?before=2024-01-01+01:01:01&after=2023-01-01+01:01:01') self.assert_equals(400, got.status_code) + def test_update_profile(self): + self.make_user('fake:user', cls=Fake) + + actor = { + 'objectType': 'person', + 'id': 'fake:user', + 'displayName': 'Ms User', + } + Fake.fetchable = {'fake:user': actor} + got = self.client.post('/fa/fake:user/update-profile') + self.assert_equals(302, got.status_code) + self.assert_equals('/fa/fake:handle:user', got.headers['Location']) + self.assertEqual(['Updating profile for fake:handle:user'], + get_flashed_messages()) + + self.assertEqual(['fake:user'], Fake.fetched) + self.assert_object('fake:user', source_protocol='fake', our_as1={ + **actor, + 'updated': '2022-01-02T03:04:05+00:00', + }) + def test_followers(self): Follower.get_or_create( to=self.user,