Skip to content

Commit ea0fc90

Browse files
committed
Add default licenses and license sync
1 parent 072d55d commit ea0fc90

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

app/Http/Controllers/SettingsController.php

+19-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
SecuritySettings
2323
};
2424
use App\Jobs\DeletePipeline\DeleteAccountPipeline;
25+
use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;
2526

2627
class SettingsController extends Controller
2728
{
@@ -274,11 +275,21 @@ public function updateMediaSettings(Request $request)
274275
$license = $request->input('default');
275276
$sync = $request->input('sync') == 'on';
276277
$media_descriptions = $request->input('media_descriptions') == 'on';
278+
$uid = $request->user()->id;
277279

278-
$setting = UserSetting::whereUserId($request->user()->id)->firstOrFail();
280+
$setting = UserSetting::whereUserId($uid)->firstOrFail();
279281
$compose = json_decode($setting->compose_settings, true);
280282
$changed = false;
281283

284+
if($sync) {
285+
$key = 'pf:settings:mls_recently:'.$uid;
286+
if(Cache::get($key) == 2) {
287+
$msg = 'You can only sync licenses twice per 24 hours. Try again later.';
288+
return redirect(route('settings'))
289+
->with('error', $msg);
290+
}
291+
}
292+
282293
if(!isset($compose['default_license']) || $compose['default_license'] !== $license) {
283294
$compose['default_license'] = (int) $license;
284295
$changed = true;
@@ -295,6 +306,13 @@ public function updateMediaSettings(Request $request)
295306
Cache::forget('profile:compose:settings:' . $request->user()->id);
296307
}
297308

309+
if($sync) {
310+
$val = Cache::has($key) ? 2 : 1;
311+
Cache::put($key, $val, 86400);
312+
MediaSyncLicensePipeline::dispatch($uid, $license);
313+
return redirect(route('settings'))->with('status', 'Media licenses successfully synced! It may take a few minutes to take effect for every post.');
314+
}
315+
298316
return redirect(route('settings'))->with('status', 'Media settings successfully updated!');
299317
}
300318

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace App\Jobs\MediaPipeline;
4+
5+
use App\Media;
6+
use App\User;
7+
use Cache;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Contracts\Queue\ShouldQueue;
10+
use Illuminate\Foundation\Bus\Dispatchable;
11+
use Illuminate\Queue\InteractsWithQueue;
12+
use Illuminate\Queue\SerializesModels;
13+
use App\Services\StatusService;
14+
15+
class MediaSyncLicensePipeline implements ShouldQueue
16+
{
17+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
18+
19+
protected $userId;
20+
protected $licenseId;
21+
22+
public function __construct($userId, $licenseId)
23+
{
24+
$this->userId = $userId;
25+
$this->licenseId = $licenseId;
26+
}
27+
28+
public function handle()
29+
{
30+
$licenseId = $this->licenseId;
31+
32+
if(!$licenseId || !$this->userId) {
33+
return 1;
34+
}
35+
36+
Media::whereUserId($this->userId)
37+
->chunk(100, function($medias) use($licenseId) {
38+
foreach($medias as $media) {
39+
$media->license = $licenseId;
40+
$media->save();
41+
Cache::forget('status:transformer:media:attachments:'. $media->status_id);
42+
StatusService::del($media->status_id);
43+
}
44+
});
45+
}
46+
47+
}

resources/views/settings/media.blade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<div class="form-check pb-3">
2727
<input class="form-check-input" type="checkbox" name="sync">
2828
<label class="form-check-label font-weight-bold" for="">Sync Licenses</label>
29-
<p class="text-muted small help-text">Update existing posts with your new default license. You can sync once every 24 hours.</p>
29+
<p class="text-muted small help-text">Update existing posts with your new default license. You can sync twice every 24 hours.<br />License changes may not be reflected on remote servers.</p>
3030
</div>
3131

3232
<div class="form-check pb-3">

0 commit comments

Comments
 (0)