PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 2.7.0
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v2.7.0
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 2.7.0, 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\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 class Notification extends Model
18 {
19 protected $table = 'fcom_notifications';
20
21 protected $primaryKey = 'id';
22
23 protected $guarded = ['id'];
24
25 protected $fillable = [
26 'feed_id',
27 'object_id',
28 'src_user_id',
29 'src_object_type',
30 'action',
31 'route',
32 'content'
33 ];
34
35 public static function boot()
36 {
37 parent::boot();
38
39 static::deleting(function ($notification) {
40 NotificationSubscriber::where('object_id', $notification->id)
41 ->delete();
42 });
43 }
44
45 public function setRouteAttribute($value)
46 {
47 $this->attributes['route'] = maybe_serialize($value);
48 }
49
50 public function getRouteAttribute($value)
51 {
52 return Utility::safeUnserialize($value);
53 }
54
55 public function subscribers()
56 {
57 return $this->hasMany(NotificationSubscriber::class, 'object_id');
58 }
59
60 public function subscriber()
61 {
62 return $this->hasOne(NotificationSubscriber::class, 'object_id');
63 }
64
65 public function feed()
66 {
67 return $this->belongsTo(Feed::class, 'feed_id');
68 }
69
70 public function src_user()
71 {
72 return $this->belongsTo(User::class, 'src_user_id');
73 }
74
75 public function xprofile()
76 {
77 return $this->belongsTo(XProfile::class, 'src_user_id', 'user_id');
78 }
79
80 public function subscribe($userIds = [])
81 {
82 if (empty($userIds)) {
83 return $this;
84 }
85
86 $subscribersData = [];
87
88 $currentDateTime = current_time('mysql');
89
90 foreach ($userIds as $userId) {
91 $subscribersData[] = [
92 'user_id' => $userId,
93 'object_id' => $this->id,
94 'object_type' => 'notification',
95 'notification_type' => 'web',
96 'created_at' => $currentDateTime,
97 'updated_at' => $currentDateTime,
98 'is_read' => 0
99 ];
100 }
101
102 // let's insert by chunk, 50 per chunk
103 $chunks = array_chunk($subscribersData, 50);
104 foreach ($chunks as $chunk) {
105 NotificationSubscriber::insert($chunk);
106 }
107
108 return $this;
109 }
110
111 public function scopeByStatus($query, $status, $userId)
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