PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.96
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.96
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / app / Models / Notification.php

Notification.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.96, at app/Models/Notification.php

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