From 094825792a802807860236bf68e9e16cafa69563 Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 1 Jul 2015 13:19:24 +0930 Subject: [PATCH] Clean up activity model --- src/Core/Models/Activity.php | 78 ++++++++++++++----- .../EloquentActivityRepository.php | 2 +- src/Extend/ActivityType.php | 2 +- 3 files changed, 59 insertions(+), 23 deletions(-) diff --git a/src/Core/Models/Activity.php b/src/Core/Models/Activity.php index d12c6434e..57f50df31 100644 --- a/src/Core/Models/Activity.php +++ b/src/Core/Models/Activity.php @@ -1,5 +1,18 @@ attributes['data'] = json_encode($value); } + /** + * Get the subject model for this activity record by looking up its type in + * our subject model map. + * + * @return string|null + */ + public function getSubjectModelAttribute() + { + return array_get(static::$subjectModels, $this->type); + } + /** * Define the relationship with the activity's recipient. * @@ -54,27 +82,35 @@ class Activity extends Model return $this->belongsTo('Flarum\Core\Models\User', 'user_id'); } + /** + * Define the relationship with the activity's subject. + * + * @return \Illuminate\Database\Eloquent\Relations\MorphTo + */ public function subject() { - return $this->mappedMorphTo(static::$subjects, 'subject', 'type', 'subject_id'); - } - - public static function getTypes() - { - return static::$subjects; + return $this->morphTo('subject', 'subjectModel', 'subject_id'); } /** - * Register a notification type. + * Get the type-to-subject-model map. * - * @param string $type - * @param string $class + * @return array + */ + public static function getSubjectModels() + { + return static::$subjectModels; + } + + /** + * Set the subject model for the given activity type. + * + * @param string $type The activity type. + * @param string $class The class name of the subject model for that type. * @return void */ - public static function registerType($class) + public static function setSubjectModel($type, $subjectModel) { - if ($subject = $class::getSubjectModel()) { - static::$subjects[$class::getType()] = $subject; - } + static::$subjectModels[$type] = $subjectModel; } } diff --git a/src/Core/Repositories/EloquentActivityRepository.php b/src/Core/Repositories/EloquentActivityRepository.php index 809be04bc..7c85b7768 100644 --- a/src/Core/Repositories/EloquentActivityRepository.php +++ b/src/Core/Repositories/EloquentActivityRepository.php @@ -8,7 +8,7 @@ class EloquentActivityRepository implements ActivityRepositoryInterface public function findByUser($userId, User $viewer, $limit = null, $offset = 0, $type = null) { $query = Activity::where('user_id', $userId) - ->whereIn('type', array_keys(Activity::getTypes())) + ->whereIn('type', array_keys(Activity::getSubjectModels())) ->orderBy('time', 'desc') ->skip($offset) ->take($limit); diff --git a/src/Extend/ActivityType.php b/src/Extend/ActivityType.php index 0b3dcf59b..0de3a0ecc 100644 --- a/src/Extend/ActivityType.php +++ b/src/Extend/ActivityType.php @@ -20,7 +20,7 @@ class ActivityType implements ExtenderInterface { $class = $this->class; - Activity::registerType($class); + Activity::setSubjectModel($class::getType(), $class::getSubjectModel()); ActivitySerializer::$subjects[$class::getType()] = $this->serializer; }