From e10610acc0ad14cd08c99026bd0368087b9e3a4a Mon Sep 17 00:00:00 2001 From: notfire Date: Mon, 10 Mar 2025 10:12:34 -0400 Subject: [PATCH] make fedi client edit existing posts --- admin/fedi.php | 49 ++++++++++++++----- admin/migrate.php | 3 +- .../20250310-AddFediIDColumn-64520350.php | 18 +++++++ 3 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 migrations/20250310-AddFediIDColumn-64520350.php diff --git a/admin/fedi.php b/admin/fedi.php index a02d210..964e008 100644 --- a/admin/fedi.php +++ b/admin/fedi.php @@ -10,26 +10,49 @@ $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 ($arr["isrespondedto"] == "f") { + 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 { + if (strlen($arr["fedipostid"]) > 0) { + 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/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