deleting messages
This commit is contained in:
parent
b053ea000b
commit
156a9becc1
2 changed files with 46 additions and 7 deletions
|
@ -89,7 +89,26 @@ function replyTo(messageID) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteMessage(messageID) {
|
function deleteMessage(messageID) {
|
||||||
//
|
let msgContainer = document.querySelector(`.message-container[data-message-id="${messageID}"]`);
|
||||||
|
let err = msgContainer.querySelector(`.msg-error`);
|
||||||
|
|
||||||
|
fetch("/api/messages/", {
|
||||||
|
method: "DELETE",
|
||||||
|
body: JSON.stringify({
|
||||||
|
id: messageID
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.then((response) => (response.json()))
|
||||||
|
.then((json) => {
|
||||||
|
if (json.success) {
|
||||||
|
msgContainer.remove();
|
||||||
|
} else {
|
||||||
|
err.innerHTML = "Something went wrong!";
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
err.innerHTML = `Something went wrong: ${err}`;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function _updateURL_replaceElement(element, to) {
|
function _updateURL_replaceElement(element, to) {
|
||||||
|
@ -138,8 +157,11 @@ function fetchMessages(fetchFromStart=false) {
|
||||||
if (json.success) {
|
if (json.success) {
|
||||||
let frag = document.createDocumentFragment();
|
let frag = document.createDocumentFragment();
|
||||||
|
|
||||||
|
console.log(fetchFromStart);
|
||||||
if (fetchFromStart && json.messages.length == 0) {
|
if (fetchFromStart && json.messages.length == 0) {
|
||||||
out = "<i>No messages</i>";
|
let el = document.createElement("i");
|
||||||
|
el.innerText = "No messages";
|
||||||
|
frag.append(el)
|
||||||
} else {
|
} else {
|
||||||
for (const message of json.messages) {
|
for (const message of json.messages) {
|
||||||
frag.append(getMessageHTML(message, json.canRespond));
|
frag.append(getMessageHTML(message, json.canRespond));
|
||||||
|
|
|
@ -20,6 +20,7 @@ def _json_response(data: dict | list, /, *, status: int=200, content_type: str="
|
||||||
|
|
||||||
RESPONSE_400 = _json_response({ "success": False }, status=400)
|
RESPONSE_400 = _json_response({ "success": False }, status=400)
|
||||||
RESPONSE_401 = _json_response({ "success": False }, status=401)
|
RESPONSE_401 = _json_response({ "success": False }, status=401)
|
||||||
|
RESPONSE_403 = _json_response({ "success": False }, status=403)
|
||||||
|
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
def api_messages(request: WSGIRequest) -> HttpResponse:
|
def api_messages(request: WSGIRequest) -> HttpResponse:
|
||||||
|
@ -28,6 +29,8 @@ def api_messages(request: WSGIRequest) -> HttpResponse:
|
||||||
if username is None:
|
if username is None:
|
||||||
return RESPONSE_401
|
return RESPONSE_401
|
||||||
|
|
||||||
|
user = get_user_object(username, i_promise_this_user_exists=True)
|
||||||
|
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
body = json.loads(request.body)
|
body = json.loads(request.body)
|
||||||
reply = body["content"].strip()
|
reply = body["content"].strip()
|
||||||
|
@ -36,15 +39,13 @@ def api_messages(request: WSGIRequest) -> HttpResponse:
|
||||||
if not (isinstance(message_id, int) and isinstance(reply, str)):
|
if not (isinstance(message_id, int) and isinstance(reply, str)):
|
||||||
return RESPONSE_400
|
return RESPONSE_400
|
||||||
|
|
||||||
user = get_user_object(username, i_promise_this_user_exists=True)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
message = tMMessage.objects.get(message_id=message_id)
|
message = tMMessage.objects.get(message_id=message_id)
|
||||||
except tMMessage.DoesNotExist:
|
except tMMessage.DoesNotExist:
|
||||||
return RESPONSE_400
|
return RESPONSE_400
|
||||||
|
|
||||||
if username != message.u_to.username:
|
if username != message.u_to.username:
|
||||||
return RESPONSE_401
|
return RESPONSE_403
|
||||||
|
|
||||||
if message.response is not None:
|
if message.response is not None:
|
||||||
return RESPONSE_400
|
return RESPONSE_400
|
||||||
|
@ -60,9 +61,25 @@ def api_messages(request: WSGIRequest) -> HttpResponse:
|
||||||
})
|
})
|
||||||
|
|
||||||
elif request.method == "DELETE":
|
elif request.method == "DELETE":
|
||||||
...
|
body = json.loads(request.body)
|
||||||
|
message_id = body["id"]
|
||||||
|
|
||||||
user = get_user_object(username, i_promise_this_user_exists=True)
|
if not isinstance(message_id, int):
|
||||||
|
return RESPONSE_400
|
||||||
|
|
||||||
|
try:
|
||||||
|
message = tMMessage.objects.get(message_id=message_id)
|
||||||
|
except tMMessage.DoesNotExist:
|
||||||
|
return RESPONSE_400
|
||||||
|
|
||||||
|
if username != message.u_to.username:
|
||||||
|
return RESPONSE_403
|
||||||
|
|
||||||
|
message.delete()
|
||||||
|
|
||||||
|
return _json_response({
|
||||||
|
"success": True
|
||||||
|
})
|
||||||
|
|
||||||
queryFilter = {}
|
queryFilter = {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue