diff options
author | Jonas Kohl | 2024-12-26 20:12:34 +0100 |
---|---|---|
committer | Jonas Kohl | 2024-12-26 20:12:34 +0100 |
commit | 686fff945e0b4697aa74da404ce90534bb7b121d (patch) | |
tree | cb5582c8d118cb480f2978fa821950f4a2259401 /src/application/actions | |
parent | 11fed2c8ce3dd38fe686e7b27db738f64373fe3d (diff) |
Add async email and topic subscribingv0.7.2
Diffstat (limited to 'src/application/actions')
-rw-r--r-- | src/application/actions/subscribetopic/post.php | 39 | ||||
-rw-r--r-- | src/application/actions/viewtopic/get.php | 15 | ||||
-rw-r--r-- | src/application/actions/viewtopic/post.php | 52 |
3 files changed, 106 insertions, 0 deletions
diff --git a/src/application/actions/subscribetopic/post.php b/src/application/actions/subscribetopic/post.php new file mode 100644 index 0000000..78dad04 --- /dev/null +++ b/src/application/actions/subscribetopic/post.php @@ -0,0 +1,39 @@ +<?php + +/** @var ?User $currentUser */ +/** @var \mystic\forum\Database $db */ + +use mystic\forum\orm\Subscription; +use mystic\forum\orm\Topic; +use mystic\forum\utils\RequestUtils; + +if (!$currentUser) { + http_response_code(403); + msg_error(__("You need to be logged in to delete topics!")); + exit; +} + +$formId = "subscribetopic"; +$topicId = RequestUtils::getRequiredField("topic", $formId); + +$topic = new Topic(); +$topic->id = $topicId; + +if (!$db->fetch($topic)) { + http_response_code(404); + msg_error(__("No topic exists with this id")); + exit; +} + +$subscription = new Subscription; +$subscription->userId = $currentUser->id; +$subscription->topicId = $topic->id; + +if ($db->fetchWhere($subscription, ["user_id", "topic_id"])) { + $db->delete($subscription); +} else { + $subscription->id = $db->generateId(); + $db->insert($subscription); +} + +header("Location: ?_action=viewtopic&topic=" . urlencode($topic->id)); diff --git a/src/application/actions/viewtopic/get.php b/src/application/actions/viewtopic/get.php index 636d791..56308bf 100644 --- a/src/application/actions/viewtopic/get.php +++ b/src/application/actions/viewtopic/get.php @@ -1,9 +1,12 @@ <?php /** @var Post[] $posts */ +/** @var ?User $currentUser */ +/** @var \mystic\forum\Database $db */ use mystic\forum\orm\Attachment; use mystic\forum\orm\Post; +use mystic\forum\orm\Subscription; use mystic\forum\orm\TopicLogMessage; use mystic\forum\orm\User; @@ -58,8 +61,20 @@ $allItems = array_map(function(Post|TopicLogMessage $item) use (&$db, &$topicAut } }, $allItems); +$subscription = null; +if ($currentUser !== null) { + $subscription = new Subscription; + $subscription->userId = $currentUser->id; + $subscription->topicId = $topic->id; + if (!$db->fetchWhere($subscription, ["user_id", "topic_id"])) { + $subscription = null; + } +} + render("view_topic.twig", [ "topic" => $topic, "topicAuthor" => $topicAuthor, "allItems" => &$allItems, + "subscription" => $subscription, + "subscription_count" => count($db->fetchCustom(Subscription::class, "WHERE topic_id = $1", [ $topic->id ])), ]); diff --git a/src/application/actions/viewtopic/post.php b/src/application/actions/viewtopic/post.php index 1038222..b50a2e9 100644 --- a/src/application/actions/viewtopic/post.php +++ b/src/application/actions/viewtopic/post.php @@ -1,8 +1,18 @@ <?php use mystic\forum\orm\Attachment; +use mystic\forum\orm\PendingEmail; use mystic\forum\orm\Post; +use mystic\forum\orm\Subscription; +use mystic\forum\orm\User; use mystic\forum\utils\RequestUtils; +use Symfony\Component\Mime\Address; + +/** @var string $formId */ +/** @var string $topicId */ +/** @var mystic\forum\orm\Topic $topic */ +/** @var \mystic\forum\Database $db */ +/** @var \mystic\forum\User $currentUser */ if (!$currentUser) { http_response_code(403); @@ -61,4 +71,46 @@ foreach ($attachments as $att) { $db->insert($attachment); } +if (($_POST["subscribe"] ?? null) === "on") { + $subscription = new Subscription; + $subscription->userId = $currentUser->id; + $subscription->topicId = $topic->id; + + if (!$db->fetchWhere($subscription, ["user_id", "topic_id"])) { + $subscription->id = $db->generateId(); + $db->insert($subscription); + } +} + +/** @var Subscription[] $allSubscriptions */ +$allSubscriptions = $db->fetchCustom(Subscription::class, "WHERE topic_id = $1 AND user_id <> $2", [ $topicId, $currentUser->id ]); + +foreach ($allSubscriptions as $subscription) { + $subUser = new User; + $subUser->id = $subscription->userId; + if (!$db->fetch($subUser)) + continue; + + $email = new PendingEmail; + $email->id = $db->generateId(); + $email->sender = env("MAILER_FROM"); + $email->recipient = (new Address($subUser->email, $subUser->displayName))->toString(); + $email->subject = __("Someone made a new post in \"%topic%\"", [ + "topic" => $topic->title, + ]); + $email->plaintextBody = __("Hello %name%,\n" . + "\n" . + "%otherName% has added the following post to \"%topic%\":\n" . + "%content%\n" . + "\n" . + "Click here to view: %link%\n", [ + "name" => $subUser->displayName, + "otherName" => $currentUser->displayName, + "topic" => $topic->title, + "content" => quote_message(html_entity_decode(renderPostSummary($message))), + "link" => env("PUBLIC_URL") . "?_action=viewtopic&topic=" . urlencode($topicId) . "#post-" . urlencode($item->id), + ]); + $db->insert($email); +} + header("Location: ?_action=viewtopic&topic=" . urlencode($topicId) . "#form"); |