storing analytics data
This commit is contained in:
93
app/Models/ActivityEvent.php
Normal file
93
app/Models/ActivityEvent.php
Normal file
@@ -0,0 +1,93 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* Unified activity feed event.
|
||||
*
|
||||
* Types: upload | comment | favorite | award | follow
|
||||
* target_type: artwork | user
|
||||
*
|
||||
* @property int $id
|
||||
* @property int $actor_id
|
||||
* @property string $type
|
||||
* @property string $target_type
|
||||
* @property int $target_id
|
||||
* @property array|null $meta
|
||||
* @property \Illuminate\Support\Carbon $created_at
|
||||
*/
|
||||
class ActivityEvent extends Model
|
||||
{
|
||||
protected $table = 'activity_events';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
const CREATED_AT = 'created_at';
|
||||
const UPDATED_AT = null;
|
||||
|
||||
protected $fillable = [
|
||||
'actor_id',
|
||||
'type',
|
||||
'target_type',
|
||||
'target_id',
|
||||
'meta',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'actor_id' => 'integer',
|
||||
'target_id' => 'integer',
|
||||
'meta' => 'array',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
|
||||
// ── Event type constants ──────────────────────────────────────────────────
|
||||
|
||||
const TYPE_UPLOAD = 'upload';
|
||||
const TYPE_COMMENT = 'comment';
|
||||
const TYPE_FAVORITE = 'favorite';
|
||||
const TYPE_AWARD = 'award';
|
||||
const TYPE_FOLLOW = 'follow';
|
||||
|
||||
const TARGET_ARTWORK = 'artwork';
|
||||
const TARGET_USER = 'user';
|
||||
|
||||
// ── Relations ─────────────────────────────────────────────────────────────
|
||||
|
||||
/** The user who performed the action */
|
||||
public function actor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'actor_id');
|
||||
}
|
||||
|
||||
// ── Factory helpers ───────────────────────────────────────────────────────
|
||||
|
||||
public static function record(
|
||||
int $actorId,
|
||||
string $type,
|
||||
string $targetType,
|
||||
int $targetId,
|
||||
array $meta = []
|
||||
): static {
|
||||
$event = static::create([
|
||||
'actor_id' => $actorId,
|
||||
'type' => $type,
|
||||
'target_type' => $targetType,
|
||||
'target_id' => $targetId,
|
||||
'meta' => $meta ?: null,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
|
||||
// Ensure created_at is available on the returned instance
|
||||
// ($timestamps = false means Eloquent doesn't auto-populate it)
|
||||
if ($event->created_at === null) {
|
||||
$event->created_at = now();
|
||||
}
|
||||
|
||||
return $event;
|
||||
}
|
||||
}
|
||||
@@ -250,6 +250,12 @@ class Artwork extends Model
|
||||
'created_at' => $this->published_at?->toDateString() ?? $this->created_at?->toDateString() ?? '',
|
||||
'is_public' => (bool) $this->is_public,
|
||||
'is_approved' => (bool) $this->is_approved,
|
||||
// ── Trending / discovery fields ────────────────────────────────────
|
||||
'trending_score_24h' => (float) ($this->trending_score_24h ?? 0),
|
||||
'trending_score_7d' => (float) ($this->trending_score_7d ?? 0),
|
||||
'favorites_count' => (int) ($stat?->favorites ?? 0),
|
||||
'awards_received_count' => (int) ($awardStat?->score_total ?? 0),
|
||||
'downloads_count' => (int) ($stat?->downloads ?? 0),
|
||||
'awards' => [
|
||||
'gold' => $awardStat?->gold_count ?? 0,
|
||||
'silver' => $awardStat?->silver_count ?? 0,
|
||||
|
||||
Reference in New Issue
Block a user