async def download_media( chat_id: int, message_id: int, current_user: UserResponse = Depends(get_current_verified_user), pool: Pool = Depends(get_pool), db: Session = Depends(get_db), ): """Download a media file. Not all messages have a media file attached to it.""" chat = crud.get_chat(db, current_user.user_id, chat_id) if not chat: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"The chat with id {chat_id} does not exist.", ) connector_id = chat.contacts[0].contact.connector_id connector = crud.get_connector(db, current_user.user_id, connector_id) message = db.query(models.Message).filter( models.Message.message_id == message_id).first() filename, mime, data = await pool.get_bytes( f"{connector.connector_type}/{connector_id}/{uuid.uuid4()}/chats/{chat.internal_id}/messages/{message.internal_id}/get_media", {}) if data: return Response(data, media_type=mime) raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"File {filename} does not exist.", )
async def get_messages( chat_id: int, pagination: Pagination = Depends(get_pagination), current_user: UserResponse = Depends(get_current_verified_user), db: Session = Depends(get_db), ): """Get a list of all messages for a specific chat.""" chat = crud.get_chat(db, current_user.user_id, chat_id) if not chat: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"The chat with id {chat_id} does not exist.", ) messages = (db.query( models.Message).filter(models.Message.chat_id == chat_id).order_by( desc_op(models.Message.sent_datetime)).limit( pagination.count).offset(pagination.page * pagination.count).all()) if not messages: return [] return [ MessageResponse( message_id=message.message_id, contact_id=message.contact_id, sent_datetime=message.sent_datetime, message=prepare_message(chat.chat_id, message.message_id, message.message), ) for message in messages ]
async def unpin_chat( chat_id: int, current_user: UserResponse = Depends(get_current_verified_user), db: Session = Depends(get_db), ): """Unpin a chat.""" chat: Chat = crud.get_chat(db, current_user.user_id, chat_id) if not chat: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"The chat with id {chat_id} does not exist.", ) chat.pin_position = None db.commit()
async def send_message( chat_id: int, message: Message, pool: Pool = Depends(get_pool), current_user: UserResponse = Depends(get_current_verified_user), db: Session = Depends(get_db), ): """Send a message to a chat.""" chat = crud.get_chat(db, current_user.user_id, chat_id) if not chat: raise HTTPException( status_code=status.HTTP_404_NOT_FOUND, detail=f"The chat with id {chat_id} does not exist.", ) connector_id = chat.contacts[0].contact.connector_id connector = crud.get_connector(db, current_user.user_id, connector_id) if connector.connector_type == "DEMO": new_message = models.Message( chat_id=chat_id, contact_id=connector.connector_user_id, message=json.dumps(message.message.__dict__), sent_datetime=datetime.now(), ) db.add(new_message) # add is ok here db.commit() else: sent = await pool.get( f"{connector.connector_type}/{connector.connector_id}/{str(uuid.uuid4())}/send_message", { "chat_id": chat.internal_id, "message": message.message.__dict__ }, ) # This should never be null contact = db.query(models.Contact).filter( models.Contact.connector_id == connector_id, models.Contact.is_self).first() new_message = models.Message( message_id=crud.get_message_id(connector.connector_id, sent.get("message_id"), chat_id), internal_id=sent.get("message_id"), chat_id=chat_id, contact_id=contact.contact_id, message=json.dumps(message.message.__dict__), sent_datetime=datetime.now(), ) try: db.merge(new_message) db.commit() except sqlalchemy.exc.IntegrityError: # happens when the service sends the message already back to the server via push api pass return MessageResponse( message_id=new_message.message_id, contact_id=new_message.contact_id, sent_datetime=new_message.sent_datetime, message=message.message.__dict__, )