From f34a1e9d8e2d72c1fb9401a811dcc76e107a92a6 Mon Sep 17 00:00:00 2001 From: Daniel Supernault Date: Wed, 20 Apr 2022 05:07:20 -0600 Subject: [PATCH] Add Conversations model --- app/Http/Controllers/Api/ApiV1Controller.php | 25 +++++--- .../Controllers/DirectMessageController.php | 27 ++++++++ .../Controllers/StoryComposeController.php | 27 ++++++++ app/Models/Conversation.php | 11 ++++ app/Util/ActivityPub/Inbox.php | 39 ++++++++++++ ...4_20_061915_create_conversations_table.php | 62 +++++++++++++++++++ 6 files changed, 181 insertions(+), 10 deletions(-) create mode 100644 app/Models/Conversation.php create mode 100644 database/migrations/2022_04_20_061915_create_conversations_table.php diff --git a/app/Http/Controllers/Api/ApiV1Controller.php b/app/Http/Controllers/Api/ApiV1Controller.php index 0bdf12213..f2c2007a9 100644 --- a/app/Http/Controllers/Api/ApiV1Controller.php +++ b/app/Http/Controllers/Api/ApiV1Controller.php @@ -81,6 +81,7 @@ use App\Jobs\MediaPipeline\MediaSyncLicensePipeline; use App\Services\DiscoverService; use App\Services\CustomEmojiService; use App\Services\MarkerService; +use App\Models\Conversation; class ApiV1Controller extends Controller { @@ -1876,18 +1877,22 @@ class ApiV1Controller extends Controller return $q->whereToId($pid)->whereIsHidden(true); }); } else { - $dms = DirectMessage::when($scope === 'inbox', function($q, $scope) use($pid) { - return $q->whereIsHidden(false)->where('to_id', $pid)->orWhere('from_id', $pid); - }) - ->when($scope === 'sent', function($q, $scope) use($pid) { - return $q->whereFromId($pid)->groupBy('to_id'); - }) - ->when($scope === 'requests', function($q, $scope) use($pid) { - return $q->whereToId($pid)->whereIsHidden(true); - }); + $dms = Conversation::when($scope === 'inbox', function($q, $scope) use($pid) { + return $q->whereIsHidden(false) + ->where('to_id', $pid) + ->orWhere('from_id', $pid) + ->orderByDesc('status_id') + ->groupBy(['to_id', 'from_id']); + }) + ->when($scope === 'sent', function($q, $scope) use($pid) { + return $q->whereFromId($pid)->groupBy('to_id'); + }) + ->when($scope === 'requests', function($q, $scope) use($pid) { + return $q->whereToId($pid)->whereIsHidden(true); + }); } - $dms = $dms->orderByDesc('created_at') + $dms = $dms->orderByDesc('status_id') ->simplePaginate($limit) ->map(function($dm) use($pid) { $from = $pid == $dm->to_id ? $dm->from_id : $dm->to_id; diff --git a/app/Http/Controllers/DirectMessageController.php b/app/Http/Controllers/DirectMessageController.php index d59abc0eb..8b48f0ec5 100644 --- a/app/Http/Controllers/DirectMessageController.php +++ b/app/Http/Controllers/DirectMessageController.php @@ -20,6 +20,7 @@ use App\Jobs\StatusPipeline\NewStatusPipeline; use Illuminate\Support\Str; use App\Util\ActivityPub\Helpers; use App\Services\WebfingerService; +use App\Models\Conversation; class DirectMessageController extends Controller { @@ -329,6 +330,19 @@ class DirectMessageController extends Controller $dm->type = $request->input('type'); $dm->save(); + Conversation::updateOrInsert( + [ + 'to_id' => $recipient->id, + 'from_id' => $profile->id + ], + [ + 'type' => $dm->type, + 'status_id' => $status->id, + 'dm_id' => $dm->id, + 'is_hidden' => $hidden + ] + ); + if(filter_var($msg, FILTER_VALIDATE_URL)) { if(Helpers::validateUrl($msg)) { $dm->type = 'link'; @@ -573,6 +587,19 @@ class DirectMessageController extends Controller $dm->is_hidden = $hidden; $dm->save(); + Conversation::updateOrInsert( + [ + 'to_id' => $recipient->id, + 'from_id' => $profile->id + ], + [ + 'type' => $dm->type, + 'status_id' => $status->id, + 'dm_id' => $dm->id, + 'is_hidden' => $hidden + ] + ); + if($recipient->domain) { $this->remoteDeliver($dm); } diff --git a/app/Http/Controllers/StoryComposeController.php b/app/Http/Controllers/StoryComposeController.php index ec2ebfcf3..93486a488 100644 --- a/app/Http/Controllers/StoryComposeController.php +++ b/app/Http/Controllers/StoryComposeController.php @@ -28,6 +28,7 @@ use App\Jobs\StoryPipeline\StoryReplyDeliver; use App\Jobs\StoryPipeline\StoryFanout; use App\Jobs\StoryPipeline\StoryDelete; use ImageOptimizer; +use App\Models\Conversation; class StoryComposeController extends Controller { @@ -420,6 +421,19 @@ class StoryComposeController extends Controller ]); $dm->save(); + Conversation::updateOrInsert( + [ + 'to_id' => $story->profile_id, + 'from_id' => $pid + ], + [ + 'type' => 'story:react', + 'status_id' => $status->id, + 'dm_id' => $dm->id, + 'is_hidden' => false + ] + ); + if($story->local) { // generate notification $n = new Notification; @@ -481,6 +495,19 @@ class StoryComposeController extends Controller ]); $dm->save(); + Conversation::updateOrInsert( + [ + 'to_id' => $story->profile_id, + 'from_id' => $pid + ], + [ + 'type' => 'story:comment', + 'status_id' => $status->id, + 'dm_id' => $dm->id, + 'is_hidden' => false + ] + ); + if($story->local) { // generate notification $n = new Notification; diff --git a/app/Models/Conversation.php b/app/Models/Conversation.php new file mode 100644 index 000000000..4f541a097 --- /dev/null +++ b/app/Models/Conversation.php @@ -0,0 +1,11 @@ +type = 'text'; $dm->save(); + Conversation::updateOrInsert( + [ + 'to_id' => $profile->id, + 'from_id' => $actor->id + ], + [ + 'type' => 'text', + 'status_id' => $status->id, + 'dm_id' => $dm->id, + 'is_hidden' => $hidden + ] + ); + if(count($activity['attachment'])) { $photos = 0; $videos = 0; @@ -911,6 +924,19 @@ class Inbox ]); $dm->save(); + Conversation::updateOrInsert( + [ + 'to_id' => $story->profile_id, + 'from_id' => $actorProfile->id + ], + [ + 'type' => 'story:react', + 'status_id' => $status->id, + 'dm_id' => $dm->id, + 'is_hidden' => false + ] + ); + $n = new Notification; $n->profile_id = $dm->to_id; $n->actor_id = $dm->from_id; @@ -1007,6 +1033,19 @@ class Inbox ]); $dm->save(); + Conversation::updateOrInsert( + [ + 'to_id' => $story->profile_id, + 'from_id' => $actorProfile->id + ], + [ + 'type' => 'story:comment', + 'status_id' => $status->id, + 'dm_id' => $dm->id, + 'is_hidden' => false + ] + ); + $n = new Notification; $n->profile_id = $dm->to_id; $n->actor_id = $dm->from_id; diff --git a/database/migrations/2022_04_20_061915_create_conversations_table.php b/database/migrations/2022_04_20_061915_create_conversations_table.php new file mode 100644 index 000000000..e94b6fdc5 --- /dev/null +++ b/database/migrations/2022_04_20_061915_create_conversations_table.php @@ -0,0 +1,62 @@ +bigIncrements('id'); + $table->bigInteger('to_id')->unsigned()->index(); + $table->bigInteger('from_id')->unsigned()->index(); + $table->bigInteger('dm_id')->unsigned()->nullable(); + $table->bigInteger('status_id')->unsigned()->nullable(); + $table->string('type')->nullable(); + $table->boolean('is_hidden')->default(false)->index(); + $table->boolean('has_seen')->default(false)->index(); + $table->json('metadata')->nullable(); + $table->unique(['to_id', 'from_id']); + $table->timestamps(); + }); + + sleep(10); + + if(DirectMessage::count()) { + + foreach(DirectMessage::lazy() as $msg) { + Conversation::updateOrInsert([ + 'to_id' => $msg->to_id, + 'from_id' => $msg->from_id, + ], + [ + 'dm_id' => $msg->id, + 'status_id' => $msg->status_id, + 'type' => $msg->type, + 'created_at' => $msg->created_at, + 'updated_at' => $msg->updated_at + ]); + } + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('conversations'); + } +}