Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion app/Http/Controllers/UserProfileController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace App\Http\Controllers;

use App\Mail\UserAppreciationMail;
use App\Models\BlogComment;
use App\Models\BlogReaction;
use App\Models\Node;
use App\Models\NodeVote;
use App\Models\Resource;
use App\Models\ResourceCompletion;
use App\Models\User;
use App\Models\UserAppreciation;
use Illuminate\Support\Facades\Mail;
use Inertia\Inertia;

class UserProfileController extends Controller
Expand Down Expand Up @@ -40,7 +43,28 @@ public function show(string $username)
$totalBlogLikes = BlogReaction::whereIn('blog_id', $user->blogs()->where('is_published', true)->select('id'))->count();
$sharedResourcesCount = Resource::where('user_id', $user->id)->count();

// Recent Community Activities (Uploads, completed topics, comments made, reactions given, upvoted folders)
// Appreciations (Received & Given)
$appreciationsCount = $user->appreciationsReceived()->count();
$appreciatingCount = $user->appreciationsGiven()->count();
$isAppreciated = auth()->check()
? $user->appreciationsReceived()->where('appreciator_id', auth()->id())->exists()
: false;

$appreciators = $user->appreciators()
->select(['users.id', 'users.name', 'users.username', 'users.image_path', 'users.institution'])
->with('roles:id,name')
->latest('user_appreciations.id')
->take(50)
->get();

$appreciating = $user->appreciatingUsers()
->select(['users.id', 'users.name', 'users.username', 'users.image_path', 'users.institution'])
->with('roles:id,name')
->latest('user_appreciations.id')
->take(50)
->get();

// Recent Community Activities (Uploads, completed topics, comments made, reactions given, upvoted folders, appreciations given)
$recentUploads = Resource::where('user_id', $user->id)
->with(['node:id,name,subject_id', 'node.subject:id,name'])
->latest()
Expand Down Expand Up @@ -125,6 +149,21 @@ public function show(string $username)
->filter()
->values();

$recentAppreciations = UserAppreciation::where('appreciator_id', $user->id)
->with('user:id,name,username')
->latest('id')
->take(4)
->get()
->map(fn ($item) => [
'type' => 'appreciation',
'title' => $item->user?->name,
'username' => $item->user?->username,
'url' => $item->user ? "/u/{$item->user->username}" : null,
'created_at' => $item->created_at?->diffForHumans(),
])
->filter(fn ($item) => $item['title'] !== null)
->values();

// Suggested / Discover community members: 2 contributors + 2 general users
$contributorUsers = User::where('id', '!=', $user->id)
->whereNotNull('username')
Expand Down Expand Up @@ -171,6 +210,11 @@ public function show(string $username)
'totalBlogViews' => (int) $totalBlogViews,
'sharedResourcesCount' => $sharedResourcesCount,
],
'appreciationsCount' => $appreciationsCount,
'appreciatingCount' => $appreciatingCount,
'isAppreciated' => $isAppreciated,
'appreciators' => $appreciators,
'appreciating' => $appreciating,
'recentCompletions' => $recentCompletions,
'blogs' => $publishedBlogs,
'recentActivities' => [
Expand All @@ -179,11 +223,45 @@ public function show(string $username)
'reactions' => $recentReactions->values(),
'comments' => $recentComments->values(),
'upvotes' => $recentUpvotes->values(),
'appreciations' => $recentAppreciations->values(),
],
'suggestedUsers' => $suggestedUsers,
]);
}

public function toggleAppreciate(User $user)
{
$currentAuthUser = auth()->user();

// Cannot appreciate own profile
if ($currentAuthUser->id === $user->id) {
return back();
}

$existing = UserAppreciation::where('user_id', $user->id)
->where('appreciator_id', $currentAuthUser->id)
->first();

if ($existing) {
$existing->delete();
} else {
UserAppreciation::create([
'user_id' => $user->id,
'appreciator_id' => $currentAuthUser->id,
]);

$appreciationsCount = $user->appreciationsReceived()->count();
$milestones = [1, 10, 25, 50, 100, 250, 500, 1000];
$isMilestone = in_array($appreciationsCount, $milestones, true) || ($appreciationsCount > 1000 && $appreciationsCount % 500 === 0);

if ($isMilestone && $user->email && $user->receive_emails !== false) {
Mail::to($user->email)->queue(UserAppreciationMail::forMilestone($user, $currentAuthUser, $appreciationsCount));
}
}

return back();
}

