def on_post(self, req, resp): if req.get_param('media_ids'): user = req.context['user'] status = Status( caption=req.get_param('status') or '', visibility=bool(req.get_param('visibility')), #False if None user=user, sensitive=bool(req.get_param('sensitive')), remote=False, story=bool(req.get_param('is_story')) ) if status.sensitive: status.spoliet_text=req.get_param('spoiler_text') status.save() if req.get_param('media_ids') != None: for image in req.get_param('media_ids').split(','): m = Media.get_or_none(media_name=image) m.status = status m.save() #Increment the number of posts uploaded UserProfile.update({UserProfile.statuses_count: UserProfile.statuses_count + 1}).where(UserProfile.id == user.id).execute() spread_status(status) resp.status = falcon.HTTP_200 resp.body = json.dumps(status.to_json(),default=str) elif req.get_param('in_reply_to_id'): replying_to = Status.get_or_none(id=req.get_param('in_reply_to_id')) if replying_to: status = Status( caption = req.get_param('status'), user = user, remote = False, story = False, in_reply_to = replying_to, sensitive = replying_to.sensitive, spoiler_text = req.get_param('spoiler_text') or replying_to.spoiler_text ) else: resp.status = falcon.HTTP_500 resp.body = json.dumps({"Error": "Replying to bad ID"}) else: resp.status = falcon.HTTP_500 resp.body = json.dumps({"Error": "No photo attached"})
def remove_status(status): r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost')) # Remove it from the own timeline TimelineManager(status.user).remove_from_home(status) # Remove it from the followers timeline for follower in status.user.followers(): TimelineManager(follower).remove_from_home(status) #Update the user posts count UserProfile.update({ UserProfile.statuses_count: UserProfile.statuses_count - 1 }).where(UserProfile.id == status.user.id).execute() # Remove the status status.delete_instance(recursive=True)
def create(self, user, caption, sensitive, public, spoiler=None, remote=False, story=False, media_ids): """ user: UserProfile - User creating the status caption: str - Image caption sensitive: bool - Content has something sensitive. spoiler: str - Content spoiler text public: bool - True if yes media_ids: list - A list with the media related to the status Returns the created status """ status = Status.create(caption=caption, is_public=public, user=user, sensitive=sensitive, spoiler_text=spoiler, remote=remote, is_story=story) for image in req.get_param('media_ids').split(','): m = Media.get_or_none(media_name=image) m.status = status m.save() UserProfile.update({ UserProfile.statuses_count: UserProfile.statuses_count + 1 }).where(UserProfile.id == status.user.id).execute() spread_status(status)
async def post(self, user): if self.get_argument('media_ids', False): data = { "caption": self.get_argument('status', ''), "visibility": bool(self.get_argument('visibility', False)), "user": user, "sensitive": bool(self.get_argument('sensitive', False)), "remote": False, "sotory": bool(self.get_argument('story', False)) } if data['sensitive']: data['spoliet_text'] = self.get_argument('spoiler_text', '') status = await self.application.objects.create(Status, **data) for image in self.get_argument('media_ids').split(","): try: m = await self.application.objects.get(Media, media_name=image) m.status = status await self.application.objects.update(m) except Media.DoesNotExist: logger.error(f"Media id not found {image} for {status.id}") await self.application.objects.execute( UserProfile.update({ UserProfile.statuses_count: UserProfile.statuses_count + 1 }).where(UserProfile.id == user.id)) spread_status(status) self.write(json.dumps(status.to_json(), default=str)) elif self.get_argument('in_reply_to_id', False): try: replying_to = await self.application.objects.get( Status, id=int(self.get_argument('in_reply_to_id'))) except Status.DoesNotExist: self.set_status(500) self.write({"Error": "Replying to bad ID"}) else: self.set_status(500) self.write({"Error": "No media attached nor in reply to"})
async def post(self, *args, **kwargs): """ Handle creation of statuses """ user = self.current_user print(user.username) hashids = Hashids(salt=salt_code, min_length=9) if self.kwargs.get('media_ids', False): data = { "caption": self.get_argument('status', ''), "visibility": bool(self.get_argument('visibility', False)), "user": user, "sensitive": bool(self.get_argument('sensitive', False)), "remote": False, "sotory": bool(self.get_argument('story', False)), "identifier": hashids.encode( int( str(user.id) + str(int(datetime.datetime.now().timestamp()))) ) # TODO: We're losing time here } if data['sensitive']: data['spoliet_text'] = self.get_argument('spoiler_text', '') par = Parser(domain=BASE_URL) parsed = par.parse(html.escape(data["caption"])) mentions = parsed.users data['caption'] = parsed.html status = await self.application.objects.create(Status, **data) imgs = self.kwargs.get('media_ids') task = atach_media_to_status.s(status, imgs[0]) for image in imgs[1:]: task = self._then(task, atach_media_to_status, status, image) await self.application.objects.execute( UserProfile.update({ UserProfile.statuses_count: UserProfile.statuses_count + 1 }).where(UserProfile.id == user.id)) task = self._then(task, spread_status, status, mentions, True) huey.enqueue(task) self.write(json.dumps(status.to_json(), default=str)) elif self.get_argument('in_reply_to_id', False): try: replying_to = await self.application.objects.get( Status, identifier=self.get_argument('in_reply_to_id')) data = { "caption": self.get_argument('status', ''), "visibility": bool(self.get_argument('visibility', False)), "user": user, "sensitive": bool(self.get_argument('sensitive', False)), "remote": False, "identifier": hashids.encode( int( str(user.id) + str(int(datetime.datetime.now().timestamp())))), "in_reply_to": replying_to } status = await self.application.objects.create(Status, **data) self.write(json.dumps(status.to_json(), default=str)) except Status.DoesNotExist: raise CustomError(reason="Replying to bad ID", status_code=400) else: raise CustomError(reason="No media attached nor in reply to", status_code=400)
async def post(self, user, **kwargs): """ Handle creation of statuses """ hashids = Hashids(salt=salt_code, min_length=9) if self.kwargs('media_ids', False): data = { "caption": self.get_argument('status', ''), "visibility": bool(self.get_argument('visibility', False)), "user": user, "sensitive": bool(self.get_argument('sensitive', False)), "remote": False, "sotory": bool(self.get_argument('story', False)), "identifier": hashids.encode( int( str(user.id) + str(int(datetime.datetime.now().timestamp())))) } if data['sensitive']: data['spoliet_text'] = self.get_argument('spoiler_text', '') status = await self.application.objects.create(Status, **data) for image in self.kwargs('media_ids'): try: m = await self.application.objects.get(Media, media_name=image) m.status = status await self.application.objects.update(m) except Media.DoesNotExist: logger.error(f"Media id not found {image} for {status.id}") await self.application.objects.execute( UserProfile.update({ UserProfile.statuses_count: UserProfile.statuses_count + 1 }).where(UserProfile.id == user.id)) spread_status(status) self.write(json.dumps(status.to_json(), default=str)) elif self.get_argument('in_reply_to_id', False): try: replying_to = await self.application.objects.get( Status, identifier=self.get_argument('in_reply_to_id')) data = { "caption": self.get_argument('status', ''), "visibility": bool(self.get_argument('visibility', False)), "user": user, "sensitive": bool(self.get_argument('sensitive', False)), "remote": False, "identifier": hashids.encode( int( str(user.id) + str(int(datetime.datetime.now().timestamp())))), "in_reply_to": replying_to } status = await self.application.objects.create(Status, **data) self.write(json.dumps(status.to_json(), default=str)) except Status.DoesNotExist: self.set_status(500) self.write({"Error": "Replying to bad ID"}) else: self.set_status(500) self.write({"Error": "No media attached nor in reply to"})