forked from agorakit/agorakit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiscussion.php
150 lines (127 loc) · 3.71 KB
/
Discussion.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace App;
use App\Traits\HasStatus;
use App\Traits\HasControlledTags;
use Cviebrock\EloquentTaggable\Taggable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Nicolaslopezj\Searchable\SearchableTrait;
use Venturecraft\Revisionable\RevisionableTrait;
use Watson\Validating\ValidatingTrait;
use App\User;
use App\UserReadDiscussion;
use Auth;
use Carbon\Carbon;
class Discussion extends Model
{
use RevisionableTrait;
use ValidatingTrait;
use SoftDeletes;
use Taggable;
use SearchableTrait;
use HasStatus;
use HasControlledTags;
protected $rules = [
'name' => 'required',
'body' => 'required',
'user_id' => 'required|exists:users,id',
'group_id' => 'required|exists:groups,id',
];
protected $keepRevisionOf = ['name', 'body', 'status'];
protected $table = 'discussions';
protected $fillable = ['name', 'body', 'group_id'];
public $timestamps = true;
public $unreadcounter;
public $read_comments;
protected $casts = [
'user_id' => 'integer',
'deleted_at' => 'datetime'
];
public $modelName = 'discussion';
/**
* Searchable rules.
*
* @var array
*/
protected $searchable = [
/*
* Columns and their priority in search results.
* Columns with higher values are more important.
* Columns with equal values have equal importance.
*
* @var array
*/
'columns' => [
'discussions.name' => 10,
'discussions.body' => 10,
],
];
public function getType()
{
return 'discussion';
}
/**
* Unread count of comments for the current user
*/
public function unReadCount()
{
if (Auth::guest()) {
return 0;
}
$userReadDiscussion = $this->userReadDiscussion->first();
if ($userReadDiscussion) {
return $this->comments->count() - $userReadDiscussion->read_comments + 1;
}
return $this->comments->count() + 1;
}
/**
* Mark this discussion as read for the current user
*/
public function markAsRead()
{
$userReadDiscussion = UserReadDiscussion::firstOrNew(['user_id' => Auth::user()->id, 'discussion_id' => $this->id]);
$userReadDiscussion->read_comments = $this->comments->count() + 1;
$userReadDiscussion->read_at = Carbon::now();
return $userReadDiscussion->save();
}
public function group()
{
return $this->belongsTo(\App\Group::class)->withTrashed();
}
public function user()
{
return $this->belongsTo(\App\User::class)->withTrashed();
}
public function comments()
{
return $this->hasMany(\App\Comment::class);
}
public function userReadDiscussion()
{
if (\Auth::check()) {
return $this->hasMany(\App\UserReadDiscussion::class)->where('user_id', '=', \Auth::user()->id);
} else {
abort(500, 'Need to be logged in to access this userReadDiscussion relation');
}
}
public function link()
{
return route('groups.discussions.show', [$this->group, $this]);
}
/**
* Returns the inbox email of this discussion (if it has one).
* A discussion has an inbox if INBOX_DRIVER is not null in .env
*/
public function inbox()
{
if (config('agorakit.inbox_driver')) {
return config('agorakit.inbox_prefix') . 'reply-' . $this->id . config('agorakit.inbox_suffix');
} else {
return false;
}
}
public function reactions()
{
return $this->morphMany(Reaction::class, 'reactable');
}
}