From 54cf6163db11855696e4f81623a4bae1cb040f2e Mon Sep 17 00:00:00 2001 From: FChannel <> Date: Sun, 6 Jun 2021 12:02:00 -0700 Subject: better handling when delete or deleteattachment when cannot connect to origin instance --- main.go | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 70 insertions(+), 18 deletions(-) diff --git a/main.go b/main.go index eb3c4f0..34c8186 100644 --- a/main.go +++ b/main.go @@ -713,10 +713,8 @@ func main() { http.HandleFunc("/delete", func(w http.ResponseWriter, r *http.Request){ id := r.URL.Query().Get("id") - manage := r.URL.Query().Get("manage") board := r.URL.Query().Get("board") - col := GetCollectionFromID(id) - actor := col.OrderedItems[0].Actor + _, auth := GetPasswordFromSession(r) if id == "" || auth == "" { @@ -725,6 +723,33 @@ func main() { return } + manage := r.URL.Query().Get("manage") + col := GetCollectionFromID(id) + + if len(col.OrderedItems) < 1 { + if !HasAuth(db, auth, GetActorByNameFromDB(db, board).Id) { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("")) + return + } + + if !CheckIfObjectOP(db, id) { + TombstoneObject(db, id) + } else { + TombstoneObjectAndReplies(db, id) + } + + if(manage == "t"){ + http.Redirect(w, r, "/" + *Key + "/" + board , http.StatusSeeOther) + return + } else { + http.Redirect(w, r, "/" + board, http.StatusSeeOther) + return + } + } + + actor := col.OrderedItems[0].Actor + if !HasAuth(db, auth, actor.Id) { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("")) @@ -775,18 +800,8 @@ func main() { http.HandleFunc("/deleteattach", func(w http.ResponseWriter, r *http.Request){ id := r.URL.Query().Get("id") - manage := r.URL.Query().Get("manage") board := r.URL.Query().Get("board") - col := GetCollectionFromID(id) - actor := col.OrderedItems[0].Actor - var OP string - if (len(col.OrderedItems[0].InReplyTo) > 0 && col.OrderedItems[0].InReplyTo[0].Id != "") { - OP = col.OrderedItems[0].InReplyTo[0].Id - } else { - OP = id - } - _, auth := GetPasswordFromSession(r) if id == "" || auth == "" { @@ -794,7 +809,41 @@ func main() { w.Write([]byte("")) return } + + manage := r.URL.Query().Get("manage") + col := GetCollectionFromID(id) + + if len(col.OrderedItems) < 1 { + if !HasAuth(db, auth, GetActorByNameFromDB(db, board).Id) { + w.WriteHeader(http.StatusBadRequest) + w.Write([]byte("")) + return + } + + DeleteAttachmentFromFile(db, id) + TombstoneAttachmentFromDB(db, id) + + DeletePreviewFromFile(db, id) + TombstonePreviewFromDB(db, id) + + if(manage == "t"){ + http.Redirect(w, r, "/" + *Key + "/" + board , http.StatusSeeOther) + return + } else { + http.Redirect(w, r, "/" + board, http.StatusSeeOther) + return + } + } + + actor := col.OrderedItems[0].Actor + var OP string + if (len(col.OrderedItems[0].InReplyTo) > 0 && col.OrderedItems[0].InReplyTo[0].Id != "") { + OP = col.OrderedItems[0].InReplyTo[0].Id + } else { + OP = id + } + if !HasAuth(db, auth, actor.Id) { w.WriteHeader(http.StatusBadRequest) w.Write([]byte("")) @@ -802,7 +851,10 @@ func main() { } DeleteAttachmentFromFile(db, id) + TombstoneAttachmentFromDB(db, id) + DeletePreviewFromFile(db, id) + TombstonePreviewFromDB(db, id) if (manage == "t") { http.Redirect(w, r, "/" + *Key + "/" + board, http.StatusSeeOther) @@ -1832,19 +1884,19 @@ func GetCollectionFromID(id string) Collection { resp, err := http.DefaultClient.Do(req) if err != nil { - CheckError(err, "could not get collection from " + id) return nColl } if resp.StatusCode == 200 { defer resp.Body.Close() - - body, _ := ioutil.ReadAll(resp.Body) - err = json.Unmarshal(body, &nColl) + body, _ := ioutil.ReadAll(resp.Body) - CheckError(err, "error getting collection resp from json body") + if len(body) > 0 { + err = json.Unmarshal(body, &nColl) + CheckError(err, "error getting collection resp from json body") + } } return nColl -- cgit v1.2.3