From fabb57a9d5d72a57f0a42b2e8e0e690c04b1d145 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Tue, 11 May 2021 23:25:10 -0600 Subject: [PATCH] Add profile pronouns --- .../Controllers/Settings/HomeSettings.php | 27 +++-- app/Models/UserPronoun.php | 11 ++ app/Services/PronounService.php | 102 ++++++++++++++++++ app/Transformer/Api/AccountTransformer.php | 4 +- ...5_12_042153_create_user_pronouns_table.php | 34 ++++++ resources/assets/js/components/Profile.vue | 4 +- resources/views/settings/home.blade.php | 12 +++ 7 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 app/Models/UserPronoun.php create mode 100644 app/Services/PronounService.php create mode 100644 database/migrations/2021_05_12_042153_create_user_pronouns_table.php diff --git a/app/Http/Controllers/Settings/HomeSettings.php b/app/Http/Controllers/Settings/HomeSettings.php index ab9a6fbb7..39e3edd87 100644 --- a/app/Http/Controllers/Settings/HomeSettings.php +++ b/app/Http/Controllers/Settings/HomeSettings.php @@ -16,6 +16,7 @@ use Mail; use Purify; use App\Mail\PasswordChange; use Illuminate\Http\Request; +use App\Services\PronounService; trait HomeSettings { @@ -30,18 +31,20 @@ trait HomeSettings $storage['percentUsed'] = ceil($storage['used'] / $storage['limit'] * 100); $storage['limitPretty'] = PrettyNumber::size($storage['limit']); $storage['usedPretty'] = PrettyNumber::size($storage['used']); + $pronouns = PronounService::get($id); - return view('settings.home', compact('storage')); + return view('settings.home', compact('storage', 'pronouns')); } public function homeUpdate(Request $request) { - $this->validate($request, [ - 'name' => 'required|string|max:'.config('pixelfed.max_name_length'), - 'bio' => 'nullable|string|max:'.config('pixelfed.max_bio_length'), - 'website' => 'nullable|url', - 'language' => 'nullable|string|min:2|max:5' - ]); + $this->validate($request, [ + 'name' => 'required|string|max:'.config('pixelfed.max_name_length'), + 'bio' => 'nullable|string|max:'.config('pixelfed.max_bio_length'), + 'website' => 'nullable|url', + 'language' => 'nullable|string|min:2|max:5', + 'pronouns' => 'nullable|array|max:4' + ]); $changes = false; $name = strip_tags(Purify::clean($request->input('name'))); @@ -50,6 +53,8 @@ trait HomeSettings $language = $request->input('language'); $user = Auth::user(); $profile = $user->profile; + $pronouns = $request->input('pronouns'); + $existingPronouns = PronounService::get($profile->id); $layout = $request->input('profile_layout'); if($layout) { $layout = !in_array($layout, ['metro', 'moment']) ? 'metro' : $layout; @@ -82,6 +87,14 @@ trait HomeSettings $user->language = $language; session()->put('locale', $language); } + + if($existingPronouns != $pronouns) { + if($pronouns && in_array('Select Pronoun(s)', $pronouns)) { + PronounService::clear($profile->id); + } else { + PronounService::put($profile->id, $pronouns); + } + } } if ($changes === true) { diff --git a/app/Models/UserPronoun.php b/app/Models/UserPronoun.php new file mode 100644 index 000000000..9fbf77d79 --- /dev/null +++ b/app/Models/UserPronoun.php @@ -0,0 +1,11 @@ +addHours(12); + + return Cache::remember($key, $ttl, function() use($id) { + $res = UserPronoun::whereProfileId($id)->first(); + return $res ? json_decode($res->pronouns, true) : []; + }); + } + + public static function put($id, $pronouns) + { + $res = UserPronoun::whereProfileId($id)->first(); + $key = 'user:pronouns:' . $id; + + if($res) { + $res->pronouns = json_encode($pronouns); + $res->save(); + Cache::forget($key); + AccountService::del($id); + return $res->pronouns; + } + + $res = new UserPronoun; + $res->profile_id = $id; + $res->pronouns = json_encode($pronouns); + $res->save(); + Cache::forget($key); + AccountService::del($id); + return $res->pronouns; + } + + public static function clear($id) + { + $res = UserPronoun::whereProfileId($id)->first(); + if($res) { + $res->pronouns = null; + $res->save(); + } + $key = 'user:pronouns:' . $id; + Cache::forget($key); + AccountService::del($id); + } + + public static function pronouns() + { + return [ + 'co', + 'cos', + 'e', + 'ey', + 'em', + 'eir', + 'fae', + 'faer', + 'he', + 'him', + 'his', + 'her', + 'hers', + 'hir', + 'mer', + 'mers', + 'ne', + 'nir', + 'nirs', + 'nee', + 'ner', + 'ners', + 'per', + 'pers', + 'she', + 'they', + 'them', + 'theirs', + 'thon', + 'thons', + 've', + 'ver', + 'vis', + 'vi', + 'vir', + 'xe', + 'xem', + 'xyr', + 'ze', + 'zir', + 'zie' + ]; + } +} diff --git a/app/Transformer/Api/AccountTransformer.php b/app/Transformer/Api/AccountTransformer.php index 16a45b97c..4b8e16976 100644 --- a/app/Transformer/Api/AccountTransformer.php +++ b/app/Transformer/Api/AccountTransformer.php @@ -5,6 +5,7 @@ namespace App\Transformer\Api; use Auth; use App\Profile; use League\Fractal; +use App\Services\PronounService; class AccountTransformer extends Fractal\TransformerAbstract { @@ -35,7 +36,8 @@ class AccountTransformer extends Fractal\TransformerAbstract 'is_admin' => (bool) $is_admin, 'created_at' => $profile->created_at->toJSON(), 'header_bg' => $profile->header_bg, - 'last_fetched_at' => optional($profile->last_fetched_at)->toJSON() + 'last_fetched_at' => optional($profile->last_fetched_at)->toJSON(), + 'pronouns' => PronounService::get($profile->id) ]; } diff --git a/database/migrations/2021_05_12_042153_create_user_pronouns_table.php b/database/migrations/2021_05_12_042153_create_user_pronouns_table.php new file mode 100644 index 000000000..9e394e2b1 --- /dev/null +++ b/database/migrations/2021_05_12_042153_create_user_pronouns_table.php @@ -0,0 +1,34 @@ +id(); + $table->unsignedInteger('user_id')->nullable()->unique()->index(); + $table->bigInteger('profile_id')->unique()->index(); + $table->json('pronouns')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('user_pronouns'); + } +} diff --git a/resources/assets/js/components/Profile.vue b/resources/assets/js/components/Profile.vue index 5cae79fd1..35d2f4e4b 100644 --- a/resources/assets/js/components/Profile.vue +++ b/resources/assets/js/components/Profile.vue @@ -145,7 +145,8 @@

- {{profile.display_name}} + {{profile.display_name}} + {{profile.pronouns.join('/')}}

{{truncate(profile.website,24)}}

@@ -387,6 +388,7 @@ + +
+ +
+ +

Select up to 4 pronouns that will appear on your profile.

+
+
@if(config_cache('pixelfed.enforce_account_limit'))

Storage Usage