From 68aa25400b1244eb3858fb3a5f89cd288fe26a8a Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Thu, 10 Jun 2021 21:07:35 -0600 Subject: [PATCH] Add Auto Following support for admins --- .../Admin/AdminSettingsController.php | 18 ++- app/Observers/UserObserver.php | 121 +++++++++++------- app/Services/ConfigCacheService.php | 5 +- resources/views/admin/settings/home.blade.php | 34 +++-- 4 files changed, 118 insertions(+), 60 deletions(-) diff --git a/app/Http/Controllers/Admin/AdminSettingsController.php b/app/Http/Controllers/Admin/AdminSettingsController.php index cdd68ea7f..380e7d8a0 100644 --- a/app/Http/Controllers/Admin/AdminSettingsController.php +++ b/app/Http/Controllers/Admin/AdminSettingsController.php @@ -135,7 +135,8 @@ trait AdminSettingsController 'enforce_account_limit' => 'pixelfed.enforce_account_limit', 'show_custom_css' => 'uikit.show_custom.css', 'show_custom_js' => 'uikit.show_custom.js', - 'cloud_storage' => 'pixelfed.cloud_storage' + 'cloud_storage' => 'pixelfed.cloud_storage', + 'account_autofollow' => 'account.autofollow' ]; foreach ($bools as $key => $value) { @@ -171,6 +172,21 @@ trait AdminSettingsController } } + if($request->filled('account_autofollow_usernames')) { + $usernames = explode(',', $request->input('account_autofollow_usernames')); + $names = []; + + foreach($usernames as $n) { + $p = Profile::whereUsername($n)->first(); + if(!$p) { + continue; + } + array_push($names, $p->username); + } + + ConfigCacheService::put('account.autofollow_usernames', implode(',', $names)); + } + Cache::forget(Config::CACHE_KEY); return redirect('/i/admin/settings')->with('status', 'Successfully updated settings!'); diff --git a/app/Observers/UserObserver.php b/app/Observers/UserObserver.php index 54398c62c..66e55eacc 100644 --- a/app/Observers/UserObserver.php +++ b/app/Observers/UserObserver.php @@ -3,63 +3,88 @@ namespace App\Observers; use App\Jobs\AvatarPipeline\CreateAvatar; +use App\Follower; use App\Profile; use App\User; use App\UserSetting; +use App\Jobs\FollowPipeline\FollowPipeline; use DB; class UserObserver { - /** - * Listen to the User created event. - * - * @param \App\User $user - * - * @return void - */ - public function saved(User $user) - { - if($user->status == 'deleted') { - return; - } - - if (empty($user->profile)) { - $profile = DB::transaction(function() use($user) { - $profile = new Profile(); - $profile->user_id = $user->id; - $profile->username = $user->username; - $profile->name = $user->name; - $pkiConfig = [ - 'digest_alg' => 'sha512', - 'private_key_bits' => 2048, - 'private_key_type' => OPENSSL_KEYTYPE_RSA, - ]; - $pki = openssl_pkey_new($pkiConfig); - openssl_pkey_export($pki, $pki_private); - $pki_public = openssl_pkey_get_details($pki); - $pki_public = $pki_public['key']; + /** + * Listen to the User created event. + * + * @param \App\User $user + * + * @return void + */ + public function saved(User $user) + { + if($user->status == 'deleted') { + return; + } - $profile->private_key = $pki_private; - $profile->public_key = $pki_public; - $profile->save(); - return $profile; - }); - DB::transaction(function() use($user, $profile) { - $user = User::findOrFail($user->id); - $user->profile_id = $profile->id; - $user->save(); + if (empty($user->profile)) { + $profile = DB::transaction(function() use($user) { + $profile = new Profile(); + $profile->user_id = $user->id; + $profile->username = $user->username; + $profile->name = $user->name; + $pkiConfig = [ + 'digest_alg' => 'sha512', + 'private_key_bits' => 2048, + 'private_key_type' => OPENSSL_KEYTYPE_RSA, + ]; + $pki = openssl_pkey_new($pkiConfig); + openssl_pkey_export($pki, $pki_private); + $pki_public = openssl_pkey_get_details($pki); + $pki_public = $pki_public['key']; - CreateAvatar::dispatch($profile); - }); + $profile->private_key = $pki_private; + $profile->public_key = $pki_public; + $profile->save(); + return $profile; + }); - } + DB::transaction(function() use($user, $profile) { + $user = User::findOrFail($user->id); + $user->profile_id = $profile->id; + $user->save(); - if (empty($user->settings)) { - DB::transaction(function() use($user) { - UserSetting::firstOrCreate([ - 'user_id' => $user->id - ]); - }); - } - } + CreateAvatar::dispatch($profile); + }); + + if(config_cache('account.autofollow') == true) { + $names = config_cache('account.autofollow_usernames'); + $names = explode(',', $names); + + if(!$names || !last($names)) { + return; + } + + $profiles = Profile::whereIn('username', $names)->get(); + + if($profiles) { + foreach($profiles as $p) { + $follower = new Follower; + $follower->profile_id = $profile->id; + $follower->following_id = $p->id; + $follower->save(); + + FollowPipeline::dispatch($follower); + } + } + } + } + + if (empty($user->settings)) { + DB::transaction(function() use($user) { + UserSetting::firstOrCreate([ + 'user_id' => $user->id + ]); + }); + } + + } } diff --git a/app/Services/ConfigCacheService.php b/app/Services/ConfigCacheService.php index 18ccab050..55c26475b 100644 --- a/app/Services/ConfigCacheService.php +++ b/app/Services/ConfigCacheService.php @@ -45,7 +45,10 @@ class ConfigCacheService 'uikit.show_custom.js', 'about.title', - 'pixelfed.cloud_storage' + 'pixelfed.cloud_storage', + + 'account.autofollow', + 'account.autofollow_usernames' ]; if(!config('instance.enable_cc')) { diff --git a/resources/views/admin/settings/home.blade.php b/resources/views/admin/settings/home.blade.php index 14bbf636c..3952b0491 100644 --- a/resources/views/admin/settings/home.blade.php +++ b/resources/views/admin/settings/home.blade.php @@ -184,19 +184,33 @@ +
-
-
- - -

Set a storage limit per user account.

-
- - -

Account limit size in KB.

-

{{config_cache('pixelfed.max_account_size')}} KB = {{floor(config_cache('pixelfed.max_account_size') / 1024)}} MB

+
+
+ + +

Set a storage limit per user account.

+ + +

Account limit size in KB.

+

{{config_cache('pixelfed.max_account_size')}} KB = {{floor(config_cache('pixelfed.max_account_size') / 1024)}} MB

+
+ +
+
+
+ + +

Enable auto follow accounts, new accounts will follow accounts you set.

+
+ + +

Add account usernames to follow separated by commas.

+
+