Style improvements

This commit is contained in:
Anthony Wang 2023-01-18 20:00:54 +00:00
parent f36b56e1cf
commit 183267030d
No known key found for this signature in database
GPG key ID: 42A5B952E6DD8D38

View file

@ -12,33 +12,36 @@ from urllib.parse import quote_plus
domain = 'https://0.exozy.me' domain = 'https://0.exozy.me'
def collection_append(file, item): def collection_append(username, file, item):
with open(file) as f: with open(f'users/{username}.{file}') as f:
collection = load(f) collection = load(f)
collection['orderedItems'].append(item) collection['orderedItems'].append(item)
collection['totalItems'] += 1 collection['totalItems'] += 1
with open(file, 'w') as f: with open(f'users/{username}.{file}', 'w') as f:
dump(collection, f) dump(collection, f)
def collection_pop(file, item): def collection_pop(username, file, item):
with open(file) as f: with open(f'users/{username}.{file}') as f:
collection = load(f) collection = load(f)
collection['orderedItems'].pop(item) collection['orderedItems'].pop(item)
collection['totalItems'] -= 1 collection['totalItems'] -= 1
with open(file, 'w') as f: with open(f'users/{username}.{file}', 'w') as f:
dump(collection, f) dump(collection, f)
def iri_to_actor(iri): def iri_to_actor(iri):
if domain in 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}' actorfile = f'users/{name}'
else: else:
actorfile = f'users/{quote_plus(iri.removesuffix("#main-key"))}' actorfile = f'users/{quote_plus(iri.removesuffix("#main-key"))}'
if not isfile(actorfile): if not isfile(actorfile):
with open(actorfile, 'w') as f: with open(actorfile, 'w') as f:
resp = get(iri, headers={'Accept': 'application/activity+json'}) resp = get(iri, headers={'Accept': 'application/activity+json'})
print(resp)
print(resp.text)
f.write(resp.text) f.write(resp.text)
with open(actorfile) as f: with open(actorfile) as f:
return load(f) return load(f)
@ -79,7 +82,8 @@ class fuwuqi(SimpleHTTPRequestHandler):
message += f'{header}: {headerval}\n' message += f'{header}: {headerval}\n'
# Verify HTTP signature # Verify HTTP signature
signature = search('signature="(.*?)"', self.headers['Signature']).group(1) signature = search('signature="(.*?)"',
self.headers['Signature']).group(1)
pubkey.verify( pubkey.verify(
b64decode(signature), b64decode(signature),
message[:-1].encode('utf8'), message[:-1].encode('utf8'),
@ -118,20 +122,20 @@ class fuwuqi(SimpleHTTPRequestHandler):
dump(activity['object'], f) dump(activity['object'], f)
elif activity['type'] == 'Accept': elif activity['type'] == 'Accept':
# Accept follow request # Accept follow request
collection_append(f'users/{username}.followers', activity['object']['actor']) collection_append(username, 'followers', activity['object']['actor'])
elif activity['type'] == 'Follow': elif activity['type'] == 'Follow':
# Follow request # Follow request
collection_append(f'users/{username}.following', activity['object']) collection_append(username, 'following', activity['object'])
elif activity['type'] == 'Like': elif activity['type'] == 'Like':
# Like post # Like post
collection_append(f'users/{username}.liked', activity['object']) collection_append(username, 'liked', activity['object'])
elif activity['type'] == 'Undo': elif activity['type'] == 'Undo':
if activity['object']['type'] == 'Follow': if activity['object']['type'] == 'Follow':
# Unfollow request # Unfollow request
collection_remove(f'users/{username}.following', activity['object']['object']) collection_remove(username, 'following', activity['object']['object'])
elif activity['object']['type'] == 'Like': elif activity['object']['type'] == 'Like':
# Unlike post # Unlike post
collection_remove(f'users/{username}.liked', activity['object']['object']) collection_remove(username, 'liked', activity['object']['object'])
self.send_response(200) self.send_response(200)
self.end_headers() self.end_headers()