From c9452639945aa7c3eb1580283690214a0093f631 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Fri, 24 Jul 2020 19:49:49 -0600 Subject: [PATCH] Update Tag People, allow untagging yourself --- .../Controllers/InternalApiController.php | 16 ++++---- app/Http/Controllers/MediaTagController.php | 41 +++++++++++++++++++ app/Services/MediaTagService.php | 25 ++++++++++- .../assets/js/components/PostComponent.vue | 29 ++++++++++--- routes/web.php | 2 +- 5 files changed, 97 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/InternalApiController.php b/app/Http/Controllers/InternalApiController.php index 764893259..31ecd2563 100644 --- a/app/Http/Controllers/InternalApiController.php +++ b/app/Http/Controllers/InternalApiController.php @@ -332,6 +332,14 @@ class InternalApiController extends Controller $media->save(); } + $visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility; + $cw = $profile->cw == true ? true : $cw; + $status->is_nsfw = $cw; + $status->visibility = $visibility; + $status->scope = $visibility; + $status->type = $mediaType; + $status->save(); + foreach($tagged as $tg) { $mt = new MediaTag; $mt->status_id = $status->id; @@ -347,14 +355,6 @@ class InternalApiController extends Controller MediaTagService::sendNotification($mt); } - $visibility = $profile->unlisted == true && $visibility == 'public' ? 'unlisted' : $visibility; - $cw = $profile->cw == true ? true : $cw; - $status->is_nsfw = $cw; - $status->visibility = $visibility; - $status->scope = $visibility; - $status->type = $mediaType; - $status->save(); - NewStatusPipeline::dispatch($status); Cache::forget('user:account:id:'.$profile->user_id); Cache::forget('profile:status_count:'.$profile->id); diff --git a/app/Http/Controllers/MediaTagController.php b/app/Http/Controllers/MediaTagController.php index 54d2c2620..3b583f95d 100644 --- a/app/Http/Controllers/MediaTagController.php +++ b/app/Http/Controllers/MediaTagController.php @@ -3,7 +3,9 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use App\Services\MediaTagService; use App\MediaTag; +use App\Notification; use App\Profile; use App\UserFilter; use App\User; @@ -11,6 +13,11 @@ use Illuminate\Support\Str; class MediaTagController extends Controller { + public function __construct() + { + $this->middleware('auth'); + } + public function usernameLookup(Request $request) { abort_if(!$request->user(), 403); @@ -52,4 +59,38 @@ class MediaTagController extends Controller return $results; } + + public function untagProfile(Request $request) + { + abort_if(!$request->user(), 403); + + $this->validate($request, [ + 'status_id' => 'required', + 'profile_id' => 'required' + ]); + + $user = $request->user(); + $status_id = $request->input('status_id'); + $profile_id = (int) $request->input('profile_id'); + + abort_if((int) $user->profile_id !== $profile_id, 400); + + $tag = MediaTag::whereStatusId($status_id) + ->whereProfileId($profile_id) + ->first(); + + if(!$tag) { + return []; + } + Notification::whereItemType('App\MediaTag') + ->whereItemId($tag->id) + ->whereProfileId($profile_id) + ->whereAction('tagged') + ->delete(); + + MediaTagService::untag($status_id, $profile_id); + + return [200]; + + } } diff --git a/app/Services/MediaTagService.php b/app/Services/MediaTagService.php index 087487d44..fea1f2b5e 100644 --- a/app/Services/MediaTagService.php +++ b/app/Services/MediaTagService.php @@ -15,12 +15,24 @@ class MediaTagService const CACHE_KEY = 'pf:services:media_tags:id:'; public static function get($mediaId, $usernames = true) + { + return self::coldGet($mediaId, $usernames); + } + + public static function coldGet($mediaId, $usernames = true) { $k = 'pf:services:media_tags:get:sid:' . $mediaId; return Cache::remember($k, now()->addMinutes(60), function() use($mediaId, $usernames) { $key = self::CACHE_KEY . $mediaId; if(Redis::zCount($key, '-inf', '+inf') == 0) { - return []; + $tags = MediaTag::whereStatusId($mediaId)->get(); + if($tags->count() == 0) { + return []; + } + + foreach ($tags as $t) { + self::set($mediaId, $t->profile_id); + } } $res = Redis::zRange($key, 0, -1); if(!$usernames) { @@ -52,6 +64,7 @@ class MediaTagService } return [ + 'id' => (string) $id, 'username' => $profile->username, 'avatar' => $profile->avatarUrl() ]; @@ -75,4 +88,14 @@ class MediaTagService return; } + public static function untag($statusId, $profileId) + { + MediaTag::whereStatusId($statusId) + ->whereProfileId($profileId) + ->delete(); + $key = 'pf:services:media_tags:get:sid:' . $statusId; + Redis::zRem(self::CACHE_KEY.$statusId, $profileId); + Cache::forget($key); + return true; + } } \ No newline at end of file diff --git a/resources/assets/js/components/PostComponent.vue b/resources/assets/js/components/PostComponent.vue index 14fcd03d3..d6e3934af 100644 --- a/resources/assets/js/components/PostComponent.vue +++ b/resources/assets/js/components/PostComponent.vue @@ -595,16 +595,17 @@ title="Tagged People" body-class="list-group-flush py-3 px-0">
-
+
@@ -1392,6 +1393,22 @@ export default { showTaggedPeopleModal() { this.$refs.taggedModal.show(); + }, + + untagMe() { + this.$refs.taggedModal.hide(); + let id = this.user.id; + axios.post('/api/local/compose/tag/untagme', { + status_id: this.statusId, + profile_id: id + }).then(res => { + this.status.taggedPeople = this.status.taggedPeople.filter(t => { + return t.id != id; + }); + swal('Untagged', 'You have been untagged from this post.', 'success'); + }).catch(err => { + swal('An Error Occurred', 'Please try again later.', 'error'); + }); } }, diff --git a/routes/web.php b/routes/web.php index bfec403d8..9323b25d3 100644 --- a/routes/web.php +++ b/routes/web.php @@ -187,7 +187,7 @@ Route::domain(config('pixelfed.domain.app'))->middleware(['validemail', 'twofact Route::post('compose/media/update/{id}', 'MediaController@composeUpdate')->middleware('throttle:maxComposeMediaUpdatesPerHour,60')->middleware('throttle:maxComposeMediaUpdatesPerDay,1440')->middleware('throttle:maxComposeMediaUpdatesPerMonth,43800'); Route::get('compose/location/search', 'ApiController@composeLocationSearch'); Route::get('compose/tag/search', 'MediaTagController@usernameLookup'); - + Route::post('compose/tag/untagme', 'MediaTagController@untagProfile'); }); Route::group(['prefix' => 'admin'], function () { Route::post('moderate', 'Api\AdminApiController@moderate');