1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Transmissor\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\MessageRequest; |
6
|
|
|
use App\Jobs\SendNotifyMail; |
7
|
|
|
use App\Models\Message; |
8
|
|
|
use App\Models\Participant; |
9
|
|
|
use App\Models\Thread; |
10
|
|
|
use App\Models\User; |
11
|
|
|
use App\Phphub\Markdown\Markdown; |
12
|
|
|
use Carbon\Carbon; |
13
|
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException; |
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use Illuminate\Support\Facades\Auth; |
16
|
|
|
use Illuminate\Support\Facades\Input; |
17
|
|
|
use Illuminate\Support\Facades\Session; |
18
|
|
|
|
19
|
|
|
class MessagesController extends Controller |
20
|
|
|
{ |
21
|
|
|
public function index(Request $request) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
$threads = Thread::participateBy(Auth::id()); |
24
|
|
|
if (Auth::user()->newThreadsCount() == 0) { |
|
|
|
|
25
|
|
|
Auth::user()->message_count = 0; |
|
|
|
|
26
|
|
|
Auth::user()->save(); |
|
|
|
|
27
|
|
|
} |
28
|
|
|
return view('transmissor::users.messages.index', compact('threads', 'currentUserId')); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
View Code Duplication |
public function show($id) |
|
|
|
|
32
|
|
|
{ |
33
|
|
|
$thread = Thread::findOrFail($id); |
34
|
|
|
$participant = $thread->participant(); |
35
|
|
|
$messages = $thread->messages()->recent()->get(); |
36
|
|
|
|
37
|
|
|
$this->authorize('show', $thread); |
38
|
|
|
|
39
|
|
|
// counters |
40
|
|
|
$unread_message_count = $thread->userUnreadMessagesCount(Auth::id()); |
41
|
|
|
if ($unread_message_count > 0) { |
42
|
|
|
Auth::user()->message_count -= $unread_message_count; |
|
|
|
|
43
|
|
|
Auth::user()->save(); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
$thread->markAsRead(Auth::id()); |
46
|
|
|
|
47
|
|
|
return view('transmissor::users.messages.show', compact('thread', 'participant', 'messages', 'unread_message_count')); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
public function create($id = null) |
|
|
|
|
51
|
|
|
{ |
52
|
|
|
if ($id) { |
53
|
|
|
$recipient = User::findOrFail($id); |
54
|
|
|
$thread = Thread::between([$recipient->id, Auth::id()])->first(); |
55
|
|
|
if ($thread) { |
56
|
|
|
return redirect()->route('profile.transmissor.messages.show', $thread->id); |
57
|
|
|
} |
58
|
|
|
return view('transmissor::users.messages.create', compact('recipient')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$recipient = User::pluck('name'); |
62
|
|
|
|
63
|
|
|
return view('transmissor::users.messages.create', compact('recipient')); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
public function createSecureMessage($id) |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
// @todo Fazer com as seguintes opcoes |
69
|
|
|
// timePraAutoDestruicao |
70
|
|
|
// Grava em disco ou só memoria |
71
|
|
|
// Criptografia ponta a ponta com limite de data |
72
|
|
|
$recipient = User::findOrFail($id); |
73
|
|
|
|
74
|
|
|
$thread = Thread::between([$recipient->id, Auth::id()])->first(); |
75
|
|
|
if ($thread) { |
76
|
|
|
return redirect()->route('profile.transmissor.messages.show', $thread->id); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return view('transmissor::users.messages.create', compact('recipient')); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
public function store(MessageRequest $request, Markdown $markdown) |
|
|
|
|
83
|
|
|
{ |
84
|
|
|
$recipient = User::findOrFail($request->recipient_id); |
85
|
|
|
|
86
|
|
|
if ($request->thread_id) { |
87
|
|
|
$thread = Thread::findOrFail($request->thread_id); |
88
|
|
|
} else { |
89
|
|
|
$subject = Auth::user()->name . ' 给 ' . $recipient->name . ' 的私信。'; |
|
|
|
|
90
|
|
|
$thread = Thread::create(['subject' => $subject]); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// Message |
94
|
|
|
$message = $markdown->convertMarkdownToHtml($request->message); |
95
|
|
|
Message::create( |
96
|
|
|
[ |
97
|
|
|
'messageable_type' => Thread::class, |
98
|
|
|
'messageable_id' => $thread->id, |
99
|
|
|
'actorable_type' => User::class, |
100
|
|
|
'actorable_id' => Auth::id(), |
101
|
|
|
'body' => $message |
102
|
|
|
] |
103
|
|
|
); |
104
|
|
|
|
105
|
|
|
// Sender |
106
|
|
|
$participant = Participant::firstOrCreate( |
107
|
|
|
[ |
108
|
|
|
'messageable_type' => Thread::class, |
109
|
|
|
'messageable_id' => $thread->id, |
110
|
|
|
'actorable_type' => User::class, |
111
|
|
|
'actorable_id' => Auth::id() |
112
|
|
|
] |
113
|
|
|
); |
114
|
|
|
$participant->last_read = new Carbon; |
115
|
|
|
$participant->save(); |
116
|
|
|
|
117
|
|
|
// Recipient |
118
|
|
|
$thread->addParticipant($recipient->id); |
119
|
|
|
|
120
|
|
|
// Notify user by Email |
121
|
|
|
$job = (new SendNotifyMail('new_message', Auth::user(), $recipient, null, null, $message)) |
122
|
|
|
->delay(config('phphub.notify_delay')); |
123
|
|
|
dispatch($job); |
124
|
|
|
|
125
|
|
|
// notifications count |
126
|
|
|
$recipient->message_count++; |
127
|
|
|
$recipient->save(); |
128
|
|
|
|
129
|
|
|
return redirect()->route('profile.transmissor.messages.show', $thread->id); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.