| 1 | import Foundation |
| 2 | import RastrilloApple |
| 3 | |
| 4 | /// Delete… (spec 2026-11-22 §1): POST /d/{sqid}/delete, and what its answer |
| 5 | /// means. The rule also covers version skew and lost answers: |
| 6 | /// - A 2xx JSON `{"ok":true}` is deleted (the server from S2b on). |
| 7 | /// - Anything else, including an older or lagged server's 303 to the list |
| 8 | /// (sent after the delete committed) and a 404 (a non-owner gets 404 with |
| 9 | /// nothing deleted, and so does a retry after a lost answer), is checked |
| 10 | /// with a GET of /d/{sqid} as JSON: 404 is deleted, 200 is not (nothing |
| 11 | /// is removed), and anything else is not known, so nothing is removed and |
| 12 | /// the person may try again. |
| 13 | /// - The request never reaching the server is not known either. |
| 14 | /// |
| 15 | /// Only `.deleted` lets the caller drop the document's outbox lane and |
| 16 | /// replica. |
| 17 | enum DocsDeletion { |
| 18 | enum Outcome: Equatable, Sendable { |
| 19 | case deleted |
| 20 | /// The server still has the document: the delete did not happen. |
| 21 | case stillThere |
| 22 | /// Neither answer said: nothing is removed, and it may be tried again. |
| 23 | case unknown |
| 24 | } |
| 25 | |
| 26 | static func run(sqid: String, transport: NativeTransport) async -> Outcome { |
| 27 | let path = "/d/\(sqid)" |
| 28 | do { |
| 29 | let reply = try await transport.responseDetails(path + "/delete", method: "POST", body: Data(), |
| 30 | contentType: "application/x-www-form-urlencoded", maxBytes: 64 * 1024) |
| 31 | if (200..<300).contains(reply.status), isOK(reply) { return .deleted } |
| 32 | } catch { |
| 33 | // Offline, or the answer was lost: the GET below may still say. |
| 34 | } |
| 35 | do { |
| 36 | let check = try await transport.responseDetails(path, maxBytes: 1024 * 1024) |
| 37 | switch check.status { |
| 38 | // A 404 here is read as deleted, though not every 404 is one: a |
| 39 | // request that lands on a server mid-deploy, or after this |
| 40 | // person lost their membership, answers 404 for a document that |
| 41 | // still exists. That is safe to act on. Delete… is refused with |
| 42 | // anything unsent, so the dropped outbox lane is empty and no |
| 43 | // change is lost; the replica and metadata dropped with it are a |
| 44 | // cache. If the document does still exist, the next list reload |
| 45 | // shows it again, and opening it downloads it afresh. |
| 46 | case 404: return .deleted |
| 47 | case 200..<300: return .stillThere |
| 48 | default: return .unknown |
| 49 | } |
| 50 | } catch { |
| 51 | return .unknown |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | /// `{"ok":true}`, and nothing a redirect target or a page could be. |
| 56 | private static func isOK(_ reply: NativeResponse) -> Bool { |
| 57 | guard reply.header("Content-Type")?.lowercased().hasPrefix("application/json") != false, |
| 58 | let object = try? JSONSerialization.jsonObject(with: reply.data) as? [String: Any] else { return false } |
| 59 | return object["ok"] as? Bool == true |
| 60 | } |
| 61 | } |
| 62 | |