Skip to content

Commit

Permalink
add better attachment handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ntietje1 committed Aug 8, 2024
1 parent 0d253bf commit f4a59e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
10 changes: 5 additions & 5 deletions services/message/src/api/post_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ async def create_post(
content=content
)

post_id = await post.create_post(request.current_user_email, request.chat_id, request.content, files)
return {'Status': 'SUCCESS', "post_id": post_id}
post_id, attachment_ids = await post.create_post(request.current_user_email, request.chat_id, request.content, files)
return {'Status': 'SUCCESS', "post_id": post_id, "attachment_ids": attachment_ids}

class EditPostRequest(BaseModel):
current_user_email: str
Expand All @@ -47,9 +47,9 @@ def delete_post(post_id: int, request: DeletePostRequest):
post.delete_post(request.current_user_email, post_id)
return {'Status': 'SUCCESS'}

@router.delete("/{post_id}/attachment/{attachment_url}", response_model=dict, tags=['Public'])
def remove_attachment(post_id: int, attachment_url: int, request: DeletePostRequest):
post.remove_attachment(request.current_user_email, post_id, attachment_url)
@router.delete("/{post_id}/attachment/{attachment_id}", response_model=dict, tags=['Public'])
def remove_attachment(post_id: int, attachment_id: int, request: DeletePostRequest):
post.remove_attachment(request.current_user_email, post_id, attachment_id)
return {'Status': 'SUCCESS'}

# @router.post("/send")
Expand Down
21 changes: 15 additions & 6 deletions services/message/src/core/post.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import List
from typing import List, Tuple
from fastapi import File, UploadFile
import requests
from core.chat.shared import check_chat_edit_perm, get_org_id
Expand Down Expand Up @@ -62,8 +62,11 @@ async def process_files(files: List[UploadFile]):
return distributions


async def create_post(current_user_email: str, chat_id: int, content: str, files: List[UploadFile] = File(...)) -> int:
async def create_post(
current_user_email: str, chat_id: int, content: str, files: List[UploadFile] = File(...)
) -> Tuple[int, List[int]]:
post_id = None
attachment_ids = []
with get_cursor() as cursor:
if not check_chat_edit_perm(current_user_email, chat_id):
raise UserError("User does not have permission to create posts in this chat")
Expand All @@ -83,6 +86,7 @@ async def create_post(current_user_email: str, chat_id: int, content: str, files
raise ServerError("Failed to create post")

if post_id:

distributions = await process_files(files)
with get_cursor() as cursor:
for url in distributions:
Expand All @@ -92,7 +96,12 @@ async def create_post(current_user_email: str, chat_id: int, content: str, files
INSERT INTO attachments (post_id, url)
VALUES (%s, %s)
""", (post_id, url))
return post_id
if not cursor.rowcount == 1:
raise ServerError("Failed to add attachment")
attachment_id = cursor.fetchone()[0]
attachment_ids.append(attachment_id)

return (post_id, attachment_ids)


def edit_post(current_user_email: str, post_id: int, content: str):
Expand Down Expand Up @@ -128,16 +137,16 @@ def delete_post(current_user_email: str, post_id: int):
if not cursor.rowcount == 1:
raise ServerError("Failed to delete post")

def remove_attachment(current_user_email: str, post_id: int, url: str):
def remove_attachment(current_user_email: str, post_id: int, attachment_id: int):
with get_cursor() as cursor:
if not check_post_edit_perm(current_user_email, post_id):
raise UserError("User does not have permission to remove attachments from this post")

cursor.execute(
"""
DELETE FROM attachments
WHERE post_id = %s AND url = %s
""", (post_id, url)
WHERE post_id = %s AND id = %s
""", (post_id, attachment_id)
)

if not cursor.rowcount == 1:
Expand Down

0 comments on commit f4a59e7

Please sign in to comment.