From 4739c25fe01c0ff0172758c349fd5c37aabb196b Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 25 Feb 2019 11:56:24 -0700 Subject: [PATCH 1/4] Update Cache, prepare for laravel 5.8 --- .../Controllers/Admin/AdminSettingsController.php | 6 +++--- app/Http/Controllers/AdminController.php | 2 +- app/Http/Controllers/Api/BaseApiController.php | 2 +- app/Http/Controllers/Api/InstanceApiController.php | 4 ++-- app/Http/Controllers/ApiController.php | 3 ++- app/Http/Controllers/FederationController.php | 2 +- app/Http/Controllers/InternalApiController.php | 12 ++++++------ app/Http/Controllers/LikeController.php | 2 +- app/Http/Controllers/PublicApiController.php | 4 ++-- app/Http/Controllers/SearchController.php | 3 ++- app/Http/Controllers/SettingsController.php | 6 +++--- app/Http/Controllers/SiteController.php | 6 +++--- app/Profile.php | 2 +- app/Status.php | 5 ++--- app/Transformer/Api/StatusTransformer.php | 2 +- 15 files changed, 31 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/Admin/AdminSettingsController.php b/app/Http/Controllers/Admin/AdminSettingsController.php index a87499bc..97304f9f 100644 --- a/app/Http/Controllers/Admin/AdminSettingsController.php +++ b/app/Http/Controllers/Admin/AdminSettingsController.php @@ -36,16 +36,16 @@ trait AdminSettingsController public function settingsStorage(Request $request) { - $databaseSum = Cache::remember('admin:settings:storage:db:storageUsed', 360, function() { + $databaseSum = Cache::remember('admin:settings:storage:db:storageUsed', now()->addMinutes(360), function() { $q = 'SELECT sum(ROUND(((data_length + index_length)), 0)) AS size FROM information_schema.TABLES WHERE table_schema = ?'; $db = config('database.default'); $db = config("database.connections.{$db}.database"); return DB::select($q, [$db])[0]->size; }); - $mediaSum = Cache::remember('admin:settings:storage:media:storageUsed', 360, function() { + $mediaSum = Cache::remember('admin:settings:storage:media:storageUsed', now()->addMinutes(360), function() { return Media::sum('size'); }); - $backupSum = Cache::remember('admin:settings:storage:backups:storageUsed', 360, function() { + $backupSum = Cache::remember('admin:settings:storage:backups:storageUsed', now()->addMinutes(360), function() { $dir = storage_path('app/'.config('app.name')); $size = 0; foreach (glob(rtrim($dir, '/').'/*', GLOB_NOSORT) as $each) { diff --git a/app/Http/Controllers/AdminController.php b/app/Http/Controllers/AdminController.php index c0645ae5..019f7402 100644 --- a/app/Http/Controllers/AdminController.php +++ b/app/Http/Controllers/AdminController.php @@ -44,7 +44,7 @@ class AdminController extends Controller public function home() { - $data = Cache::remember('admin:dashboard:home:data', 15, function() { + $data = Cache::remember('admin:dashboard:home:data', now()->addMinutes(15), function() { $day = config('database.default') == 'pgsql' ? 'DATE_PART(\'day\',' : 'day('; return [ 'failedjobs' => [ diff --git a/app/Http/Controllers/Api/BaseApiController.php b/app/Http/Controllers/Api/BaseApiController.php index 8b115b71..c3a72e15 100644 --- a/app/Http/Controllers/Api/BaseApiController.php +++ b/app/Http/Controllers/Api/BaseApiController.php @@ -57,7 +57,7 @@ class BaseApiController extends Controller { $pid = Auth::user()->profile->id; $page = $request->input('page') ?? 1; - $res = Cache::remember('profile:notifications:'.$pid.':page:'.$page, 5, function() use($pid) { + $res = Cache::remember('profile:notifications:'.$pid.':page:'.$page, now()->addMinutes(5), function() use($pid) { $timeago = Carbon::now()->subMonths(6); $notifications = Notification::whereHas('actor') ->whereProfileId($pid) diff --git a/app/Http/Controllers/Api/InstanceApiController.php b/app/Http/Controllers/Api/InstanceApiController.php index 1608dc4e..9d078d64 100644 --- a/app/Http/Controllers/Api/InstanceApiController.php +++ b/app/Http/Controllers/Api/InstanceApiController.php @@ -11,7 +11,7 @@ class InstanceApiController extends Controller { protected function getData() { - $contact = Cache::remember('api:v1:instance:contact', 1440, function() { + $contact = Cache::remember('api:v1:instance:contact', now()->addMinutes(1440), function() { $admin = User::whereIsAdmin(true)->first()->profile; return [ 'id' => $admin->id, @@ -56,7 +56,7 @@ class InstanceApiController extends Controller { public function instance() { - $res = Cache::remember('api:v1:instance', 60, function() { + $res = Cache::remember('api:v1:instance', now()->addMinutes(60), function() { return json_encode($this->getData()); }); diff --git a/app/Http/Controllers/ApiController.php b/app/Http/Controllers/ApiController.php index b6810f02..399d99a8 100644 --- a/app/Http/Controllers/ApiController.php +++ b/app/Http/Controllers/ApiController.php @@ -10,6 +10,7 @@ use Illuminate\Http\Request; class ApiController extends BaseApiController { + // todo: deprecate and remove public function hydrateLikes(Request $request) { $this->validate($request, [ @@ -18,7 +19,7 @@ class ApiController extends BaseApiController ]); $profile = Auth::user()->profile; - $res = Cache::remember('api:like-ids:user:'.$profile->id, 1440, function () use ($profile) { + $res = Cache::remember('api:like-ids:user:'.$profile->id, now()->addDays(1), function () use ($profile) { return Like::whereProfileId($profile->id) ->orderBy('id', 'desc') ->take(1000) diff --git a/app/Http/Controllers/FederationController.php b/app/Http/Controllers/FederationController.php index cc21e962..9ff70c20 100644 --- a/app/Http/Controllers/FederationController.php +++ b/app/Http/Controllers/FederationController.php @@ -80,7 +80,7 @@ class FederationController extends Controller public function nodeinfo() { - $res = Cache::remember('api:nodeinfo', 60, function () { + $res = Cache::remember('api:nodeinfo', now()->addHours(1), function () { return [ 'metadata' => [ 'nodeName' => config('app.name'), diff --git a/app/Http/Controllers/InternalApiController.php b/app/Http/Controllers/InternalApiController.php index 8e41f9f3..c52e917d 100644 --- a/app/Http/Controllers/InternalApiController.php +++ b/app/Http/Controllers/InternalApiController.php @@ -105,10 +105,10 @@ class InternalApiController extends Controller { $profile = Auth::user()->profile; $pid = $profile->id; - $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) { + $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(60), function() use ($pid) { return Follower::whereProfileId($pid)->pluck('following_id')->toArray(); }); - $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) { + $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(60), function() use($pid) { return UserFilter::whereUserId($pid) ->whereFilterableType('App\Profile') ->whereIn('filter_type', ['mute', 'block']) @@ -161,10 +161,10 @@ class InternalApiController extends Controller { $profile = Auth::user()->profile; $pid = $profile->id; - $following = Cache::remember('feature:discover:following:'.$pid, 60, function() use ($pid) { + $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(60), function() use ($pid) { return Follower::whereProfileId($pid)->pluck('following_id')->toArray(); }); - $filters = Cache::remember("user:filter:list:$pid", 60, function() use($pid) { + $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(60), function() use($pid) { return UserFilter::whereUserId($pid) ->whereFilterableType('App\Profile') ->whereIn('filter_type', ['mute', 'block']) @@ -200,10 +200,10 @@ class InternalApiController extends Controller { $profile = Auth::user()->profile; $pid = $profile->id; - $following = Cache::remember('feature:discover:following:'.$pid, 15, function() use ($pid) { + $following = Cache::remember('feature:discover:following:'.$pid, now()->addMinutes(15), function() use ($pid) { return Follower::whereProfileId($pid)->pluck('following_id')->toArray(); }); - $filters = Cache::remember("user:filter:list:$pid", 15, function() use($pid) { + $filters = Cache::remember("user:filter:list:$pid", now()->addMinutes(15), function() use($pid) { $private = Profile::whereIsPrivate(true) ->orWhere('unlisted', true) ->orWhere('status', '!=', null) diff --git a/app/Http/Controllers/LikeController.php b/app/Http/Controllers/LikeController.php index ecb6b29d..9ee452d3 100644 --- a/app/Http/Controllers/LikeController.php +++ b/app/Http/Controllers/LikeController.php @@ -48,7 +48,7 @@ class LikeController extends Controller ->take(1000) ->pluck('status_id'); - Cache::put('api:like-ids:user:'.$profile->id, $likes, 1440); + Cache::put('api:like-ids:user:'.$profile->id, $likes, now()->addMinutes(1440)); if ($request->ajax()) { $response = ['code' => 200, 'msg' => 'Like saved', 'count' => $count]; diff --git a/app/Http/Controllers/PublicApiController.php b/app/Http/Controllers/PublicApiController.php index 6bd1ef91..7917335c 100644 --- a/app/Http/Controllers/PublicApiController.php +++ b/app/Http/Controllers/PublicApiController.php @@ -223,7 +223,7 @@ class PublicApiController extends Controller // $timeline = Timeline::build()->local(); $pid = Auth::user()->profile->id; - $private = Cache::remember('profiles:private', 1440, function() { + $private = Cache::remember('profiles:private', now()->addMinutes(1440), function() { return Profile::whereIsPrivate(true) ->orWhere('unlisted', true) ->orWhere('status', '!=', null) @@ -317,7 +317,7 @@ class PublicApiController extends Controller // $timeline = Timeline::build()->local(); $pid = Auth::user()->profile->id; - $following = Cache::remember('profile:following:'.$pid, 1440, function() use($pid) { + $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) { $following = Follower::whereProfileId($pid)->pluck('following_id'); return $following->push($pid)->toArray(); }); diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 59cb65cc..ea6c074a 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -23,7 +23,7 @@ class SearchController extends Controller return; } $hash = hash('sha256', $tag); - $tokens = Cache::remember('api:search:tag:'.$hash, 5, function () use ($tag) { + $tokens = Cache::remember('api:search:tag:'.$hash, now()->addMinutes(5), function () use ($tag) { $tokens = collect([]); if(Helpers::validateUrl($tag)) { $remote = Helpers::fetchFromUrl($tag); @@ -85,6 +85,7 @@ class SearchController extends Controller 'value' => $item->username, 'tokens' => [$item->username], 'name' => $item->name, + 'id' => $item->id ]; }); $tokens->push($profiles); diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 6fdf858e..3c3be8d8 100644 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -74,7 +74,7 @@ class SettingsController extends Controller public function exportFollowing() { - $data = Cache::remember('account:export:profile:following:'.Auth::user()->profile->id, 1440, function() { + $data = Cache::remember('account:export:profile:following:'.Auth::user()->profile->id, now()->addMinutes(1440), function() { return Auth::user()->profile->following()->get()->map(function($i) { return $i->url(); }); @@ -86,7 +86,7 @@ class SettingsController extends Controller public function exportFollowers() { - $data = Cache::remember('account:export:profile:followers:'.Auth::user()->profile->id, 1440, function() { + $data = Cache::remember('account:export:profile:followers:'.Auth::user()->profile->id, now()->addMinutes(1440), function() { return Auth::user()->profile->followers()->get()->map(function($i) { return $i->url(); }); @@ -105,7 +105,7 @@ class SettingsController extends Controller if(!$exists) { return redirect()->back(); } - $data = Cache::remember('account:export:profile:muteblocklist:'.Auth::user()->profile->id, 1440, function() use($profile) { + $data = Cache::remember('account:export:profile:muteblocklist:'.Auth::user()->profile->id, now()->addMinutes(1440), function() use($profile) { return json_encode([ 'muted' => $profile->mutedProfileUrls(), 'blocked' => $profile->blockedProfileUrls() diff --git a/app/Http/Controllers/SiteController.php b/app/Http/Controllers/SiteController.php index a5a5f12e..2134a942 100644 --- a/app/Http/Controllers/SiteController.php +++ b/app/Http/Controllers/SiteController.php @@ -41,10 +41,10 @@ class SiteController extends Controller public function about() { - $res = Cache::remember('site:about', 120, function() { + $res = Cache::remember('site:about', now()->addMinutes(120), function() { $custom = Page::whereSlug('/site/about')->whereActive(true)->exists(); if($custom) { - $stats = Cache::remember('site:about:stats', 60, function() { + $stats = Cache::remember('site:about:stats', now()->addMinutes(60), function() { return [ 'posts' => Status::whereLocal(true)->count(), 'users' => User::count(), @@ -53,7 +53,7 @@ class SiteController extends Controller }); return View::make('site.about')->with('stats', $stats)->render(); } else { - $stats = Cache::remember('site:about:stats', 60, function() { + $stats = Cache::remember('site:about:stats', now()->addMinutes(60), function() { return [ 'posts' => Status::whereLocal(true)->count(), 'users' => User::count(), diff --git a/app/Profile.php b/app/Profile.php index 8cf7d110..0eb8255f 100644 --- a/app/Profile.php +++ b/app/Profile.php @@ -124,7 +124,7 @@ class Profile extends Model public function avatarUrl() { - $url = Cache::remember("avatar:{$this->id}", 1440, function () { + $url = Cache::remember("avatar:{$this->id}", now()->addDays(1), function () { $path = optional($this->avatar)->media_path; $version = hash('sha1', $this->avatar->updated_at); $path = "{$path}?v={$version}"; diff --git a/app/Status.php b/app/Status.php index 466ed410..6d8defa2 100644 --- a/app/Status.php +++ b/app/Status.php @@ -2,11 +2,10 @@ namespace App; -use Auth, Cache; +use Auth, Cache, Hashids, Storage; use App\Http\Controllers\StatusController; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; -use Storage; class Status extends Model { @@ -77,7 +76,7 @@ class Status extends Model public function thumb($showNsfw = false) { - return Cache::remember('status:thumb:'.$this->id, 40320, function() use ($showNsfw) { + return Cache::remember('status:thumb:'.$this->id, now()->addMinutes(15), function() use ($showNsfw) { $type = $this->type ?? $this->setType(); $is_nsfw = !$showNsfw ? $this->is_nsfw : false; if ($this->media->count() == 0 || $is_nsfw || !in_array($type,['photo', 'photo:album'])) { diff --git a/app/Transformer/Api/StatusTransformer.php b/app/Transformer/Api/StatusTransformer.php index 198a098f..9e09bd47 100644 --- a/app/Transformer/Api/StatusTransformer.php +++ b/app/Transformer/Api/StatusTransformer.php @@ -62,7 +62,7 @@ class StatusTransformer extends Fractal\TransformerAbstract public function includeMediaAttachments(Status $status) { - return Cache::remember('status:transformer:media:attachments:'.$status->id, 1440, function() use($status) { + return Cache::remember('status:transformer:media:attachments:'.$status->id, now()->addMinutes(1440), function() use($status) { $media = $status->media()->orderBy('order')->get(); return $this->collection($media, new MediaTransformer()); }); From e2707ba1747a8174cb26f705046650b908ee5791 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 25 Feb 2019 12:33:47 -0700 Subject: [PATCH 2/4] Update Image util --- app/Util/Media/Image.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Util/Media/Image.php b/app/Util/Media/Image.php index 9351b8c0..06e8be9f 100644 --- a/app/Util/Media/Image.php +++ b/app/Util/Media/Image.php @@ -4,7 +4,7 @@ namespace App\Util\Media; use App\Media; use Image as Intervention; -use Storage; +use Cache, Storage; class Image { @@ -141,6 +141,7 @@ class Image } $media->save(); + Cache::forget('status:thumb:'.$media->status_id); } catch (Exception $e) { } } From d2823e64fa108a00ed03fafde1d1f3b73057bce4 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 25 Feb 2019 14:23:54 -0700 Subject: [PATCH 3/4] Update ActivityPub --- app/Util/ActivityPub/Helpers.php | 10 +++++----- app/Util/ActivityPub/HttpSignature.php | 3 ++- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/app/Util/ActivityPub/Helpers.php b/app/Util/ActivityPub/Helpers.php index 3a73c1a9..d0f1296f 100644 --- a/app/Util/ActivityPub/Helpers.php +++ b/app/Util/ActivityPub/Helpers.php @@ -30,7 +30,7 @@ class Helpers { public static function validateObject($data) { // todo: undo - $verbs = ['Create', 'Announce', 'Like', 'Follow', 'Delete', 'Accept', 'Reject']; + $verbs = ['Create', 'Announce', 'Like', 'Follow', 'Delete', 'Accept', 'Reject', 'Undo']; $valid = Validator::make($data, [ 'type' => [ @@ -41,7 +41,7 @@ class Helpers { 'actor' => 'required|string', 'object' => 'required', 'object.type' => 'required_if:type,Create', - 'object.attachment' => 'required_if:type,Create', + //'object.attachment' => 'required_if:type,Create', 'object.attributedTo' => 'required_if:type,Create', 'published' => 'required_if:type,Create|date' ])->passes(); @@ -136,7 +136,7 @@ class Helpers { '127.0.0.1', 'localhost', '::1' ]; - $valid = filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_SCHEME_REQUIRED|FILTER_FLAG_HOST_REQUIRED); + $valid = filter_var($url, FILTER_VALIDATE_URL); if(in_array(parse_url($valid, PHP_URL_HOST), $localhosts)) { return false; @@ -232,8 +232,8 @@ class Helpers { $ts = is_array($res['published']) ? $res['published'][0] : $res['published']; $status = new Status; $status->profile_id = $profile->id; - $status->url = $url; - $status->uri = $url; + $status->url = isset($res['url']) ? $res['url'] : $url; + $status->uri = isset($res['url']) ? $res['url'] : $url; $status->caption = strip_tags($res['content']); $status->rendered = Purify::clean($res['content']); $status->created_at = Carbon::parse($ts); diff --git a/app/Util/ActivityPub/HttpSignature.php b/app/Util/ActivityPub/HttpSignature.php index 72a874f7..d6ed0040 100644 --- a/app/Util/ActivityPub/HttpSignature.php +++ b/app/Util/ActivityPub/HttpSignature.php @@ -108,7 +108,8 @@ class HttpSignature { '(request-target)' => 'post '.parse_url($url, PHP_URL_PATH), 'Date' => $date->format('D, d M Y H:i:s \G\M\T'), 'Host' => parse_url($url, PHP_URL_HOST), - 'Content-Type' => 'application/activity+json', + 'Accept' => 'application/activity+json, application/json', + 'Content-Type' => 'application/activity+json' ]; if($digest) { From a5742f5729444d4bd537b28717b6e8a9d38e95bd Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 25 Feb 2019 14:25:20 -0700 Subject: [PATCH 4/4] Update ActivityPub --- app/Util/ActivityPub/Helpers.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Util/ActivityPub/Helpers.php b/app/Util/ActivityPub/Helpers.php index d0f1296f..298bd68a 100644 --- a/app/Util/ActivityPub/Helpers.php +++ b/app/Util/ActivityPub/Helpers.php @@ -41,7 +41,7 @@ class Helpers { 'actor' => 'required|string', 'object' => 'required', 'object.type' => 'required_if:type,Create', - //'object.attachment' => 'required_if:type,Create', + 'object.attachment' => 'required_if:type,Create', 'object.attributedTo' => 'required_if:type,Create', 'published' => 'required_if:type,Create|date' ])->passes();