2016-05-27 21:12:40 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pony.fm - A community for pony fan music.
|
2021-02-14 03:39:15 +01:00
|
|
|
* Copyright (C) 2016 Feld0.
|
2016-05-27 21:12:40 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-02-14 03:34:58 +01:00
|
|
|
namespace App\Models;
|
2016-05-27 21:12:40 +02:00
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
/**
|
2021-02-14 03:39:15 +01:00
|
|
|
* App\Models\Notification.
|
2016-05-27 21:12:40 +02:00
|
|
|
*
|
2021-02-14 03:39:15 +01:00
|
|
|
* @property int $id
|
|
|
|
* @property int $activity_id
|
|
|
|
* @property int $user_id
|
|
|
|
* @property bool $is_read
|
2021-02-14 03:34:58 +01:00
|
|
|
* @property-read \App\Models\Activity $activity
|
|
|
|
* @property-read \App\Models\Email $email
|
|
|
|
* @property-read \App\Models\User $recipient
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification forUser($user)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereId($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereActivityId($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereUserId($value)
|
|
|
|
* @method static \Illuminate\Database\Query\Builder|\App\Models\Notification whereIsRead($value)
|
2016-12-10 17:47:49 +01:00
|
|
|
* @mixin \Eloquent
|
2016-05-27 21:12:40 +02:00
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
class Notification extends Model
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
public $timestamps = false;
|
|
|
|
protected $fillable = ['activity_id', 'user_id'];
|
|
|
|
protected $casts = [
|
|
|
|
'id' => 'integer',
|
|
|
|
'activity_id' => 'integer',
|
|
|
|
'user_id' => 'integer',
|
|
|
|
'is_read' => 'boolean',
|
|
|
|
];
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
public function activity()
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
return $this->belongsTo(Activity::class, 'activity_id', 'id');
|
|
|
|
}
|
2021-02-14 03:39:15 +01:00
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
public function recipient()
|
|
|
|
{
|
2016-05-27 21:12:40 +02:00
|
|
|
return $this->belongsTo(User::class, 'user_id', 'id');
|
|
|
|
}
|
|
|
|
|
2016-12-09 11:53:32 +01:00
|
|
|
public function email()
|
|
|
|
{
|
|
|
|
return $this->hasOne(Email::class, 'notification_id', 'id');
|
|
|
|
}
|
|
|
|
|
2016-05-27 21:12:40 +02:00
|
|
|
/**
|
|
|
|
* This scope grabs eager-loaded notifications for the given user.
|
|
|
|
*
|
|
|
|
* @param Builder $query
|
|
|
|
* @param User $user
|
|
|
|
* @return Builder
|
|
|
|
*/
|
2016-09-30 00:26:31 +02:00
|
|
|
public function scopeForUser(Builder $query, User $user)
|
|
|
|
{
|
2016-06-07 18:49:00 +02:00
|
|
|
$result = $query->with([
|
2016-05-27 21:12:40 +02:00
|
|
|
'activity',
|
|
|
|
'activity.initiatingUser',
|
|
|
|
'activity.resource',
|
|
|
|
])
|
2016-07-10 03:16:25 +02:00
|
|
|
->join('activities', 'notifications.activity_id', '=', 'activities.id')
|
|
|
|
->where('notifications.user_id', $user->id)
|
|
|
|
->whereNull('activities.deleted_at')
|
|
|
|
->select('*', 'notifications.id as id')
|
2021-02-14 20:45:58 +01:00
|
|
|
->orderByDesc('activities.created_at');
|
2016-06-07 18:49:00 +02:00
|
|
|
|
|
|
|
return $result;
|
2016-05-27 21:12:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-30 00:26:31 +02:00
|
|
|
public function toArray()
|
|
|
|
{
|
2016-08-28 02:01:37 +02:00
|
|
|
if (is_null($this->activity->resource)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-05-27 21:12:40 +02:00
|
|
|
return [
|
|
|
|
'id' => $this->id,
|
|
|
|
'date' => $this->activity->created_at->toAtomString(),
|
|
|
|
'thumbnail_url' => $this->activity->thumbnail_url,
|
|
|
|
'text' => $this->activity->text,
|
2016-06-07 01:37:41 +02:00
|
|
|
'url' => $this->activity->url,
|
2021-02-14 03:39:15 +01:00
|
|
|
'is_read' => $this->is_read,
|
2016-05-27 21:12:40 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|