def create_post(u, content, addresses): """ """ user_api_uri = API_URI + '/' + u['username'] post_number = str(u['metrics']['post_count']) id = user_api_uri + '/posts/' + post_number note_url = SERVER_URI + '/@' + u['username'] + '/' + post_number time = get_time() return vocab.Create( id + '/activity', actor=vocab.Person(u['@id'], preferredUsername=u['preferredUsername']), published=time, to=addresses['to'], bto=addresses['bto'], cc=addresses['cc'], bcc=addresses['bcc'], audience=addresses['audience'], object=vocab.Note( id, published=time, url=note_url, # summary=summary, # inReplyTo=inReplyTo, to=addresses['to'], bto=addresses['bto'], cc=addresses['cc'], bcc=addresses['bcc'], audience=addresses['audience'], attributedTo=vocab.Person( u['@id'], preferredUsername=u['preferredUsername']), # sensitive=sensitive, content=content), signature=dict(created=time, creator=u['@id'] + '?get=main-key', signatureValue=sign_object(u, content), type='rsa-sha256'))
def feed(handle): if request.method == 'GET': """ per AP spec, returns a reverse-chronological OrderedCollection of items in the outbox, pending privacy settings """ print('feed get') u = find_user(handle) items = list( mongo.db.posts.find({ 'object.attributedTo': u['@id'] }, { '_id': False }).sort('published', -1)) resp = vocab.OrderedCollection(u['outbox'], totalItems=len(items), orderedItems=items) return Response(json.dumps(resp.json()), headers=content_headers(u)) if request.method == 'POST': """ adds objects that it receives to mongodb and sends them along to appropriate Actor inboxes """ r = core.ASObj(request.get_json(), vocab.BasicEnv) u = find_user(handle) # if it's a note it turns it into a Create object if 'Note' in r.types: obj = r.get_json() r = vocab.Create(obj['@id'] + '/activity', actor=u['@id'], published=obj['published'], to=obj['to'], bto=obj['bto'], cc=obj['cc'], bcc=obj['bcc'], audience=obj['audience'], obj=obj) if 'Create' in r.types: mongo.db.users.update({'acct': u['acct']}, {'$inc': { 'metrics.post_count': 1 }}) elif 'Update' in r.types: return Response(status=501) elif 'Delete' in r.types: return Response(status=501) elif 'Follow' in r.types: return Response(status=501) elif 'Accept' in r.types: return Response(status=501) elif 'Reject' in r.types: return Response(status=501) elif 'Add' in r.types: return Response(status=501) elif 'Remove' in r.types: return Response(status=501) elif 'Like' in r.types: if u['acct'] not in mongo.db.posts.find( {'@id': r['object']['@id']})['likes']: try: mongo.db.posts.update({'@id': r['object']['@id']}, {'$push': { 'likes': u['acct'] }}) return Response(status=201) except: return Response(status=500) elif 'Announce' in r.types: return Response(status=501) elif 'Undo' in r.types: return Response(status=501) recipients = [] r = r.json() for group in ['to', 'bto', 'cc', 'bcc', 'audience']: addresses = r.get(group, []) recipients.extend(addresses) for address in addresses: requests.post(address, json=r, headers=content_headers(u)) try: mongo.db.posts.insert_one(r) return Response(status=201) except: return Response(status=500)
def feed_post(handle): """ adds objects that it receives to mongodb and sends them along to appropriate Actor inboxes """ r = core.ASObj(request.get_json(), vocab.BasicEnv) u = find_user(handle) # if it's a note it turns it into a Create object if 'Note' in r.types: obj = r.get_json() r = vocab.Create(obj['@id'] + '/activity', actor=u['@id'], published=obj['published'], to=obj['to'], bto=obj['bto'], cc=obj['cc'], bcc=obj['bcc'], audience=obj['audience'], obj=obj) if 'Create' in r.types: if r['object']['@type'] != 'Note': print(str(r)) print('not a note') return Response(status=403) mongo.db.users.update({'acct': u['acct']}, {'$inc': { 'metrics.post_count': 1 }}) elif 'Update' in r.types: return Response(status=501) elif 'Delete' in r.types: return Response(status=501) elif 'Follow' in r.types: return Response(status=501) elif 'Accept' in r.types: return Response(status=501) elif 'Reject' in r.types: return Response(status=501) elif 'Add' in r.types: return Response(status=501) elif 'Remove' in r.types: return Response(status=501) elif 'Like' in r.types: if u['acct'] not in mongo.db.posts.find({'@id': r['object']['@id']})['likes']: mongo.db.posts.update({'@id': r['object']['@id']}, {'$push': { 'likes': u['acct'] }}) elif 'Announce' in r.types: return Response(status=501) elif 'Undo' in r.types: return Response(status=501) recipients = [] r = r.json() for group in ['to', 'bto', 'cc', 'bcc', 'audience']: addresses = r.get(group, []) recipients.extend(addresses) for address in addresses: requests.post(address, json=r, headers=content_headers(u)) try: mongo.db.posts.insert_one(r) return Response(status=201) except: return Response(status=500)
"acct:[email protected]", "acct:[email protected]", "acct:[email protected]" ], "object": core.ASObj({ "@type": "Note", "@id": "http://tsyesika.co.uk/chat/sup-yo/", "content": "Up for some root beer floats?" }) } ROOT_BEER_NOTE_VOCAB = vocab.Create( "http://tsyesika.co.uk/act/foo-id-here/", actor=vocab.Person("http://tsyesika.co.uk/", displayName="Jessica Tallon"), to=[ "acct:[email protected]", "acct:[email protected]", "acct:[email protected]" ], object=vocab.Note("http://tsyesika.co.uk/chat/sup-yo/", content="Up for some root beer floats?")) def _looks_like_root_beer_note(jsobj): return (len(jsobj) == 5 and jsobj["@type"] == "Create" and jsobj["@id"] == "http://tsyesika.co.uk/act/foo-id-here/" and isinstance(jsobj["actor"], dict) and len(jsobj["actor"]) == 3 and jsobj["actor"]["@type"] == "Person" and jsobj["actor"]["@id"] == "http://tsyesika.co.uk/" and jsobj["actor"]["displayName"] == "Jessica Tallon" and isinstance(jsobj["to"], list) and jsobj["to"] == [ "acct:[email protected]", "acct:[email protected]",