From b3fd5715a33f6bbbb9be6ed3e81a05d6aa0b834d Mon Sep 17 00:00:00 2001 From: trinkey Date: Mon, 7 Apr 2025 10:19:18 -0400 Subject: [PATCH] pre-bake emojis instead of parsing every time --- admin/emoji.php | 35 +++++++++++++++++++++++++++ admin/migrate.php | 3 ++- admin/respond.php | 15 ++++++++++-- boilerplate/question.php | 34 ++++++++++++++++++++------ migrations/20250407-AddParsedText.php | 30 +++++++++++++++++++++++ send.php | 11 +++++++++ setup.php | 9 ++++--- 7 files changed, 123 insertions(+), 14 deletions(-) create mode 100644 migrations/20250407-AddParsedText.php diff --git a/admin/emoji.php b/admin/emoji.php index 10a3db4..b9400d8 100644 --- a/admin/emoji.php +++ b/admin/emoji.php @@ -5,6 +5,8 @@ if (!$emojiEnabled) { exit(); } +include "../boilerplate/question.php"; + $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Authorization: {$emojiAPIToken}")); @@ -44,6 +46,39 @@ if (count($jsonResp)) { pg_query($db, rtrim($query, ",") . ";"); } +$query = " +SELECT * FROM data; +"; + +$queryResponse = pg_query($db, $query); +$questions = pg_fetch_all($queryResponse); + +foreach ($questions as $question) { + $parsed_text = insertEmoji($question["text"]); + + $parsed_cw = NULL; + if ($question["iscwed"] === "t") { + $parsed_cw = insertEmoji($question["cw"]); + } + + $parsed_response = NULL; + if ($question["isrespondedto"] === "t") { + $parsed_response = insertEmoji($question["responsetext"]); + } + + pg_update( + $db, "data", + array( + "parsedtext" => $parsed_text, + "parsedcw" => $parsed_cw, + "parsedresponsetext" => $parsed_response + ), + array( + "id" => $question["id"] + ) + ); +} + header("Location: index.php?pw={$adminPassword}&emoji=1"); ?> diff --git a/admin/migrate.php b/admin/migrate.php index cc6c8e7..c67642a 100644 --- a/admin/migrate.php +++ b/admin/migrate.php @@ -13,7 +13,8 @@ $migrations = array( "20240218-AddMarkReadOption-a7e43358", "20240218-AddFalsesToPrivRead-67d82b18", "20250310-AddFediIDColumn-64520350", - "20250314-AddEmoji" + "20250314-AddEmoji", + "20250407-AddParsedText" ); foreach ($migrations as $mig) { diff --git a/admin/respond.php b/admin/respond.php index 9ea8fb6..eb0f451 100644 --- a/admin/respond.php +++ b/admin/respond.php @@ -7,7 +7,6 @@ if ($id === null) { } if ($_GET["text"] !== null) { - $text = $_GET["text"]; $cdate = date("Y-m-d H:i:sP"); $query = " @@ -16,16 +15,28 @@ if ($_GET["text"] !== null) { WHERE id = {$id}; "; + $text = $_GET["text"]; $cw = $_GET["cw"]; $cwEnabled = strlen($cw) !== 0; + $parsed_response = NULL; + $parsed_cw = NULL; + + if ($emojiEnabled) { + include "../boilerplate/question.php"; + $parsed_response = insertEmoji($_GET["text"]); + $parsed_cw = insertEmoji($_GET["cw"]); + } + pg_query($db, $query); pg_update( $db, "data", array( "responsetext" => $text, + "parsedresponsetext" => $parsed_response, "iscwed" => $cwEnabled, - "cw" => $cw + "cw" => $cw, + "parsedcw" => $parsed_cw ), array("id" => $id) ); diff --git a/boilerplate/question.php b/boilerplate/question.php index f7afd0c..a73f178 100644 --- a/boilerplate/question.php +++ b/boilerplate/question.php @@ -1,11 +1,9 @@ {$question['time']}"; if ($question["responsetext"]) { - $q .= "
" . insertEmoji($question['responsetext']); + $q .= "
"; + + if ($emojiEnabled) { + $q .= $question['parsedresponsetext']; + } else { + $q .= $question['responsetext']; + } + $q .= "
{$question['responsetime']}
"; } @@ -75,8 +87,8 @@ function getQuestion(
- "; + "; if ($question["iscwed"] === "t") { $q .= "cw:
"; } else { @@ -131,7 +143,13 @@ function getQuestion( $open = " open"; } - $q = "cw: " . insertEmoji($question["cw"]) . "{$q}"; + if ($emojiEnabled) { + $cw = $question["parsedcw"]; + } else { + $cw = $question["cw"]; + } + + $q = "cw: {$cw}{$q}"; } return "
{$q}
"; diff --git a/migrations/20250407-AddParsedText.php b/migrations/20250407-AddParsedText.php new file mode 100644 index 0000000..308a805 --- /dev/null +++ b/migrations/20250407-AddParsedText.php @@ -0,0 +1,30 @@ + "20250407-AddParsedText" +); + +pg_insert($db, "migrations", $dataArray); + +?> diff --git a/send.php b/send.php index e2dca0a..b5e7662 100644 --- a/send.php +++ b/send.php @@ -6,9 +6,20 @@ if ($_GET["text"] === null) { exit(); } +$parsed_text = NULL; +$parsed_cw = NULL; + +if ($emojiEnabled) { + include "boilerplate/question.php"; + $parsed_text = insertEmoji(substr($_GET["text"], 0, $maxQuestionLength)); + $parsed_cw = insertEmoji(substr($_GET["cw"], 0, $maxCWLength)); +} + $dataArray = array( "text" => substr($_GET["text"], 0, $maxQuestionLength), + "parsedtext" => $parsed_text, "cw" => substr($_GET["cw"], 0, $maxCWLength), + "parsedcw" => $parsed_cw, "iscwed" => !($_GET["cw"] === null || $_GET["cw"] === ""), "time" => date("Y-m-d H:i:sP"), "ispublic" => $_GET["public"] == 1, diff --git a/setup.php b/setup.php index 46d675c..3ee9818 100644 --- a/setup.php +++ b/setup.php @@ -6,12 +6,15 @@ $query = " CREATE TABLE data ( id SERIAL PRIMARY KEY, text TEXT NOT NULL, + parsedtext TEXT, cw TEXT, + parsedcw TEXT, iscwed BOOLEAN NOT NULL, time TIMESTAMPTZ NOT NULL, ispublic BOOLEAN NOT NULL, isrespondedto BOOLEAN NOT NULL, responsetext TEXT, + parsedresponsetext TEXT, responsetime TIMESTAMPTZ, isprivread BOOLEAN ); @@ -29,14 +32,14 @@ pg_query($db, $query); $query = " CREATE TABLE emoji ( - name TEXT NOT NULL PRIMARY KEY, - url TEXT NOT NULL + name TEXT NOT NULL PRIMARY KEY, + url TEXT NOT NULL ); "; pg_query($db, $query); -include "admin/migrate.php"; +// include "admin/migrate.php"; echo "database set up";