kopia lustrzana https://github.com/snarfed/bridgy-fed
abstract "update profile" button across protocols
adds new /[protocol]/[id]/update-profile endpointpull/968/head
rodzic
03b0f54cfe
commit
115d85909a
27
pages.py
27
pages.py
|
@ -19,6 +19,8 @@ from oauth_dropins.webutil.flask_util import (
|
||||||
flash,
|
flash,
|
||||||
redirect,
|
redirect,
|
||||||
)
|
)
|
||||||
|
import requests
|
||||||
|
import werkzeug.exceptions
|
||||||
|
|
||||||
import common
|
import common
|
||||||
from common import DOMAIN_RE
|
from common import DOMAIN_RE
|
||||||
|
@ -27,6 +29,7 @@ import ids
|
||||||
from models import fetch_objects, fetch_page, Follower, Object, PAGE_SIZE, PROTOCOLS
|
from models import fetch_objects, fetch_page, Follower, Object, PAGE_SIZE, PROTOCOLS
|
||||||
from protocol import Protocol
|
from protocol import Protocol
|
||||||
|
|
||||||
|
|
||||||
# precompute this because we get a ton of requests for non-existing users
|
# precompute this because we get a ton of requests for non-existing users
|
||||||
# from weird open redirect referrers:
|
# from weird open redirect referrers:
|
||||||
# https://github.com/snarfed/bridgy-fed/issues/422
|
# https://github.com/snarfed/bridgy-fed/issues/422
|
||||||
|
@ -160,6 +163,30 @@ def notifications(protocol, id):
|
||||||
return render_template('notifications.html', **TEMPLATE_VARS, **locals())
|
return render_template('notifications.html', **TEMPLATE_VARS, **locals())
|
||||||
|
|
||||||
|
|
||||||
|
@app.post(f'/<any({",".join(PROTOCOLS)}):protocol>/<id>/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'/<any({",".join(PROTOCOLS)}):protocol>/<id>/<any(followers,following):collection>')
|
@app.get(f'/<any({",".join(PROTOCOLS)}):protocol>/<id>/<any(followers,following):collection>')
|
||||||
@canonicalize_request_domain(common.PROTOCOL_DOMAINS, common.PRIMARY_DOMAIN)
|
@canonicalize_request_domain(common.PROTOCOL_DOMAINS, common.PRIMARY_DOMAIN)
|
||||||
def followers_or_following(protocol, id, collection):
|
def followers_or_following(protocol, id, collection):
|
||||||
|
|
|
@ -47,13 +47,10 @@
|
||||||
{{ user.handle_or_id() }}
|
{{ user.handle_or_id() }}
|
||||||
</a>
|
</a>
|
||||||
|
|
||||||
{% if user.LABEL == 'web' %}
|
<form method="post" action="{{ user.user_page_path('update-profile') }}">
|
||||||
<form method="post" action="/webmention-interactive">
|
<button id="update-profile-button" type="submit" title="Update profile"
|
||||||
<input name="source" type="hidden" value="{{ user.web_url() }}" />
|
class="btn btn-default glyphicon glyphicon-refresh"></button>
|
||||||
<button id="update-profile-button" type="submit" title="Update profile"
|
</form>
|
||||||
class="btn btn-default glyphicon glyphicon-refresh"></button>
|
|
||||||
</form>
|
|
||||||
{% endif %}
|
|
||||||
</nobr>
|
</nobr>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
|
|
@ -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')
|
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)
|
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):
|
def test_followers(self):
|
||||||
Follower.get_or_create(
|
Follower.get_or_create(
|
||||||
to=self.user,
|
to=self.user,
|
||||||
|
|
Ładowanie…
Reference in New Issue