implemented following users to receive (opt-in) email notifications when they publish an article.

This commit is contained in:
2025-04-14 22:32:32 -06:00
parent fdcc1a3349
commit 420022dbf8
11 changed files with 248 additions and 82 deletions
+10
View File
@@ -4,6 +4,16 @@ require_once 'article_functions.php';
use Postmark\PostmarkClient;
function follow_user($follower_user_id, $following_user_id, $notify)
{
exec_stmt('INSERT INTO follows (follower_user_id, following_user_id, notify_user) VALUES (?, ?, ?)', 'iii', $follower_user_id, $following_user_id, $notify);
}
function unfollow_user($follower_user_id, $following_user_id)
{
exec_stmt('DELETE FROM follows WHERE follower_user_id = ? AND following_user_id = ?', 'ii', $follower_user_id, $following_user_id);
}
function user_id_exists($user_id)
{
$result = exec_stmt('SELECT user_id FROM user WHERE user_id = ?', 'i', $user_id)->fetch_assoc();
+36 -18
View File
@@ -1,17 +1,36 @@
<?php
include_once 'functions/init.php';
if (!empty($_FILES['upload'])) {
require_once '../vendor/autoload.php';
use Postmark\PostmarkClient;
// Load environment variables.
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/../');
$dotenv->safeLoad();
if (!empty($_FILES['upload']))
upload($_FILES['upload'], $_ENV['UPLOAD_TMP_FQ_PATH'], isset($_POST['filename']) ? $_POST['filename'] : null);
function notify_users($emails, $auther_username, $article_id, $article_title, $excerpt)
{
foreach ($emails as $email) {
$email_html = '
<h1>' . $article_title . '</h1>
<p>An author you follow just published a new article! Read it <a href="https://vintagecoding.net/articles.php?view=read&article_id=' . $article_id . '" target="_blank">here</a></p>
<p><strong>Here is a snippet from the article!</strong></p>
<p>' . $excerpt . '</p>
';
$client = new PostmarkClient($_ENV['POSTMARK_API_TOKEN']);
$client->sendEmail(
'mailer@joshashton.dev',
$email,
$auther_username . ' just published a new article! - vintagecoding.net',
$email_html
);
}
}
function delete_dir($dir)
{
if (!is_dir($dir)) return false;
if (!is_dir($dir))
return false;
if (substr($dir, strlen($dir) - 1, 1) != '/') {
$dir .= '/';
@@ -28,7 +47,8 @@ function delete_dir($dir)
function delete_file($file)
{
if (!file_exists($file)) return false;
if (!file_exists($file))
return false;
unlink($file);
}
@@ -54,7 +74,7 @@ function upload($input, $dest_dir, $filename = null)
break;
default:
# code...
// code...
break;
}
@@ -66,7 +86,7 @@ function upload($input, $dest_dir, $filename = null)
function crop_image($img_path, $x, $y, $height, $width)
{
//$dest = $_ENV['UPLOAD_TMP_FQ_PATH'];
// $dest = $_ENV['UPLOAD_TMP_FQ_PATH'];
$dest = '/tmp/';
// Get the image type
$imageType = exif_imagetype($img_path);
@@ -80,11 +100,11 @@ function crop_image($img_path, $x, $y, $height, $width)
$sourceImage = imagecreatefrompng($img_path);
break;
default:
return false; // Unsupported image type
return false; // Unsupported image type
}
if (!$sourceImage)
return false; // Failed to create image resource
return false; // Failed to create image resource
// Create a new image resource for the cropped image
$croppedImage = imagecreatetruecolor($width, $height);
@@ -109,7 +129,7 @@ function crop_image($img_path, $x, $y, $height, $width)
imagedestroy($sourceImage);
imagedestroy($croppedImage);
return true; // Cropping successful
return true; // Cropping successful
}
/*
@@ -132,7 +152,7 @@ function extract_content_from_tag($string, $start, $end)
{
$ini = strpos($string, $start);
if ($ini != 0)
return "nope";
return 'nope';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
@@ -151,9 +171,7 @@ function check_csv_upload($target)
function pretty_dump($arr)
{
echo '
<pre>
' . print_r($arr) . '
</pre>
';
echo '<pre>';
print_r($arr);
echo '</pre>';
}