pre-bake emojis instead of parsing every time

This commit is contained in:
trinkey 2025-04-07 10:19:18 -04:00
parent 6d8fd143dc
commit b3fd5715a3
7 changed files with 123 additions and 14 deletions

View file

@ -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");
?>

View file

@ -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) {

View file

@ -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)
);

View file

@ -1,11 +1,9 @@
<?php
function insertEmoji($text) {
global $emojiEnabled, $db;
global $db;
if (!$emojiEnabled) {
return htmlspecialchars($text);
}
echo "gay";
$out = "";
$arr = explode(":", htmlspecialchars($text));
@ -55,11 +53,25 @@ function getQuestion(
$isResponding=0,
$adminPassword=""
) {
$q = insertEmoji($question["text"]);
global $emojiEnabled;
if ($emojiEnabled) {
$q = $question["parsedtext"];
} else {
$q = $question["text"];
}
$q .= "<div class=\"time\">{$question['time']}</div>";
if ($question["responsetext"]) {
$q .= "<div class=\"response\">" . insertEmoji($question['responsetext']);
$q .= "<div class=\"response\">";
if ($emojiEnabled) {
$q .= $question['parsedresponsetext'];
} else {
$q .= $question['responsetext'];
}
$q .= "<div class=\"time\">{$question['responsetime']}</div></div>";
}
@ -75,8 +87,8 @@ function getQuestion(
<input hidden name=\"page\" value=\"respond\">
<input hidden name=\"pw\" value=\"{$adminPassword}\">
<input name=\"text\" value=\"{$question['responsetext']}\" autofocus required><br>
";
";
if ($question["iscwed"] === "t") {
$q .= "cw: <input name=\"cw\" value=\"{$question['cw']}\"><br>";
} else {
@ -131,7 +143,13 @@ function getQuestion(
$open = " open";
}
$q = "<details{$open}><summary>cw: " . insertEmoji($question["cw"]) . "</summary>{$q}</details>";
if ($emojiEnabled) {
$cw = $question["parsedcw"];
} else {
$cw = $question["cw"];
}
$q = "<details{$open}><summary>cw: {$cw}</summary>{$q}</details>";
}
return "<div class=\"question\">{$q}</div>";

View file

@ -0,0 +1,30 @@
<?php
$query = "
ALTER TABLE data
ADD COLUMN parsedtext TEXT;
";
pg_query($db, $query);
$query = "
ALTER TABLE data
ADD COLUMN parsedresponsetext TEXT;
";
pg_query($db, $query);
$query = "
ALTER TABLE data
ADD COLUMN parsedcw TEXT;
";
pg_query($db, $query);
$dataArray = array(
"id" => "20250407-AddParsedText"
);
pg_insert($db, "migrations", $dataArray);
?>

View file

@ -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,

View file

@ -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
);
@ -36,7 +39,7 @@ CREATE TABLE emoji (
pg_query($db, $query);
include "admin/migrate.php";
// include "admin/migrate.php";
echo "database set up";