Переглянути джерело

Add default licenses and license sync

Daniel Supernault 4 роки тому
батько
коміт
ea0fc90c92

+ 19 - 1
app/Http/Controllers/SettingsController.php

@@ -22,6 +22,7 @@ use App\Http\Controllers\Settings\{
 	SecuritySettings
 	SecuritySettings
 };
 };
 use App\Jobs\DeletePipeline\DeleteAccountPipeline;
 use App\Jobs\DeletePipeline\DeleteAccountPipeline;
+use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;
 
 
 class SettingsController extends Controller
 class SettingsController extends Controller
 {
 {
@@ -274,11 +275,21 @@ class SettingsController extends Controller
 		$license = $request->input('default');
 		$license = $request->input('default');
 		$sync = $request->input('sync') == 'on';
 		$sync = $request->input('sync') == 'on';
 		$media_descriptions = $request->input('media_descriptions') == '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);
 		$compose = json_decode($setting->compose_settings, true);
 		$changed = false;
 		$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) {
 		if(!isset($compose['default_license']) || $compose['default_license'] !== $license) {
 			$compose['default_license'] = (int) $license;
 			$compose['default_license'] = (int) $license;
 			$changed = true;
 			$changed = true;
@@ -295,6 +306,13 @@ class SettingsController extends Controller
 			Cache::forget('profile:compose:settings:' . $request->user()->id);
 			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!');
 		return redirect(route('settings'))->with('status', 'Media settings successfully updated!');
 	}
 	}
 
 

+ 47 - 0
app/Jobs/MediaPipeline/MediaSyncLicensePipeline.php

@@ -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);
+				}
+		});
+	}
+
+}

+ 1 - 1
resources/views/settings/media.blade.php

@@ -26,7 +26,7 @@
 		<div class="form-check pb-3">
 		<div class="form-check pb-3">
 			<input class="form-check-input" type="checkbox" name="sync">
 			<input class="form-check-input" type="checkbox" name="sync">
 			<label class="form-check-label font-weight-bold" for="">Sync Licenses</label>
 			<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>
 
 
 		<div class="form-check pb-3">
 		<div class="form-check pb-3">