From 43e18b3d89b6c5e08d1f8e7546896eee5454ef8d Mon Sep 17 00:00:00 2001 From: notfire Date: Mon, 10 Mar 2025 10:24:27 -0400 Subject: [PATCH] make fedi client edit existing posts; bit of cleanup --- admin/fedi.php | 47 ++++++++++++++----- admin/migrate.php | 3 +- fetch.php | 2 +- .../20250310-AddFediIDColumn-64520350.php | 18 +++++++ 4 files changed, 55 insertions(+), 15 deletions(-) create mode 100644 migrations/20250310-AddFediIDColumn-64520350.php diff --git a/admin/fedi.php b/admin/fedi.php index a02d210..58847cd 100644 --- a/admin/fedi.php +++ b/admin/fedi.php @@ -10,26 +10,47 @@ $arr = pg_fetch_array($qresp); $ch = curl_init(); -curl_setopt($ch, CURLOPT_URL,$fediAPIRoute); -curl_setopt($ch, CURLOPT_POST, true); -curl_setopt($ch, CURLOPT_HTTPHEADER, - array("Authorization: {$fediAPIToken}")); +curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: {$fediAPIToken}")); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $contentO = $arr["text"]; $contentR = $arr["responsetext"]; $url = "{$pageProto}://" . $_SERVER["HTTP_HOST"] . $pagePath . "/fetch.php?id={$id}"; -if ($arr["iscwed"] == "t") { - $contentCW = $arr["cw"]; - curl_setopt($ch, CURLOPT_POSTFIELDS, - http_build_query(array('status' => "question: {$contentO}\n\nresponse: {$contentR}\n\nlink: {$url}", 'visibility' => "{$fediVisibility}", 'spoiler_text' => "anonymous question response (automated); cw: {$contentCW}"))); -} else { - curl_setopt($ch, CURLOPT_POSTFIELDS, - http_build_query(array('status' => "question: {$contentO}\n\nresponse: {$contentR}\n\nlink: {$url}", 'visibility' => "{$fediVisibility}", 'spoiler_text' => "anonymous question response (automated)"))); -} +if (strlen($arr["fedipostid"]) == 0) { + curl_setopt($ch, CURLOPT_URL, $fediAPIRoute); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); -curl_exec($ch); + if ($arr["iscwed"] == "t") { + $contentCW = $arr["cw"]; + curl_setopt($ch, CURLOPT_POSTFIELDS, + http_build_query(array('status' => "question: {$contentO}\n\nresponse: {$contentR}\n\nlink: {$url}", 'visibility' => "{$fediVisibility}", 'spoiler_text' => "anonymous question response (automated); cw: {$contentCW}"))); + } else { + curl_setopt($ch, CURLOPT_POSTFIELDS, + http_build_query(array('status' => "question: {$contentO}\n\nresponse: {$contentR}\n\nlink: {$url}", 'visibility' => "{$fediVisibility}", 'spoiler_text' => "anonymous question response (automated)"))); + } + + $resp = curl_exec($ch); + $jsonResp = json_decode($resp, true); + + $fediID = $jsonResp["id"]; + + pg_update($db, "data", array("fedipostid" => $fediID), array("id" => $id)); +} else { + curl_setopt($ch, CURLOPT_URL, ($fediAPIRoute . "/" . $arr["fedipostid"])); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); + + if ($arr["iscwed"] == "t") { + $contentCW = $arr["cw"]; + curl_setopt($ch, CURLOPT_POSTFIELDS, + http_build_query(array('status' => "question: {$contentO}\n\nresponse: {$contentR}\n\nlink: {$url}", 'visibility' => "{$fediVisibility}", 'spoiler_text' => "anonymous question response (automated); cw: {$contentCW}"))); + } else { + curl_setopt($ch, CURLOPT_POSTFIELDS, + http_build_query(array('status' => "question: {$contentO}\n\nresponse: {$contentR}\n\nlink: {$url}", 'visibility' => "{$fediVisibility}", 'spoiler_text' => "anonymous question response (automated)"))); + } + + curl_exec($ch); +} curl_close($ch); diff --git a/admin/migrate.php b/admin/migrate.php index 19a2191..ec16fc8 100644 --- a/admin/migrate.php +++ b/admin/migrate.php @@ -11,7 +11,8 @@ $rows = pg_fetch_all($qresp); $migrations = array( "20240218-AddMigrationsTable-40641e8d", "20240218-AddMarkReadOption-a7e43358", - "20240218-AddFalsesToPrivRead-67d82b18" + "20240218-AddFalsesToPrivRead-67d82b18", + "20250310-AddFediIDColumn-64520350" ); foreach ($migrations as $mig) { diff --git a/fetch.php b/fetch.php index 60a562a..c7a8d77 100644 --- a/fetch.php +++ b/fetch.php @@ -37,7 +37,7 @@ if (pg_num_rows($qresp) === 0 || $arr["ispublic"] === "f" || $arr["isrespondedto echo("
" . $arr["time"] . "
"); echo("
" . $arr["responsetext"] . ""); echo("
" . $arr["responsetime"] . "
"); - if ($i["iscwed"] === "t") { + if ($arr["iscwed"] === "t") { echo(""); } else { echo(""); diff --git a/migrations/20250310-AddFediIDColumn-64520350.php b/migrations/20250310-AddFediIDColumn-64520350.php new file mode 100644 index 0000000..042b751 --- /dev/null +++ b/migrations/20250310-AddFediIDColumn-64520350.php @@ -0,0 +1,18 @@ + "20250310-AddFediIDColumn-64520350" +); + +pg_insert($db, "migrations", $dataArray); + +?> \ No newline at end of file