| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCommunity\App\Models; |
| 4 |
|
| 5 |
use FluentCommunity\App\Functions\Utility; |
| 6 |
use FluentCommunity\App\Models\XProfile; |
| 7 |
|
| 8 |
/** |
| 9 |
* Meta Model - DB Model for Notifications table |
| 10 |
* |
| 11 |
* Database Model |
| 12 |
* |
| 13 |
* @package FluentCommunity\App\Models |
| 14 |
* |
| 15 |
* @version 1.0.0 |
| 16 |
* |
| 17 |
* @property int $id |
| 18 |
* @property int|null $feed_id |
| 19 |
* @property int|null $object_id |
| 20 |
* @property int|null $src_user_id |
| 21 |
* @property string|null $src_object_type |
| 22 |
* @property string|null $action |
| 23 |
* @property mixed $route |
| 24 |
* @property string|null $content |
| 25 |
*/ |
| 26 |
class Notification extends Model |
| 27 |
{ |
| 28 |
protected $table = 'fcom_notifications'; |
| 29 |
|
| 30 |
protected $primaryKey = 'id'; |
| 31 |
|
| 32 |
protected $guarded = ['id']; |
| 33 |
|
| 34 |
protected $fillable = [ |
| 35 |
'feed_id', |
| 36 |
'object_id', |
| 37 |
'src_user_id', |
| 38 |
'src_object_type', |
| 39 |
'action', |
| 40 |
'route', |
| 41 |
'content' |
| 42 |
]; |
| 43 |
|
| 44 |
public static function boot() |
| 45 |
{ |
| 46 |
parent::boot(); |
| 47 |
|
| 48 |
static::deleting(function ($notification) { |
| 49 |
NotificationSubscriber::where('object_id', $notification->id) |
| 50 |
->delete(); |
| 51 |
}); |
| 52 |
} |
| 53 |
|
| 54 |
public function setRouteAttribute($value) |
| 55 |
{ |
| 56 |
$this->attributes['route'] = maybe_serialize($value); |
| 57 |
} |
| 58 |
|
| 59 |
public function getRouteAttribute($value) |
| 60 |
{ |
| 61 |
return Utility::safeUnserialize($value); |
| 62 |
} |
| 63 |
|
| 64 |
public function subscribers() |
| 65 |
{ |
| 66 |
return $this->hasMany(NotificationSubscriber::class, 'object_id'); |
| 67 |
} |
| 68 |
|
| 69 |
public function subscriber() |
| 70 |
{ |
| 71 |
return $this->hasOne(NotificationSubscriber::class, 'object_id'); |
| 72 |
} |
| 73 |
|
| 74 |
public function feed() |
| 75 |
{ |
| 76 |
return $this->belongsTo(Feed::class, 'feed_id'); |
| 77 |
} |
| 78 |
|
| 79 |
public function src_user() |
| 80 |
{ |
| 81 |
return $this->belongsTo(User::class, 'src_user_id'); |
| 82 |
} |
| 83 |
|
| 84 |
public function xprofile() |
| 85 |
{ |
| 86 |
return $this->belongsTo(XProfile::class, 'src_user_id', 'user_id'); |
| 87 |
} |
| 88 |
|
| 89 |
public function subscribe($userIds = []) |
| 90 |
{ |
| 91 |
if (empty($userIds)) { |
| 92 |
return $this; |
| 93 |
} |
| 94 |
|
| 95 |
$subscribersData = []; |
| 96 |
|
| 97 |
$currentDateTime = current_time('mysql'); |
| 98 |
|
| 99 |
foreach ($userIds as $userId) { |
| 100 |
$subscribersData[] = [ |
| 101 |
'user_id' => $userId, |
| 102 |
'object_id' => $this->id, |
| 103 |
'object_type' => 'notification', |
| 104 |
'notification_type' => 'web', |
| 105 |
'created_at' => $currentDateTime, |
| 106 |
'updated_at' => $currentDateTime, |
| 107 |
'is_read' => 0 |
| 108 |
]; |
| 109 |
} |
| 110 |
|
| 111 |
// let's insert by chunk, 50 per chunk |
| 112 |
$chunks = array_chunk($subscribersData, 50); |
| 113 |
foreach ($chunks as $chunk) { |
| 114 |
NotificationSubscriber::insert($chunk); |
| 115 |
} |
| 116 |
|
| 117 |
return $this; |
| 118 |
} |
| 119 |
|
| 120 |
public function scopeByStatus($query, $status, $userId) |
| 121 |
{ |
| 122 |
$valids = ['unread', 'read']; |
| 123 |
if (!in_array($status, $valids)) { |
| 124 |
return $query; |
| 125 |
} |
| 126 |
|
| 127 |
$value = $status === 'unread' ? 0 : 1; |
| 128 |
|
| 129 |
$query->whereHas('subscribers', function ($query) use ($userId, $value) { |
| 130 |
return $query->where('user_id', $userId) |
| 131 |
->where('is_read', $value); |
| 132 |
}); |
| 133 |
} |
| 134 |
|
| 135 |
public function scopeByType($query, $type) |
| 136 |
{ |
| 137 |
$valids = ['mentioned', 'following']; |
| 138 |
if (!in_array($type, $valids)) { |
| 139 |
return $query; |
| 140 |
} |
| 141 |
|
| 142 |
if ($type == 'mentioned') { |
| 143 |
$query->whereIn('action', ['feed/mentioned', 'mention_added']); |
| 144 |
} else { |
| 145 |
$query->whereNotIn('action', ['feed/mentioned', 'mention_added']); |
| 146 |
} |
| 147 |
|
| 148 |
return $query; |
| 149 |
} |
| 150 |
} |
| 151 |
|