mirror of
https://git.exozy.me/a/fuwuqi.git
synced 2024-11-22 09:01:05 +00:00
Style improvements
This commit is contained in:
parent
f36b56e1cf
commit
183267030d
1 changed files with 20 additions and 16 deletions
34
server.py
34
server.py
|
@ -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,20 +122,20 @@ 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()
|
||||
|
|
Loading…
Reference in a new issue