1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Coyote\Http\Controllers\Microblog; |
4
|
|
|
|
5
|
|
|
use Coyote\Events\MicroblogDeleted; |
6
|
|
|
use Coyote\Events\MicroblogSaved; |
7
|
|
|
use Coyote\Http\Controllers\Controller; |
8
|
|
|
use Coyote\Http\Requests\MicroblogRequest; |
9
|
|
|
use Coyote\Http\Resources\MicroblogResource; |
10
|
|
|
use Coyote\Microblog; |
11
|
|
|
use Coyote\Notifications\Microblog\DeletedNotification; |
12
|
|
|
use Coyote\Repositories\Contracts\MicroblogRepositoryInterface; |
13
|
|
|
use Coyote\Repositories\Criteria\WithTrashed; |
14
|
|
|
use Coyote\Repositories\Contracts\UserRepositoryInterface as UserRepository; |
15
|
|
|
use Coyote\Services\Stream\Activities\Create as Stream_Create; |
16
|
|
|
use Coyote\Services\Stream\Activities\Update as Stream_Update; |
17
|
|
|
use Coyote\Services\Stream\Activities\Delete as Stream_Delete; |
18
|
|
|
use Coyote\Services\Stream\Activities\Restore as Stream_Restore; |
19
|
|
|
use Coyote\Services\Stream\Objects\Microblog as Stream_Microblog; |
20
|
|
|
use Illuminate\Http\Request; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class SubmitController |
24
|
|
|
* @package Coyote\Http\Controllers\Microblog |
25
|
|
|
*/ |
26
|
|
|
class SubmitController extends Controller |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var UserRepository |
30
|
|
|
*/ |
31
|
|
|
private $user; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param UserRepository $user |
35
|
|
|
*/ |
36
|
|
|
public function __construct(UserRepository $user) |
37
|
|
|
{ |
38
|
|
|
parent::__construct(); |
39
|
|
|
|
40
|
|
|
$this->user = $user; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param MicroblogRequest $request |
45
|
|
|
* @param $microblog |
46
|
|
|
* @return MicroblogResource |
47
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
48
|
|
|
*/ |
49
|
|
|
public function save(MicroblogRequest $request, ?Microblog $microblog) |
50
|
|
|
{ |
51
|
|
|
$this->user->pushCriteria(new WithTrashed()); |
52
|
|
|
|
53
|
|
|
if (!$microblog->exists) { |
54
|
|
|
$microblog->user()->associate($this->auth); |
|
|
|
|
55
|
|
|
} else { |
56
|
|
|
$this->authorize('update', $microblog); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$microblog->fill($request->only(['text'])); |
60
|
|
|
|
61
|
|
|
$this->transaction(function () use ($microblog, $request) { |
62
|
|
|
$microblog->save(); |
63
|
|
|
$microblog->assets()->sync($request->input('assets')); |
64
|
|
|
|
65
|
|
|
$object = (new Stream_Microblog())->map($microblog); |
|
|
|
|
66
|
|
|
|
67
|
|
|
if ($microblog->wasRecentlyCreated) { |
68
|
|
|
// increase reputation points |
69
|
|
|
app('reputation.microblog.create')->map($microblog)->save(); |
|
|
|
|
70
|
|
|
|
71
|
|
|
// put this to activity stream |
72
|
|
|
stream(Stream_Create::class, $object); |
73
|
|
|
|
74
|
|
|
if ($this->auth->allow_subscribe) { |
75
|
|
|
// enable subscribe button |
76
|
|
|
$microblog->is_subscribed = true; |
|
|
|
|
77
|
|
|
$microblog->subscribers()->create(['user_id' => $this->auth->id]); |
78
|
|
|
} |
79
|
|
|
} else { |
80
|
|
|
stream(Stream_Update::class, $object); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$microblog->setTags(array_pluck($request->input('tags', []), 'name')); |
84
|
|
|
}); |
85
|
|
|
|
86
|
|
|
broadcast(new MicroblogSaved($microblog))->toOthers(); |
|
|
|
|
87
|
|
|
|
88
|
|
|
MicroblogResource::withoutWrapping(); |
89
|
|
|
|
90
|
|
|
$microblog->unsetRelation('assets'); |
91
|
|
|
$microblog->load(['assets', 'tags']); |
92
|
|
|
|
93
|
|
|
return new MicroblogResource($microblog); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Usuniecie wpisu z mikrobloga |
98
|
|
|
* |
99
|
|
|
* @param \Coyote\Microblog $microblog |
100
|
|
|
*/ |
101
|
|
|
public function delete($microblog) |
102
|
|
|
{ |
103
|
|
|
abort_if(!$microblog->exists, 404); |
104
|
|
|
$this->authorize('delete', $microblog); |
105
|
|
|
|
106
|
|
|
$this->transaction(function () use ($microblog) { |
107
|
|
|
$microblog->delete(); |
108
|
|
|
// cofniecie pkt reputacji |
109
|
|
|
app('reputation.microblog.create')->undo($microblog->id); |
|
|
|
|
110
|
|
|
|
111
|
|
|
// put this to activity stream |
112
|
|
|
stream(Stream_Delete::class, (new Stream_Microblog())->map($microblog)); |
113
|
|
|
}); |
114
|
|
|
|
115
|
|
|
if ($this->userId !== $microblog->user_id) { |
116
|
|
|
$microblog->user->notify(new DeletedNotification($microblog, $this->auth)); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
event(new MicroblogDeleted($microblog)); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function restore(int $id, MicroblogRepositoryInterface $repository) |
|
|
|
|
123
|
|
|
{ |
124
|
|
|
$microblog = Microblog::withTrashed()->findOrFail($id); |
125
|
|
|
|
126
|
|
|
$this->authorize('delete', $microblog); |
127
|
|
|
|
128
|
|
|
$this->transaction(function () use ($microblog) { |
129
|
|
|
$microblog->restore(); |
130
|
|
|
|
131
|
|
|
// put this to activity stream |
132
|
|
|
stream(Stream_Restore::class, (new Stream_Microblog())->map($microblog)); |
133
|
|
|
}); |
134
|
|
|
|
135
|
|
|
event(new MicroblogSaved($microblog)); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* @param Request $request |
140
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
141
|
|
|
*/ |
142
|
|
|
public function preview(Request $request) |
143
|
|
|
{ |
144
|
|
|
return response($this->getParser()->parse((string) $request->get('text'))); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param Microblog $microblog |
149
|
|
|
* @throws \Illuminate\Auth\Access\AuthorizationException |
150
|
|
|
*/ |
151
|
|
|
public function toggleSponsored(Microblog $microblog) |
152
|
|
|
{ |
153
|
|
|
$this->authorize('moderate', $microblog); |
154
|
|
|
|
155
|
|
|
$microblog->is_sponsored = !$microblog->is_sponsored; |
156
|
|
|
$microblog->save(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @return \Coyote\Services\Parser\Factories\PmFactory |
161
|
|
|
*/ |
162
|
|
|
private function getParser() |
163
|
|
|
{ |
164
|
|
|
return app('parser.microblog'); |
|
|
|
|
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.