From a4659fd2ab19f1385a896f6258de874e7aa929f1 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Mon, 9 May 2022 00:15:03 -0600 Subject: [PATCH] Update DirectMessageController to support new Metro 2.0 UI DMs --- .../Controllers/DirectMessageController.php | 72 ++++++++++++++++--- 1 file changed, 61 insertions(+), 11 deletions(-) diff --git a/app/Http/Controllers/DirectMessageController.php b/app/Http/Controllers/DirectMessageController.php index 8b48f0ec..8de1694f 100644 --- a/app/Http/Controllers/DirectMessageController.php +++ b/app/Http/Controllers/DirectMessageController.php @@ -19,6 +19,8 @@ use App\Services\MediaBlocklistService; use App\Jobs\StatusPipeline\NewStatusPipeline; use Illuminate\Support\Str; use App\Util\ActivityPub\Helpers; +use App\Services\AccountService; +use App\Services\StatusService; use App\Services\WebfingerService; use App\Models\Conversation; @@ -435,8 +437,10 @@ class DirectMessageController extends Controller ->get(); } - - $res = $res->map(function($s) use ($uid){ + $res = $res->filter(function($s) { + return $s && $s->status; + }) + ->map(function($s) use ($uid) { return [ 'id' => (string) $s->id, 'hidden' => (bool) $s->is_hidden, @@ -449,7 +453,8 @@ class DirectMessageController extends Controller 'reportId' => (string) $s->status_id, 'meta' => json_decode($s->meta,true) ]; - }); + }) + ->values(); $w = [ 'id' => (string) $r->id, @@ -458,10 +463,10 @@ class DirectMessageController extends Controller 'avatar' => $r->avatarUrl(), 'url' => $r->url(), 'muted' => UserFilter::whereUserId($uid) - ->whereFilterableId($r->id) - ->whereFilterableType('App\Profile') - ->whereFilterType('dm.mute') - ->first() ? true : false, + ->whereFilterableId($r->id) + ->whereFilterableType('App\Profile') + ->whereFilterType('dm.mute') + ->first() ? true : false, 'isLocal' => (bool) !$r->domain, 'domain' => $r->domain, 'timeAgo' => $r->created_at->diffForHumans(null, true, true), @@ -482,17 +487,62 @@ class DirectMessageController extends Controller $pid = $request->user()->profile_id; $dm = DirectMessage::whereFromId($pid) - ->whereStatusId($sid) - ->firstOrFail(); + ->whereStatusId($sid) + ->firstOrFail(); $status = Status::whereProfileId($pid) - ->findOrFail($dm->status_id); + ->findOrFail($dm->status_id); - if($dm->recipient->domain) { + $recipient = AccountService::get($dm->to_id); + + if(!$recipient) { + return response('', 422); + } + + if($recipient['local'] == false) { $dmc = $dm; $this->remoteDelete($dmc); } + if(Conversation::whereStatusId($sid)->count()) { + $latest = DirectMessage::where(['from_id' => $dm->from_id, 'to_id' => $dm->to_id]) + ->orWhere(['to_id' => $dm->from_id, 'from_id' => $dm->to_id]) + ->latest() + ->first(); + + if($latest->status_id == $sid) { + Conversation::where(['to_id' => $dm->from_id, 'from_id' => $dm->to_id]) + ->update([ + 'updated_at' => $latest->updated_at, + 'status_id' => $latest->status_id, + 'type' => $latest->type, + 'is_hidden' => false + ]); + + Conversation::where(['to_id' => $dm->to_id, 'from_id' => $dm->from_id]) + ->update([ + 'updated_at' => $latest->updated_at, + 'status_id' => $latest->status_id, + 'type' => $latest->type, + 'is_hidden' => false + ]); + } else { + Conversation::where([ + 'status_id' => $sid, + 'to_id' => $dm->from_id, + 'from_id' => $dm->to_id + ])->delete(); + + Conversation::where([ + 'status_id' => $sid, + 'from_id' => $dm->from_id, + 'to_id' => $dm->to_id + ])->delete(); + } + } + + StatusService::del($status->id, true); + $status->delete(); $dm->delete();