mirror of
https://github.com/flarum/framework.git
synced 2025-02-14 00:22:48 +08:00
![Toby Zerner](/assets/img/avatar_default.png)
Originally the user activity feed was implemented using UNIONs. I was looking at make an API to add activity “sources”, or extra UNION queries (select from posts, mentions, etc.) but quickly realised that this is too slow and there’s no way to make it scale. So I’ve implemented an API which is very similar to how notifications work (see previous commit). The `activity` table is an aggregation of stuff that happens, and it’s kept in sync by an ActivitySyncer which is used whenever a post it created/edited/deleted, a user is mentioned/unmentioned, etc. Again, the API is very simple (see Core\Activity\PostedActivity + Core\Handlers\Events\UserActivitySyncer)
81 lines
1.6 KiB
PHP
81 lines
1.6 KiB
PHP
<?php namespace Flarum\Core\Models;
|
|
|
|
class Activity extends Model
|
|
{
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'activity';
|
|
|
|
/**
|
|
* The attributes that should be mutated to dates.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $dates = ['time'];
|
|
|
|
/**
|
|
*
|
|
*
|
|
* @var array
|
|
*/
|
|
protected static $subjects = [];
|
|
|
|
/**
|
|
* Unserialize the data attribute.
|
|
*
|
|
* @param string $value
|
|
* @return string
|
|
*/
|
|
public function getDataAttribute($value)
|
|
{
|
|
return json_decode($value);
|
|
}
|
|
|
|
/**
|
|
* Serialize the data attribute.
|
|
*
|
|
* @param string $value
|
|
*/
|
|
public function setDataAttribute($value)
|
|
{
|
|
$this->attributes['data'] = json_encode($value);
|
|
}
|
|
|
|
/**
|
|
* Define the relationship with the activity's recipient.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo('Flarum\Core\Models\User', 'user_id');
|
|
}
|
|
|
|
public function subject()
|
|
{
|
|
return $this->mappedMorphTo(static::$subjects, 'subject', 'type', 'subject_id');
|
|
}
|
|
|
|
public static function getTypes()
|
|
{
|
|
return static::$subjects;
|
|
}
|
|
|
|
/**
|
|
* Register a notification type.
|
|
*
|
|
* @param string $type
|
|
* @param string $class
|
|
* @return void
|
|
*/
|
|
public static function registerType($class)
|
|
{
|
|
if ($subject = $class::getSubjectModel()) {
|
|
static::$subjects[$class::getType()] = $subject;
|
|
}
|
|
}
|
|
}
|