Skip to content

Commit

Permalink
refactor: refactor activity system
Browse files Browse the repository at this point in the history
  • Loading branch information
abourtnik committed Jan 22, 2024
1 parent 3df842c commit 0b5b225
Show file tree
Hide file tree
Showing 23 changed files with 265 additions and 337 deletions.
25 changes: 25 additions & 0 deletions app/Events/Activity/ActivityCreated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Events\Activity;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ActivityCreated
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public Model $model;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Model $model)
{
$this->model = $model;
}
}
25 changes: 25 additions & 0 deletions app/Events/Activity/ActivityDeleted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Events\Activity;

use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class ActivityDeleted
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public Model $model;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Model $model)
{
$this->model = $model;
}
}
2 changes: 2 additions & 0 deletions app/Filters/ActivityFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class ActivityFilters extends MySQLFilter
{
use DateFilter;

protected string $dateField = 'perform_at';

public function type(string $value): Builder
{
return $this->builder->when($value === 'video_likes', fn($q) => $q->whereHasMorph('subject', [Interaction::class], fn($query) => $query->whereHasMorph('likeable', [Video::class])->where('status', true)))
Expand Down
16 changes: 10 additions & 6 deletions app/Http/Controllers/InteractionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,19 @@ private function perform (InteractionRequest $request, $status) : JsonResponse {
$interaction = $likeable->interactions()->whereRelation('user', 'id', Auth::user()->id)->first();

if ($interaction) {
if ($interaction->status == $status) {
$interaction->delete();
} else {
$interaction->update([

$interaction->delete();

if ($interaction->status != $status) {
Auth::user()->interactions()->create([
'likeable_type' => get_class($likeable),
'likeable_id' => $likeable->id,
'status' => $status,
'perform_at' => now()
]);
}
} else {
}

else {
Auth::user()->interactions()->create([
'likeable_type' => get_class($likeable),
'likeable_id' => $likeable->id,
Expand Down
11 changes: 5 additions & 6 deletions app/Http/Controllers/User/ActivityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use App\Models\Comment;
use App\Models\Interaction;
use App\Models\Video;
use Carbon\Carbon;
use Illuminate\Contracts\View\View;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Facades\Auth;
Expand All @@ -24,7 +23,8 @@ public function index(ActivityFilters $filters) : View {
'activity as comment_dislikes_count' => fn($query) => $query->filter($filters)->whereHasMorph('subject', [Interaction::class], fn($query) => $query->whereHasMorph('likeable', [Comment::class])->where('status', false)),
'activity as comments_count' => fn($query) => $query->filter($filters)->whereHasMorph('subject', [Comment::class]),
]),
'activity_log' => Activity::causedBy(Auth::user())
'activities' => Activity::query()
->where('user_id', Auth::id())
->filter($filters)
->with([
'subject' => function (MorphTo $morphTo) {
Expand All @@ -41,10 +41,9 @@ public function index(ActivityFilters $filters) : View {
]);
}
])
->latest('updated_at')
->get()
->groupBy(fn ($item) => Carbon::parse($item->updated_at)->format('Y-m-d'))
->all(),
->latest('perform_at')
->paginate(12)
->withQueryString(),
'filters' => $filters->receivedFilters(),
'types' => [
'video_likes' => 'Video Likes',
Expand Down
44 changes: 44 additions & 0 deletions app/Listeners/ActivityEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace App\Listeners;

use App\Events\Activity\ActivityCreated;
use App\Events\Activity\ActivityDeleted;
use App\Models\Activity;
use Illuminate\Events\Dispatcher;
use Illuminate\Support\Facades\Auth;

class ActivityEventSubscriber
{
/**
* Handle user have new activity event.
*/
public function saveActivity(ActivityCreated $event): void {
Auth::user()->activity()->create([
'subject_type' => get_class($event->model),
'subject_id' => $event->model->id
]);
}

/**
* Handle user delete activity event.
*/
public function deleteActivity(ActivityDeleted $event): void {
Activity::query()
->where('subject_id', $event->model->id)
->delete();
}

/**
* Register the listeners for the subscriber.
*
* @return array<string, string>
*/
public function subscribe(Dispatcher $events): array
{
return [
ActivityCreated::class => 'saveActivity',
ActivityDeleted::class => 'deleteActivity',
];
}
}
26 changes: 23 additions & 3 deletions app/Models/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,38 @@
namespace App\Models;

use App\Models\Traits\Filterable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Spatie\Activitylog\Models\Activity as SpatieActivity;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;

class Activity extends SpatieActivity
class Activity extends Model
{
use HasFactory, Filterable;

protected $table = 'users_activity';

protected $guarded = ['id'];

protected $dates = [
'perform_at'
];

public $timestamps = false;

/**
* Get the parent subject model (comment or interaction).
*/
public function subject(): MorphTo
{
return $this->morphTo();
}

protected function type(): Attribute
{
return Attribute::make(
get: fn () => strtolower(class_basename($this->subject)) . ($this->subject->likeable ? '_'.strtolower(class_basename($this->subject->likeable)) : ''),
);
}

}
24 changes: 3 additions & 21 deletions app/Models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,34 @@
use App\Models\Interfaces\Likeable;
use App\Models\Interfaces\Reportable;
use App\Models\Traits\Filterable;
use App\Models\Traits\HasActivity;
use App\Models\Traits\HasLike;
use App\Models\Traits\HasReport;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Str;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Staudenmeir\EloquentEagerLimit\HasEagerLimit;

class Comment extends Model implements Likeable, Reportable
{
use HasLike, LogsActivity, HasReport, HasEagerLimit, Filterable;
use HasLike, HasReport, HasEagerLimit, Filterable, HasFactory, HasActivity;

protected $guarded = ['id'];

protected static $recordEvents = ['created'];

protected $dates = [
'banned_at'
];

public const SHORT_CONTENT_LIMIT = 500;

use HasFactory;

/**
* -------------------- RELATIONS --------------------
*/
Expand Down Expand Up @@ -150,17 +145,4 @@ public function scopePublic(QueryBuilder|EloquentBuilder $query): QueryBuilder|E
{
return $query->whereNull('banned_at');
}

/**
* -------------------- METHODS --------------------
*/

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}




}
17 changes: 2 additions & 15 deletions app/Models/Interaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,20 @@
namespace App\Models;

use App\Models\Traits\Filterable;
use App\Models\Traits\HasActivity;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class Interaction extends Model
{
use HasFactory, LogsActivity, Filterable;
use HasFactory, Filterable, HasActivity;

protected $guarded = ['id'];

public $timestamps = false;

protected static $recordEvents = ['created'];

protected $casts = [
'perform_at' => 'datetime',
];
Expand All @@ -33,14 +30,4 @@ public function likeable() : MorphTo
{
return $this->morphTo();
}

public function tapActivity(Activity $activity, string $eventName)
{
$activity->properties = ['status' => $activity->subject->status];
}

public function getActivitylogOptions(): LogOptions
{
return LogOptions::defaults();
}
}
24 changes: 24 additions & 0 deletions app/Models/Traits/HasActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\Models\Traits;

use App\Events\Activity\ActivityCreated;
use App\Events\Activity\ActivityDeleted;
use Illuminate\Database\Eloquent\Model;

trait HasActivity
{
/**
* The "booted" method of the model.
*/
protected static function booted(): void
{
static::created(function (Model $model) {
ActivityCreated::dispatch($model);
});

static::deleted(function (Model $model) {
ActivityDeleted::dispatch($model);
});
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public function pinned_video () : HasOne {
}

public function activity () : HasMany {
return $this->hasMany(Activity::class, 'causer_id');
return $this->hasMany(Activity::class);
}

public function user_reports () : HasMany {
Expand Down
4 changes: 0 additions & 4 deletions app/Observers/CommentObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace App\Observers;

use App\Models\Activity;
use App\Models\Comment;
use App\Notifications\UserNotification;

Expand Down Expand Up @@ -44,8 +43,5 @@ public function deleting(Comment $comment) : void

// Remove video reports
$comment->reports()->delete();

// Remove activity for this comment
Activity::forSubject($comment)->delete();
}
}
37 changes: 0 additions & 37 deletions app/Observers/InteractionObserver.php

This file was deleted.

Loading

0 comments on commit 0b5b225

Please sign in to comment.