async def get(self): # TODO(iceboy): projection, pagination. mdocs = await message.get_multi(self.user['_id']).sort([('_id', -1)]).to_list(50) udict = await user.get_dict( itertools.chain.from_iterable((mdoc['sender_uid'], mdoc['sendee_uid']) for mdoc in mdocs), fields=user.PROJECTION_PUBLIC) # TODO(twd2): improve here: for mdoc in mdocs: sender_udoc, sendee_udoc = udict[mdoc['sender_uid']], udict[mdoc['sendee_uid']] if 'gravatar' in sender_udoc: sender_udoc['gravatar_url'] = template.gravatar_url(sender_udoc.pop('gravatar')) if 'gravatar' in sendee_udoc: sendee_udoc['gravatar_url'] = template.gravatar_url(sendee_udoc.pop('gravatar')) self.json_or_render('home_messages.html', messages=mdocs, udict=udict)
def modify_udoc(self, udict, key): udoc = udict.get(key) if not udoc: return gravatar_url = template.gravatar_url(udoc.get('gravatar')) if 'gravatar' in udoc and udoc['gravatar']: udict[key] = {**udoc, 'gravatar_url': gravatar_url, 'gravatar': ''}
async def post_send_message(self, *, uid: int, content: str): udoc = await user.get_by_uid(uid, user.PROJECTION_PUBLIC) if not udoc: raise error.UserNotFoundError(uid) mdoc = await message.add(self.user['_id'], udoc['_id'], content) # projection mdoc['sender_udoc'] = await user.get_by_uid(self.user['_id'], user.PROJECTION_PUBLIC) # TODO(twd2): improve here: mdoc['sender_udoc']['gravatar_url'] = ( template.gravatar_url(mdoc['sender_udoc'].pop('gravatar'))) mdoc['sendee_udoc'] = udoc # TODO(twd2): improve here: mdoc['sendee_udoc']['gravatar_url'] = ( template.gravatar_url(mdoc['sendee_udoc'].pop('gravatar'))) if self.user['_id'] != uid: await bus.publish('message_received-' + str(uid), {'type': 'new', 'data': mdoc}) self.json_or_redirect(self.url, mdoc=mdoc)
async def get(self, *, q: str): udocs = await user.get_prefix_list(q, user.PROJECTION_PUBLIC, 20) try: udoc = await user.get_by_uid(int(q), user.PROJECTION_PUBLIC) if udoc: udocs.insert(0, udoc) except ValueError as e: pass for udoc in udocs: if 'gravatar' in udoc: udoc['gravatar_url'] = template.gravatar_url( udoc.pop('gravatar')) self.json(udocs)