diff options
author | Anthony Wang | 2023-01-18 20:00:54 +0000 |
---|---|---|
committer | Anthony Wang | 2023-01-18 20:00:54 +0000 |
commit | 183267030d756936ae90e8b6cc646a378fe79ff1 (patch) | |
tree | f22c57424884eeb26b20247166a17643884874f7 | |
parent | f36b56e1cfe7ccddec12cb2f12d0b38d1d03a939 (diff) |
Style improvements
-rw-r--r-- | server.py | 36 |
1 files changed, 20 insertions, 16 deletions
@@ -12,33 +12,36 @@ from urllib.parse import quote_plus domain = 'https://0.exozy.me' -def collection_append(file, item): - with open(file) as f: +def collection_append(username, file, item): + with open(f'users/{username}.{file}') as f: collection = load(f) collection['orderedItems'].append(item) collection['totalItems'] += 1 - with open(file, 'w') as f: - dump(collection, f) + with open(f'users/{username}.{file}', 'w') as f: + dump(collection, f) -def collection_pop(file, item): - with open(file) as f: +def collection_pop(username, file, item): + with open(f'users/{username}.{file}') as f: collection = load(f) collection['orderedItems'].pop(item) collection['totalItems'] -= 1 - with open(file, 'w') as f: - dump(collection, f) + with open(f'users/{username}.{file}', 'w') as f: + dump(collection, f) def iri_to_actor(iri): if domain in iri: - name = search(f'^{domain}/users/(.*?)$', iri.removesuffix('#main-key')).group(1) + name = search(f'^{domain}/users/(.*?)$', + iri.removesuffix('#main-key')).group(1) actorfile = f'users/{name}' else: actorfile = f'users/{quote_plus(iri.removesuffix("#main-key"))}' if not isfile(actorfile): with open(actorfile, 'w') as f: resp = get(iri, headers={'Accept': 'application/activity+json'}) + print(resp) + print(resp.text) f.write(resp.text) with open(actorfile) as f: return load(f) @@ -79,7 +82,8 @@ class fuwuqi(SimpleHTTPRequestHandler): message += f'{header}: {headerval}\n' # Verify HTTP signature - signature = search('signature="(.*?)"', self.headers['Signature']).group(1) + signature = search('signature="(.*?)"', + self.headers['Signature']).group(1) pubkey.verify( b64decode(signature), message[:-1].encode('utf8'), @@ -118,21 +122,21 @@ class fuwuqi(SimpleHTTPRequestHandler): dump(activity['object'], f) elif activity['type'] == 'Accept': # Accept follow request - collection_append(f'users/{username}.followers', activity['object']['actor']) + collection_append(username, 'followers', activity['object']['actor']) elif activity['type'] == 'Follow': # Follow request - collection_append(f'users/{username}.following', activity['object']) + collection_append(username, 'following', activity['object']) elif activity['type'] == 'Like': # Like post - collection_append(f'users/{username}.liked', activity['object']) + collection_append(username, 'liked', activity['object']) elif activity['type'] == 'Undo': if activity['object']['type'] == 'Follow': # Unfollow request - collection_remove(f'users/{username}.following', activity['object']['object']) + collection_remove(username, 'following', activity['object']['object']) elif activity['object']['type'] == 'Like': # Unlike post - collection_remove(f'users/{username}.liked', activity['object']['object']) - + collection_remove(username, 'liked', activity['object']['object']) + self.send_response(200) self.end_headers() |