async def save(cls, user, activity): exists = await cls.find_one({"activity.id": activity["id"]}) if exists: if user.name in exists['users']: return False else: users = exists['users'] users.append(user.name) await cls.update_one( {'_id': exists.id}, {'$set': { "users": users, "updated": datetime.now() }}) await cls.cache.clear() else: # TODO validate actor and activity pg_id = random_object_id() # TODO how to get single object from inbox # activity = copy.deepcopy(activity) # activity["pg_id"] = pg_id await cls.insert_one({ "_id": pg_id, "users": [user.name], "activity": activity, "label": make_label(activity), "deleted": False, "first_user": user.name, "created": datetime.now() }) await cls.cache.clear() return True
async def save(self): await Outbox.insert_one({ "_id": self.id, "user_id": self.user.name, "activity": self.render, "label": make_label(self.render), "meta": {"undo": False, "deleted": False}, })
async def save(cls, activity, **kwargs): db_obj = { "_id": activity.id, "user_id": activity.user.name, "activity": activity.render, "label": make_label(activity.render), "deleted": False, } db_obj.update(kwargs) await Outbox.insert_one(db_obj)
async def inbox_post(request, user): 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) exists = await Inbox.find_one(dict(_id=activity["id"])) if exists: if user.name in exists['users']: return response.json({"zrada": "activity already delivered"}, status=403) else: users = exists['users'] users.append(user.name) await Inbox.update_one({'_id': exists.id}, {'$set': { "users": users }}) else: # TODO validate actor and activity await Inbox.insert_one({ "_id": activity["id"], "users": [user.name], "activity": activity, "label": make_label(activity), "meta": { "undo": False, "deleted": False }, }) if activity["type"] == "Follow": deliverance = { "type": "Accept", "actor": activity["object"], "object": { "type": "Follow", "id": activity["id"], "actor": activity["actor"], "object": activity["object"] } } deliverance = Activity(user, deliverance) await Outbox.insert_one({ "_id": deliverance.id, "user_id": user.name, "activity": deliverance.render, "label": make_label(deliverance.render), "meta": { "undo": False, "deleted": False }, }) # post_to_remote_inbox asyncio.ensure_future( deliver(user.key, deliverance.render, [activity["actor"]])) #forward reactions #TODO validate if activity exists in outbox elif (activity["type"] in ["Announce", "Like"] and activity["object"].startswith(user.uri)) or \ (activity["type"] == "Create" and activity["object"]["inReplyTo"] and activity["object"]["inReplyTo"].startswith(user.uri)): recipients = await user.followers_get() try: recipients.remove(activity["actor"]) except ValueError: pass # activity = await fetch(activity["id"]) asyncio.ensure_future(deliver(user.key, activity, recipients)) return response.json({'peremoga': 'yep'}, status=202)