async def inbox_post(request, user): # TODO implement shared inbox # TODO https://www.w3.org/TR/activitypub/#inbox-forwarding activity = request.json.copy() # TODO The receiver must verify the notification by fetching its source from the origin server. verified = await verify_request(request) if not verified: if getattr(request.app.config, 'DEBUG_INBOX', False): logger.info("signature incorrect") else: return response.json({"error": "signature incorrect"}, status=401) # TODO skip blocked # if Outbox.find_one( # { # "activity.type": "Block", # "user_id": user_id, # "meta.undo": False, # }): # return response.json({"zrada": "actor is blocked"}, status=403) if activity["type"] == "Follow": saved = await Inbox.save(user, activity) if saved: deliverance = {"type": "Accept", "object": activity} deliverance = Activity(user, deliverance) await deliverance.save() asyncio.ensure_future( deliver(user.key, deliverance.render, [activity["actor"]])) elif activity["type"] in ["Announce", "Like", "Create"]: # TODO validate if local object of reaction exists in outbox saved = await Inbox.save(user, activity) local_user = check_origin(activity["object"], user.uri) if saved: if local_user: await user.forward_to_followers(activity) elif activity["type"] in ["Announce", "Like"]: if not check_origin(activity["object"], request.app.base_url): await ensure_cached(activity['object']) elif activity["type"] == "Undo": deleted = await Inbox.delete(activity["object"]["id"]) undo_obj = activity["object"].get("object", "") if undo_obj.startswith(user.uri) and deleted: if activity["object"]["type"] == "Follow": await Outbox.delete(activity["object"]["id"]) elif activity["object"]["type"] in ["Announce", "Like"]: await user.forward_to_followers(activity) elif activity["type"] == "Delete": await Inbox.delete(activity["object"]["id"]) # TODO handle(forward) delete of reply to local user post else: await Inbox.save(user, activity) return response.json({'Created': 'success'}, status=202)
async def inbox_post(request, user): # TODO implement shared inbox # TODO https://www.w3.org/TR/activitypub/#inbox-forwarding activity = request.json.copy() verified = await verify_request( request.method, request.path, request.headers, request.body ) if not verified: if request.app.config.DEBUG: logger.info("signature incorrect") else: return response.json({"zrada": "signature incorrect"}, status=401) # TODO skip blocked # if Outbox.find_one( # { # "activity.type": "Block", # "user_id": user_id, # "meta.undo": False, # }): # return response.json({"zrada": "actor is blocked"}, status=403) if activity["type"] == "Follow": saved = await Inbox.save(user, activity) if saved: deliverance = { "type": "Accept", "object": activity } deliverance = Activity(user, deliverance) await deliverance.save() asyncio.ensure_future(deliver(user.key, deliverance.render, [activity["actor"]])) elif activity["type"] in ["Announce", "Like", "Create"]: # TODO validate if local object of reaction exists in outbox saved = await Inbox.save(user, activity) local = check_origin(activity["object"], user.uri) if local and saved: await user.forward_to_followers(activity) elif activity["type"] == "Undo": deleted = await Inbox.delete(activity["object"]["id"]) undo_obj = activity["object"].get("object", "") if undo_obj.startswith(user.uri) and deleted: if activity["object"]["type"] == "Follow": await Outbox.delete(activity["object"]["id"]) elif activity["object"]["type"] in ["Announce", "Like"]: await user.forward_to_followers(activity) elif activity["type"] == "Delete": await Inbox.delete(activity["object"]["id"]) # TODO handle(forward) delete of reply to local user post else: await Inbox.save(user, activity) return response.json({'peremoga': 'yep'}, status=202)
async def reaction_undo(cls, activity): reaction_type = activity.render['object']['type'] local = check_origin(activity.render["object"]["object"], activity.render["actor"]) search_model = cls if local else Inbox undo_from = await search_model.find_one( {"activity.object.id": activity.render['object']['object']} ) if undo_from: if undo_from.reactions and undo_from.reactions.get(reaction_type) \ and undo_from.reactions[reaction_type].get(activity.user.name): await cls.update_one( {'_id': undo_from.id}, {'$unset': {f"reactions.{reaction_type}.{activity.user.name}": 1}} ) else: raise SanicException(f'This post is not {reaction_type}d', status_code=409) await cls.delete(activity.render["object"]["id"])
async def save(self): local = check_origin(self.render["object"], self.render["actor"]) if not local: await ensure_cached(self.render['object']) await Outbox.reaction_add(self, local)
async def save(self): local = check_origin(self.render["object"], self.render["actor"]) await Outbox.reaction_add(self, local)