private function buildNodeUrl(Node $node): ?string
{
if (! $node->subject) {
Expand Down
72 changes: 72 additions & 0 deletions app/Mail/UserAppreciationMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Mail;

use App\Models\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Queue\SerializesModels;

class UserAppreciationMail extends Mailable implements ShouldQueue
{
use Queueable, SerializesModels;

public function __construct(
public string $mailSubject,
public string $mailContent,
public ?string $recipientName = null,
) {}

public static function forMilestone(User $user, User $appreciator, int $milestoneCount): self
{
$appUrl = config('app.url', 'https://hscstack.site');
$profileUrl = rtrim($appUrl, '/')."/u/{$user->username}";
$appreciatorName = htmlspecialchars($appreciator->name, ENT_QUOTES, 'UTF-8');
$recipientName = htmlspecialchars($user->name, ENT_QUOTES, 'UTF-8');

if ($milestoneCount === 1) {
$mailSubject = 'Someone appreciated your profile on HSCStack! ❤️';
$headline = 'Congratulations! You received your first community appreciation ❤️';
$message = "<p><strong>{$appreciatorName}</strong> just appreciated your profile and contributions on <a href=\"{$profileUrl}\" target=\"_blank\"><strong>HSCStack</strong></a>.</p>"
.'<p>Your presence and contributions are helping fellow students in the community. Keep up the amazing work!</p>';
} else {
$mailSubject = "🎉 Milestone: {$milestoneCount} people have appreciated your profile!";
$headline = "🎉 Exciting News! {$milestoneCount} Appreciations Milestone reached!";
$message = "<p>Your profile on <a href=\"{$profileUrl}\" target=\"_blank\"><strong>HSCStack</strong></a> just reached <strong>{$milestoneCount} community appreciations</strong>, with the latest from <strong>{$appreciatorName}</strong>!</p>"
.'<p>Thank you for inspiring and supporting students and contributors across the platform.</p>';
}

$mailContent = "<p style=\"font-size: 16px; font-weight: 700; color: #4f46e5;\">{$headline}</p>"
.$message
.'<p style="margin-top: 24px;">'
."<a href=\"{$profileUrl}\" target=\"_blank\" style=\"display: inline-block; background-color: #4f46e5; color: #ffffff; padding: 10px 20px; font-weight: 600; text-decoration: none; border-radius: 10px; font-size: 13px;\">"
.'View Your Profile &rarr;'
.'</a>'
.'</p>';

return new self($mailSubject, $mailContent, $user->name);
}

public function envelope(): Envelope
{
return new Envelope(
subject: $this->mailSubject,
);
}

public function content(): Content
{
return new Content(
view: 'emails.bulk_announcement',
with: [
'mailSubject' => $this->mailSubject,
'mailContent' => $this->mailContent,
'recipientName' => $this->recipientName,
'imageUrl' => null,
],
);
}
}
20 changes: 20 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,4 +113,24 @@ public function nodes(): HasMany
{
return $this->hasMany(Node::class);
}

public function appreciationsReceived(): HasMany
{
return $this->hasMany(UserAppreciation::class, 'user_id');
}

public function appreciationsGiven(): HasMany
{
return $this->hasMany(UserAppreciation::class, 'appreciator_id');
}

public function appreciators()
{
return $this->belongsToMany(User::class, 'user_appreciations', 'user_id', 'appreciator_id');
}

public function appreciatingUsers()
{
return $this->belongsToMany(User::class, 'user_appreciations', 'appreciator_id', 'user_id');
}
}
24 changes: 24 additions & 0 deletions app/Models/UserAppreciation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class UserAppreciation extends Model
{
protected $fillable = [
'user_id',
'appreciator_id',
];

public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}

public function appreciator(): BelongsTo
{
return $this->belongsTo(User::class, 'appreciator_id');
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('user_appreciations', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->cascadeOnDelete();
$table->foreignId('appreciator_id')->constrained('users')->cascadeOnDelete();
$table->unique(['user_id', 'appreciator_id']);
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('user_appreciations');
}
};
5 changes: 5 additions & 0 deletions resources/js/pages/Node.vue
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ const handleVote = (type: 'up' | 'down') => {
if (previousVote === type) {
// Toggle off
localUserVote.value = null;

if (type === 'up') {
localUpvotesCount.value = Math.max(0, localUpvotesCount.value - 1);
localUpvoters.value = localUpvoters.value.filter(
Expand All @@ -133,8 +134,10 @@ const handleVote = (type: 'up' | 'down') => {
} else if (previousVote === null) {
// New vote
localUserVote.value = type;

if (type === 'up') {
localUpvotesCount.value += 1;

if (currentUser.value) {
localUpvoters.value = [
{
Expand All @@ -157,12 +160,14 @@ const handleVote = (type: 'up' | 'down') => {
} else {
// Switching vote
localUserVote.value = type;

if (type === 'up') {
localUpvotesCount.value += 1;
localDownvotesCount.value = Math.max(
0,
localDownvotesCount.value - 1,
);

if (currentUser.value) {
localUpvoters.value = [
{
Expand Down
Loading