1
0
mirror of https://github.com/flarum/core.git synced 2025-08-04 15:37:51 +02:00

Implement notifications

This commit is contained in:
Toby Zerner
2015-03-24 15:07:38 +10:30
parent 1d1025dcd2
commit 4a1550215c
34 changed files with 808 additions and 38 deletions

View File

@@ -0,0 +1,135 @@
<?php namespace Flarum\Core\Models;
use Flarum\Core\Support\MappedMorphTo;
class Notification extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'notifications';
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['time'];
/**
* A map of notification types, as specified in the `type` column, to
* their subject classes.
*
* @var array
*/
protected static $types = [];
public static function notify($userId, $type, $senderId, $subjectId, $data)
{
$notification = new static;
$notification->user_id = $userId;
$notification->sender_id = $senderId;
$notification->type = $type;
$notification->subject_id = $subjectId;
$notification->data = $data;
$notification->time = time();
return $notification;
}
public function read()
{
$this->is_read = true;
}
/**
* 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 notification's recipient.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('Flarum\Core\Models\User', 'user_id');
}
/**
* Define the relationship with the notification's sender.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function sender()
{
return $this->belongsTo('Flarum\Core\Models\User', 'sender_id');
}
public function subject()
{
$name = 'subject';
$typeColumn = 'type';
$idColumn = 'subject_id';
// If the type value is null it is probably safe to assume we're eager loading
// the relationship. When that is the case we will pass in a dummy query as
// there are multiple types in the morph and we can't use single queries.
if (is_null($type = $this->$typeColumn))
{
return new MappedMorphTo(
$this->newQuery(), $this, $idColumn, null, $typeColumn, $name, static::$types
);
}
// If we are not eager loading the relationship we will essentially treat this
// as a belongs-to style relationship since morph-to extends that class and
// we will pass in the appropriate values so that it behaves as expected.
else
{
$class = static::$types[$type];
$instance = new $class;
return new MappedMorphTo(
$instance->newQuery(), $this, $idColumn, $instance->getKeyName(), $typeColumn, $name, static::$types
);
}
}
public static function getTypes()
{
return static::$types;
}
/**
* Register a notification type and its subject class.
*
* @param string $type
* @param string $class
* @return void
*/
public static function addType($type, $class)
{
static::$types[$type] = $class;
}
}

View File

@@ -51,7 +51,7 @@ class User extends Model
*
* @var array
*/
protected $dates = ['join_time', 'last_seen_time', 'read_time'];
protected $dates = ['join_time', 'last_seen_time', 'read_time', 'notification_read_time'];
/**
* The hasher with which to hash passwords.
@@ -198,6 +198,18 @@ class User extends Model
return $this;
}
/**
* Mark all notifications as read by setting the user's notification_read_time.
*
* @return $this
*/
public function markNotificationsAsRead()
{
$this->notification_read_time = time();
return $this;
}
/**
* Check if a given password matches the user's password.
*
@@ -303,6 +315,11 @@ class User extends Model
return (bool) $count;
}
public function getUnreadNotificationsCount()
{
return $this->notifications()->where('time', '>', $this->notification_read_time ?: 0)->where('is_read', 0)->count(\DB::raw('DISTINCT type, subject_id'));
}
/**
* Check whether or not the user is an administrator.
*
@@ -343,6 +360,16 @@ class User extends Model
return $this->belongsToMany('Flarum\Core\Models\Group', 'users_groups');
}
/**
* Define the relationship with the user's notifications.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function notifications()
{
return $this->hasMany('Flarum\Core\Models\Notification');
}
/**
* Define the relationship with the user's permissions.
*