diff --git a/backend/routes/quests.go b/backend/routes/quests.go index 2dddaa6c..bfdd65fa 100644 --- a/backend/routes/quests.go +++ b/backend/routes/quests.go @@ -126,7 +126,6 @@ func SendChat(w http.ResponseWriter, r *http.Request) { return } - // Parse request body var chat FactionChat err := json.NewDecoder(r.Body).Decode(&chat) if err != nil { @@ -135,7 +134,6 @@ func SendChat(w http.ResponseWriter, r *http.Request) { } defer r.Body.Close() - // Insert chat into database err = core.PostgresExec( r.Context(), "INSERT INTO FactionChats (sender, faction_key, message) VALUES ($1, $2, $3)", chat.Sender, chat.FactionKey, chat.Message) if err != nil { WriteErrorJson(w, http.StatusInternalServerError, "Failed to send chat") @@ -152,7 +150,6 @@ func DeleteChat(w http.ResponseWriter, r *http.Request) { return } - // Parse query params chatID := r.URL.Query().Get("id") if chatID == "" { WriteErrorJson(w, http.StatusBadRequest, "Missing chat ID parameter") @@ -161,7 +158,6 @@ func DeleteChat(w http.ResponseWriter, r *http.Request) { canDelete := false - // Retrieve the faction leader from the database leader, err := core.PostgresQueryOne[string]("SELECT ftns.leader FROM Factions ftns INNER JOIN FactionChats ftcs ON ftns.faction_key = ftcs.key WHERE ftcs.id = $1", chatID) if err != nil { http.Error(w, "Failed to retrieve faction leader", http.StatusInternalServerError) @@ -178,21 +174,18 @@ func DeleteChat(w http.ResponseWriter, r *http.Request) { http.Error(w, "Failed to retrieve sender", http.StatusInternalServerError) return } - + if *sender == userAddress { canDelete = true } } - - // Check if the authenticated user is the sender or the faction leader if !canDelete { http.Error(w, "Not authorized to delete this chat", http.StatusForbidden) return } - // Delete chat from database err = core.PostgresExec(r.Context(), "DELETE FROM FactionChats WHERE id = $1", chatID) if err != nil { WriteErrorJson(w, http.StatusInternalServerError, "Failed to delete chat") @@ -208,14 +201,12 @@ func GetFactionChats(w http.ResponseWriter, r *http.Request) { return } - // Parse query params factionKey := r.URL.Query().Get("faction_key") if factionKey == "" { WriteErrorJson(w, http.StatusBadRequest, "Missing faction key parameter") return } - // Retrieve faction chats from database chats, err := core.PostgresQueryJson[FactionChat]("SELECT id, sender, faction_key, message, time FROM FactionChats WHERE faction_key = $1 ORDER BY time ASC", factionKey) if err != nil { WriteErrorJson(w, http.StatusInternalServerError, "Failed to get faction chats")