1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* rmarchiv.tk |
5
|
|
|
* (c) 2016-2017 by Marcel 'ryg' Hering |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace App\Http\Controllers; |
9
|
|
|
|
10
|
|
|
use Carbon\Carbon; |
11
|
|
|
use App\Models\User; |
12
|
|
|
use Cmgmyr\Messenger\Models\Thread; |
13
|
|
|
use Cmgmyr\Messenger\Models\Message; |
14
|
|
|
use Illuminate\Support\Facades\Input; |
15
|
|
|
use Illuminate\Support\Facades\Session; |
16
|
|
|
use Cmgmyr\Messenger\Models\Participant; |
17
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
18
|
|
|
|
19
|
|
|
class MessagesController extends Controller |
20
|
|
|
{ |
21
|
|
|
public function index() |
22
|
|
|
{ |
23
|
|
|
$currentUserId = \Auth::id(); |
24
|
|
|
|
25
|
|
|
//Alle threads laden, abgesehen von gelöscht und archivierten empfängern |
26
|
|
|
//$threads = Thread::getAllLatest()->get(); |
|
|
|
|
27
|
|
|
|
28
|
|
|
// All threads that user is participating in |
29
|
|
|
$threads = Thread::forUser($currentUserId)->latest('updated_at')->paginate(25); |
|
|
|
|
30
|
|
|
// All threads that user is participating in, with new messages |
31
|
|
|
// $threads = Thread::forUserWithNewMessages($currentUserId)->latest('updated_at')->get(); |
|
|
|
|
32
|
|
|
|
33
|
|
|
$users = User::where('id', '!=', \Auth::id())->get(); |
34
|
|
|
|
35
|
|
|
return view('messenger.index', compact('threads', 'currentUserId', 'users')); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function create() |
39
|
|
|
{ |
40
|
|
|
$users = User::where('id', '!=', \Auth::id())->get(); |
41
|
|
|
|
42
|
|
|
return view('messenger.create', compact('users')); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function store() |
46
|
|
|
{ |
47
|
|
|
$input = Input::get(); |
|
|
|
|
48
|
|
|
$thread = Thread::create( |
|
|
|
|
49
|
|
|
[ |
50
|
|
|
'subject' => $input['subject'], |
51
|
|
|
] |
52
|
|
|
); |
53
|
|
|
// Message |
54
|
|
|
Message::create( |
|
|
|
|
55
|
|
|
[ |
56
|
|
|
'thread_id' => $thread->id, |
57
|
|
|
'user_id' => \Auth::user()->id, |
58
|
|
|
'body' => $input['msg'], |
59
|
|
|
] |
60
|
|
|
); |
61
|
|
|
// Sender |
62
|
|
|
Participant::create( |
|
|
|
|
63
|
|
|
[ |
64
|
|
|
'thread_id' => $thread->id, |
65
|
|
|
'user_id' => \Auth::user()->id, |
66
|
|
|
'last_read' => new Carbon(), |
67
|
|
|
] |
68
|
|
|
); |
69
|
|
|
// Recipients |
70
|
|
|
foreach ($input['recipients'] as $rec) { |
71
|
|
|
Participant::create( |
|
|
|
|
72
|
|
|
[ |
73
|
|
|
'thread_id' => $thread->id, |
74
|
|
|
'user_id' => $rec, |
75
|
|
|
] |
76
|
|
|
); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return redirect('messages'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function show($id) |
83
|
|
|
{ |
84
|
|
|
if (\Auth::check()) { |
85
|
|
|
try { |
86
|
|
|
$thread = Thread::findOrFail($id); |
87
|
|
|
} catch (ModelNotFoundException $e) { |
88
|
|
|
Session::flash('error_message', 'Thema mit der ID: '.$id.' konnte nicht gefunden werden.'); |
89
|
|
|
|
90
|
|
|
return redirect('messages'); |
91
|
|
|
} |
92
|
|
|
// show current user in list if not a current participant |
93
|
|
|
// $users = User::whereNotIn('id', $thread->participantsUserIds())->get(); |
|
|
|
|
94
|
|
|
// don't show the current user in list |
95
|
|
|
$userId = \Auth::user()->id; |
96
|
|
|
if ($thread->hasParticipant($userId)) { |
97
|
|
|
$users = User::whereNotIn('id', $thread->participantsUserIds($userId))->get(); |
98
|
|
|
$thread->markAsRead($userId); |
99
|
|
|
$messages = $thread->messages()->paginate(25); |
100
|
|
|
|
101
|
|
|
if (! Input::get('page')) { |
102
|
|
|
return redirect('messages/'.$id.'?page='.$messages->lastPage()); |
103
|
|
|
} else { |
104
|
|
|
return view('messenger.show', compact('thread', 'users', 'messages')); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
//Todo:View für Keine Berechtigung. |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function update($id) |
112
|
|
|
{ |
113
|
|
|
if (\Auth::check()) { |
114
|
|
|
try { |
115
|
|
|
$thread = Thread::findOrFail($id); |
116
|
|
|
} catch (ModelNotFoundException $e) { |
117
|
|
|
Session::flash('error_message', 'Thema mit der ID: '.$id.' konnte nicht gefunden werden.'); |
118
|
|
|
|
119
|
|
|
return redirect('messages'); |
120
|
|
|
} |
121
|
|
|
$thread->activateAllParticipants(); |
122
|
|
|
// Message |
123
|
|
|
Message::create( |
|
|
|
|
124
|
|
|
[ |
125
|
|
|
'thread_id' => $thread->id, |
126
|
|
|
'user_id' => \Auth::id(), |
127
|
|
|
'body' => Input::get('msg'), |
128
|
|
|
] |
129
|
|
|
); |
130
|
|
|
// Add replier as a participant |
131
|
|
|
$participant = Participant::firstOrCreate( |
|
|
|
|
132
|
|
|
[ |
133
|
|
|
'thread_id' => $thread->id, |
134
|
|
|
'user_id' => \Auth::user()->id, |
135
|
|
|
] |
136
|
|
|
); |
137
|
|
|
$participant->last_read = new Carbon(); |
138
|
|
|
$participant->save(); |
139
|
|
|
// Recipients |
140
|
|
|
if (Input::has('recipients')) { |
141
|
|
|
$thread->addParticipant(Input::get('recipients')); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return redirect('messages/'.$id); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
//Todo:View für Keine Berechtigung |
|
|
|
|
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.