Skip to content

Commit

Permalink
Merge pull request #2222 from bakaphp/feat-user-like-messages
Browse files Browse the repository at this point in the history
feat: user like messages
  • Loading branch information
kaioken authored Oct 19, 2024
2 parents cd2efea + 3660360 commit 455f404
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 34 deletions.
33 changes: 13 additions & 20 deletions app/GraphQL/Social/Builders/Messages/MessageBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

namespace App\GraphQL\Social\Builders\Messages;

use Algolia\AlgoliaSearch\SearchClient;
use GraphQL\Type\Definition\ResolveInfo;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use Kanvas\Apps\Models\Apps;
use Kanvas\Social\Enums\AppEnum;
use Kanvas\Social\Interactions\Models\Interactions;
use Kanvas\Social\Messages\Models\Message;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

Expand Down Expand Up @@ -61,30 +60,24 @@ public function getGroupByDate(
);
}

public function searchSuggestions(
public function likedMessagesByUser(
mixed $root,
array $args,
GraphQLContext $context,
ResolveInfo $resolveInfo
): array {
$client = SearchClient::create(
config('scout.algolia.id'),
config('scout.algolia.secret')
);

): Builder {
$userId = (int) $args['id'];
$app = app(Apps::class);
$suggestionIndex = AppEnum::MESSAGE_SEARCH_SUGGESTION_INDEX->value;
if (! $app->get($suggestionIndex)) {
return ['error' => 'No index for message suggestion configure in your app'];
}

$index = $client->initIndex($app->get($suggestionIndex));

$results = $index->search($args['search'], [
'hitsPerPage' => 15,
'attributesToRetrieve' => ['name', 'description'],
]);
$like = Interactions::getByName('like', $app);

return $results['hits'];
return Message::join('users_interactions', 'messages.id', '=', 'users_interactions.entity_id')
->where('users_interactions.entity_namespace', '=', Message::class)
->where('users_interactions.interactions_id', '=', $like->getId())
->where('users_interactions.users_id', '=', $userId)
->where('users_interactions.is_deleted', '=', 0)
->where('messages.is_deleted', '=', 0)
->where('messages.apps_id', '=', $app->getId())
->select('messages.*');
}
}
28 changes: 28 additions & 0 deletions graphql/schemas/Social/message.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,32 @@ extend type Query @guard {
@field(
resolver: "App\\GraphQL\\Social\\Builders\\Messages\\MessageBuilder@searchSuggestions"
)

messagesLikedByUser(
id: ID!
where: _
@whereConditions(
columns: [
"id"
"parent_id"
"parent_unique_id"
"companies_id"
"uuid"
"slug"
"users_id"
"message_types_id"
"message"
"reactions_count"
"comments_count"
"total_liked"
"total_saved"
"total_shared"
]
)
orderBy: _ @orderBy(columns: ["created_at", "updated_at", "id"])
): [Message!]!
@paginate(
defaultCount: 25
builder: "App\\GraphQL\\Social\\Builders\\Messages\\MessageBuilder@likedMessagesByUser"
)
}
28 changes: 14 additions & 14 deletions tests/GraphQL/Event/EventSupportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,21 @@ public function testGetEventType(): void
}')->assertSee('eventTypes');
}

public function testGetEventDate(): void
{
$this->graphQL('
query {
eventDates {
data {
id,
date,
startTime,
endTime
/* public function testGetEventDate(): void
{
$this->graphQL('
query {
eventDates {
data {
id,
date,
startTime,
endTime
}
}
}
}')->assertSee('eventDates');
}

}')->assertSee('eventDates');
}
*/
public function testGetEventStatus(): void
{
$this->graphQL('
Expand Down
82 changes: 82 additions & 0 deletions tests/GraphQL/Social/MessageInteractionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,86 @@ public function testDisLikeMessage()
],
]);
}

/**
* testMessagesLikedByUser
*
* @return void
*/
public function testMessagesLikedByUser()
{
$messageType = MessageType::factory()->create();
$message = fake()->text();
$user = auth()->user();

$response = $this->graphQL(
'
mutation createMessage($input: MessageInput!) {
createMessage(input: $input) {
id
message
}
}
',
[
'input' => [
'message' => $message,
'message_verb' => $messageType->verb,
'system_modules_id' => 1,
'entity_id' => '1',
],
]
)->assertJson([
'data' => [
'createMessage' => [
'message' => $message,
],
],
]);

$id = $response->json('data.createMessage.id');

$response = $this->graphQL(
'
mutation likeMessage($id: ID!) {
likeMessage(id: $id)
}
',
[
'id' => $id,
]
)->assertJson([
'data' => [
'likeMessage' => true,
],
]);

// Run the GraphQL query
$this->graphQL(
'
query messagesLikedByUser($id: ID!) {
messagesLikedByUser(id: $id) {
data {
id
slug
message
}
}
}
',
[
'id' => $user->id,
]
)->assertJson([
'data' => [
'messagesLikedByUser' => [
'data' => [
[
'id' => $id,
],
],
],
],
]);
}
}

0 comments on commit 455f404

Please sign in to comment.