Skip to content

Commit

Permalink
Add default licenses and license sync
Browse files Browse the repository at this point in the history
  • Loading branch information
dansup committed Jul 25, 2021
1 parent 072d55d commit ea0fc90
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
20 changes: 19 additions & 1 deletion app/Http/Controllers/SettingsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
SecuritySettings
};
use App\Jobs\DeletePipeline\DeleteAccountPipeline;
use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;

class SettingsController extends Controller
{
Expand Down Expand Up @@ -274,11 +275,21 @@ public function updateMediaSettings(Request $request)
$license = $request->input('default');
$sync = $request->input('sync') == 'on';
$media_descriptions = $request->input('media_descriptions') == 'on';
$uid = $request->user()->id;

$setting = UserSetting::whereUserId($request->user()->id)->firstOrFail();
$setting = UserSetting::whereUserId($uid)->firstOrFail();
$compose = json_decode($setting->compose_settings, true);
$changed = false;

if($sync) {
$key = 'pf:settings:mls_recently:'.$uid;
if(Cache::get($key) == 2) {
$msg = 'You can only sync licenses twice per 24 hours. Try again later.';
return redirect(route('settings'))
->with('error', $msg);
}
}

if(!isset($compose['default_license']) || $compose['default_license'] !== $license) {
$compose['default_license'] = (int) $license;
$changed = true;
Expand All @@ -295,6 +306,13 @@ public function updateMediaSettings(Request $request)
Cache::forget('profile:compose:settings:' . $request->user()->id);
}

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

return redirect(route('settings'))->with('status', 'Media settings successfully updated!');
}

Expand Down
47 changes: 47 additions & 0 deletions app/Jobs/MediaPipeline/MediaSyncLicensePipeline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace App\Jobs\MediaPipeline;

use App\Media;
use App\User;
use Cache;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Services\StatusService;

class MediaSyncLicensePipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

protected $userId;
protected $licenseId;

public function __construct($userId, $licenseId)
{
$this->userId = $userId;
$this->licenseId = $licenseId;
}

public function handle()
{
$licenseId = $this->licenseId;

if(!$licenseId || !$this->userId) {
return 1;
}

Media::whereUserId($this->userId)
->chunk(100, function($medias) use($licenseId) {
foreach($medias as $media) {
$media->license = $licenseId;
$media->save();
Cache::forget('status:transformer:media:attachments:'. $media->status_id);
StatusService::del($media->status_id);
}
});
}

}
2 changes: 1 addition & 1 deletion resources/views/settings/media.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<div class="form-check pb-3">
<input class="form-check-input" type="checkbox" name="sync">
<label class="form-check-label font-weight-bold" for="">Sync Licenses</label>
<p class="text-muted small help-text">Update existing posts with your new default license. You can sync once every 24 hours.</p>
<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>
</div>

<div class="form-check pb-3">
Expand Down

0 comments on commit ea0fc90

Please sign in to comment.