From 7cb795457ec0f81bfdab3926c3c60b5a8e30df7e Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Thu, 16 Aug 2018 23:18:03 +0100
Subject: [PATCH 01/52] Start adding feature: conversation with title
---
README.md | 30 +++++++++++++++---------------
1 file changed, 15 insertions(+), 15 deletions(-)
diff --git a/README.md b/README.md
index 9205558..00e973a 100644
--- a/README.md
+++ b/README.md
@@ -4,16 +4,16 @@
[](https://raw.githubusercontent.com/nahid/talk/master/LICENSE)
[](https://travis-ci.org/nahid/talk)
-Talk is a Laravel 5 based user conversation (inbox) system with realtime messaging. You can easily integrate this package with any Laravel based project. It helps you to develop a messaging system in just few minutes. Here is a project screenshot that was developed by Talk.
+Talk is a Laravel 5 based user conversation (inbox) system with realtime messaging. You can easily integrate this package with any Laravel based project. It helps you to develop a messaging system in just few minutes. Here is a project screenshot that was developed by Talk.
-Talk v2.1.0 supports realtime messaging. Learn more about [Talk Live Messaging](https://github.com/nahid/talk#realtime-messaging)
+Talk v2.1.0 supports realtime messaging. Learn more about [Talk Live Messaging](https://github.com/nahid/talk#realtime-messaging)
#### Feedback
-If you already used Talk, please share your experience with us. It will make the project better.
+If you already used Talk, please share your experience with us. It will make the project better.
-[Give us your feedback](https://github.com/nahid/talk/issues/43)
+[Give us your feedback](https://github.com/nahid/talk/issues/43)
#### Built with Talk
@@ -21,7 +21,7 @@ If you are using Talk in your project please share your project URL or project n
See which project was [Built with Talk](https://github.com/nahid/talk/issues/42).
-## Caution
+## Caution
> Do not migrate 1.1.7 from its higher version directly. Please try our [sample project](https://github.com/nahid/talk-example) first and then apply it on your project.
@@ -33,7 +33,7 @@ You may try [Talk-Example](https://github.com/nahid/talk-example) project.
Or you can try live [Demo](http://portal.inilabs.net/baseapp/v1.0/admin/message/inbox) by using this credentials:
```
-username: admin
+username: admin
password: admin
```
@@ -45,7 +45,7 @@ So let's start your tour :)
* Head to head messaging
* Realtime messaging
-* Creating new conversation
+* Creating new conversation with titles
* Message threads with latest one
* View conversations by user id or conversation id
* Support pagination in threads and messages
@@ -124,11 +124,11 @@ Its very easy to use. If you want to set authenticate user id globally then you
And now you can use it from anywhere with middleware. Suppose you have a Controller and you want to set authenticate user id globally then write this in controller constructor:
-
+
```php
$this->middleware('talk');
```
-
+
But instead of set id globally you can use these procedure from any method in controller:
@@ -185,7 +185,7 @@ void setAuthUserId($userid)
**Example**
-Constructor of a Controller is the best place to write this method.
+Constructor of a Controller is the best place to write this method.
```php
function __construct()
@@ -227,7 +227,7 @@ int|false isConversationExists($userid)
```php
if ($conversationId = Talk::isConversationExists($userId)) {
Talk::sendMessage($conversationId, $message);
-}
+}
```
### isAuthenticUser
@@ -245,7 +245,7 @@ boolean isAuthenticUser($conversationId, $userId)
```php
if (Talk::isAuthenticUser($conversationId, $userId)) {
Talk::sendMessage($conversationId, $message);
-}
+}
```
### sendMessage
@@ -375,7 +375,7 @@ Let's see how to use it with your views
{{$msg->humans_time}}
{{$msg->message}}
-
+
@endforeach
@@ -463,7 +463,7 @@ array readMessage($messageId)
### getReceiverInfo
-This method returns all the information about message receiver.
+This method returns all the information about message receiver.
> This method is deprecated from version 2.0.0 and it will be removed from version 2.0.2
@@ -544,7 +544,7 @@ Go to where you want to subscribe to work with message data follow this code.
From 5257a6871fdea8bad5c220e875eaecf663a34cd9 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 17 Aug 2018 02:40:59 +0100
Subject: [PATCH 02/52] minor changes - nothing to care about :)
---
.../2015_10_05_110621_create_tags_table.php | 33 +++++++++++++++++++
...5_110622_create_conversation_tag_table.php | 33 +++++++++++++++++++
...0_05_110622_create_conversations_table.php | 9 ++---
3 files changed, 71 insertions(+), 4 deletions(-)
create mode 100644 database/migrations/2015_10_05_110621_create_tags_table.php
create mode 100644 database/migrations/2015_10_05_110622_create_conversation_tag_table.php
diff --git a/database/migrations/2015_10_05_110621_create_tags_table.php b/database/migrations/2015_10_05_110621_create_tags_table.php
new file mode 100644
index 0000000..420d69f
--- /dev/null
+++ b/database/migrations/2015_10_05_110621_create_tags_table.php
@@ -0,0 +1,33 @@
+increments('id');
+ $table->integer('user_id'); //so that different users can own different tags
+ $table->string('name');
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('conversations');
+ }
+}
diff --git a/database/migrations/2015_10_05_110622_create_conversation_tag_table.php b/database/migrations/2015_10_05_110622_create_conversation_tag_table.php
new file mode 100644
index 0000000..0a08ddd
--- /dev/null
+++ b/database/migrations/2015_10_05_110622_create_conversation_tag_table.php
@@ -0,0 +1,33 @@
+increments('id');
+ $table->integer('conversation_id');
+ $table->integer('tag_id');
+ $table->timestamps();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::dropIfExists('conversations');
+ }
+}
diff --git a/database/migrations/2015_10_05_110622_create_conversations_table.php b/database/migrations/2015_10_05_110622_create_conversations_table.php
index 03f126e..77ded81 100644
--- a/database/migrations/2015_10_05_110622_create_conversations_table.php
+++ b/database/migrations/2015_10_05_110622_create_conversations_table.php
@@ -1,20 +1,21 @@
increments('id');
+ $table->varchar('title');
$table->integer('user_one');
$table->integer('user_two');
$table->boolean('status');
@@ -24,7 +25,7 @@ public function up()
/**
* Reverse the migrations.
- *
+ *
* @return void
*/
public function down()
From 62221281f6a41cf6c2b5d1781cf8f3260a835677 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 17 Aug 2018 04:07:39 +0100
Subject: [PATCH 03/52] fixed bugs
---
src/Conversations/Conversation.php | 39 +++++++++++++++++++-----
src/Tags/Tag.php | 27 +++++++++++++++++
src/Talk.php | 48 ++++++++++++++++++------------
3 files changed, 87 insertions(+), 27 deletions(-)
create mode 100644 src/Tags/Tag.php
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index 820073c..d4a4235 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -6,14 +6,26 @@
class Conversation extends Model
{
- protected $table = 'conversations';
+ protected $table = 'conversations';
public $timestamps = true;
- public $fillable = [
+ public $fillable = [
'user_one',
'user_two',
+ 'title',
'status',
];
+ /*
+ * make a relation between message
+ *
+ * return collection
+ * */
+ public function tags()
+ {
+ return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
+ ->with('sender');
+ }
+
/*
* make a relation between message
*
@@ -32,16 +44,27 @@ public function messages()
* */
public function userone()
{
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one');
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one');
}
/*
- * make a relation between second user from conversation
- *
- * return collection
- * */
+ * make a relation between second user from conversation
+ *
+ * return collection
+ * */
public function usertwo()
{
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two');
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two');
+ }
+
+ /*
+ * adds a tag to this conversation
+ *
+ * return bool
+ * */
+ public function addTag(\Nahid\Talk\Tags $tag)
+ {
+ $this->tags()->attach($tag->id);
+ return true;
}
}
diff --git a/src/Tags/Tag.php b/src/Tags/Tag.php
new file mode 100644
index 0000000..b614769
--- /dev/null
+++ b/src/Tags/Tag.php
@@ -0,0 +1,27 @@
+hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
+ ->with('sender');
+ }
+}
diff --git a/src/Talk.php b/src/Talk.php
index ce03929..eea0d02 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -13,8 +13,8 @@
use Illuminate\Contracts\Config\Repository;
use Nahid\Talk\Conversations\ConversationRepository;
-use Nahid\Talk\Messages\MessageRepository;
use Nahid\Talk\Live\Broadcast;
+use Nahid\Talk\Messages\MessageRepository;
class Talk
{
@@ -61,10 +61,10 @@ class Talk
*/
public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
{
- $this->config = $config;
+ $this->config = $config;
$this->conversation = $conversation;
- $this->message = $message;
- $this->broadcast = $broadcast;
+ $this->message = $message;
+ $this->broadcast = $broadcast;
}
/**
@@ -77,7 +77,7 @@ public function __construct(Repository $config, Broadcast $broadcast, Conversati
*/
protected function getSerializeUser($user1, $user2)
{
- $user = [];
+ $user = [];
$user['one'] = ($user1 < $user2) ? $user1 : $user2;
$user['two'] = ($user1 < $user2) ? $user2 : $user1;
@@ -95,14 +95,13 @@ protected function getSerializeUser($user1, $user2)
protected function makeMessage($conversationId, $message)
{
$message = $this->message->create([
- 'message' => $message,
+ 'message' => $message,
'conversation_id' => $conversationId,
- 'user_id' => $this->authUserId,
- 'is_seen' => 0,
+ 'user_id' => $this->authUserId,
+ 'is_seen' => 0,
]);
$message->conversation->touch();
-
$this->broadcast->transmission($message);
return $message;
@@ -122,7 +121,7 @@ protected function makeMessageCollection($conversations)
$collection = (object) null;
if ($conversations->user_one == $this->authUserId || $conversations->user_two == $this->authUserId) {
- $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
+ $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
$collection->withUser = $withUser;
$collection->messages = $conversations->messages;
@@ -133,25 +132,35 @@ protected function makeMessageCollection($conversations)
}
/**
- * make new conversation the given receiverId with currently loggedin user.
+ * make new conversation with the given receiverId with currently loggedin user.
*
* @param int $receiverId
*
* @return int
*/
- protected function newConversation($receiverId)
+ protected function newConversation($receiverId, $title, $tagName = null)
{
$conversationId = $this->isConversationExists($receiverId);
- $user = $this->getSerializeUser($this->authUserId, $receiverId);
+ $user = $this->getSerializeUser($this->authUserId, $receiverId);
if ($conversationId === false) {
$conversation = $this->conversation->create([
'user_one' => $user['one'],
'user_two' => $user['two'],
- 'status' => 1,
+ 'title' => $title,
+ 'status' => 1,
]);
if ($conversation) {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
+ }
+
+ $conversation->addTag($tag);
+ }
+
return $conversation->id;
}
}
@@ -256,15 +265,14 @@ public function sendMessage($conversatonId, $message)
*
* @return \Nahid\Talk\Messages\Message
*/
- public function sendMessageByUserId($receiverId, $message)
+ public function sendMessageByUserId($receiverId, $message, $title, $tag = null)
{
if ($conversationId = $this->isConversationExists($receiverId)) {
$message = $this->makeMessage($conversationId, $message);
-
return $message;
}
- $convId = $this->newConversation($receiverId);
+ $convId = $this->newConversation($receiverId, $title);
$message = $this->makeMessage($convId, $message);
return $message;
@@ -333,6 +341,8 @@ public function threadsAll($order = 'desc', $offset = 0, $take = 20)
*/
public function getConversationsById($conversationId, $offset = 0, $take = 20)
{
+ // dump($conversationId);
+ // dd($this->authUserId);
$conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
return $this->makeMessageCollection($conversations);
@@ -489,7 +499,7 @@ public function makeSeen($messageId)
public function getReceiverInfo($conversationId)
{
$conversation = $this->conversation->find($conversationId);
- $receiver = '';
+ $receiver = '';
if ($conversation->user_one == $this->authUserId) {
$receiver = $conversation->user_two;
} else {
@@ -497,7 +507,7 @@ public function getReceiverInfo($conversationId)
}
$userModel = $this->config('talk.user.model');
- $user = new $userModel();
+ $user = new $userModel();
return $user->find($receiver);
}
From 9b09dfcee55d67d43ee26bb3a04f5c7df359bc33 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 17 Aug 2018 04:17:33 +0100
Subject: [PATCH 04/52] added 'ago' to humans_time
---
src/Messages/Message.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index eb9f5a9..97e0aa8 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -30,7 +30,7 @@ public function getHumansTimeAttribute()
$date = $this->created_at;
$now = $date->now();
- return $date->diffForHumans($now, true);
+ return $date->diffForHumans($now, true) . ' ago';
}
/*
From cc58e427b05474ab2f3bdf464dbbe57b0c0ec871 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 17 Aug 2018 04:54:05 +0100
Subject: [PATCH 05/52] users can now maintain multiple conversations
---
src/Talk.php | 7 +++++--
1 file changed, 5 insertions(+), 2 deletions(-)
diff --git a/src/Talk.php b/src/Talk.php
index eea0d02..68af491 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -268,8 +268,11 @@ public function sendMessage($conversatonId, $message)
public function sendMessageByUserId($receiverId, $message, $title, $tag = null)
{
if ($conversationId = $this->isConversationExists($receiverId)) {
- $message = $this->makeMessage($conversationId, $message);
- return $message;
+ $con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
+ if ($con->title == $title) {
+ $message = $this->makeMessage($conversationId, $message);
+ return $message;
+ }
}
$convId = $this->newConversation($receiverId, $title);
From d36b1d34b8d2819484ac45e124b61c900fd6c60a Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 17 Aug 2018 05:00:43 +0100
Subject: [PATCH 06/52] improvment on ser-to-user multi-conversation
---
src/Talk.php | 41 +++++++++++++++++++++--------------------
1 file changed, 21 insertions(+), 20 deletions(-)
diff --git a/src/Talk.php b/src/Talk.php
index 68af491..8d0066f 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -140,30 +140,30 @@ protected function makeMessageCollection($conversations)
*/
protected function newConversation($receiverId, $title, $tagName = null)
{
- $conversationId = $this->isConversationExists($receiverId);
- $user = $this->getSerializeUser($this->authUserId, $receiverId);
-
- if ($conversationId === false) {
- $conversation = $this->conversation->create([
- 'user_one' => $user['one'],
- 'user_two' => $user['two'],
- 'title' => $title,
- 'status' => 1,
- ]);
-
- if ($conversation) {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
- }
-
- $conversation->addTag($tag);
+ // $conversationId = $this->isConversationExists($receiverId);
+ $user = $this->getSerializeUser($this->authUserId, $receiverId);
+
+ // if ($conversationId === false) {
+ $conversation = $this->conversation->create([
+ 'user_one' => $user['one'],
+ 'user_two' => $user['two'],
+ 'title' => $title,
+ 'status' => 1,
+ ]);
+
+ if ($conversation) {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
}
- return $conversation->id;
+ $conversation->addTag($tag);
}
+
+ return $conversation->id;
}
+ // }
return $conversationId;
}
@@ -270,6 +270,7 @@ public function sendMessageByUserId($receiverId, $message, $title, $tag = null)
if ($conversationId = $this->isConversationExists($receiverId)) {
$con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
if ($con->title == $title) {
+ // dd("same: {$con->title} == $title");
$message = $this->makeMessage($conversationId, $message);
return $message;
}
From bfaf5d77b63ec2540c788add1501ce7b03dedd0f Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 17 Aug 2018 21:02:24 +0100
Subject: [PATCH 07/52] done adding feature: mark readable
---
...015_10_05_110608_create_messages_table.php | 9 +-
src/Conversations/ConversationRepository.php | 32 +++---
src/Messages/Message.php | 31 +++---
src/Talk.php | 98 ++++++++++++++++++-
4 files changed, 134 insertions(+), 36 deletions(-)
diff --git a/database/migrations/2015_10_05_110608_create_messages_table.php b/database/migrations/2015_10_05_110608_create_messages_table.php
index cad9f29..fd867e2 100644
--- a/database/migrations/2015_10_05_110608_create_messages_table.php
+++ b/database/migrations/2015_10_05_110608_create_messages_table.php
@@ -1,14 +1,14 @@
increments('id');
$table->text('message');
$table->boolean('is_seen')->default(0);
+ $table->boolean('is_read')->default(0);
$table->boolean('deleted_from_sender')->default(0);
$table->boolean('deleted_from_receiver')->default(0);
$table->integer('user_id');
@@ -27,7 +28,7 @@ public function up()
/**
* Reverse the migrations.
- *
+ *
* @return void
*/
public function down()
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index cf0aaae..63233df 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -80,19 +80,19 @@ public function isUserExists($conversationId, $userId)
* */
public function threads($user, $order, $offset, $take)
{
- $conv = new Conversation();
+ $conv = new Conversation();
$conv->authUser = $user;
- $msgThread = $conv->with(['messages' => function ($q) use ($user) {
+ $msgThread = $conv->with(['messages' => function ($q) use ($user) {
return $q->where(function ($q) use ($user) {
$q->where('user_id', $user)
- ->where('deleted_from_sender', 0);
+ ->where('deleted_from_sender', 0);
})
->orWhere(function ($q) use ($user) {
$q->where('user_id', '!=', $user);
$q->where('deleted_from_receiver', 0);
})
- ->latest();
- },'messages.sender', 'userone', 'usertwo'])
+ ->latest();
+ }, 'messages.sender', 'userone', 'usertwo'])
->where('user_one', $user)
->orWhere('user_two', $user)
->offset($offset)
@@ -103,11 +103,11 @@ public function threads($user, $order, $offset, $take)
$threads = [];
foreach ($msgThread as $thread) {
- $collection = (object) null;
- $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
- $collection->thread = $thread->messages->first();
+ $collection = (object) null;
+ $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
+ $collection->thread = $thread->messages->first();
$collection->withUser = $conversationWith;
- $threads[] = $collection;
+ $threads[] = $collection;
}
return collect($threads);
@@ -132,9 +132,9 @@ public function threadsAll($user, $offset, $take)
foreach ($msgThread as $thread) {
$conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
- $message = $thread->messages->first();
- $message->user = $conversationWith;
- $threads[] = $message;
+ $message = $thread->messages->first();
+ $message->user = $conversationWith;
+ $threads[] = $message;
}
return collect($threads);
@@ -156,10 +156,10 @@ public function getMessagesById($conversationId, $userId, $offset, $take)
$qr->where('user_id', '=', $userId)
->where('deleted_from_sender', 0);
})
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
$query->offset($offset)->take($take);
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index 97e0aa8..28c159e 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -10,10 +10,10 @@ class Message extends Model
public $timestamps = true;
-
public $fillable = [
'message',
'is_seen',
+ 'is_read',
'deleted_from_sender',
'deleted_from_receiver',
'user_id',
@@ -28,9 +28,18 @@ class Message extends Model
public function getHumansTimeAttribute()
{
$date = $this->created_at;
- $now = $date->now();
+ $now = $date->now();
+
+ if ($date->isToday()) {
+ return $date->diffForHumans(null, false, true);
+ } else {
+ if ($date->isSameYear($now)) {
+ return $date->format("M j");
+ }
+ }
- return $date->diffForHumans($now, true) . ' ago';
+ return $date->format("M j, Y");
+ // return $date->diffForHumans(null, true, true) . ' ago';
}
/*
@@ -44,20 +53,20 @@ public function conversation()
}
/*
- * make a relation between user model
- *
- * @return collection
- * */
+ * make a relation between user model
+ *
+ * @return collection
+ * */
public function user()
{
return $this->belongsTo(config('talk.user.model', 'App\User'));
}
/*
- * its an alias of user relation
- *
- * @return collection
- * */
+ * its an alias of user relation
+ *
+ * @return collection
+ * */
public function sender()
{
return $this->user();
diff --git a/src/Talk.php b/src/Talk.php
index 8d0066f..1683e70 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -125,6 +125,15 @@ protected function makeMessageCollection($conversations)
$collection->withUser = $withUser;
$collection->messages = $conversations->messages;
+ //mark them as read
+ foreach ($collection->messages as $mssg) {
+ if ($mssg->sender->id != $this->authUserId) {
+ if (!Talk::user($this->authUserId)->markRead($mssg->id)) {
+ return false;
+ }
+ }
+ }
+
return $collection;
}
@@ -335,7 +344,7 @@ public function threadsAll($order = 'desc', $offset = 0, $take = 20)
}
/**
- * fetch all conversation by using coversation id.
+ * fetch all conversation by using conversation id.
*
* @param int $conversationId
* @param int $offset = 0
@@ -348,12 +357,11 @@ public function getConversationsById($conversationId, $offset = 0, $take = 20)
// dump($conversationId);
// dd($this->authUserId);
$conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
-
return $this->makeMessageCollection($conversations);
}
/**
- * fetch all conversation with soft deleted messages by using coversation id.
+ * fetch all conversation with soft deleted messages by using conversation id.
*
* @param int $conversationId
* @param int $offset = 0
@@ -466,7 +474,7 @@ public function readMessage($messageId = null)
if (!is_null($messageId)) {
$message = $this->message->with(['sender', 'conversation'])->find($messageId);
- if ($message->coversation->user_one == $this->authUserId || $message->coversation->user_two == $this->authUserId) {
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
return $message;
}
}
@@ -475,7 +483,7 @@ public function readMessage($messageId = null)
}
/**
- * make a message as seen.
+ * marks a message as seen.
*
* @param int $messageId
*
@@ -490,6 +498,86 @@ public function makeSeen($messageId)
return false;
}
+ /**
+ * marks a message as read.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function markRead($messageId)
+ {
+ if (!is_null($messageId)) {
+ $message = $this->message->with(['sender', 'conversation'])->find($messageId);
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+
+ //only needful to mark as read if you are the recipient
+ if ($message->sender->id != $this->authUserId) {
+ $read = $this->message->update($messageId, ['is_read' => 1]);
+ if (!$read) {
+ return falsse;
+ }
+ }
+
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * gets messages not yet read in a particular conversation
+ *
+ * @param int $conversationId
+ *
+ * @return mixed
+ */
+ public function getUnreadMessagesInConversation($conversationId)
+ {
+ if (!is_null($conversationId)) {
+ $message = $this->conversation->with(['messages'])->find($conversationId);
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+
+ $unread = [];
+ $unread = collect($this->conversation->messages)->filter(function ($message) use ($authUserId) {
+ return (($message->sender->id != $authUserId) && ($message->is_read != 1));
+ });
+
+ return $unread;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * gets the count of all messages not yet read
+ *
+ * @param int $conversationId
+ *
+ * @return int
+ */
+ public function getUnreadMessagesCount()
+ {
+ $int = 0;
+ $user = \Illuminate\Support\Facades\Auth::user();
+ // dump($user->id);
+ $conv = new \Nahid\Talk\Conversations\Conversation();
+ $msgThread = $conv->with(['messages' => function ($query) use ($user) {
+ return $query->where('is_read', '0')
+ ->where('user_id', '!=', $user->id);
+ }, 'userone', 'usertwo'])
+ ->where('user_one', $user->id)
+ ->orWhere('user_two', $user->id)
+ ->get();
+
+ foreach ($msgThread as $thread) {
+ $int += $thread->messages->count();
+ }
+
+ return $int;
+ }
/**
* get receiver information for this conversation.
From 62406fec09054e9a8f0a96144f0eda5f19cf0e6b Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 19 Aug 2018 00:23:04 +0100
Subject: [PATCH 08/52] updates on tags and other enhancements
---
src/Conversations/Conversation.php | 1 +
src/Conversations/ConversationRepository.php | 47 ++++----
src/Talk.php | 107 ++++++++++++++++---
3 files changed, 122 insertions(+), 33 deletions(-)
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index d4a4235..5a1e705 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -8,6 +8,7 @@ class Conversation extends Model
{
protected $table = 'conversations';
public $timestamps = true;
+
public $fillable = [
'user_one',
'user_two',
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 63233df..3327b9f 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -73,28 +73,28 @@ public function isUserExists($conversationId, $userId)
* retrieve all message thread without soft deleted message with latest one message and
* sender and receiver user model
*
- * @param int $user
+ * @param int $user_id
* @param int $offset
* @param int $take
* @return collection
* */
- public function threads($user, $order, $offset, $take)
+ public function threads($user_id, $order, $offset, $take)
{
$conv = new Conversation();
- $conv->authUser = $user;
- $msgThread = $conv->with(['messages' => function ($q) use ($user) {
- return $q->where(function ($q) use ($user) {
- $q->where('user_id', $user)
+ $conv->authUser = $user_id;
+ $conversations = $conv->with(['messages' => function ($q) use ($user_id) {
+ return $q->where(function ($q) use ($user_id) {
+ $q->where('user_id', $user_id)
->where('deleted_from_sender', 0);
})
- ->orWhere(function ($q) use ($user) {
- $q->where('user_id', '!=', $user);
+ ->orWhere(function ($q) use ($user_id) {
+ $q->where('user_id', '!=', $user_id);
$q->where('deleted_from_receiver', 0);
})
->latest();
}, 'messages.sender', 'userone', 'usertwo'])
- ->where('user_one', $user)
- ->orWhere('user_two', $user)
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
->offset($offset)
->take($take)
->orderBy('updated_at', $order)
@@ -102,10 +102,19 @@ public function threads($user, $order, $offset, $take)
$threads = [];
- foreach ($msgThread as $thread) {
- $collection = (object) null;
- $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
- $collection->thread = $thread->messages->first();
+ foreach ($conversations as $conversation) {
+ $collection = (object) null;
+ $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
+ $collection->thread = $conversation->messages->first();
+ $collection->conversation = $conversation;
+ $collection->messages = $conversation->messages;
+ $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
+ return $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0');
+ })->get();
+ // dump($conversation->id);
+ // dump($collection->unreadmessages);
$collection->withUser = $conversationWith;
$threads[] = $collection;
}
@@ -116,22 +125,22 @@ public function threads($user, $order, $offset, $take)
/*
* retrieve all message thread with latest one message and sender and receiver user model
*
- * @param int $user
+ * @param int $user_id
* @param int $offset
* @param int $take
* @return collection
* */
- public function threadsAll($user, $offset, $take)
+ public function threadsAll($user_id, $offset, $take)
{
- $msgThread = Conversation::with(['messages' => function ($q) use ($user) {
+ $msgThread = Conversation::with(['messages' => function ($q) use ($user_id) {
return $q->latest();
}, 'userone', 'usertwo'])
- ->where('user_one', $user)->orWhere('user_two', $user)->offset($offset)->take($take)->get();
+ ->where('user_one', $user_id)->orWhere('user_two', $user_id)->offset($offset)->take($take)->get();
$threads = [];
foreach ($msgThread as $thread) {
- $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
+ $conversationWith = ($thread->userone->id == $user_id) ? $thread->usertwo : $thread->userone;
$message = $thread->messages->first();
$message->user = $conversationWith;
$threads[] = $message;
diff --git a/src/Talk.php b/src/Talk.php
index 1683e70..5606126 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -274,7 +274,7 @@ public function sendMessage($conversatonId, $message)
*
* @return \Nahid\Talk\Messages\Message
*/
- public function sendMessageByUserId($receiverId, $message, $title, $tag = null)
+ public function sendMessageByUserId($receiverId, $message, $title)
{
if ($conversationId = $this->isConversationExists($receiverId)) {
$con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
@@ -414,6 +414,42 @@ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
return false;
}
+ /**
+ * gets tags owned/created by this user
+ *
+ * @return collection
+ */
+ public function getUserTags()
+ {
+ return Tags\Tag::where(['user_id' => $authUserId])->get();
+ }
+
+ /**
+ * adds a tag to a conversation
+ *
+ * @param int $conversationId
+ *
+ * @return bool
+ */
+ public function addTagToConversation($conversationId, $tagName)
+ {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
+ }
+
+ $conversation = Conversation::find($conversationId)->with('tags');
+ if (!$conversation->tags->pluck('id')->containss($tag)) {
+ $conversation->addTag($tag);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
/**
* its an alias of getConversationById.
*
@@ -552,31 +588,74 @@ public function getUnreadMessagesInConversation($conversationId)
}
/**
- * gets the count of all messages not yet read
+ * gets all messages not yet read is all conversations altogether
*
* @param int $conversationId
*
- * @return int
+ * @return collection
+ */
+ public function getAllUnreadMessages()
+ {
+ $messages = collect();
+ $user_id = \Illuminate\Support\Facades\Auth::user();
+ $conv = new \Nahid\Talk\Conversations\Conversation();
+ $conversations = $conv->with(['messages' => function ($query) use ($user_id) {
+ return $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0')
+ ;
+ }])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->get();
+
+ foreach ($conversations as $conversation) {
+ $messages = $messages->merge($conversation->messages);
+ }
+
+ // dump($messages);
+ return $messages;
+ }
+
+ /**
+ * gets all latest messages sent to auth'ed user
+ *
+ * @param int $conversationId
+ *
+ * @return collection
*/
- public function getUnreadMessagesCount()
+ public function getLatestMessages()
{
- $int = 0;
- $user = \Illuminate\Support\Facades\Auth::user();
+ // dump($this->authUserId);
+ $messages = [];
+ $user_id = \Illuminate\Support\Facades\Auth::user()->id;
// dump($user->id);
$conv = new \Nahid\Talk\Conversations\Conversation();
- $msgThread = $conv->with(['messages' => function ($query) use ($user) {
- return $query->where('is_read', '0')
- ->where('user_id', '!=', $user->id);
- }, 'userone', 'usertwo'])
- ->where('user_one', $user->id)
- ->orWhere('user_two', $user->id)
+ $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
+ return $query->where('user_id', '!=', $user_id)->with(['conversation']);
+ }])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->orderBy('created_at')
->get();
foreach ($msgThread as $thread) {
- $int += $thread->messages->count();
+ $messages = collect($messages)->merge($thread->messages);
}
- return $int;
+ return $messages;
+ }
+
+ /**
+ * gets the count of all messages not yet read in all conversations altogether
+ *
+ * @param int $conversationId
+ *
+ * @return int
+ */
+ public function getUnreadMessagesCount()
+ {
+ return $this->getAllUnreadMessages()->count();
}
/**
From c8919891060ddfa48c59f47d2e539c1d982b5640 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 19 Aug 2018 11:38:38 +0100
Subject: [PATCH 09/52] updates
---
src/Conversations/Conversation.php | 10 +++++-----
src/Talk.php | 10 +++++-----
2 files changed, 10 insertions(+), 10 deletions(-)
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index 5a1e705..7b7a188 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -17,14 +17,13 @@ class Conversation extends Model
];
/*
- * make a relation between message
+ * make a relation with tags
*
- * return collection
+ * return relationship
* */
public function tags()
{
- return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
- ->with('sender');
+ return $this->belongsToMany('Nahid\Talk\Tags\Tag');
}
/*
@@ -63,8 +62,9 @@ public function usertwo()
*
* return bool
* */
- public function addTag(\Nahid\Talk\Tags $tag)
+ public function addTag(\Nahid\Talk\Tags\Tag $tag)
{
+ // var_dump($this);
$this->tags()->attach($tag->id);
return true;
}
diff --git a/src/Talk.php b/src/Talk.php
index 5606126..a0fe0b8 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -421,7 +421,7 @@ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
*/
public function getUserTags()
{
- return Tags\Tag::where(['user_id' => $authUserId])->get();
+ return Tags\Tag::where(['user_id' => $this->authUserId])->get();
}
/**
@@ -434,13 +434,13 @@ public function getUserTags()
public function addTagToConversation($conversationId, $tagName)
{
if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
+ $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
}
- $conversation = Conversation::find($conversationId)->with('tags');
- if (!$conversation->tags->pluck('id')->containss($tag)) {
+ $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')->findOrFail($conversationId);
+ if (!$conversation->tags->pluck('id')->contains($tag->id)) {
$conversation->addTag($tag);
}
From 9702ec76d6032128d8ce43c020567e8c9924bed1 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 19 Aug 2018 14:26:50 +0100
Subject: [PATCH 10/52] bug fix
---
src/Talk.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/Talk.php b/src/Talk.php
index a0fe0b8..d6b1c40 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -627,7 +627,7 @@ public function getAllUnreadMessages()
public function getLatestMessages()
{
// dump($this->authUserId);
- $messages = [];
+ $messages = collect();
$user_id = \Illuminate\Support\Facades\Auth::user()->id;
// dump($user->id);
$conv = new \Nahid\Talk\Conversations\Conversation();
From 34cd4ba168f614c9f5c15a1fe9c4832d7f803b7e Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 19 Aug 2018 16:41:40 +0100
Subject: [PATCH 11/52] bug fixes
---
src/Talk.php | 21 ++++++++++++++++-----
1 file changed, 16 insertions(+), 5 deletions(-)
diff --git a/src/Talk.php b/src/Talk.php
index d6b1c40..415966d 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -46,6 +46,11 @@ class Talk
*/
protected $broadcast;
+ /*
+ * the number of messages that the auth'ed user has not read
+ */
+ protected $unreadMessageCount = null;
+
/**
* Currently loggedin user id.
*
@@ -61,6 +66,7 @@ class Talk
*/
public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
{
+ // dump('calling meeeeeeeeee');
$this->config = $config;
$this->conversation = $conversation;
$this->message = $message;
@@ -301,6 +307,7 @@ public function sendMessageByUserId($receiverId, $message, $title)
*/
public function getInbox($order = 'desc', $offset = 0, $take = 20)
{
+ // dump($this->authUserId);
return $this->conversation->threads($this->authUserId, $order, $offset, $take);
}
@@ -597,7 +604,7 @@ public function getUnreadMessagesInConversation($conversationId)
public function getAllUnreadMessages()
{
$messages = collect();
- $user_id = \Illuminate\Support\Facades\Auth::user();
+ $user_id = $this->authUserId;
$conv = new \Nahid\Talk\Conversations\Conversation();
$conversations = $conv->with(['messages' => function ($query) use ($user_id) {
return $query
@@ -627,9 +634,8 @@ public function getAllUnreadMessages()
public function getLatestMessages()
{
// dump($this->authUserId);
- $messages = collect();
- $user_id = \Illuminate\Support\Facades\Auth::user()->id;
- // dump($user->id);
+ $messages = collect();
+ $user_id = $this->authUserId;
$conv = new \Nahid\Talk\Conversations\Conversation();
$msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
return $query->where('user_id', '!=', $user_id)->with(['conversation']);
@@ -643,6 +649,7 @@ public function getLatestMessages()
$messages = collect($messages)->merge($thread->messages);
}
+ // dump($messages);
return $messages;
}
@@ -655,7 +662,11 @@ public function getLatestMessages()
*/
public function getUnreadMessagesCount()
{
- return $this->getAllUnreadMessages()->count();
+ if ($this->unreadMessageCount == null) {
+ $this->unreadMessageCount = $this->getAllUnreadMessages()->count();
+ }
+
+ return $this->unreadMessageCount;
}
/**
From 6827d472e6bf77f32206aafae06b54c88a826ede Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Mon, 20 Aug 2018 01:56:24 +0100
Subject: [PATCH 12/52] feature_add_star stage 1
---
src/Conversations/ConversationRepository.php | 25 ++++
src/Talk.php | 126 ++++++++++++++++---
2 files changed, 134 insertions(+), 17 deletions(-)
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 3327b9f..8c3b5ce 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -173,7 +173,32 @@ public function getMessagesById($conversationId, $userId, $offset, $take)
$query->offset($offset)->take($take);
}])->with(['userone', 'usertwo'])->find($conversationId);
+ }
+
+ /*
+ * get all conversations by given tag id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesByTagId($conversationId, $userId)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+
+ $query->offset($offset)->take($take);
+ }])->with(['userone', 'usertwo', 'tags'])->find($conversationId);
}
/*
diff --git a/src/Talk.php b/src/Talk.php
index 415966d..3d0eecd 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -18,6 +18,14 @@
class Talk
{
+
+ /**
+ * Now users can attach special importance to conversations by star-ing them.
+ * This is the name of the special 'star' tag
+ *
+ */
+ const starTag = "talk_special_tag_star";
+
/**
* configurations instance.
*
@@ -51,6 +59,11 @@ class Talk
*/
protected $unreadMessageCount = null;
+ /*
+ * just a helper for the last 5 messages in your mailbox
+ */
+ protected $latestMessages = null;
+
/**
* Currently loggedin user id.
*
@@ -367,6 +380,30 @@ public function getConversationsById($conversationId, $offset = 0, $take = 20)
return $this->makeMessageCollection($conversations);
}
+ /**
+ * fetch all conversations that match the given tag id
+ *
+ * @param int $conversationId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function getConversationsByTagId($tagId)
+ {
+ // dump($tagId);
+ $conversations = Conversations\Conversation::with(
+ [
+ 'tags' => function ($query) use ($tagId) {
+ $query->where('tags.id', '=', $tagId);
+ },
+ 'user_one',
+ 'user_two',
+ ])->get();
+ // dd($conversations);
+ return $this->makeMessageCollection($conversations);
+ }
+
/**
* fetch all conversation with soft deleted messages by using conversation id.
*
@@ -428,11 +465,35 @@ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
*/
public function getUserTags()
{
- return Tags\Tag::where(['user_id' => $this->authUserId])->get();
+ return Tags\Tag::where(['user_id' => $this->authUserId])
+ ->where('name', '!=', Talk::starTag)
+ ->get();
}
/**
- * adds a tag to a conversation
+ * creares tag for user
+ *
+ * @param string $tagName
+ *
+ * @return bool
+ */
+ public function createTagForUser($tagName)
+ {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
+ }
+
+ return !empty($tag);
+ }
+
+ return false;
+ }
+
+ /**
+ * adds a tag to a conversation. Creates the tag if it does not exist for the user
+ * This allows for several users to maintain same tag name conveniently without any conflicts/issues
*
* @param int $conversationId
*
@@ -457,6 +518,33 @@ public function addTagToConversation($conversationId, $tagName)
return false;
}
+ /**
+ *removes tag from a conversation
+ *
+ * @param int $conversationId
+ * @param int $tagId
+ *
+ * @return bool
+ */
+ public function removeTagFromConversation($conversationId, $tagId)
+ {
+ if (!empty($conversationId) && !empty($tagId)) {
+ //confirm user owns this tag
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'id' => $tagId])->firstOrFail();
+ $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')
+ ->where(function ($query) {
+ $query
+ ->where("user_one", $this->authUserId)
+ ->orWhere("user_two", $this->authUserId);
+ })->findOrFail($conversationId);
+
+ $conversation->tags()->detach($tagId);
+
+ return true;
+ }
+
+ return false;
+ }
/**
* its an alias of getConversationById.
*
@@ -633,24 +721,28 @@ public function getAllUnreadMessages()
*/
public function getLatestMessages()
{
- // dump($this->authUserId);
- $messages = collect();
- $user_id = $this->authUserId;
- $conv = new \Nahid\Talk\Conversations\Conversation();
- $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
- return $query->where('user_id', '!=', $user_id)->with(['conversation']);
- }])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->orderBy('created_at')
- ->get();
+ if ($this->latestMessages == null) {
+
+ $messages = collect();
+ $user_id = $this->authUserId;
+ $conv = new \Nahid\Talk\Conversations\Conversation();
+ $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
+ return $query->where('user_id', '!=', $user_id)->with(['conversation']);
+ }])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->orderBy('created_at')
+ ->take(5)
+ ->get();
+
+ foreach ($msgThread as $thread) {
+ $messages = collect($messages)->merge($thread->messages);
+ }
- foreach ($msgThread as $thread) {
- $messages = collect($messages)->merge($thread->messages);
+ $this->latestMessages = $messages;
}
- // dump($messages);
- return $messages;
+ return $this->latestMessages;
}
/**
From b0c86edf12ebdf36541e955ebe19a2be4766df84 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 16 Sep 2018 16:27:08 +0100
Subject: [PATCH 13/52] bug fixes
---
src/Conversations/ConversationRepository.php | 34 +++++++++++---
src/Talk.php | 49 +++++++++++++-------
2 files changed, 59 insertions(+), 24 deletions(-)
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 8c3b5ce..87269b1 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -180,11 +180,34 @@ public function getMessagesById($conversationId, $userId, $offset, $take)
*
* @param int $conversationId
* @param int $userId
- * @param int $offset
- * @param int $take
* @return collection
- * */
+ * * /
public function getMessagesByTagId($conversationId, $userId)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+
+ $query->offset($offset)->take($take);
+
+ }])->with(['userone', 'usertwo', 'tags'])->find($conversationId);
+ }
+ */
+
+ /*
+ * get all conversations by given tag id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return collection
+ * */
+ public function getMessagesByTagId($tagId, $userId)
{
return Conversation::with(['messages' => function ($query) use ($userId) {
$query->where(function ($qr) use ($userId) {
@@ -195,10 +218,7 @@ public function getMessagesByTagId($conversationId, $userId)
$q->where('user_id', '!=', $userId)
->where('deleted_from_receiver', 0);
});
-
- $query->offset($offset)->take($take);
-
- }])->with(['userone', 'usertwo', 'tags'])->find($conversationId);
+ }])->with(['userone', 'usertwo', 'tags'])->get();
}
/*
diff --git a/src/Talk.php b/src/Talk.php
index 3d0eecd..837d950 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -383,25 +383,40 @@ public function getConversationsById($conversationId, $offset = 0, $take = 20)
/**
* fetch all conversations that match the given tag id
*
- * @param int $conversationId
- * @param int $offset = 0
- * @param int $take = 20
+ * @param int $tag_id
*
- * @return \Nahid\Talk\Messages\Message
+ * @return collection
*/
- public function getConversationsByTagId($tagId)
- {
- // dump($tagId);
- $conversations = Conversations\Conversation::with(
- [
- 'tags' => function ($query) use ($tagId) {
- $query->where('tags.id', '=', $tagId);
- },
- 'user_one',
- 'user_two',
- ])->get();
- // dd($conversations);
- return $this->makeMessageCollection($conversations);
+ public function getConversationsByTagId($tag_id)
+ {
+ // $threads = $this->conversation->threads($this->authUserId, 'id', 6, 6);
+ $conversations_ = $this->conversation->getMessagesByTagId($tag_id, $this->authUserId);
+ $user_id = $this->authUserId;
+ $conversations = collect($conversations_)->filter(function ($item) use ($tag_id) {
+ // dd($item);
+ return ($item->tags->pluck('id')->contains($tag_id));
+ });
+
+ $threads = [];
+
+ foreach ($conversations as $conversation) {
+ $collection = (object) null;
+ $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
+ $collection->thread = $conversation->messages->first();
+ $collection->conversation = $conversation;
+ $collection->messages = $conversation->messages;
+ $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
+ return $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0');
+ })->get();
+ // dump($conversation->id);
+ // dump($collection->unreadmessages);
+ $collection->withUser = $conversationWith;
+ $threads[] = $collection;
+ }
+
+ return collect($threads);
}
/**
From 1fd6f776e2691c7d4e9be9a360e227258ce718d7 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 21 Sep 2018 10:49:36 +0100
Subject: [PATCH 14/52] updates to tagging of conversations
---
.../2015_10_05_110621_create_tags_table.php | 33 -
.../2017_01_11_161142_create_tags_table.php | 32 +
src/Talk.php | 1663 ++++++++---------
3 files changed, 853 insertions(+), 875 deletions(-)
delete mode 100644 database/migrations/2015_10_05_110621_create_tags_table.php
create mode 100644 database/migrations/2017_01_11_161142_create_tags_table.php
diff --git a/database/migrations/2015_10_05_110621_create_tags_table.php b/database/migrations/2015_10_05_110621_create_tags_table.php
deleted file mode 100644
index 420d69f..0000000
--- a/database/migrations/2015_10_05_110621_create_tags_table.php
+++ /dev/null
@@ -1,33 +0,0 @@
-increments('id');
- $table->integer('user_id'); //so that different users can own different tags
- $table->string('name');
- $table->timestamps();
- });
- }
-
- /**
- * Reverse the migrations.
- *
- * @return void
- */
- public function down()
- {
- Schema::dropIfExists('conversations');
- }
-}
diff --git a/database/migrations/2017_01_11_161142_create_tags_table.php b/database/migrations/2017_01_11_161142_create_tags_table.php
new file mode 100644
index 0000000..27d268f
--- /dev/null
+++ b/database/migrations/2017_01_11_161142_create_tags_table.php
@@ -0,0 +1,32 @@
+increments('id');
+ $table->string('name');
+ $table->integer('user_id')->unsigned();
+ $table->integer('is_special_tag')->default('0');
+ $table->timestamps();
+
+ $table->foreign('user_id')->references('id')->on('users');
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down() {
+ Schema::drop('tags');
+ }
+}
diff --git a/src/Talk.php b/src/Talk.php
index 837d950..4e647da 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -16,846 +16,825 @@
use Nahid\Talk\Live\Broadcast;
use Nahid\Talk\Messages\MessageRepository;
-class Talk
-{
-
- /**
- * Now users can attach special importance to conversations by star-ing them.
- * This is the name of the special 'star' tag
- *
- */
- const starTag = "talk_special_tag_star";
-
- /**
- * configurations instance.
- *
- * @var \Illuminate\Contracts\Config\Repository
- */
- protected $config;
-
- /**
- * The ConversationRepository class instance.
- *
- * @var \Nahid\Talk\Conversations\ConversationRepository
- */
- protected $conversation;
-
- /**
- * The MessageRepository class instance.
- *
- * @var \Nahid\Talk\Messages\MessageRepository
- */
- protected $message;
-
- /**
- * Broadcast class instance.
- *
- * @var \Nahid\Talk\Live\Broadcast
- */
- protected $broadcast;
-
- /*
- * the number of messages that the auth'ed user has not read
- */
- protected $unreadMessageCount = null;
-
- /*
- * just a helper for the last 5 messages in your mailbox
- */
- protected $latestMessages = null;
-
- /**
- * Currently loggedin user id.
- *
- * @var int
- */
- protected $authUserId;
-
- /**
- * Initialize and instantiate conversation and message repositories.
- *
- * @param \Nahid\Talk\Conversations\ConversationRepository $conversation
- * @param \Nahid\Talk\Messages\MessageRepository $message
- */
- public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
- {
- // dump('calling meeeeeeeeee');
- $this->config = $config;
- $this->conversation = $conversation;
- $this->message = $message;
- $this->broadcast = $broadcast;
- }
-
- /**
- * make two users as serialize with ascending order.
- *
- * @param int $user1
- * @param int $user2
- *
- * @return array
- */
- protected function getSerializeUser($user1, $user2)
- {
- $user = [];
- $user['one'] = ($user1 < $user2) ? $user1 : $user2;
- $user['two'] = ($user1 < $user2) ? $user2 : $user1;
-
- return $user;
- }
-
- /**
- * create a new message by using conversationId.
- *
- * @param int $conversationId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message
- */
- protected function makeMessage($conversationId, $message)
- {
- $message = $this->message->create([
- 'message' => $message,
- 'conversation_id' => $conversationId,
- 'user_id' => $this->authUserId,
- 'is_seen' => 0,
- ]);
-
- $message->conversation->touch();
- $this->broadcast->transmission($message);
-
- return $message;
- }
-
- /*
- * Make new message collections to response with formatted data
- *
- *@param \Talk\Conversations\Conversation $conversations
- *@return object|bool
- */
- protected function makeMessageCollection($conversations)
- {
- if (!$conversations) {
- return false;
- }
-
- $collection = (object) null;
- if ($conversations->user_one == $this->authUserId || $conversations->user_two == $this->authUserId) {
- $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
- $collection->withUser = $withUser;
- $collection->messages = $conversations->messages;
-
- //mark them as read
- foreach ($collection->messages as $mssg) {
- if ($mssg->sender->id != $this->authUserId) {
- if (!Talk::user($this->authUserId)->markRead($mssg->id)) {
- return false;
- }
- }
- }
-
- return $collection;
- }
-
- return false;
- }
-
- /**
- * make new conversation with the given receiverId with currently loggedin user.
- *
- * @param int $receiverId
- *
- * @return int
- */
- protected function newConversation($receiverId, $title, $tagName = null)
- {
- // $conversationId = $this->isConversationExists($receiverId);
- $user = $this->getSerializeUser($this->authUserId, $receiverId);
-
- // if ($conversationId === false) {
- $conversation = $this->conversation->create([
- 'user_one' => $user['one'],
- 'user_two' => $user['two'],
- 'title' => $title,
- 'status' => 1,
- ]);
-
- if ($conversation) {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
- }
-
- $conversation->addTag($tag);
- }
-
- return $conversation->id;
- }
- // }
-
- return $conversationId;
- }
-
- /**
- * set currently authenticated user id for global usage.
- *
- * @param int $id
- *
- * @return int|bool
- */
- public function setAuthUserId($id = null)
- {
- if (!is_null($id)) {
- return $this->authUserId = $id;
- }
-
- return false;
- }
-
- /*
- * its set user id instantly when you fetch or access data. if you you haven't
- * set authenticated user id globally or you want to fetch work with
- * instant users information, you may use it
- *
- * @param int $id
- * @return \Nahid\Talk\Talk|bool
- * */
- public function user($id = null)
- {
- if ($this->setAuthUserId($id)) {
- return $this;
- }
-
- return false;
- }
-
- /**
- * make sure is this conversation exist for this user with currently loggedin user.
- *
- * @param int $userId
- *
- * @return bool|int
- */
- public function isConversationExists($userId)
- {
- if (empty($userId)) {
- return false;
- }
-
- $user = $this->getSerializeUser($this->authUserId, $userId);
-
- return $this->conversation->isExistsAmongTwoUsers($user['one'], $user['two']);
- }
-
- /**
- * check the given user exist for the given conversation.
- *
- * @param int $conversationId
- * @param int $userId
- *
- * @return bool
- */
- public function isAuthenticUser($conversationId, $userId)
- {
- if ($conversationId && $userId) {
- return $this->conversation->isUserExists($conversationId, $userId);
- }
-
- return false;
- }
-
- /**
- * send a message by using converstionid.
- *
- * @param int $conversationId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function sendMessage($conversatonId, $message)
- {
- if ($conversatonId && $message) {
- if ($this->conversation->existsById($conversatonId)) {
- $message = $this->makeMessage($conversatonId, $message);
-
- return $message;
- }
- }
-
- return false;
- }
-
- /**
- * create a new message by using receiverid.
- *
- * @param int $receiverId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function sendMessageByUserId($receiverId, $message, $title)
- {
- if ($conversationId = $this->isConversationExists($receiverId)) {
- $con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
- if ($con->title == $title) {
- // dd("same: {$con->title} == $title");
- $message = $this->makeMessage($conversationId, $message);
- return $message;
- }
- }
-
- $convId = $this->newConversation($receiverId, $title);
- $message = $this->makeMessage($convId, $message);
-
- return $message;
- }
-
- /**
- * fetch all inbox for currently loggedin user with pagination.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function getInbox($order = 'desc', $offset = 0, $take = 20)
- {
- // dump($this->authUserId);
- return $this->conversation->threads($this->authUserId, $order, $offset, $take);
- }
-
- /**
- * fetch all inbox with soft deleted message for currently loggedin user with pagination.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function getInboxAll($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
- }
-
- /**
- * its a alias of getInbox method.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function threads($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->getInbox($order, $offset, $take);
- }
-
- /**
- * its a alias of getInboxAll method.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function threadsAll($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->getInboxAll($order, $offset, $take);
- }
-
- /**
- * fetch all conversation by using conversation id.
- *
- * @param int $conversationId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function getConversationsById($conversationId, $offset = 0, $take = 20)
- {
- // dump($conversationId);
- // dd($this->authUserId);
- $conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
- return $this->makeMessageCollection($conversations);
- }
-
- /**
- * fetch all conversations that match the given tag id
- *
- * @param int $tag_id
- *
- * @return collection
- */
- public function getConversationsByTagId($tag_id)
- {
- // $threads = $this->conversation->threads($this->authUserId, 'id', 6, 6);
- $conversations_ = $this->conversation->getMessagesByTagId($tag_id, $this->authUserId);
- $user_id = $this->authUserId;
- $conversations = collect($conversations_)->filter(function ($item) use ($tag_id) {
- // dd($item);
- return ($item->tags->pluck('id')->contains($tag_id));
- });
-
- $threads = [];
-
- foreach ($conversations as $conversation) {
- $collection = (object) null;
- $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
- $collection->thread = $conversation->messages->first();
- $collection->conversation = $conversation;
- $collection->messages = $conversation->messages;
- $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
- return $query
- ->where('user_id', '!=', $user_id)
- ->where('is_read', '=', '0');
- })->get();
- // dump($conversation->id);
- // dump($collection->unreadmessages);
- $collection->withUser = $conversationWith;
- $threads[] = $collection;
- }
-
- return collect($threads);
- }
-
- /**
- * fetch all conversation with soft deleted messages by using conversation id.
- *
- * @param int $conversationId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function getConversationsAllById($conversationId, $offset = 0, $take = 20)
- {
- $conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
-
- return $this->makeMessageCollection($conversations);
- }
-
- /**
- * create a new message by using sender id.
- *
- * @param int $senderId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getConversationsByUserId($senderId, $offset = 0, $take = 20)
- {
- $conversationId = $this->isConversationExists($senderId, $this->authUserId);
- if ($conversationId) {
- return $this->getConversationsById($conversationId, $offset, $take);
- }
-
- return false;
- }
-
- /**
- * create a new message by using sender id.
- *
- * @param int $senderId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
- {
- $conversationId = $this->isConversationExists($senderId, $this->authUserId);
- if ($conversationId) {
- return $this->getConversationsAllById($conversationId, $offset, $take);
- }
-
- return false;
- }
-
- /**
- * gets tags owned/created by this user
- *
- * @return collection
- */
- public function getUserTags()
- {
- return Tags\Tag::where(['user_id' => $this->authUserId])
- ->where('name', '!=', Talk::starTag)
- ->get();
- }
-
- /**
- * creares tag for user
- *
- * @param string $tagName
- *
- * @return bool
- */
- public function createTagForUser($tagName)
- {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
- }
-
- return !empty($tag);
- }
-
- return false;
- }
-
- /**
- * adds a tag to a conversation. Creates the tag if it does not exist for the user
- * This allows for several users to maintain same tag name conveniently without any conflicts/issues
- *
- * @param int $conversationId
- *
- * @return bool
- */
- public function addTagToConversation($conversationId, $tagName)
- {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
- }
-
- $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')->findOrFail($conversationId);
- if (!$conversation->tags->pluck('id')->contains($tag->id)) {
- $conversation->addTag($tag);
- }
-
- return true;
- }
-
- return false;
- }
-
- /**
- *removes tag from a conversation
- *
- * @param int $conversationId
- * @param int $tagId
- *
- * @return bool
- */
- public function removeTagFromConversation($conversationId, $tagId)
- {
- if (!empty($conversationId) && !empty($tagId)) {
- //confirm user owns this tag
- $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'id' => $tagId])->firstOrFail();
- $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')
- ->where(function ($query) {
- $query
- ->where("user_one", $this->authUserId)
- ->orWhere("user_two", $this->authUserId);
- })->findOrFail($conversationId);
-
- $conversation->tags()->detach($tagId);
-
- return true;
- }
-
- return false;
- }
- /**
- * its an alias of getConversationById.
- *
- * @param int $conversationId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessages($conversationId, $offset = 0, $take = 20)
- {
- return $this->getConversationsById($conversationId, $offset, $take);
- }
-
- /**
- * its an alias of getConversationAllById.
- *
- * @param int $conversationId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesAll($conversationId, $offset = 0, $take = 20)
- {
- return $this->getConversationsAllById($conversationId, $offset, $take);
- }
-
- /**
- * its an alias by getConversationByUserId.
- *
- * @param int $senderId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesByUserId($userId, $offset = 0, $take = 20)
- {
- return $this->getConversationsByUserId($userId, $offset, $take);
- }
-
- /**
- * its an alias by getConversationAllByUserId.
- *
- * @param int $senderId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesAllByUserId($userId, $offset = 0, $take = 20)
- {
- return $this->getConversationsAllByUserId($userId, $offset, $take);
- }
-
- /**
- * read a single message by message id.
- *
- * @param int $messageId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function readMessage($messageId = null)
- {
- if (!is_null($messageId)) {
- $message = $this->message->with(['sender', 'conversation'])->find($messageId);
-
- if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
- return $message;
- }
- }
-
- return false;
- }
-
- /**
- * marks a message as seen.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function makeSeen($messageId)
- {
- $seen = $this->message->update($messageId, ['is_seen' => 1]);
- if ($seen) {
- return true;
- }
-
- return false;
- }
- /**
- * marks a message as read.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function markRead($messageId)
- {
- if (!is_null($messageId)) {
- $message = $this->message->with(['sender', 'conversation'])->find($messageId);
- if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
-
- //only needful to mark as read if you are the recipient
- if ($message->sender->id != $this->authUserId) {
- $read = $this->message->update($messageId, ['is_read' => 1]);
- if (!$read) {
- return falsse;
- }
- }
-
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * gets messages not yet read in a particular conversation
- *
- * @param int $conversationId
- *
- * @return mixed
- */
- public function getUnreadMessagesInConversation($conversationId)
- {
- if (!is_null($conversationId)) {
- $message = $this->conversation->with(['messages'])->find($conversationId);
- if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
-
- $unread = [];
- $unread = collect($this->conversation->messages)->filter(function ($message) use ($authUserId) {
- return (($message->sender->id != $authUserId) && ($message->is_read != 1));
- });
-
- return $unread;
- }
- }
-
- return false;
- }
-
- /**
- * gets all messages not yet read is all conversations altogether
- *
- * @param int $conversationId
- *
- * @return collection
- */
- public function getAllUnreadMessages()
- {
- $messages = collect();
- $user_id = $this->authUserId;
- $conv = new \Nahid\Talk\Conversations\Conversation();
- $conversations = $conv->with(['messages' => function ($query) use ($user_id) {
- return $query
- ->where('user_id', '!=', $user_id)
- ->where('is_read', '=', '0')
- ;
- }])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->get();
-
- foreach ($conversations as $conversation) {
- $messages = $messages->merge($conversation->messages);
- }
-
- // dump($messages);
- return $messages;
- }
-
- /**
- * gets all latest messages sent to auth'ed user
- *
- * @param int $conversationId
- *
- * @return collection
- */
- public function getLatestMessages()
- {
- if ($this->latestMessages == null) {
-
- $messages = collect();
- $user_id = $this->authUserId;
- $conv = new \Nahid\Talk\Conversations\Conversation();
- $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
- return $query->where('user_id', '!=', $user_id)->with(['conversation']);
- }])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->orderBy('created_at')
- ->take(5)
- ->get();
-
- foreach ($msgThread as $thread) {
- $messages = collect($messages)->merge($thread->messages);
- }
-
- $this->latestMessages = $messages;
- }
-
- return $this->latestMessages;
- }
-
- /**
- * gets the count of all messages not yet read in all conversations altogether
- *
- * @param int $conversationId
- *
- * @return int
- */
- public function getUnreadMessagesCount()
- {
- if ($this->unreadMessageCount == null) {
- $this->unreadMessageCount = $this->getAllUnreadMessages()->count();
- }
-
- return $this->unreadMessageCount;
- }
-
- /**
- * get receiver information for this conversation.
- *
- * @param int $conversationId
- *
- * @return UserModel
- *
- * @deprecated since version 2.0.0 Remove it from version 2.0.2
- */
- public function getReceiverInfo($conversationId)
- {
- $conversation = $this->conversation->find($conversationId);
- $receiver = '';
- if ($conversation->user_one == $this->authUserId) {
- $receiver = $conversation->user_two;
- } else {
- $receiver = $conversation->user_one;
- }
-
- $userModel = $this->config('talk.user.model');
- $user = new $userModel();
-
- return $user->find($receiver);
- }
-
- /**
- * delete a specific message, its a softdelete process. All message stored in db.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function deleteMessage($messageId)
- {
- return $this->message->softDeleteMessage($messageId, $this->authUserId);
- }
-
- /**
- * permanently delete message for this id.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function deleteForever($messageId)
- {
- $deleteMessage = $this->message->delete($messageId);
- if ($deleteMessage) {
- return true;
- }
-
- return false;
- }
-
- /**
- * delete message threat or conversation by conversation id.
- *
- * @param int $id
- *
- * @return bool
- */
- public function deleteConversations($id)
- {
- $deleteConversation = $this->conversation->delete($id);
- if ($deleteConversation) {
- return $this->message->deleteMessages($id);
- }
-
- return false;
- }
-
- /**
- * its an alias of deleteConversations.
- *
- * @param int $id
- *
- * @return bool
- */
- public function deleteThread($id = null)
- {
- return $this->deleteConversations($id);
- }
+class Talk {
+
+ /**
+ * Now users can attach special importance to conversations by star-ing them.
+ * This is the name of the special 'star' tag
+ * special tags do not have creator. i.e. their user_id is null. Useful to ensure when getting tags for a user, special tags are not retuened
+ *
+ */
+ const STARTAG = "talk_special_tag_star";
+
+ /**
+ * configurations instance.
+ *
+ * @var \Illuminate\Contracts\Config\Repository
+ */
+ protected $config;
+
+ /**
+ * The ConversationRepository class instance.
+ *
+ * @var \Nahid\Talk\Conversations\ConversationRepository
+ */
+ protected $conversation;
+
+ /**
+ * The MessageRepository class instance.
+ *
+ * @var \Nahid\Talk\Messages\MessageRepository
+ */
+ protected $message;
+
+ /**
+ * Broadcast class instance.
+ *
+ * @var \Nahid\Talk\Live\Broadcast
+ */
+ protected $broadcast;
+
+ /*
+ * just a helper for the last 5 messages in your mailbox
+ */
+ protected $latestMessages = null;
+
+ /**
+ * Currently loggedin user id.
+ *
+ * @var int
+ */
+ protected $authUserId;
+
+ /**
+ * Initialize and instantiate conversation and message repositories.
+ *
+ * @param \Nahid\Talk\Conversations\ConversationRepository $conversation
+ * @param \Nahid\Talk\Messages\MessageRepository $message
+ */
+ public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message) {
+ // dump('calling meeeeeeeeee');
+ $this->config = $config;
+ $this->conversation = $conversation;
+ $this->message = $message;
+ $this->broadcast = $broadcast;
+ }
+
+ /**
+ * make two users as serialize with ascending order.
+ *
+ * @param int $user1
+ * @param int $user2
+ *
+ * @return array
+ */
+ protected function getSerializeUser($user1, $user2) {
+ $user = [];
+ $user['one'] = ($user1 < $user2) ? $user1 : $user2;
+ $user['two'] = ($user1 < $user2) ? $user2 : $user1;
+
+ return $user;
+ }
+
+ /**
+ * create a new message by using conversationId.
+ *
+ * @param int $conversationId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ protected function makeMessage($conversationId, $message) {
+ $message = $this->message->create([
+ 'message' => $message,
+ 'conversation_id' => $conversationId,
+ 'user_id' => $this->authUserId,
+ 'is_seen' => 0,
+ ]);
+
+ $message->conversation->touch();
+ $this->broadcast->transmission($message);
+
+ return $message;
+ }
+
+ /*
+ * Make new message collections to response with formatted data
+ *
+ *@param \Talk\Conversations\Conversation $conversations
+ *@return object|bool
+ */
+ protected function makeMessageCollection($conversations) {
+ if (!$conversations) {
+ return false;
+ }
+
+ $collection = (object) null;
+ if ($conversations->user_one == $this->authUserId || $conversations->user_two == $this->authUserId) {
+ $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
+ $collection->withUser = $withUser;
+ $collection->messages = $conversations->messages;
+
+ //mark them as read
+ foreach ($collection->messages as $mssg) {
+ if ($mssg->sender->id != $this->authUserId) {
+ if (!Talk::user($this->authUserId)->markRead($mssg->id)) {
+ return false;
+ }
+ }
+ }
+
+ return $collection;
+ }
+
+ return false;
+ }
+
+ /**
+ * make new conversation with the given receiverId with currently loggedin user.
+ *
+ * @param int $receiverId
+ *
+ * @return int
+ */
+ protected function newConversation($receiverId, $title, $tagName = null) {
+ // $conversationId = $this->isConversationExists($receiverId);
+ $user = $this->getSerializeUser($this->authUserId, $receiverId);
+
+ // if ($conversationId === false) {
+ $conversation = $this->conversation->create([
+ 'user_one' => $user['one'],
+ 'user_two' => $user['two'],
+ 'title' => $title,
+ 'status' => 1,
+ ]);
+
+ if ($conversation) {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
+ }
+
+ $conversation->addTag($tag);
+ }
+
+ return $conversation->id;
+ }
+ // }
+
+ return $conversationId;
+ }
+
+ /**
+ * set currently authenticated user id for global usage.
+ *
+ * @param int $id
+ *
+ * @return int|bool
+ */
+ public function setAuthUserId($id = null) {
+ if (!is_null($id)) {
+ return $this->authUserId = $id;
+ }
+
+ return false;
+ }
+
+ /*
+ * its set user id instantly when you fetch or access data. if you you haven't
+ * set authenticated user id globally or you want to fetch work with
+ * instant users information, you may use it
+ *
+ * @param int $id
+ * @return \Nahid\Talk\Talk|bool
+ * */
+ public function user($id = null) {
+ if ($this->setAuthUserId($id)) {
+ return $this;
+ }
+
+ return false;
+ }
+
+ /**
+ * make sure is this conversation exist for this user with currently loggedin user.
+ *
+ * @param int $userId
+ *
+ * @return bool|int
+ */
+ public function isConversationExists($userId) {
+ if (empty($userId)) {
+ return false;
+ }
+
+ $user = $this->getSerializeUser($this->authUserId, $userId);
+
+ return $this->conversation->isExistsAmongTwoUsers($user['one'], $user['two']);
+ }
+
+ /**
+ * check the given user exist for the given conversation.
+ *
+ * @param int $conversationId
+ * @param int $userId
+ *
+ * @return bool
+ */
+ public function isAuthenticUser($conversationId, $userId) {
+ if ($conversationId && $userId) {
+ return $this->conversation->isUserExists($conversationId, $userId);
+ }
+
+ return false;
+ }
+
+ /**
+ * send a message by using converstionid.
+ *
+ * @param int $conversationId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function sendMessage($conversatonId, $message) {
+ if ($conversatonId && $message) {
+ if ($this->conversation->existsById($conversatonId)) {
+ $message = $this->makeMessage($conversatonId, $message);
+
+ return $message;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * create a new message by using receiverid.
+ *
+ * @param int $receiverId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function sendMessageByUserId($receiverId, $message, $title = null) {
+ if ($conversationId = $this->isConversationExists($receiverId)) {
+ $con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
+ if ($con->title == $title) {
+ // dd("same: {$con->title} == $title");
+ $message = $this->makeMessage($conversationId, $message);
+ return $message;
+ }
+ }
+
+ $convId = $this->newConversation($receiverId, $title);
+ $message = $this->makeMessage($convId, $message);
+
+ return $message;
+ }
+
+ /**
+ * fetch all inbox for currently loggedin user with pagination.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function getInbox($order = 'desc', $offset = 0, $take = 20) {
+ // dump($this->authUserId);
+ return $this->conversation->threads($this->authUserId, $order, $offset, $take);
+ }
+
+ /**
+ * fetch all inbox with soft deleted message for currently loggedin user with pagination.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function getInboxAll($order = 'desc', $offset = 0, $take = 20) {
+ return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
+ }
+
+ /**
+ * its a alias of getInbox method.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function threads($order = 'desc', $offset = 0, $take = 20) {
+ return $this->getInbox($order, $offset, $take);
+ }
+
+ /**
+ * its a alias of getInboxAll method.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function threadsAll($order = 'desc', $offset = 0, $take = 20) {
+ return $this->getInboxAll($order, $offset, $take);
+ }
+
+ /**
+ * fetch all conversation by using conversation id.
+ *
+ * @param int $conversationId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function getConversationsById($conversationId, $offset = 0, $take = 20) {
+ // dump($conversationId);
+ // dd($this->authUserId);
+ $conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
+ return $this->makeMessageCollection($conversations);
+ }
+
+ /**
+ * fetch all conversations that match the given tag id
+ *
+ * @param int $tag_id
+ *
+ * @return collection
+ */
+ public function getConversationsByTagId($tag_id) {
+ // $threads = $this->conversation->threads($this->authUserId, 'id', 6, 6);
+ $conversations_ = $this->conversation->getMessagesByTagId($tag_id, $this->authUserId);
+ $user_id = $this->authUserId;
+ $conversations = collect($conversations_)->filter(function ($item) use ($tag_id) {
+ // dd($item);
+ return ($item->tags->pluck('id')->contains($tag_id));
+ });
+
+ $threads = [];
+
+ foreach ($conversations as $conversation) {
+ $collection = (object) null;
+ $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
+ $collection->thread = $conversation->messages->first();
+ $collection->conversation = $conversation;
+ $collection->messages = $conversation->messages;
+ $collection->unreadmessages =
+ $conversation->messages()->where(function ($query) use ($user_id) {
+ $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0');
+ })->get();
+ // dump($conversation->id);
+ // dump($collection->unreadmessages);
+ $collection->withUser = $conversationWith;
+ $threads[] = $collection;
+ }
+
+ return collect($threads);
+ }
+
+ /**
+ * fetch all conversation with soft deleted messages by using conversation id.
+ *
+ * @param int $conversationId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function getConversationsAllById($conversationId, $offset = 0, $take = 20) {
+ $conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
+
+ return $this->makeMessageCollection($conversations);
+ }
+
+ /**
+ * create a new message by using sender id.
+ *
+ * @param int $senderId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getConversationsByUserId($senderId, $offset = 0, $take = 20) {
+ $conversationId = $this->isConversationExists($senderId, $this->authUserId);
+ if ($conversationId) {
+ return $this->getConversationsById($conversationId, $offset, $take);
+ }
+
+ return false;
+ }
+
+ /**
+ * create a new message by using sender id.
+ *
+ * @param int $senderId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20) {
+ $conversationId = $this->isConversationExists($senderId, $this->authUserId);
+ if ($conversationId) {
+ return $this->getConversationsAllById($conversationId, $offset, $take);
+ }
+
+ return false;
+ }
+
+ /**
+ * gets tags owned/created by this user
+ *
+ * @return collection
+ */
+ public function getUserTags() {
+ return Tags\Tag::where(['user_id' => $this->authUserId])
+ ->where('name', '!=', Talk::STARTAG)
+ ->get();
+ }
+
+ /**
+ * creares tag for user
+ *
+ * @param string $tagName
+ *
+ * @return bool
+ */
+ public function createTagForUser($tagName) {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
+ }
+
+ return !empty($tag);
+ }
+
+ return false;
+ }
+
+ /**
+ * adds a tag to a conversation. Creates the tag if it does not exist for the user
+ * This allows for several users to maintain same tag name conveniently without any conflicts/issues
+ *
+ * @param int $conversationId
+ * @param string $tagName
+ * @param bool $specialTagOnlyOne indicates if only one tag should b maintained, thus supporting use of custom "system tags" e.g. for notifications
+ *
+ * @return bool
+ */
+ public function addTagToConversation($conversationId, $tagName, bool $specialTagOnlyOne = false) {
+ if (!empty($tagName)) {
+ //treat star tag specially
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
+ if ($tagName == \Nahid\Talk\Talk::STARTAG || $specialTagOnlyOne) {
+ //at any time, we want to always have only one star tag, irrespective of who created it
+ //Therefore, this will ensure that we have only one star tag in our db table
+ $tag = Tags\Tag::where(['name' => $tagName])->first();
+ }
+
+ if (is_null($tag)) {
+ //special tags dn't have owners
+ if ($tagName == \Nahid\Talk\Talk::STARTAG || $specialTagOnlyOne) {
+ $tag = Tags\Tag::create(['name' => $tagName]);
+ } else {
+ $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
+ }
+ }
+
+ $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')->findOrFail($conversationId);
+ if (!$conversation->tags->pluck('id')->contains($tag->id)) {
+ $conversation->addTag($tag);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ *removes tag from a conversation
+ *
+ * @param int $conversationId
+ * @param int $tagId
+ *
+ * @return bool
+ */
+ public function removeTagFromConversation($conversationId, $tagId) {
+ if (!empty($conversationId) && !empty($tagId)) {
+ //confirm user owns this tag
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'id' => $tagId])->firstOrFail();
+ $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')
+ ->where(function ($query) {
+ $query
+ ->where("user_one", $this->authUserId)
+ ->orWhere("user_two", $this->authUserId);
+ })->findOrFail($conversationId);
+
+ $conversation->tags()->detach($tagId);
+
+ return true;
+ }
+
+ return false;
+ }
+ /**
+ * its an alias of getConversationById.
+ *
+ * @param int $conversationId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessages($conversationId, $offset = 0, $take = 20) {
+ return $this->getConversationsById($conversationId, $offset, $take);
+ }
+
+ /**
+ * its an alias of getConversationAllById.
+ *
+ * @param int $conversationId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesAll($conversationId, $offset = 0, $take = 20) {
+ return $this->getConversationsAllById($conversationId, $offset, $take);
+ }
+
+ /**
+ * its an alias by getConversationByUserId.
+ *
+ * @param int $senderId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesByUserId($userId, $offset = 0, $take = 20) {
+ return $this->getConversationsByUserId($userId, $offset, $take);
+ }
+
+ /**
+ * its an alias by getConversationAllByUserId.
+ *
+ * @param int $senderId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesAllByUserId($userId, $offset = 0, $take = 20) {
+ return $this->getConversationsAllByUserId($userId, $offset, $take);
+ }
+
+ /**
+ * read a single message by message id.
+ *
+ * @param int $messageId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function readMessage($messageId = null) {
+ if (!is_null($messageId)) {
+ $message = $this->message->with(['sender', 'conversation'])->find($messageId);
+
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+ return $message;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * marks a message as seen.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function makeSeen($messageId) {
+ $seen = $this->message->update($messageId, ['is_seen' => 1]);
+ if ($seen) {
+ return true;
+ }
+
+ return false;
+ }
+ /**
+ * marks a message as read.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function markRead($messageId) {
+ if (!is_null($messageId)) {
+ $message = $this->message->with(['sender', 'conversation'])->find($messageId);
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+
+ //only needful to mark as read if you are the recipient
+ if ($message->sender->id != $this->authUserId) {
+ $read = $this->message->update($messageId, ['is_read' => 1]);
+ if (!$read) {
+ return falsse;
+ }
+ }
+
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * gets messages not yet read in a particular conversation
+ *
+ * @param int $conversationId
+ *
+ * @return mixed
+ */
+ public function getUnreadMessagesInConversation($conversationId) {
+ if (!is_null($conversationId)) {
+ $message = $this->conversation->with(['messages'])->find($conversationId);
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+
+ $unread = [];
+ $unread = collect($this->conversation->messages)->filter(function ($message) use ($authUserId) {
+ return (($message->sender->id != $authUserId) && ($message->is_read != 1));
+ });
+
+ return $unread;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * gets all messages not yet read in all conversations altogether
+ *
+ *
+ * @param int $removeSpecialMessages : allows to use Talk also to send system notifications
+ * to users by allowing some conversations to be "tagged" as "special"
+ *
+ * @return collection
+ */
+ public function getAllUnreadMessages($removeSpecialMessages = false) {
+ $messages = collect();
+ $user_id = $this->authUserId;
+ $conv = new \Nahid\Talk\Conversations\Conversation();
+ $conversations = $conv->with(
+ [
+ 'messages' => function ($query) use ($user_id) {
+ $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0');
+ },
+ 'tags',
+ ])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->get();
+
+ if ($removeSpecialMessages) {
+ $conversations = $conversations->filter(function ($conversation) {
+ $specialTags = $conversation->tags()
+ ->where('is_special_tag', '=', '1')
+ ->get();
+ return count($specialTags->toArray()) == 0;
+ });
+ }
+
+ foreach ($conversations as $conversation) {
+ $messages = $messages->merge($conversation->messages);
+ }
+
+ return $messages;
+ }
+
+ /**
+ * gets all latest messages sent to auth'ed user
+ *
+ * @param int $conversationId
+ *
+ * @return collection
+ */
+ public function getLatestMessages() {
+ if ($this->latestMessages == null) {
+
+ $messages = collect();
+ $user_id = $this->authUserId;
+ $conv = new \Nahid\Talk\Conversations\Conversation();
+ $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
+ $query->where('user_id', '!=', $user_id)->with(['conversation']);
+ }])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->orderBy('created_at')
+ ->take(5)
+ ->get();
+
+ foreach ($msgThread as $thread) {
+ $messages = collect($messages)->merge($thread->messages);
+ }
+
+ $this->latestMessages = $messages;
+ }
+
+ return $this->latestMessages;
+ }
+
+ /**
+ * gets the count of all messages not yet read in all conversations altogether
+ *
+ * @param int $removeSpecialMessages whether to remove special tag messages: This allows for Nahid to be useful for sending custom system notifcation messages to users
+ *
+ * @return int
+ */
+ public function getUnreadMessagesCount($removeSpecialMessages = false) {
+ return $this->getAllUnreadMessages($removeSpecialMessages)->count();
+ }
+
+ /**
+ * get receiver information for this conversation.
+ *
+ * @param int $conversationId
+ *
+ * @return UserModel
+ *
+ * @deprecated since version 2.0.0 Remove it from version 2.0.2
+ */
+ public function getReceiverInfo($conversationId) {
+ $conversation = $this->conversation->find($conversationId);
+ $receiver = '';
+ if ($conversation->user_one == $this->authUserId) {
+ $receiver = $conversation->user_two;
+ } else {
+ $receiver = $conversation->user_one;
+ }
+
+ $userModel = $this->config('talk.user.model');
+ $user = new $userModel();
+
+ return $user->find($receiver);
+ }
+
+ /**
+ * delete a specific message, its a softdelete process. All message stored in db.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function deleteMessage($messageId) {
+ return $this->message->softDeleteMessage($messageId, $this->authUserId);
+ }
+
+ /**
+ * permanently delete message for this id.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function deleteForever($messageId) {
+ $deleteMessage = $this->message->delete($messageId);
+ if ($deleteMessage) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * delete message threat or conversation by conversation id.
+ *
+ * @param int $id
+ *
+ * @return bool
+ */
+ public function deleteConversations($id) {
+ $deleteConversation = $this->conversation->delete($id);
+ if ($deleteConversation) {
+ return $this->message->deleteMessages($id);
+ }
+
+ return false;
+ }
+
+ /**
+ * its an alias of deleteConversations.
+ *
+ * @param int $id
+ *
+ * @return bool
+ */
+ public function deleteThread($id = null) {
+ return $this->deleteConversations($id);
+ }
}
From 2ac6c5de225c880464a4fb1882d358a0eefe5f24 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 23 Sep 2018 23:34:10 +0100
Subject: [PATCH 15/52] bug fixes, better explanations
---
src/Messages/Message.php | 114 +++++++++++++++++++--------------------
src/Talk.php | 4 +-
2 files changed, 58 insertions(+), 60 deletions(-)
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index 28c159e..562dc03 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -4,71 +4,67 @@
use Illuminate\Database\Eloquent\Model;
-class Message extends Model
-{
- protected $table = 'messages';
+class Message extends Model {
+ protected $table = 'messages';
- public $timestamps = true;
+ public $timestamps = true;
- public $fillable = [
- 'message',
- 'is_seen',
- 'is_read',
- 'deleted_from_sender',
- 'deleted_from_receiver',
- 'user_id',
- 'conversation_id',
- ];
+ public $fillable = [
+ 'message',
+ 'is_seen',
+ 'is_read',
+ 'deleted_from_sender',
+ 'deleted_from_receiver',
+ 'user_id',
+ 'conversation_id',
+ ];
- /*
- * make dynamic attribute for human readable time
- *
- * @return string
- * */
- public function getHumansTimeAttribute()
- {
- $date = $this->created_at;
- $now = $date->now();
+ /*
+ * make dynamic attribute for human readable time
+ *
+ * @return string
+ * */
+ public function getHumansTimeAttribute() {
+ //laravel sometimes has $this=null but attributes proprty works perfectly well
+ $date = \Carbon\Carbon::parse($this->attributes['created_at']);
+ $now = $date->now();
- if ($date->isToday()) {
- return $date->diffForHumans(null, false, true);
- } else {
- if ($date->isSameYear($now)) {
- return $date->format("M j");
- }
- }
+ if ($date->isToday()) {
+ return $date->diffForHumans(null, false, true);
+ } else {
+ if ($date->isSameYear($now)) {
+ return $date->format("M j");
+ }
+ }
- return $date->format("M j, Y");
- // return $date->diffForHumans(null, true, true) . ' ago';
- }
+ return $date->format("M j, Y");
+ // return $date->diffForHumans(null, true, true) . ' ago';
+ }
- /*
- * make a relation between conversation model
- *
- * @return collection
- * */
- public function conversation()
- {
- return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
- }
+ /*
+ * make a relation between conversation model
+ *
+ * @return collection
+ * */
+ public function conversation() {
+ return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
+ }
- /*
- * make a relation between user model
- *
- * @return collection
- * */
- public function user()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'));
- }
+ /*
+ * make a relation between user model
+ *
+ * @return collection
+ * */
+ public function user() {
+ return $this->belongsTo(config('talk.user.model', 'App\User'));
+ }
- /*
- * its an alias of user relation
- *
- * @return collection
- * */
- public function sender()
- {
- return $this->user();
- }
+ /*
+ * its an alias of user relation
+ *
+ * @return collection
+ * */
+ public function sender() {
+ return $this->user();
+ }
}
diff --git a/src/Talk.php b/src/Talk.php
index 4e647da..392f3ca 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -680,7 +680,9 @@ public function getUnreadMessagesInConversation($conversationId) {
*
*
* @param int $removeSpecialMessages : allows to use Talk also to send system notifications
- * to users by allowing some conversations to be "tagged" as "special"
+ * to users by allowing some conversations to be "tagged" as "special".
+ * As a rule of thumb, any conversation that is not "normal" message/converation should simply be tagged as special, so
+ * that it is easy to get unread messages..etcwitout confusing message contexts
*
* @return collection
*/
From 2f748c84045483e252e577882e7132e709d6ee97 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 14 Oct 2018 21:56:43 +0100
Subject: [PATCH 16/52] correct PR#135 with more naturalisic time modifier.
Some more fixes
---
src/Talk.php | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/src/Talk.php b/src/Talk.php
index 392f3ca..cc2cbb4 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -503,7 +503,10 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
if (is_null($tag)) {
//special tags dn't have owners
if ($tagName == \Nahid\Talk\Talk::STARTAG || $specialTagOnlyOne) {
- $tag = Tags\Tag::create(['name' => $tagName]);
+ $tag = Tags\Tag::create([
+ 'name' => $tagName,
+ 'is_special_tag' => 1,
+ ]);
} else {
$tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
}
@@ -705,6 +708,7 @@ public function getAllUnreadMessages($removeSpecialMessages = false) {
if ($removeSpecialMessages) {
$conversations = $conversations->filter(function ($conversation) {
+ $tags = $conversation->tags;
$specialTags = $conversation->tags()
->where('is_special_tag', '=', '1')
->get();
From dac03fdd22ecec00eb708fb9b06cff8389e001c4 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 14 Oct 2018 22:00:01 +0100
Subject: [PATCH 17/52] bug fixes and some more adjustments based on PR#35
---
composer.json | 1 +
...0_05_110622_create_conversations_table.php | 3 +-
...dd_title_column_to_conversations_table.php | 33 +++++
...2_add_is_read_column_to_messages_table.php | 32 +++++
src/Conversations/Conversation.php | 119 ++++++++----------
src/Conversations/ConversationRepository.php | 27 ----
src/Messages/Message.php | 20 ++-
src/Talk.php | 9 +-
8 files changed, 145 insertions(+), 99 deletions(-)
create mode 100644 database/migrations/2018_11_10_160722_add_title_column_to_conversations_table.php
create mode 100644 database/migrations/2018_11_10_164802_add_is_read_column_to_messages_table.php
diff --git a/composer.json b/composer.json
index 418f9c5..b0d7d8a 100644
--- a/composer.json
+++ b/composer.json
@@ -14,6 +14,7 @@
"php": ">=5.5.9",
"illuminate/contracts": ">=5.0.0",
"illuminate/support": ">=5.0.0",
+ "doctrine/dbal": "~2.5.0|~2.6.0|~2.7.0",
"sebastian-berc/repositories": "^1.0",
"nesbot/carbon": "^1.21",
"pusher/pusher-php-server": "^3.0",
diff --git a/database/migrations/2015_10_05_110622_create_conversations_table.php b/database/migrations/2015_10_05_110622_create_conversations_table.php
index 77ded81..c69e792 100644
--- a/database/migrations/2015_10_05_110622_create_conversations_table.php
+++ b/database/migrations/2015_10_05_110622_create_conversations_table.php
@@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
-class CreateConversationsTable extends Migration
+class CreateConversationTagsTable extends Migration
{
/**
* Run the migrations.
@@ -15,7 +15,6 @@ public function up()
{
Schema::create('conversations', function (Blueprint $table) {
$table->increments('id');
- $table->varchar('title');
$table->integer('user_one');
$table->integer('user_two');
$table->boolean('status');
diff --git a/database/migrations/2018_11_10_160722_add_title_column_to_conversations_table.php b/database/migrations/2018_11_10_160722_add_title_column_to_conversations_table.php
new file mode 100644
index 0000000..ea298f6
--- /dev/null
+++ b/database/migrations/2018_11_10_160722_add_title_column_to_conversations_table.php
@@ -0,0 +1,33 @@
+boolean('is_read')->default(0)->change();
+ });
+ }
+
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('conversations', function (Blueprint $table) {
+ $table->dropColumn('is_read');
+ });
+ }
+}
diff --git a/database/migrations/2018_11_10_164802_add_is_read_column_to_messages_table.php b/database/migrations/2018_11_10_164802_add_is_read_column_to_messages_table.php
new file mode 100644
index 0000000..06f958f
--- /dev/null
+++ b/database/migrations/2018_11_10_164802_add_is_read_column_to_messages_table.php
@@ -0,0 +1,32 @@
+varchar('title')->change();
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('messages', function (Blueprint $table) {
+ $table->dropColumn('title');
+ });
+ }
+}
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index 7b7a188..e763055 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -4,68 +4,59 @@
use Illuminate\Database\Eloquent\Model;
-class Conversation extends Model
-{
- protected $table = 'conversations';
- public $timestamps = true;
-
- public $fillable = [
- 'user_one',
- 'user_two',
- 'title',
- 'status',
- ];
-
- /*
- * make a relation with tags
- *
- * return relationship
- * */
- public function tags()
- {
- return $this->belongsToMany('Nahid\Talk\Tags\Tag');
- }
-
- /*
- * make a relation between message
- *
- * return collection
- * */
- public function messages()
- {
- return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
- ->with('sender');
- }
-
- /*
- * make a relation between first user from conversation
- *
- * return collection
- * */
- public function userone()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one');
- }
-
- /*
- * make a relation between second user from conversation
- *
- * return collection
- * */
- public function usertwo()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two');
- }
-
- /*
- * adds a tag to this conversation
- *
- * return bool
- * */
- public function addTag(\Nahid\Talk\Tags\Tag $tag)
- {
- // var_dump($this);
- $this->tags()->attach($tag->id);
- return true;
- }
+class Conversation extends Model {
+ protected $table = 'conversations';
+ public $timestamps = true;
+ public $fillable = [
+ 'user_one',
+ 'user_two',
+ 'title',
+ 'status',
+ ];
+
+ /*
+ * make a relation with tags
+ *
+ * return relationship
+ * */
+ public function tags() {
+ return $this->belongsToMany('Nahid\Talk\Tags\Tag');
+ }
+
+ /*
+ * make a relation between message
+ *
+ * return collection
+ * */
+ public function messages() {
+ return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
+ ->with('sender');
+ }
+
+ /*
+ * make a relation between first user from conversation
+ *
+ * return collection
+ * */
+ public function userone() {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one', config('talk.user.ownerKey'));
+ }
+
+ /*
+ * make a relation between second user from conversation
+ *
+ * return collection
+ * */
+ public function usertwo() {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two', config('talk.user.ownerKey'));
+ }
+
+ /*
+ * adds a tag to this conversation
+ *
+ * return void
+ * */
+ public function addTag(\Nahid\Talk\Tags\Tag $tag) {
+ return $this->tags()->attach($tag->id);
+ }
}
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 87269b1..240c3a9 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -113,8 +113,6 @@ public function threads($user_id, $order, $offset, $take)
->where('user_id', '!=', $user_id)
->where('is_read', '=', '0');
})->get();
- // dump($conversation->id);
- // dump($collection->unreadmessages);
$collection->withUser = $conversationWith;
$threads[] = $collection;
}
@@ -175,31 +173,6 @@ public function getMessagesById($conversationId, $userId, $offset, $take)
}])->with(['userone', 'usertwo'])->find($conversationId);
}
- /*
- * get all conversations by given tag id
- *
- * @param int $conversationId
- * @param int $userId
- * @return collection
- * * /
- public function getMessagesByTagId($conversationId, $userId)
- {
- return Conversation::with(['messages' => function ($query) use ($userId) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
-
- $query->offset($offset)->take($take);
-
- }])->with(['userone', 'usertwo', 'tags'])->find($conversationId);
- }
- */
-
/*
* get all conversations by given tag id
*
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index 562dc03..022ab7a 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -29,6 +29,19 @@ public function getHumansTimeAttribute() {
$date = \Carbon\Carbon::parse($this->attributes['created_at']);
$now = $date->now();
+ return $date->diffForHumans($now, true);
+ }
+
+ /*
+ * make dynamic attribute for human readable time - with more naturalisic time modifiers
+ *
+ * @return string
+ * */
+ public function getNaturalHumansTimeAttribute() {
+ //laravel sometimes has $this=null but attributes proprty works perfectly well
+ $date = \Carbon\Carbon::parse($this->attributes['created_at']);
+ $now = $date->now();
+
if ($date->isToday()) {
return $date->diffForHumans(null, false, true);
} else {
@@ -38,7 +51,6 @@ public function getHumansTimeAttribute() {
}
return $date->format("M j, Y");
- // return $date->diffForHumans(null, true, true) . ' ago';
}
/*
@@ -56,7 +68,11 @@ public function conversation() {
* @return collection
* */
public function user() {
- return $this->belongsTo(config('talk.user.model', 'App\User'));
+ return $this->belongsTo(
+ config('talk.user.model', 'App\User'),
+ config('talk.user.foreignKey'),
+ config('talk.user.ownerKey')
+ );
}
/*
diff --git a/src/Talk.php b/src/Talk.php
index cc2cbb4..580c470 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -490,7 +490,8 @@ public function createTagForUser($tagName) {
*
* @return bool
*/
- public function addTagToConversation($conversationId, $tagName, bool $specialTagOnlyOne = false) {
+ public function addTagToConversation($conversationId, $tagName, bool $specialTagOnlyOne = null) {
+ $specialTagOnlyOne = is_bool($specialTagOnlyOne) ? $specialTagOnlyOne : false;
if (!empty($tagName)) {
//treat star tag specially
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
@@ -504,9 +505,9 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
//special tags dn't have owners
if ($tagName == \Nahid\Talk\Talk::STARTAG || $specialTagOnlyOne) {
$tag = Tags\Tag::create([
- 'name' => $tagName,
+ 'name' => $tagName,
'is_special_tag' => 1,
- ]);
+ ]);
} else {
$tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
}
@@ -708,7 +709,7 @@ public function getAllUnreadMessages($removeSpecialMessages = false) {
if ($removeSpecialMessages) {
$conversations = $conversations->filter(function ($conversation) {
- $tags = $conversation->tags;
+ $tags = $conversation->tags;
$specialTags = $conversation->tags()
->where('is_special_tag', '=', '1')
->get();
From 13a21f3ddf9fa434d538afe6da26c7ece5d9b2c9 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Fri, 17 Aug 2018 05:00:43 +0100
Subject: [PATCH 18/52] improvment on ser-to-user multi-conversation
---
README.md | 86 +++++++++++++-
composer.json | 4 +-
config/talk.php | 7 ++
docker-compose.yml | 20 ++++
docker/hhvm/Dockerfile | 21 ++++
docker/php5.5/Dockerfile | 16 +++
docker/php5.6/Dockerfile | 21 ++++
docker/php7/Dockerfile | 21 ++++
src/Conversations/Conversation.php | 110 +++++++++--------
src/Conversations/ConversationRepository.php | 114 ++++++++++++------
src/Embera/Adapter.php | 52 +++++++++
src/Html/HtmlString.php | 88 ++++++++++++++
src/Html/HtmlStringInterface.php | 20 ++++
src/Live/Webcast.php | 3 +-
src/Messages/Message.php | 117 +++++++++++--------
src/Messages/MessageRepository.php | 7 +-
src/Talk.php | 41 +++----
tests/Embera/DriverTest.php | 85 ++++++++++++++
tests/Html/HtmlStringTest.php | 65 +++++++++++
tests/Messages/MessageTest.php | 44 +++++++
tests/Mocks/EmberaMock.php | 28 +++++
21 files changed, 796 insertions(+), 174 deletions(-)
create mode 100644 docker-compose.yml
create mode 100644 docker/hhvm/Dockerfile
create mode 100644 docker/php5.5/Dockerfile
create mode 100644 docker/php5.6/Dockerfile
create mode 100644 docker/php7/Dockerfile
create mode 100644 src/Embera/Adapter.php
create mode 100644 src/Html/HtmlString.php
create mode 100644 src/Html/HtmlStringInterface.php
create mode 100644 tests/Embera/DriverTest.php
create mode 100644 tests/Html/HtmlStringTest.php
create mode 100644 tests/Messages/MessageTest.php
create mode 100644 tests/Mocks/EmberaMock.php
diff --git a/README.md b/README.md
index 00e973a..1b5031e 100644
--- a/README.md
+++ b/README.md
@@ -53,6 +53,7 @@ So let's start your tour :)
* Permanent delete message
* Mark message as seen
* Only participant can view or access there message or message threads
+* Inline url render using oembed specifications
### Installation
@@ -95,7 +96,9 @@ Okay, now you need to configure your user model for Talk. Go to `config/talk.php
```php
return [
'user' => [
- 'model' => 'App\User'
+ 'model' => 'App\User',
+ 'foreignKey' => null,
+ 'ownerKey' => null
],
'broadcast' => [
'enable' => false,
@@ -109,6 +112,11 @@ return [
'encrypted' => true
]
]
+ ],
+ 'oembed' => [
+ 'enabled' => false,
+ 'url' => null,
+ 'key' => null
]
];
```
@@ -293,7 +301,7 @@ array getInbox([$order = 'desc'[,$offset = 0[, $take = 20]]])
```php
// controller method
$inboxes = Talk::getInbox();
-return view('message.threads', compact('inboxes');
+return view('message.threads', compact('inboxes'));
```
```html
@@ -562,6 +570,80 @@ You can pass a callback for working with pusher recieved data. For both `user` a
You can watch [Talk-Live-Demo](https://youtu.be/bN3s_LbObnQ)
+## Oembed support
+
+Talk also supports embed urls simply use `$message->toHtlmString()` in you views to render an embed link
+
+Eg. `This is a youtube embed link: https://www.youtube.com/watch?v=jNQXAC9IVRw`
+
+```html
+
+
Chat with {{$withUser->name}}
+ @foreach ($messages as $msg)
+
+
{{$msg->sender->name}}
+
{{$msg->humans_time}}
+
+ {{$msg->toHtmlString()}}
+
+
+ @endforeach
+
+```
+
+## Custom embed link
+
+If you want to setup your own implementation of oembed you can configure it in the talk config file. You endpoint should follow the [Oembed](https://oembed.com/) specifications
+
+```php
+ 'user' => [
+ 'model' => 'App\User',
+ 'foreignKey' => null,
+ 'ownerKey' => null
+ ],
+ 'broadcast' => [
+ 'enable' => false,
+ 'app_name' => 'your-app-name',
+ 'pusher' => [
+ 'app_id' => '',
+ 'app_key' => '',
+ 'app_secret' => '',
+ 'options' => [
+ 'cluster' => 'ap1',
+ 'encrypted' => true
+ ]
+ ]
+ ],
+ 'oembed' => [
+ 'enabled' => true,
+ 'url' => 'http://your.domain/api/oembed',
+ 'key' => 'yout-auth-api-key'
+ ]
+```
+### Testing
+
+Talk is backwards compatible with php 5.5. Use docker to run unit tests.
+
+```bash
+docker-compose run php55 composer install
+docker-compose run php55 phpunit
+```
+
+```bash
+docker-compose run php56 composer install
+docker-compose run php56 phpunit
+```
+
+```bash
+docker-compose run php7 composer install
+docker-compose run php7 phpunit
+```
+
+```bash
+docker-compose run hhvm composer install
+docker-compose run hhvm phpunit
+```
+
### Try Demo Project
[Talk-Example](https://github.com/nahid/talk-example)
diff --git a/composer.json b/composer.json
index 418f9c5..5b49576 100644
--- a/composer.json
+++ b/composer.json
@@ -17,9 +17,11 @@
"sebastian-berc/repositories": "^1.0",
"nesbot/carbon": "^1.21",
"pusher/pusher-php-server": "^3.0",
- "predis/predis": "~1.1.1"
+ "predis/predis": "~1.1.1",
+ "mpratt/embera": "^1.9"
},
"require-dev": {
+ "ext-json": "*",
"graham-campbell/testbench": "^3.1",
"phpunit/phpunit": "^4.8 || ^5.0"
},
diff --git a/config/talk.php b/config/talk.php
index 249dbe0..5d28713 100644
--- a/config/talk.php
+++ b/config/talk.php
@@ -3,6 +3,8 @@
return [
'user' => [
'model' => 'App\User',
+ 'foreignKey' => null,
+ 'ownerKey' => null,
],
'broadcast' => [
'enable' => false,
@@ -17,4 +19,9 @@
]
],
],
+ 'oembed' => [
+ 'enabled' => false,
+ 'url' => '',
+ 'key' => ''
+ ]
];
diff --git a/docker-compose.yml b/docker-compose.yml
new file mode 100644
index 0000000..cb73188
--- /dev/null
+++ b/docker-compose.yml
@@ -0,0 +1,20 @@
+php55:
+ build: ./docker/php5.5
+ volumes:
+ - .:/app
+ working_dir: /app
+php56:
+ build: ./docker/php5.6
+ volumes:
+ - .:/app
+ working_dir: /app
+php7:
+ build: ./docker/php7
+ volumes:
+ - .:/app
+ working_dir: /app
+hhvm:
+ build: ./docker/hhvm
+ volumes:
+ - .:/app
+ working_dir: /app
diff --git a/docker/hhvm/Dockerfile b/docker/hhvm/Dockerfile
new file mode 100644
index 0000000..1dd57f7
--- /dev/null
+++ b/docker/hhvm/Dockerfile
@@ -0,0 +1,21 @@
+FROM estebanmatias92/hhvm:3.8-cli
+
+# install composer
+RUN curl -sS https://getcomposer.org/installer | php
+RUN mv composer.phar /usr/local/bin/composer
+
+# Install git
+RUN apt-get update
+RUN apt-get install -y ssh-client zip unzip git
+
+# Goto temporary directory.
+WORKDIR /tmp
+
+# Run composer and phpunit installation.
+RUN composer selfupdate && \
+ composer require "phpunit/phpunit:~5.7.27" && \
+ ln -s /tmp/vendor/bin/phpunit /usr/local/bin/phpunit
+
+# Set up the application directory.
+VOLUME ["/app"]
+WORKDIR /app
diff --git a/docker/php5.5/Dockerfile b/docker/php5.5/Dockerfile
new file mode 100644
index 0000000..1d604da
--- /dev/null
+++ b/docker/php5.5/Dockerfile
@@ -0,0 +1,16 @@
+FROM php:5.5-cli
+
+RUN curl -sS https://getcomposer.org/installer | php
+RUN mv composer.phar /usr/local/bin/composer
+
+RUN apt-get update
+RUN apt-get install -y ssh-client zip unzip git
+
+WORKDIR /tmp
+
+RUN composer self-update && \
+ composer require "phpunit/phpunit:~4.8.36" && \
+ ln -s /tmp/vendor/bin/phpunit /usr/local/bin/phpunit
+
+VOLUME ["/app"]
+WORKDIR /app
diff --git a/docker/php5.6/Dockerfile b/docker/php5.6/Dockerfile
new file mode 100644
index 0000000..8305215
--- /dev/null
+++ b/docker/php5.6/Dockerfile
@@ -0,0 +1,21 @@
+FROM php:5.6-cli
+
+# install composer
+RUN curl -sS https://getcomposer.org/installer | php
+RUN mv composer.phar /usr/local/bin/composer
+
+# Install git
+RUN apt-get update
+RUN apt-get install -y ssh-client zip unzip git
+
+# Goto temporary directory.
+WORKDIR /tmp
+
+# Run composer and phpunit installation.
+RUN composer selfupdate && \
+ composer require "phpunit/phpunit:~5.7.27" && \
+ ln -s /tmp/vendor/bin/phpunit /usr/local/bin/phpunit
+
+# Set up the application directory.
+VOLUME ["/app"]
+WORKDIR /app
diff --git a/docker/php7/Dockerfile b/docker/php7/Dockerfile
new file mode 100644
index 0000000..6527ef2
--- /dev/null
+++ b/docker/php7/Dockerfile
@@ -0,0 +1,21 @@
+FROM php:7-cli
+
+# install composer
+RUN curl -sS https://getcomposer.org/installer | php
+RUN mv composer.phar /usr/local/bin/composer
+
+# Install git
+RUN apt-get update
+RUN apt-get install -y ssh-client zip unzip git
+
+# Goto temporary directory.
+WORKDIR /tmp
+
+# Run composer and phpunit installation.
+RUN composer selfupdate && \
+ composer require "phpunit/phpunit:~5.7.27" && \
+ ln -s /tmp/vendor/bin/phpunit /usr/local/bin/phpunit
+
+# Set up the application directory.
+VOLUME ["/app"]
+WORKDIR /app
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index d4a4235..5d108fc 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -4,67 +4,61 @@
use Illuminate\Database\Eloquent\Model;
-class Conversation extends Model
-{
- protected $table = 'conversations';
- public $timestamps = true;
- public $fillable = [
- 'user_one',
- 'user_two',
- 'title',
- 'status',
- ];
+class Conversation extends Model {
+ protected $table = 'conversations';
+ public $timestamps = true;
+ public $fillable = [
+ 'user_one',
+ 'user_two',
+ 'title',
+ 'status',
+ ];
- /*
- * make a relation between message
- *
- * return collection
- * */
- public function tags()
- {
- return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation between message
+ *
+ * return collection
+ * */
+ public function tags() {
+ return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
+ ->with('sender');
+ }
- /*
- * make a relation between message
- *
- * return collection
- * */
- public function messages()
- {
- return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation between message
+ *
+ * return collection
+ * */
+ public function messages() {
+ return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
+ ->with('sender');
+ }
- /*
- * make a relation between first user from conversation
- *
- * return collection
- * */
- public function userone()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one');
- }
+ /*
+ * make a relation between first user from conversation
+ *
+ * return collection
+ * */
+ public function userone() {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one', config('talk.user.ownerKey'));
+ }
- /*
- * make a relation between second user from conversation
- *
- * return collection
- * */
- public function usertwo()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two');
- }
+ /*
+ * make a relation between second user from conversation
+ *
+ * return collection
+ * */
+ public function usertwo() {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two', config('talk.user.ownerKey'));
+ }
- /*
- * adds a tag to this conversation
- *
- * return bool
- * */
- public function addTag(\Nahid\Talk\Tags $tag)
- {
- $this->tags()->attach($tag->id);
- return true;
- }
+ /*
+ * adds a tag to this conversation
+ *
+ * return bool
+ * */
+ public function addTag(\Nahid\Talk\Tags $tag) {
+ $this->tags()->attach($tag->id);
+ return true;
+ }
}
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index cf0aaae..80645b6 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -41,8 +41,22 @@ public function existsById($id)
* */
public function isExistsAmongTwoUsers($user1, $user2)
{
- $conversation = Conversation::where('user_one', $user1)
- ->where('user_two', $user2);
+ $conversation = Conversation::where(
+ function ($query) use ($user1, $user2) {
+ $query->where(
+ function ($q) use ($user1, $user2) {
+ $q->where('user_one', $user1)
+ ->where('user_two', $user2);
+ }
+ )
+ ->orWhere(
+ function ($q) use ($user1, $user2) {
+ $q->where('user_one', $user2)
+ ->where('user_two', $user1);
+ }
+ );
+ }
+ );
if ($conversation->exists()) {
return $conversation->first()->id;
@@ -61,9 +75,11 @@ public function isExistsAmongTwoUsers($user1, $user2)
public function isUserExists($conversationId, $userId)
{
$exists = Conversation::where('id', $conversationId)
- ->where(function ($query) use ($userId) {
- $query->where('user_one', $userId)->orWhere('user_two', $userId);
- })
+ ->where(
+ function ($query) use ($userId) {
+ $query->where('user_one', $userId)->orWhere('user_two', $userId);
+ }
+ )
->exists();
return $exists;
@@ -82,17 +98,25 @@ public function threads($user, $order, $offset, $take)
{
$conv = new Conversation();
$conv->authUser = $user;
- $msgThread = $conv->with(['messages' => function ($q) use ($user) {
- return $q->where(function ($q) use ($user) {
- $q->where('user_id', $user)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($user) {
- $q->where('user_id', '!=', $user);
- $q->where('deleted_from_receiver', 0);
- })
- ->latest();
- },'messages.sender', 'userone', 'usertwo'])
+ $msgThread = $conv->with(
+ [
+ 'messages' => function ($q) use ($user) {
+ return $q->where(
+ function ($q) use ($user) {
+ $q->where('user_id', $user)
+ ->where('deleted_from_sender', 0);
+ }
+ )
+ ->orWhere(
+ function ($q) use ($user) {
+ $q->where('user_id', '!=', $user);
+ $q->where('deleted_from_receiver', 0);
+ }
+ )
+ ->latest();
+ }, 'messages.sender', 'userone', 'usertwo'
+ ]
+ )
->where('user_one', $user)
->orWhere('user_two', $user)
->offset($offset)
@@ -103,7 +127,7 @@ public function threads($user, $order, $offset, $take)
$threads = [];
foreach ($msgThread as $thread) {
- $collection = (object) null;
+ $collection = (object)null;
$conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
$collection->thread = $thread->messages->first();
$collection->withUser = $conversationWith;
@@ -123,9 +147,13 @@ public function threads($user, $order, $offset, $take)
* */
public function threadsAll($user, $offset, $take)
{
- $msgThread = Conversation::with(['messages' => function ($q) use ($user) {
- return $q->latest();
- }, 'userone', 'usertwo'])
+ $msgThread = Conversation::with(
+ [
+ 'messages' => function ($q) use ($user) {
+ return $q->latest();
+ }, 'userone', 'usertwo'
+ ]
+ )
->where('user_one', $user)->orWhere('user_two', $user)->offset($offset)->take($take)->get();
$threads = [];
@@ -151,19 +179,27 @@ public function threadsAll($user, $offset, $take)
* */
public function getMessagesById($conversationId, $userId, $offset, $take)
{
- return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
-
- $query->offset($offset)->take($take);
-
- }])->with(['userone', 'usertwo'])->find($conversationId);
+ return Conversation::with(
+ [
+ 'messages' => function ($query) use ($userId, $offset, $take) {
+ $query->where(
+ function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ }
+ )
+ ->orWhere(
+ function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ }
+ );
+
+ $query->offset($offset)->take($take);
+
+ }
+ ]
+ )->with(['userone', 'usertwo'])->find($conversationId);
}
@@ -177,9 +213,13 @@ public function getMessagesById($conversationId, $userId, $offset, $take)
* */
public function getMessagesAllById($conversationId, $offset, $take)
{
- return $this->with(['messages' => function ($q) use ($offset, $take) {
- return $q->offset($offset)
- ->take($take);
- }, 'userone', 'usertwo'])->find($conversationId);
+ return $this->with(
+ [
+ 'messages' => function ($q) use ($offset, $take) {
+ return $q->offset($offset)
+ ->take($take);
+ }, 'userone', 'usertwo'
+ ]
+ )->find($conversationId);
}
}
diff --git a/src/Embera/Adapter.php b/src/Embera/Adapter.php
new file mode 100644
index 0000000..f464595
--- /dev/null
+++ b/src/Embera/Adapter.php
@@ -0,0 +1,52 @@
+apiUrl = config('talk.oembed.url');
+ parent::__construct($url, $config, $oembed);
+ }
+
+ /**
+ * Validates that the url belongs to this service.
+ * Should be implemented on all children and should
+ * return a boolean (preg_match returns 0 or 1 that
+ * is why I'm also allowing 'int' as a return type).
+ *
+ * The current url is made available via $this->url
+ *
+ * @return bool|int
+ */
+ protected function validateUrl()
+ {
+ $parsedUrl = parse_url($this->apiUrl);
+
+ return preg_match('#' . $parsedUrl['host'] . '#i', (string)$this->url);
+ }
+}
diff --git a/src/Html/HtmlString.php b/src/Html/HtmlString.php
new file mode 100644
index 0000000..734b0f0
--- /dev/null
+++ b/src/Html/HtmlString.php
@@ -0,0 +1,88 @@
+setDriver($driver);
+ $this->setString($string);
+ }
+
+ /**
+ * Get content as a string of HTML.
+ *
+ * @return string
+ */
+ public function toHtml()
+ {
+ if (config('talk.oembed.enabled')) {
+ $parsedUrl = parse_url(config('talk.oembed.url'));
+ $this->driver->addProvider(
+ $parsedUrl['host'],
+ Adapter::class,
+ [
+ 'api_key' => config('talk.oembed.key'),
+ ]
+ );
+ }
+
+ $result = $this->driver->autoEmbed($this->string);
+
+ return $result;
+ }
+
+ /**
+ * @return string
+ */
+ public function getString()
+ {
+ return $this->string;
+ }
+
+ /**
+ * @param string $string
+ */
+ public function setString($string)
+ {
+ $this->string = $string;
+ }
+
+ /**
+ * @return Embera
+ */
+ public function getDriver()
+ {
+ return $this->driver;
+ }
+
+ /**
+ * @param Embera $driver
+ */
+ public function setDriver($driver)
+ {
+ $this->driver = $driver;
+ }
+
+}
diff --git a/src/Html/HtmlStringInterface.php b/src/Html/HtmlStringInterface.php
new file mode 100644
index 0000000..01eb3cb
--- /dev/null
+++ b/src/Html/HtmlStringInterface.php
@@ -0,0 +1,20 @@
+broadcast = $broadcast;
- $toUser = ($this->message['sender']['id'] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
+ $senderIdKey = $this->broadcast->getConfig('user.ownerKey') ? $this->broadcast->getConfig('user.ownerKey') : 'id';
+ $toUser = ($this->message['sender'][ $senderIdKey] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
$channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
$channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index 97e0aa8..7377d30 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -2,64 +2,83 @@
namespace Nahid\Talk\Messages;
+use Embera\Embera;
+use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;
+use Nahid\Talk\Html\HtmlString;
+use Nahid\Talk\Html\HtmlStringInterface;
-class Message extends Model
+class Message extends Model implements HtmlStringInterface
{
- protected $table = 'messages';
+ protected $table = 'messages';
- public $timestamps = true;
+ public $timestamps = true;
+ protected $appends = ['humans_time'];
- public $fillable = [
- 'message',
- 'is_seen',
- 'deleted_from_sender',
- 'deleted_from_receiver',
- 'user_id',
- 'conversation_id',
- ];
+ public $fillable = [
+ 'message',
+ 'is_seen',
+ 'deleted_from_sender',
+ 'deleted_from_receiver',
+ 'user_id',
+ 'conversation_id',
+ ];
- /*
- * make dynamic attribute for human readable time
- *
- * @return string
- * */
- public function getHumansTimeAttribute()
- {
- $date = $this->created_at;
- $now = $date->now();
+ /*
+ * make dynamic attribute for human readable time
+ *
+ * @return string
+ * */
+ public function getHumansTimeAttribute()
+ {
+ $date = $this->created_at;
+ $now = $date->now();
- return $date->diffForHumans($now, true) . ' ago';
- }
+ return $date->diffForHumans($now, true) . ' ago';
+ }
- /*
- * make a relation between conversation model
- *
- * @return collection
- * */
- public function conversation()
- {
- return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
- }
+ /*
+ * make a relation between conversation model
+ *
+ * @return collection
+ * */
+ public function conversation()
+ {
+ return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
+ }
- /*
- * make a relation between user model
- *
- * @return collection
- * */
- public function user()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'));
- }
+ /*
+ * make a relation between user model
+ *
+ * @return collection
+ * */
+ public function user()
+ {
+ return $this->belongsTo(
+ config('talk.user.model', 'App\User'),
+ config('talk.user.foreignKey'),
+ config('talk.user.ownerKey')
+ );
+ }
- /*
- * its an alias of user relation
- *
- * @return collection
- * */
- public function sender()
- {
- return $this->user();
- }
+ /*
+ * its an alias of user relation
+ *
+ * @return collection
+ * */
+ public function sender()
+ {
+ return $this->user();
+ }
+
+ /**
+ * @return Htmlable
+ */
+ public function toHtmlString()
+ {
+ $embera = new Embera(['http' => ['curl' => [CURLOPT_SSL_VERIFYPEER => false]]]);
+
+ return new HtmlString($this->message, $embera);
+ }
}
diff --git a/src/Messages/MessageRepository.php b/src/Messages/MessageRepository.php
index 96924ed..6b24026 100644
--- a/src/Messages/MessageRepository.php
+++ b/src/Messages/MessageRepository.php
@@ -13,12 +13,7 @@ public function takeModel()
public function deleteMessages($conversationId)
{
- $delete = Message::where('conversation_id', $conversationId)->delete();
- if ($delete) {
- return true;
- }
-
- return false;
+ return (boolean) Message::where('conversation_id', $conversationId)->delete();
}
public function softDeleteMessage($messageId, $authUserId)
diff --git a/src/Talk.php b/src/Talk.php
index 68af491..8d0066f 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -140,30 +140,30 @@ protected function makeMessageCollection($conversations)
*/
protected function newConversation($receiverId, $title, $tagName = null)
{
- $conversationId = $this->isConversationExists($receiverId);
- $user = $this->getSerializeUser($this->authUserId, $receiverId);
-
- if ($conversationId === false) {
- $conversation = $this->conversation->create([
- 'user_one' => $user['one'],
- 'user_two' => $user['two'],
- 'title' => $title,
- 'status' => 1,
- ]);
-
- if ($conversation) {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
- }
-
- $conversation->addTag($tag);
+ // $conversationId = $this->isConversationExists($receiverId);
+ $user = $this->getSerializeUser($this->authUserId, $receiverId);
+
+ // if ($conversationId === false) {
+ $conversation = $this->conversation->create([
+ 'user_one' => $user['one'],
+ 'user_two' => $user['two'],
+ 'title' => $title,
+ 'status' => 1,
+ ]);
+
+ if ($conversation) {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
}
- return $conversation->id;
+ $conversation->addTag($tag);
}
+
+ return $conversation->id;
}
+ // }
return $conversationId;
}
@@ -270,6 +270,7 @@ public function sendMessageByUserId($receiverId, $message, $title, $tag = null)
if ($conversationId = $this->isConversationExists($receiverId)) {
$con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
if ($con->title == $title) {
+ // dd("same: {$con->title} == $title");
$message = $this->makeMessage($conversationId, $message);
return $message;
}
diff --git a/tests/Embera/DriverTest.php b/tests/Embera/DriverTest.php
new file mode 100644
index 0000000..461e0ec
--- /dev/null
+++ b/tests/Embera/DriverTest.php
@@ -0,0 +1,85 @@
+ 0,
+ "success" => true,
+ "type" => "rich",
+ "version" => "1.0",
+ "provider_name" => "CodePen",
+ "provider_url" => "https://codepen.io",
+ "title" => "Rotation sphare pure css",
+ "author_name" => "Fabrice Weinberg",
+ "author_url" => "https://codepen.io/FWeinb/",
+ "height" => "300",
+ "width" => "800",
+ "thumbnail_width" => "384",
+ "thumbnail_height" => "225",
+ "thumbnail_url" => "https://s3-us-west-2.amazonaws.com/i.cdpn.io/315.wjzyH.small.61a87aea-b5d6-408a-a6f1-d679e0c32a74.png",
+ "html" => ""
+ ];
+
+
+ /**
+ * @var string
+ */
+ private $expected = "here is the codepen ";
+
+ public function setUp()
+ {
+ parent::setUp();
+ \Config::set('talk.oembed.enabled', true);
+ \Config::set('talk.oembed.url', $this->url);
+ $this->driver = new EmberaMock([], $this->mockResponse);
+ $this->driver->addProvider($this->host, Adapter::class);
+ }
+
+ public function tearDown()
+ {
+ $this->driver = null;
+ parent::tearDown();
+ }
+
+ public function testMessageIsConverted()
+ {
+ $this->assertEquals($this->expected, $this->driver->autoEmbed($this->message));
+ }
+}
diff --git a/tests/Html/HtmlStringTest.php b/tests/Html/HtmlStringTest.php
new file mode 100644
index 0000000..49c9ceb
--- /dev/null
+++ b/tests/Html/HtmlStringTest.php
@@ -0,0 +1,65 @@
+message = "A message send on chat";
+ $driver = $this->getMockBuilder(Embera::class)
+ ->getMock();
+ $driver->method('autoEmbed')
+ ->willReturn($this->message);
+ $this->instance = new HtmlString($this->message, $driver);
+ }
+
+ public function tearDown()
+ {
+ $this->instance = null;
+ parent::tearDown();
+ }
+
+ public function testInstanceExists()
+ {
+ $this->assertNotNull($this->instance);
+ }
+
+ public function testIsHtmlable()
+ {
+ $this->assertInstanceOf(Htmlable::class, $this->instance);
+ }
+
+ public function testHasEmberaDriver()
+ {
+ $this->assertInstanceOf(Embera::class, $this->instance->getDriver());
+ }
+
+ public function testToHtmlIsString()
+ {
+ $this->assertEquals($this->message, $this->instance->toHtml());
+ }
+}
diff --git a/tests/Messages/MessageTest.php b/tests/Messages/MessageTest.php
new file mode 100644
index 0000000..b624bd7
--- /dev/null
+++ b/tests/Messages/MessageTest.php
@@ -0,0 +1,44 @@
+message = new Message(['message' => 'test message']);
+ }
+
+ public function tearDown()
+ {
+ $this->message = null;
+ parent::tearDown();
+ }
+
+ public function testIsHtmlable()
+ {
+ $this->assertInstanceOf(HtmlStringInterface::class, $this->message);
+ }
+
+ public function testToHtmlString()
+ {
+ $this->assertInstanceOf(HtmlString::class, $this->message->toHtmlString());
+ }
+}
diff --git a/tests/Mocks/EmberaMock.php b/tests/Mocks/EmberaMock.php
new file mode 100644
index 0000000..57b9ce8
--- /dev/null
+++ b/tests/Mocks/EmberaMock.php
@@ -0,0 +1,28 @@
+getMock(HttpRequest::class);
+ $mockHttp->method('fetch')
+ ->willReturn(json_encode($response));
+
+ $this->oembed = new \Embera\Oembed($mockHttp);
+ $this->providers = new \Embera\Providers($this->config, $this->oembed);
+ }
+}
From 25b8f92719aca96086860854a3dc6d2046fae9fd Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sun, 14 Oct 2018 22:00:01 +0100
Subject: [PATCH 19/52] bug fixes
---
src/Conversations/Conversation.php | 111 +++--
src/Conversations/ConversationRepository.php | 460 +++++++++----------
src/Facades/Talk.php | 18 +-
src/Live/Broadcast.php | 153 +++---
src/Live/Webcast.php | 84 ++--
src/Messages/Message.php | 30 +-
src/Messages/MessageRepository.php | 70 +--
src/Middleware/TalkMiddleware.php | 28 +-
src/Tags/Tag.php | 34 +-
src/Talk.php | 129 ++++--
src/TalkServiceProvider.php | 150 +++---
src/helpers/talk.php | 26 +-
12 files changed, 674 insertions(+), 619 deletions(-)
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index 7b7a188..bea2953 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -6,66 +6,63 @@
class Conversation extends Model
{
- protected $table = 'conversations';
- public $timestamps = true;
+ protected $table = 'conversations';
+ public $timestamps = true;
+ public $fillable = [
+ 'user_one',
+ 'user_two',
+ 'title',
+ 'status',
+ ];
- public $fillable = [
- 'user_one',
- 'user_two',
- 'title',
- 'status',
- ];
+ /*
+ * make a relation with tags
+ *
+ * return relationship
+ * */
+ public function tags()
+ {
+ return $this->belongsToMany('Nahid\Talk\Tags\Tag');
+ }
- /*
- * make a relation with tags
- *
- * return relationship
- * */
- public function tags()
- {
- return $this->belongsToMany('Nahid\Talk\Tags\Tag');
- }
+ /*
+ * make a relation between message
+ *
+ * return collection
+ * */
+ public function messages()
+ {
+ return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
+ ->with('sender');
+ }
- /*
- * make a relation between message
- *
- * return collection
- * */
- public function messages()
- {
- return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation between first user from conversation
+ *
+ * return collection
+ * */
+ public function userone()
+ {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one', config('talk.user.ownerKey'));
+ }
- /*
- * make a relation between first user from conversation
- *
- * return collection
- * */
- public function userone()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one');
- }
+ /*
+ * make a relation between second user from conversation
+ *
+ * return collection
+ * */
+ public function usertwo()
+ {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two', config('talk.user.ownerKey'));
+ }
- /*
- * make a relation between second user from conversation
- *
- * return collection
- * */
- public function usertwo()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two');
- }
-
- /*
- * adds a tag to this conversation
- *
- * return bool
- * */
- public function addTag(\Nahid\Talk\Tags\Tag $tag)
- {
- // var_dump($this);
- $this->tags()->attach($tag->id);
- return true;
- }
+ /*
+ * adds a tag to this conversation
+ *
+ * return void
+ * */
+ public function addTag(\Nahid\Talk\Tags\Tag $tag)
+ {
+ return $this->tags()->attach($tag->id);
+ }
}
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 87269b1..125502a 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -6,234 +6,234 @@
class ConversationRepository extends Repository
{
- /*
- * this method is default method for repository package
- *
- * @return \Nahid\Talk\Conersations\Conversation
- * */
- public function takeModel()
- {
- return Conversation::class;
- }
-
- /*
- * check this given user is exists
- *
- * @param int $id
- * @return bool
- * */
- public function existsById($id)
- {
- $conversation = $this->find($id);
- if ($conversation) {
- return true;
- }
-
- return false;
- }
-
- /*
- * check this given two users is already make a conversation
- *
- * @param int $user1
- * @param int $user2
- * @return int|bool
- * */
- public function isExistsAmongTwoUsers($user1, $user2)
- {
- $conversation = Conversation::where('user_one', $user1)
- ->where('user_two', $user2);
-
- if ($conversation->exists()) {
- return $conversation->first()->id;
- }
-
- return false;
- }
-
- /*
- * check this given user is involved with this given $conversation
- *
- * @param int $conversationId
- * @param int $userId
- * @return bool
- * */
- public function isUserExists($conversationId, $userId)
- {
- $exists = Conversation::where('id', $conversationId)
- ->where(function ($query) use ($userId) {
- $query->where('user_one', $userId)->orWhere('user_two', $userId);
- })
- ->exists();
-
- return $exists;
- }
-
- /*
- * retrieve all message thread without soft deleted message with latest one message and
- * sender and receiver user model
- *
- * @param int $user_id
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threads($user_id, $order, $offset, $take)
- {
- $conv = new Conversation();
- $conv->authUser = $user_id;
- $conversations = $conv->with(['messages' => function ($q) use ($user_id) {
- return $q->where(function ($q) use ($user_id) {
- $q->where('user_id', $user_id)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($user_id) {
- $q->where('user_id', '!=', $user_id);
- $q->where('deleted_from_receiver', 0);
- })
- ->latest();
- }, 'messages.sender', 'userone', 'usertwo'])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->offset($offset)
- ->take($take)
- ->orderBy('updated_at', $order)
- ->get();
-
- $threads = [];
-
- foreach ($conversations as $conversation) {
- $collection = (object) null;
- $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
- $collection->thread = $conversation->messages->first();
- $collection->conversation = $conversation;
- $collection->messages = $conversation->messages;
- $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
- return $query
- ->where('user_id', '!=', $user_id)
- ->where('is_read', '=', '0');
- })->get();
- // dump($conversation->id);
- // dump($collection->unreadmessages);
- $collection->withUser = $conversationWith;
- $threads[] = $collection;
- }
-
- return collect($threads);
- }
-
- /*
- * retrieve all message thread with latest one message and sender and receiver user model
- *
- * @param int $user_id
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threadsAll($user_id, $offset, $take)
- {
- $msgThread = Conversation::with(['messages' => function ($q) use ($user_id) {
- return $q->latest();
- }, 'userone', 'usertwo'])
- ->where('user_one', $user_id)->orWhere('user_two', $user_id)->offset($offset)->take($take)->get();
-
- $threads = [];
-
- foreach ($msgThread as $thread) {
- $conversationWith = ($thread->userone->id == $user_id) ? $thread->usertwo : $thread->userone;
- $message = $thread->messages->first();
- $message->user = $conversationWith;
- $threads[] = $message;
- }
-
- return collect($threads);
- }
-
- /*
- * get all conversations by given conversation id
- *
- * @param int $conversationId
- * @param int $userId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesById($conversationId, $userId, $offset, $take)
- {
- return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
-
- $query->offset($offset)->take($take);
-
- }])->with(['userone', 'usertwo'])->find($conversationId);
- }
-
- /*
- * get all conversations by given tag id
- *
- * @param int $conversationId
- * @param int $userId
- * @return collection
- * * /
- public function getMessagesByTagId($conversationId, $userId)
- {
- return Conversation::with(['messages' => function ($query) use ($userId) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
-
- $query->offset($offset)->take($take);
-
- }])->with(['userone', 'usertwo', 'tags'])->find($conversationId);
- }
- */
-
- /*
- * get all conversations by given tag id
- *
- * @param int $conversationId
- * @param int $userId
- * @return collection
- * */
- public function getMessagesByTagId($tagId, $userId)
- {
- return Conversation::with(['messages' => function ($query) use ($userId) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
- }])->with(['userone', 'usertwo', 'tags'])->get();
- }
-
- /*
- * get all conversations with soft deleted message by given conversation id
- *
- * @param int $conversationId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesAllById($conversationId, $offset, $take)
- {
- return $this->with(['messages' => function ($q) use ($offset, $take) {
- return $q->offset($offset)
- ->take($take);
- }, 'userone', 'usertwo'])->find($conversationId);
- }
+ /*
+ * this method is default method for repository package
+ *
+ * @return \Nahid\Talk\Conersations\Conversation
+ * */
+ public function takeModel()
+ {
+ return Conversation::class;
+ }
+
+ /*
+ * check this given user is exists
+ *
+ * @param int $id
+ * @return bool
+ * */
+ public function existsById($id)
+ {
+ $conversation = $this->find($id);
+ if ($conversation) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given two users is already make a conversation
+ *
+ * @param int $user1
+ * @param int $user2
+ * @return int|bool
+ * */
+ public function isExistsAmongTwoUsers($user1, $user2)
+ {
+ $conversation = Conversation::where('user_one', $user1)
+ ->where('user_two', $user2);
+
+ if ($conversation->exists()) {
+ return $conversation->first()->id;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given user is involved with this given $conversation
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return bool
+ * */
+ public function isUserExists($conversationId, $userId)
+ {
+ $exists = Conversation::where('id', $conversationId)
+ ->where(function ($query) use ($userId) {
+ $query->where('user_one', $userId)->orWhere('user_two', $userId);
+ })
+ ->exists();
+
+ return $exists;
+ }
+
+ /*
+ * retrieve all message thread without soft deleted message with latest one message and
+ * sender and receiver user model
+ *
+ * @param int $user_id
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threads($user_id, $order, $offset, $take)
+ {
+ $conv = new Conversation();
+ $conv->authUser = $user_id;
+ $conversations = $conv->with(['messages' => function ($q) use ($user_id) {
+ return $q->where(function ($q) use ($user_id) {
+ $q->where('user_id', $user_id)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($user_id) {
+ $q->where('user_id', '!=', $user_id);
+ $q->where('deleted_from_receiver', 0);
+ })
+ ->latest();
+ }, 'messages.sender', 'userone', 'usertwo'])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->offset($offset)
+ ->take($take)
+ ->orderBy('updated_at', $order)
+ ->get();
+
+ $threads = [];
+
+ foreach ($conversations as $conversation) {
+ $collection = (object) null;
+ $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
+ $collection->thread = $conversation->messages->first();
+ $collection->conversation = $conversation;
+ $collection->messages = $conversation->messages;
+ $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
+ return $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0');
+ })->get();
+ // dump($conversation->id);
+ // dump($collection->unreadmessages);
+ $collection->withUser = $conversationWith;
+ $threads[] = $collection;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * retrieve all message thread with latest one message and sender and receiver user model
+ *
+ * @param int $user_id
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threadsAll($user_id, $offset, $take)
+ {
+ $msgThread = Conversation::with(['messages' => function ($q) use ($user_id) {
+ return $q->latest();
+ }, 'userone', 'usertwo'])
+ ->where('user_one', $user_id)->orWhere('user_two', $user_id)->offset($offset)->take($take)->get();
+
+ $threads = [];
+
+ foreach ($msgThread as $thread) {
+ $conversationWith = ($thread->userone->id == $user_id) ? $thread->usertwo : $thread->userone;
+ $message = $thread->messages->first();
+ $message->user = $conversationWith;
+ $threads[] = $message;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * get all conversations by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesById($conversationId, $userId, $offset, $take)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+
+ $query->offset($offset)->take($take);
+
+ }])->with(['userone', 'usertwo'])->find($conversationId);
+ }
+
+ /*
+ * get all conversations by given tag id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return collection
+ * * /
+ public function getMessagesByTagId($conversationId, $userId)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+
+ $query->offset($offset)->take($take);
+
+ }])->with(['userone', 'usertwo', 'tags'])->find($conversationId);
+ }
+ */
+
+ /*
+ * get all conversations by given tag id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return collection
+ * */
+ public function getMessagesByTagId($tagId, $userId)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+ }])->with(['userone', 'usertwo', 'tags'])->get();
+ }
+
+ /*
+ * get all conversations with soft deleted message by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesAllById($conversationId, $offset, $take)
+ {
+ return $this->with(['messages' => function ($q) use ($offset, $take) {
+ return $q->offset($offset)
+ ->take($take);
+ }, 'userone', 'usertwo'])->find($conversationId);
+ }
}
diff --git a/src/Facades/Talk.php b/src/Facades/Talk.php
index 86c531f..ec1394c 100644
--- a/src/Facades/Talk.php
+++ b/src/Facades/Talk.php
@@ -6,13 +6,13 @@
class Talk extends Facade
{
- /**
- * Get the registered name of the component.
- *
- * @return string
- */
- protected static function getFacadeAccessor()
- {
- return 'talk';
- }
+ /**
+ * Get the registered name of the component.
+ *
+ * @return string
+ */
+ protected static function getFacadeAccessor()
+ {
+ return 'talk';
+ }
}
diff --git a/src/Live/Broadcast.php b/src/Live/Broadcast.php
index a786cdd..9ef8b32 100644
--- a/src/Live/Broadcast.php
+++ b/src/Live/Broadcast.php
@@ -2,98 +2,97 @@
namespace Nahid\Talk\Live;
-use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Config\Repository;
+use Illuminate\Foundation\Bus\DispatchesJobs;
use Nahid\Talk\Messages\Message;
use Pusher\Pusher;
class Broadcast
{
- use DispatchesJobs;
-
- /*
- * Constant for talk config prefix
- *
- * @const string
- * */
- const CONFIG_PATH = 'talk';
+ use DispatchesJobs;
- /*
- * Set all configs from talk configurations
- *
- * @var array
- * */
- protected $config;
+ /*
+ * Constant for talk config prefix
+ *
+ * @const string
+ * */
+ const CONFIG_PATH = 'talk';
+ /*
+ * Set all configs from talk configurations
+ *
+ * @var array
+ * */
+ protected $config;
- /*
- * Pusher instance
- *
- * @var object
- * */
- public $pusher;
+ /*
+ * Pusher instance
+ *
+ * @var object
+ * */
+ public $pusher;
- /**
- * Connect pusher and get all credentials from config.
- *
- * @param \Illuminate\Contracts\Config\Repository $config
- */
- public function __construct(Repository $config)
- {
- $this->config = $config;
- $this->pusher = $this->connectPusher();
- }
+ /**
+ * Connect pusher and get all credentials from config.
+ *
+ * @param \Illuminate\Contracts\Config\Repository $config
+ */
+ public function __construct(Repository $config)
+ {
+ $this->config = $config;
+ $this->pusher = $this->connectPusher();
+ }
- /**
- * Make pusher connection.
- *
- * @param array $options
- *
- * @return object | bool
- */
- protected function connectPusher($options = [])
- {
- if ($this->getConfig('broadcast.enable')) {
- $appId = $this->getConfig('broadcast.pusher.app_id');
- $appKey = $this->getConfig('broadcast.pusher.app_key');
- $appSecret = $this->getConfig('broadcast.pusher.app_secret');
- $appOptions = $this->getConfig('broadcast.pusher.options');
+ /**
+ * Make pusher connection.
+ *
+ * @param array $options
+ *
+ * @return object | bool
+ */
+ protected function connectPusher($options = [])
+ {
+ if ($this->getConfig('broadcast.enable')) {
+ $appId = $this->getConfig('broadcast.pusher.app_id');
+ $appKey = $this->getConfig('broadcast.pusher.app_key');
+ $appSecret = $this->getConfig('broadcast.pusher.app_secret');
+ $appOptions = $this->getConfig('broadcast.pusher.options');
- $newOptions = array_merge($appOptions, $options);
- $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
+ $newOptions = array_merge($appOptions, $options);
+ $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
- return $pusher;
- }
+ return $pusher;
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Dispatch the job to the queue.
- *
- * @param \Nahid\Talk\Messages\Message $message
- */
- public function transmission(Message $message)
- {
- if (!$this->pusher) {
- return false;
- }
+ /**
+ * Dispatch the job to the queue.
+ *
+ * @param \Nahid\Talk\Messages\Message $message
+ */
+ public function transmission(Message $message)
+ {
+ if (!$this->pusher) {
+ return false;
+ }
- $sender = $message->sender->toArray();
- $messageArray = $message->toArray();
- $messageArray['sender'] = $sender;
- $this->dispatch(new Webcast($messageArray));
- }
+ $sender = $message->sender->toArray();
+ $messageArray = $message->toArray();
+ $messageArray['sender'] = $sender;
+ $this->dispatch(new Webcast($messageArray));
+ }
- /**
- * get specific config from talk configurations.
- *
- * @param string
- *
- * @return string|array|int
- */
- public function getConfig($name)
- {
- return $this->config->get(self::CONFIG_PATH.'.'.$name);
- }
+ /**
+ * get specific config from talk configurations.
+ *
+ * @param string
+ *
+ * @return string|array|int
+ */
+ public function getConfig($name)
+ {
+ return $this->config->get(self::CONFIG_PATH . '.' . $name);
+ }
}
diff --git a/src/Live/Webcast.php b/src/Live/Webcast.php
index 33e0fe6..fda899b 100644
--- a/src/Live/Webcast.php
+++ b/src/Live/Webcast.php
@@ -3,50 +3,50 @@
namespace Nahid\Talk\Live;
use Illuminate\Bus\Queueable;
-use Illuminate\Queue\SerializesModels;
-use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
class Webcast implements ShouldQueue
{
- use InteractsWithQueue, Queueable, SerializesModels;
-
- /*
- * Message Model Instance
- *
- * @var object
- * */
- protected $message;
-
- /*
- * Broadcast class instance
- *
- * @var object
- * */
- protected $broadcast;
-
- /**
- * Set message collections to the properties.
- */
- public function __construct($message)
- {
- $this->message = $message;
- }
-
- /*
- * Execute the job and broadcast to the pusher channels
- *
- * @param \Nahid\Talk\Live\Broadcast $broadcast
- * @return void
- */
- public function handle(Broadcast $broadcast)
- {
- $this->broadcast = $broadcast;
- $toUser = ($this->message['sender']['id'] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
-
- $channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
- $channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];
-
- $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
- }
+ use InteractsWithQueue, Queueable, SerializesModels;
+
+ /*
+ * Message Model Instance
+ *
+ * @var object
+ * */
+ protected $message;
+
+ /*
+ * Broadcast class instance
+ *
+ * @var object
+ * */
+ protected $broadcast;
+
+ /**
+ * Set message collections to the properties.
+ */
+ public function __construct($message)
+ {
+ $this->message = $message;
+ }
+
+ /*
+ * Execute the job and broadcast to the pusher channels
+ *
+ * @param \Nahid\Talk\Live\Broadcast $broadcast
+ * @return void
+ */
+ public function handle(Broadcast $broadcast)
+ {
+ $this->broadcast = $broadcast;
+ $toUser = ($this->message['sender']['id'] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
+
+ $channelForUser = $this->broadcast->getConfig('broadcast.app_name') . '-user-' . $toUser;
+ $channelForConversation = $this->broadcast->getConfig('broadcast.app_name') . '-conversation-' . $this->message['conversation_id'];
+
+ $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
+ }
}
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index 562dc03..b8214a2 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -4,7 +4,8 @@
use Illuminate\Database\Eloquent\Model;
-class Message extends Model {
+class Message extends Model
+{
protected $table = 'messages';
public $timestamps = true;
@@ -24,7 +25,22 @@ class Message extends Model {
*
* @return string
* */
- public function getHumansTimeAttribute() {
+ public function getHumansTimeAttribute()
+ {
+ //laravel sometimes has $this=null but attributes proprty works perfectly well
+ $date = \Carbon\Carbon::parse($this->attributes['created_at']);
+ $now = $date->now();
+
+ return $date->diffForHumans($now, true);
+ }
+
+ /*
+ * make dynamic attribute for human readable time - with more naturalisic time modifiers
+ *
+ * @return string
+ * */
+ public function getNaturalHumansTimeAttribute()
+ {
//laravel sometimes has $this=null but attributes proprty works perfectly well
$date = \Carbon\Carbon::parse($this->attributes['created_at']);
$now = $date->now();
@@ -38,7 +54,6 @@ public function getHumansTimeAttribute() {
}
return $date->format("M j, Y");
- // return $date->diffForHumans(null, true, true) . ' ago';
}
/*
@@ -46,7 +61,8 @@ public function getHumansTimeAttribute() {
*
* @return collection
* */
- public function conversation() {
+ public function conversation()
+ {
return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
}
@@ -55,7 +71,8 @@ public function conversation() {
*
* @return collection
* */
- public function user() {
+ public function user()
+ {
return $this->belongsTo(config('talk.user.model', 'App\User'));
}
@@ -64,7 +81,8 @@ public function user() {
*
* @return collection
* */
- public function sender() {
+ public function sender()
+ {
return $this->user();
}
}
diff --git a/src/Messages/MessageRepository.php b/src/Messages/MessageRepository.php
index 96924ed..bd5bad5 100644
--- a/src/Messages/MessageRepository.php
+++ b/src/Messages/MessageRepository.php
@@ -6,39 +6,39 @@
class MessageRepository extends Repository
{
- public function takeModel()
- {
- return Message::class;
- }
-
- public function deleteMessages($conversationId)
- {
- $delete = Message::where('conversation_id', $conversationId)->delete();
- if ($delete) {
- return true;
- }
-
- return false;
- }
-
- public function softDeleteMessage($messageId, $authUserId)
- {
- $message = $this->with(['conversation' => function ($q) use ($authUserId) {
- $q->where('user_one', $authUserId);
- $q->orWhere('user_two', $authUserId);
- }])->find($messageId);
-
- if (is_null($message->conversation)) {
- return false;
- }
-
- if ($message->user_id == $authUserId) {
- $message->deleted_from_sender = 1;
- } else {
- $message->deleted_from_receiver = 1;
- }
-
- return (boolean) $this->update($message);
-
- }
+ public function takeModel()
+ {
+ return Message::class;
+ }
+
+ public function deleteMessages($conversationId)
+ {
+ $delete = Message::where('conversation_id', $conversationId)->delete();
+ if ($delete) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public function softDeleteMessage($messageId, $authUserId)
+ {
+ $message = $this->with(['conversation' => function ($q) use ($authUserId) {
+ $q->where('user_one', $authUserId);
+ $q->orWhere('user_two', $authUserId);
+ }])->find($messageId);
+
+ if (is_null($message->conversation)) {
+ return false;
+ }
+
+ if ($message->user_id == $authUserId) {
+ $message->deleted_from_sender = 1;
+ } else {
+ $message->deleted_from_receiver = 1;
+ }
+
+ return (boolean) $this->update($message);
+
+ }
}
diff --git a/src/Middleware/TalkMiddleware.php b/src/Middleware/TalkMiddleware.php
index 3fbc8da..f620e8e 100644
--- a/src/Middleware/TalkMiddleware.php
+++ b/src/Middleware/TalkMiddleware.php
@@ -8,20 +8,20 @@
class TalkMiddleware
{
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Illuminate\Http\Request $request
+ * @param \Closure $next
+ * @return mixed
+ */
- public function handle($request, Closure $next, $guard = null)
- {
- if (Auth::guard($guard)->check()) {
- Talk::setAuthUserId(Auth::guard($guard)->user()->id);
- }
+ public function handle($request, Closure $next, $guard = null)
+ {
+ if (Auth::guard($guard)->check()) {
+ Talk::setAuthUserId(Auth::guard($guard)->user()->id);
+ }
- return $next($request);
- }
+ return $next($request);
+ }
}
diff --git a/src/Tags/Tag.php b/src/Tags/Tag.php
index b614769..d166772 100644
--- a/src/Tags/Tag.php
+++ b/src/Tags/Tag.php
@@ -6,22 +6,22 @@
class Tag extends Model
{
- protected $table = 'tags';
- public $timestamps = true;
- public $fillable = [
- 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
- 'name',
- ];
+ protected $table = 'tags';
+ public $timestamps = true;
+ public $fillable = [
+ 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
+ 'name',
+ ];
- /*
- * make a relation with conversations
- *
- * returns collection
- *
- * */
- public function conversations()
- {
- return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation with conversations
+ *
+ * returns collection
+ *
+ * */
+ public function conversations()
+ {
+ return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
+ ->with('sender');
+ }
}
diff --git a/src/Talk.php b/src/Talk.php
index cc2cbb4..a35d855 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -16,7 +16,8 @@
use Nahid\Talk\Live\Broadcast;
use Nahid\Talk\Messages\MessageRepository;
-class Talk {
+class Talk
+{
/**
* Now users can attach special importance to conversations by star-ing them.
@@ -72,7 +73,8 @@ class Talk {
* @param \Nahid\Talk\Conversations\ConversationRepository $conversation
* @param \Nahid\Talk\Messages\MessageRepository $message
*/
- public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message) {
+ public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
+ {
// dump('calling meeeeeeeeee');
$this->config = $config;
$this->conversation = $conversation;
@@ -88,7 +90,8 @@ public function __construct(Repository $config, Broadcast $broadcast, Conversati
*
* @return array
*/
- protected function getSerializeUser($user1, $user2) {
+ protected function getSerializeUser($user1, $user2)
+ {
$user = [];
$user['one'] = ($user1 < $user2) ? $user1 : $user2;
$user['two'] = ($user1 < $user2) ? $user2 : $user1;
@@ -104,7 +107,8 @@ protected function getSerializeUser($user1, $user2) {
*
* @return \Nahid\Talk\Messages\Message
*/
- protected function makeMessage($conversationId, $message) {
+ protected function makeMessage($conversationId, $message)
+ {
$message = $this->message->create([
'message' => $message,
'conversation_id' => $conversationId,
@@ -124,7 +128,8 @@ protected function makeMessage($conversationId, $message) {
*@param \Talk\Conversations\Conversation $conversations
*@return object|bool
*/
- protected function makeMessageCollection($conversations) {
+ protected function makeMessageCollection($conversations)
+ {
if (!$conversations) {
return false;
}
@@ -157,7 +162,8 @@ protected function makeMessageCollection($conversations) {
*
* @return int
*/
- protected function newConversation($receiverId, $title, $tagName = null) {
+ protected function newConversation($receiverId, $title, $tagName = null)
+ {
// $conversationId = $this->isConversationExists($receiverId);
$user = $this->getSerializeUser($this->authUserId, $receiverId);
@@ -193,7 +199,8 @@ protected function newConversation($receiverId, $title, $tagName = null) {
*
* @return int|bool
*/
- public function setAuthUserId($id = null) {
+ public function setAuthUserId($id = null)
+ {
if (!is_null($id)) {
return $this->authUserId = $id;
}
@@ -209,7 +216,8 @@ public function setAuthUserId($id = null) {
* @param int $id
* @return \Nahid\Talk\Talk|bool
* */
- public function user($id = null) {
+ public function user($id = null)
+ {
if ($this->setAuthUserId($id)) {
return $this;
}
@@ -224,7 +232,8 @@ public function user($id = null) {
*
* @return bool|int
*/
- public function isConversationExists($userId) {
+ public function isConversationExists($userId)
+ {
if (empty($userId)) {
return false;
}
@@ -242,7 +251,8 @@ public function isConversationExists($userId) {
*
* @return bool
*/
- public function isAuthenticUser($conversationId, $userId) {
+ public function isAuthenticUser($conversationId, $userId)
+ {
if ($conversationId && $userId) {
return $this->conversation->isUserExists($conversationId, $userId);
}
@@ -258,7 +268,8 @@ public function isAuthenticUser($conversationId, $userId) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function sendMessage($conversatonId, $message) {
+ public function sendMessage($conversatonId, $message)
+ {
if ($conversatonId && $message) {
if ($this->conversation->existsById($conversatonId)) {
$message = $this->makeMessage($conversatonId, $message);
@@ -278,7 +289,8 @@ public function sendMessage($conversatonId, $message) {
*
* @return \Nahid\Talk\Messages\Message
*/
- public function sendMessageByUserId($receiverId, $message, $title = null) {
+ public function sendMessageByUserId($receiverId, $message, $title = null)
+ {
if ($conversationId = $this->isConversationExists($receiverId)) {
$con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
if ($con->title == $title) {
@@ -302,7 +314,8 @@ public function sendMessageByUserId($receiverId, $message, $title = null) {
*
* @return array
*/
- public function getInbox($order = 'desc', $offset = 0, $take = 20) {
+ public function getInbox($order = 'desc', $offset = 0, $take = 20)
+ {
// dump($this->authUserId);
return $this->conversation->threads($this->authUserId, $order, $offset, $take);
}
@@ -315,7 +328,8 @@ public function getInbox($order = 'desc', $offset = 0, $take = 20) {
*
* @return array
*/
- public function getInboxAll($order = 'desc', $offset = 0, $take = 20) {
+ public function getInboxAll($order = 'desc', $offset = 0, $take = 20)
+ {
return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
}
@@ -327,7 +341,8 @@ public function getInboxAll($order = 'desc', $offset = 0, $take = 20) {
*
* @return array
*/
- public function threads($order = 'desc', $offset = 0, $take = 20) {
+ public function threads($order = 'desc', $offset = 0, $take = 20)
+ {
return $this->getInbox($order, $offset, $take);
}
@@ -339,7 +354,8 @@ public function threads($order = 'desc', $offset = 0, $take = 20) {
*
* @return array
*/
- public function threadsAll($order = 'desc', $offset = 0, $take = 20) {
+ public function threadsAll($order = 'desc', $offset = 0, $take = 20)
+ {
return $this->getInboxAll($order, $offset, $take);
}
@@ -352,7 +368,8 @@ public function threadsAll($order = 'desc', $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message
*/
- public function getConversationsById($conversationId, $offset = 0, $take = 20) {
+ public function getConversationsById($conversationId, $offset = 0, $take = 20)
+ {
// dump($conversationId);
// dd($this->authUserId);
$conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
@@ -366,7 +383,8 @@ public function getConversationsById($conversationId, $offset = 0, $take = 20) {
*
* @return collection
*/
- public function getConversationsByTagId($tag_id) {
+ public function getConversationsByTagId($tag_id)
+ {
// $threads = $this->conversation->threads($this->authUserId, 'id', 6, 6);
$conversations_ = $this->conversation->getMessagesByTagId($tag_id, $this->authUserId);
$user_id = $this->authUserId;
@@ -407,7 +425,8 @@ public function getConversationsByTagId($tag_id) {
*
* @return \Nahid\Talk\Messages\Message
*/
- public function getConversationsAllById($conversationId, $offset = 0, $take = 20) {
+ public function getConversationsAllById($conversationId, $offset = 0, $take = 20)
+ {
$conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
return $this->makeMessageCollection($conversations);
@@ -422,7 +441,8 @@ public function getConversationsAllById($conversationId, $offset = 0, $take = 20
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getConversationsByUserId($senderId, $offset = 0, $take = 20) {
+ public function getConversationsByUserId($senderId, $offset = 0, $take = 20)
+ {
$conversationId = $this->isConversationExists($senderId, $this->authUserId);
if ($conversationId) {
return $this->getConversationsById($conversationId, $offset, $take);
@@ -440,7 +460,8 @@ public function getConversationsByUserId($senderId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20) {
+ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
+ {
$conversationId = $this->isConversationExists($senderId, $this->authUserId);
if ($conversationId) {
return $this->getConversationsAllById($conversationId, $offset, $take);
@@ -454,7 +475,8 @@ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
*
* @return collection
*/
- public function getUserTags() {
+ public function getUserTags()
+ {
return Tags\Tag::where(['user_id' => $this->authUserId])
->where('name', '!=', Talk::STARTAG)
->get();
@@ -467,7 +489,8 @@ public function getUserTags() {
*
* @return bool
*/
- public function createTagForUser($tagName) {
+ public function createTagForUser($tagName)
+ {
if (!empty($tagName)) {
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
if (is_null($tag)) {
@@ -490,7 +513,8 @@ public function createTagForUser($tagName) {
*
* @return bool
*/
- public function addTagToConversation($conversationId, $tagName, bool $specialTagOnlyOne = false) {
+ public function addTagToConversation($conversationId, $tagName, bool $specialTagOnlyOne = false)
+ {
if (!empty($tagName)) {
//treat star tag specially
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
@@ -504,9 +528,9 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
//special tags dn't have owners
if ($tagName == \Nahid\Talk\Talk::STARTAG || $specialTagOnlyOne) {
$tag = Tags\Tag::create([
- 'name' => $tagName,
+ 'name' => $tagName,
'is_special_tag' => 1,
- ]);
+ ]);
} else {
$tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
}
@@ -531,7 +555,8 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
*
* @return bool
*/
- public function removeTagFromConversation($conversationId, $tagId) {
+ public function removeTagFromConversation($conversationId, $tagId)
+ {
if (!empty($conversationId) && !empty($tagId)) {
//confirm user owns this tag
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'id' => $tagId])->firstOrFail();
@@ -556,7 +581,8 @@ public function removeTagFromConversation($conversationId, $tagId) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessages($conversationId, $offset = 0, $take = 20) {
+ public function getMessages($conversationId, $offset = 0, $take = 20)
+ {
return $this->getConversationsById($conversationId, $offset, $take);
}
@@ -567,7 +593,8 @@ public function getMessages($conversationId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessagesAll($conversationId, $offset = 0, $take = 20) {
+ public function getMessagesAll($conversationId, $offset = 0, $take = 20)
+ {
return $this->getConversationsAllById($conversationId, $offset, $take);
}
@@ -578,7 +605,8 @@ public function getMessagesAll($conversationId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessagesByUserId($userId, $offset = 0, $take = 20) {
+ public function getMessagesByUserId($userId, $offset = 0, $take = 20)
+ {
return $this->getConversationsByUserId($userId, $offset, $take);
}
@@ -589,7 +617,8 @@ public function getMessagesByUserId($userId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessagesAllByUserId($userId, $offset = 0, $take = 20) {
+ public function getMessagesAllByUserId($userId, $offset = 0, $take = 20)
+ {
return $this->getConversationsAllByUserId($userId, $offset, $take);
}
@@ -600,7 +629,8 @@ public function getMessagesAllByUserId($userId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function readMessage($messageId = null) {
+ public function readMessage($messageId = null)
+ {
if (!is_null($messageId)) {
$message = $this->message->with(['sender', 'conversation'])->find($messageId);
@@ -619,7 +649,8 @@ public function readMessage($messageId = null) {
*
* @return bool
*/
- public function makeSeen($messageId) {
+ public function makeSeen($messageId)
+ {
$seen = $this->message->update($messageId, ['is_seen' => 1]);
if ($seen) {
return true;
@@ -634,7 +665,8 @@ public function makeSeen($messageId) {
*
* @return bool
*/
- public function markRead($messageId) {
+ public function markRead($messageId)
+ {
if (!is_null($messageId)) {
$message = $this->message->with(['sender', 'conversation'])->find($messageId);
if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
@@ -661,7 +693,8 @@ public function markRead($messageId) {
*
* @return mixed
*/
- public function getUnreadMessagesInConversation($conversationId) {
+ public function getUnreadMessagesInConversation($conversationId)
+ {
if (!is_null($conversationId)) {
$message = $this->conversation->with(['messages'])->find($conversationId);
if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
@@ -689,7 +722,8 @@ public function getUnreadMessagesInConversation($conversationId) {
*
* @return collection
*/
- public function getAllUnreadMessages($removeSpecialMessages = false) {
+ public function getAllUnreadMessages($removeSpecialMessages = false)
+ {
$messages = collect();
$user_id = $this->authUserId;
$conv = new \Nahid\Talk\Conversations\Conversation();
@@ -708,7 +742,7 @@ public function getAllUnreadMessages($removeSpecialMessages = false) {
if ($removeSpecialMessages) {
$conversations = $conversations->filter(function ($conversation) {
- $tags = $conversation->tags;
+ $tags = $conversation->tags;
$specialTags = $conversation->tags()
->where('is_special_tag', '=', '1')
->get();
@@ -730,7 +764,8 @@ public function getAllUnreadMessages($removeSpecialMessages = false) {
*
* @return collection
*/
- public function getLatestMessages() {
+ public function getLatestMessages()
+ {
if ($this->latestMessages == null) {
$messages = collect();
@@ -762,7 +797,8 @@ public function getLatestMessages() {
*
* @return int
*/
- public function getUnreadMessagesCount($removeSpecialMessages = false) {
+ public function getUnreadMessagesCount($removeSpecialMessages = false)
+ {
return $this->getAllUnreadMessages($removeSpecialMessages)->count();
}
@@ -775,7 +811,8 @@ public function getUnreadMessagesCount($removeSpecialMessages = false) {
*
* @deprecated since version 2.0.0 Remove it from version 2.0.2
*/
- public function getReceiverInfo($conversationId) {
+ public function getReceiverInfo($conversationId)
+ {
$conversation = $this->conversation->find($conversationId);
$receiver = '';
if ($conversation->user_one == $this->authUserId) {
@@ -797,7 +834,8 @@ public function getReceiverInfo($conversationId) {
*
* @return bool
*/
- public function deleteMessage($messageId) {
+ public function deleteMessage($messageId)
+ {
return $this->message->softDeleteMessage($messageId, $this->authUserId);
}
@@ -808,7 +846,8 @@ public function deleteMessage($messageId) {
*
* @return bool
*/
- public function deleteForever($messageId) {
+ public function deleteForever($messageId)
+ {
$deleteMessage = $this->message->delete($messageId);
if ($deleteMessage) {
return true;
@@ -824,7 +863,8 @@ public function deleteForever($messageId) {
*
* @return bool
*/
- public function deleteConversations($id) {
+ public function deleteConversations($id)
+ {
$deleteConversation = $this->conversation->delete($id);
if ($deleteConversation) {
return $this->message->deleteMessages($id);
@@ -840,7 +880,8 @@ public function deleteConversations($id) {
*
* @return bool
*/
- public function deleteThread($id = null) {
+ public function deleteThread($id = null)
+ {
return $this->deleteConversations($id);
}
}
diff --git a/src/TalkServiceProvider.php b/src/TalkServiceProvider.php
index cd8048b..97fc42a 100644
--- a/src/TalkServiceProvider.php
+++ b/src/TalkServiceProvider.php
@@ -11,83 +11,83 @@
class TalkServiceProvider extends ServiceProvider
{
- /**
- * Bootstrap the application services.
- */
- public function boot()
- {
- $this->setupConfig();
- $this->setupMigrations();
- $this->loadViewsFrom(__DIR__.'/views', 'talk');
- }
- /**
- * Register the application services.
- */
- public function register()
- {
- $this->registerBroadcast();
- $this->registerTalk();
- }
- /**
- * Setup the config.
- */
- protected function setupConfig()
- {
- $source = realpath(__DIR__.'/../config/talk.php');
- // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
- if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
- $this->publishes([$source => config_path('talk.php')]);
- }
-
- if ($this->app instanceof LumenApplication) {
- $this->app->configure('talk');
- }
-
- $this->mergeConfigFrom($source, 'talk');
- }
- /**
- * Publish migrations files.
- */
- protected function setupMigrations()
- {
- $this->publishes([
- realpath(__DIR__.'/../database/migrations/') => database_path('migrations'),
- ], 'migrations');
- }
- /**
- * Register Talk class.
- */
- protected function registerTalk()
- {
- $this->app->singleton('talk', function (Container $app) {
- return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
- });
+ /**
+ * Bootstrap the application services.
+ */
+ public function boot()
+ {
+ $this->setupConfig();
+ $this->setupMigrations();
+ $this->loadViewsFrom(__DIR__ . '/views', 'talk');
+ }
+ /**
+ * Register the application services.
+ */
+ public function register()
+ {
+ $this->registerBroadcast();
+ $this->registerTalk();
+ }
+ /**
+ * Setup the config.
+ */
+ protected function setupConfig()
+ {
+ $source = realpath(__DIR__ . '/../config/talk.php');
+ // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
+ if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
+ $this->publishes([$source => config_path('talk.php')]);
+ }
- $this->app->alias('talk', Talk::class);
- }
+ if ($this->app instanceof LumenApplication) {
+ $this->app->configure('talk');
+ }
- /**
- * Register Talk class.
- */
- protected function registerBroadcast()
- {
- $this->app->singleton('talk.broadcast', function (Container $app) {
- return new Live\Broadcast($app['config']);
- });
+ $this->mergeConfigFrom($source, 'talk');
+ }
+ /**
+ * Publish migrations files.
+ */
+ protected function setupMigrations()
+ {
+ $this->publishes([
+ realpath(__DIR__ . '/../database/migrations/') => database_path('migrations'),
+ ], 'migrations');
+ }
+ /**
+ * Register Talk class.
+ */
+ protected function registerTalk()
+ {
+ $this->app->singleton('talk', function (Container $app) {
+ return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
+ });
- $this->app->alias('talk.broadcast', Live\Broadcast::class);
- }
+ $this->app->alias('talk', Talk::class);
+ }
- /**
- * Get the services provided by the provider.
- *
- * @return string[]
- */
- public function provides()
- {
- return [
- 'talk',
- 'talk.broadcast',
- ];
- }
+ /**
+ * Register Talk class.
+ */
+ protected function registerBroadcast()
+ {
+ $this->app->singleton('talk.broadcast', function (Container $app) {
+ return new Live\Broadcast($app['config']);
+ });
+
+ $this->app->alias('talk.broadcast', Live\Broadcast::class);
+ }
+
+ /**
+ * Get the services provided by the provider.
+ *
+ * @return string[]
+ */
+ public function provides()
+ {
+ return [
+ 'talk',
+ 'talk.broadcast',
+ ];
+ }
}
diff --git a/src/helpers/talk.php b/src/helpers/talk.php
index f2a9738..93399ef 100644
--- a/src/helpers/talk.php
+++ b/src/helpers/talk.php
@@ -6,19 +6,19 @@
* Time: 4:58 PM.
*/
if (!function_exists('talk_live')) {
- function talk_live($options)
- {
- $talk__appKey = config('talk.broadcast.pusher.app_key');
- $talk__appName = config('talk.broadcast.app_name');
- $talk__options = json_encode(config('talk.broadcast.pusher.options'));
+ function talk_live($options)
+ {
+ $talk__appKey = config('talk.broadcast.pusher.app_key');
+ $talk__appName = config('talk.broadcast.app_name');
+ $talk__options = json_encode(config('talk.broadcast.pusher.options'));
- $talk_user_channel = isset($options['user']['id']) ? $talk__appName.'-user-'.$options['user']['id'] : '';
- $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName.'-conversation-'.$options['conversation']['id'] : '';
- $talk__userChannel['name'] = sha1($talk_user_channel);
- $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
- $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
- $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
+ $talk_user_channel = isset($options['user']['id']) ? $talk__appName . '-user-' . $options['user']['id'] : '';
+ $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName . '-conversation-' . $options['conversation']['id'] : '';
+ $talk__userChannel['name'] = sha1($talk_user_channel);
+ $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
+ $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
+ $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
- return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
- }
+ return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
+ }
}
From 4fc63264d95693a6c56446d69a8257247e51d57a Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Tue, 5 Mar 2019 11:57:56 +0100
Subject: [PATCH 20/52] code formatting to ensure consistency in coding style
---
src/Conversations/Conversation.php | 112 +-
src/Conversations/ConversationRepository.php | 370 ++---
src/Facades/Talk.php | 18 +-
src/Live/Broadcast.php | 153 +-
src/Live/Webcast.php | 84 +-
src/Messages/Message.php | 114 +-
src/Messages/MessageRepository.php | 70 +-
src/Middleware/TalkMiddleware.php | 28 +-
src/Tags/Tag.php | 34 +-
src/Talk.php | 1444 +++++++++---------
src/TalkServiceProvider.php | 150 +-
src/helpers/talk.php | 26 +-
12 files changed, 1301 insertions(+), 1302 deletions(-)
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index 7b7a188..b34e5fa 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -6,66 +6,66 @@
class Conversation extends Model
{
- protected $table = 'conversations';
- public $timestamps = true;
+ protected $table = 'conversations';
+ public $timestamps = true;
- public $fillable = [
- 'user_one',
- 'user_two',
- 'title',
- 'status',
- ];
+ public $fillable = [
+ 'user_one',
+ 'user_two',
+ 'title',
+ 'status',
+ ];
- /*
- * make a relation with tags
- *
- * return relationship
- * */
- public function tags()
- {
- return $this->belongsToMany('Nahid\Talk\Tags\Tag');
- }
+ /*
+ * make a relation with tags
+ *
+ * return relationship
+ * */
+ public function tags()
+ {
+ return $this->belongsToMany('Nahid\Talk\Tags\Tag');
+ }
- /*
- * make a relation between message
- *
- * return collection
- * */
- public function messages()
- {
- return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation between message
+ *
+ * return collection
+ * */
+ public function messages()
+ {
+ return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
+ ->with('sender');
+ }
- /*
- * make a relation between first user from conversation
- *
- * return collection
- * */
- public function userone()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one');
- }
+ /*
+ * make a relation between first user from conversation
+ *
+ * return collection
+ * */
+ public function userone()
+ {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one');
+ }
- /*
- * make a relation between second user from conversation
- *
- * return collection
- * */
- public function usertwo()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two');
- }
+ /*
+ * make a relation between second user from conversation
+ *
+ * return collection
+ * */
+ public function usertwo()
+ {
+ return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two');
+ }
- /*
- * adds a tag to this conversation
- *
- * return bool
- * */
- public function addTag(\Nahid\Talk\Tags\Tag $tag)
- {
- // var_dump($this);
- $this->tags()->attach($tag->id);
- return true;
- }
+ /*
+ * adds a tag to this conversation
+ *
+ * return bool
+ * */
+ public function addTag(\Nahid\Talk\Tags\Tag $tag)
+ {
+ // var_dump($this);
+ $this->tags()->attach($tag->id);
+ return true;
+ }
}
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 3327b9f..fcf927c 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -6,189 +6,189 @@
class ConversationRepository extends Repository
{
- /*
- * this method is default method for repository package
- *
- * @return \Nahid\Talk\Conersations\Conversation
- * */
- public function takeModel()
- {
- return Conversation::class;
- }
-
- /*
- * check this given user is exists
- *
- * @param int $id
- * @return bool
- * */
- public function existsById($id)
- {
- $conversation = $this->find($id);
- if ($conversation) {
- return true;
- }
-
- return false;
- }
-
- /*
- * check this given two users is already make a conversation
- *
- * @param int $user1
- * @param int $user2
- * @return int|bool
- * */
- public function isExistsAmongTwoUsers($user1, $user2)
- {
- $conversation = Conversation::where('user_one', $user1)
- ->where('user_two', $user2);
-
- if ($conversation->exists()) {
- return $conversation->first()->id;
- }
-
- return false;
- }
-
- /*
- * check this given user is involved with this given $conversation
- *
- * @param int $conversationId
- * @param int $userId
- * @return bool
- * */
- public function isUserExists($conversationId, $userId)
- {
- $exists = Conversation::where('id', $conversationId)
- ->where(function ($query) use ($userId) {
- $query->where('user_one', $userId)->orWhere('user_two', $userId);
- })
- ->exists();
-
- return $exists;
- }
-
- /*
- * retrieve all message thread without soft deleted message with latest one message and
- * sender and receiver user model
- *
- * @param int $user_id
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threads($user_id, $order, $offset, $take)
- {
- $conv = new Conversation();
- $conv->authUser = $user_id;
- $conversations = $conv->with(['messages' => function ($q) use ($user_id) {
- return $q->where(function ($q) use ($user_id) {
- $q->where('user_id', $user_id)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($user_id) {
- $q->where('user_id', '!=', $user_id);
- $q->where('deleted_from_receiver', 0);
- })
- ->latest();
- }, 'messages.sender', 'userone', 'usertwo'])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->offset($offset)
- ->take($take)
- ->orderBy('updated_at', $order)
- ->get();
-
- $threads = [];
-
- foreach ($conversations as $conversation) {
- $collection = (object) null;
- $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
- $collection->thread = $conversation->messages->first();
- $collection->conversation = $conversation;
- $collection->messages = $conversation->messages;
- $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
- return $query
- ->where('user_id', '!=', $user_id)
- ->where('is_read', '=', '0');
- })->get();
- // dump($conversation->id);
- // dump($collection->unreadmessages);
- $collection->withUser = $conversationWith;
- $threads[] = $collection;
- }
-
- return collect($threads);
- }
-
- /*
- * retrieve all message thread with latest one message and sender and receiver user model
- *
- * @param int $user_id
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threadsAll($user_id, $offset, $take)
- {
- $msgThread = Conversation::with(['messages' => function ($q) use ($user_id) {
- return $q->latest();
- }, 'userone', 'usertwo'])
- ->where('user_one', $user_id)->orWhere('user_two', $user_id)->offset($offset)->take($take)->get();
-
- $threads = [];
-
- foreach ($msgThread as $thread) {
- $conversationWith = ($thread->userone->id == $user_id) ? $thread->usertwo : $thread->userone;
- $message = $thread->messages->first();
- $message->user = $conversationWith;
- $threads[] = $message;
- }
-
- return collect($threads);
- }
-
- /*
- * get all conversations by given conversation id
- *
- * @param int $conversationId
- * @param int $userId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesById($conversationId, $userId, $offset, $take)
- {
- return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
-
- $query->offset($offset)->take($take);
-
- }])->with(['userone', 'usertwo'])->find($conversationId);
-
- }
-
- /*
- * get all conversations with soft deleted message by given conversation id
- *
- * @param int $conversationId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesAllById($conversationId, $offset, $take)
- {
- return $this->with(['messages' => function ($q) use ($offset, $take) {
- return $q->offset($offset)
- ->take($take);
- }, 'userone', 'usertwo'])->find($conversationId);
- }
+ /*
+ * this method is default method for repository package
+ *
+ * @return \Nahid\Talk\Conersations\Conversation
+ * */
+ public function takeModel()
+ {
+ return Conversation::class;
+ }
+
+ /*
+ * check this given user is exists
+ *
+ * @param int $id
+ * @return bool
+ * */
+ public function existsById($id)
+ {
+ $conversation = $this->find($id);
+ if ($conversation) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given two users is already make a conversation
+ *
+ * @param int $user1
+ * @param int $user2
+ * @return int|bool
+ * */
+ public function isExistsAmongTwoUsers($user1, $user2)
+ {
+ $conversation = Conversation::where('user_one', $user1)
+ ->where('user_two', $user2);
+
+ if ($conversation->exists()) {
+ return $conversation->first()->id;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given user is involved with this given $conversation
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return bool
+ * */
+ public function isUserExists($conversationId, $userId)
+ {
+ $exists = Conversation::where('id', $conversationId)
+ ->where(function ($query) use ($userId) {
+ $query->where('user_one', $userId)->orWhere('user_two', $userId);
+ })
+ ->exists();
+
+ return $exists;
+ }
+
+ /*
+ * retrieve all message thread without soft deleted message with latest one message and
+ * sender and receiver user model
+ *
+ * @param int $user_id
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threads($user_id, $order, $offset, $take)
+ {
+ $conv = new Conversation();
+ $conv->authUser = $user_id;
+ $conversations = $conv->with(['messages' => function ($q) use ($user_id) {
+ return $q->where(function ($q) use ($user_id) {
+ $q->where('user_id', $user_id)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($user_id) {
+ $q->where('user_id', '!=', $user_id);
+ $q->where('deleted_from_receiver', 0);
+ })
+ ->latest();
+ }, 'messages.sender', 'userone', 'usertwo'])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->offset($offset)
+ ->take($take)
+ ->orderBy('updated_at', $order)
+ ->get();
+
+ $threads = [];
+
+ foreach ($conversations as $conversation) {
+ $collection = (object) null;
+ $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
+ $collection->thread = $conversation->messages->first();
+ $collection->conversation = $conversation;
+ $collection->messages = $conversation->messages;
+ $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
+ return $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0');
+ })->get();
+ // dump($conversation->id);
+ // dump($collection->unreadmessages);
+ $collection->withUser = $conversationWith;
+ $threads[] = $collection;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * retrieve all message thread with latest one message and sender and receiver user model
+ *
+ * @param int $user_id
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threadsAll($user_id, $offset, $take)
+ {
+ $msgThread = Conversation::with(['messages' => function ($q) use ($user_id) {
+ return $q->latest();
+ }, 'userone', 'usertwo'])
+ ->where('user_one', $user_id)->orWhere('user_two', $user_id)->offset($offset)->take($take)->get();
+
+ $threads = [];
+
+ foreach ($msgThread as $thread) {
+ $conversationWith = ($thread->userone->id == $user_id) ? $thread->usertwo : $thread->userone;
+ $message = $thread->messages->first();
+ $message->user = $conversationWith;
+ $threads[] = $message;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * get all conversations by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesById($conversationId, $userId, $offset, $take)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+
+ $query->offset($offset)->take($take);
+
+ }])->with(['userone', 'usertwo'])->find($conversationId);
+
+ }
+
+ /*
+ * get all conversations with soft deleted message by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesAllById($conversationId, $offset, $take)
+ {
+ return $this->with(['messages' => function ($q) use ($offset, $take) {
+ return $q->offset($offset)
+ ->take($take);
+ }, 'userone', 'usertwo'])->find($conversationId);
+ }
}
diff --git a/src/Facades/Talk.php b/src/Facades/Talk.php
index 86c531f..ec1394c 100644
--- a/src/Facades/Talk.php
+++ b/src/Facades/Talk.php
@@ -6,13 +6,13 @@
class Talk extends Facade
{
- /**
- * Get the registered name of the component.
- *
- * @return string
- */
- protected static function getFacadeAccessor()
- {
- return 'talk';
- }
+ /**
+ * Get the registered name of the component.
+ *
+ * @return string
+ */
+ protected static function getFacadeAccessor()
+ {
+ return 'talk';
+ }
}
diff --git a/src/Live/Broadcast.php b/src/Live/Broadcast.php
index a786cdd..9ef8b32 100644
--- a/src/Live/Broadcast.php
+++ b/src/Live/Broadcast.php
@@ -2,98 +2,97 @@
namespace Nahid\Talk\Live;
-use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Config\Repository;
+use Illuminate\Foundation\Bus\DispatchesJobs;
use Nahid\Talk\Messages\Message;
use Pusher\Pusher;
class Broadcast
{
- use DispatchesJobs;
-
- /*
- * Constant for talk config prefix
- *
- * @const string
- * */
- const CONFIG_PATH = 'talk';
+ use DispatchesJobs;
- /*
- * Set all configs from talk configurations
- *
- * @var array
- * */
- protected $config;
+ /*
+ * Constant for talk config prefix
+ *
+ * @const string
+ * */
+ const CONFIG_PATH = 'talk';
+ /*
+ * Set all configs from talk configurations
+ *
+ * @var array
+ * */
+ protected $config;
- /*
- * Pusher instance
- *
- * @var object
- * */
- public $pusher;
+ /*
+ * Pusher instance
+ *
+ * @var object
+ * */
+ public $pusher;
- /**
- * Connect pusher and get all credentials from config.
- *
- * @param \Illuminate\Contracts\Config\Repository $config
- */
- public function __construct(Repository $config)
- {
- $this->config = $config;
- $this->pusher = $this->connectPusher();
- }
+ /**
+ * Connect pusher and get all credentials from config.
+ *
+ * @param \Illuminate\Contracts\Config\Repository $config
+ */
+ public function __construct(Repository $config)
+ {
+ $this->config = $config;
+ $this->pusher = $this->connectPusher();
+ }
- /**
- * Make pusher connection.
- *
- * @param array $options
- *
- * @return object | bool
- */
- protected function connectPusher($options = [])
- {
- if ($this->getConfig('broadcast.enable')) {
- $appId = $this->getConfig('broadcast.pusher.app_id');
- $appKey = $this->getConfig('broadcast.pusher.app_key');
- $appSecret = $this->getConfig('broadcast.pusher.app_secret');
- $appOptions = $this->getConfig('broadcast.pusher.options');
+ /**
+ * Make pusher connection.
+ *
+ * @param array $options
+ *
+ * @return object | bool
+ */
+ protected function connectPusher($options = [])
+ {
+ if ($this->getConfig('broadcast.enable')) {
+ $appId = $this->getConfig('broadcast.pusher.app_id');
+ $appKey = $this->getConfig('broadcast.pusher.app_key');
+ $appSecret = $this->getConfig('broadcast.pusher.app_secret');
+ $appOptions = $this->getConfig('broadcast.pusher.options');
- $newOptions = array_merge($appOptions, $options);
- $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
+ $newOptions = array_merge($appOptions, $options);
+ $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
- return $pusher;
- }
+ return $pusher;
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Dispatch the job to the queue.
- *
- * @param \Nahid\Talk\Messages\Message $message
- */
- public function transmission(Message $message)
- {
- if (!$this->pusher) {
- return false;
- }
+ /**
+ * Dispatch the job to the queue.
+ *
+ * @param \Nahid\Talk\Messages\Message $message
+ */
+ public function transmission(Message $message)
+ {
+ if (!$this->pusher) {
+ return false;
+ }
- $sender = $message->sender->toArray();
- $messageArray = $message->toArray();
- $messageArray['sender'] = $sender;
- $this->dispatch(new Webcast($messageArray));
- }
+ $sender = $message->sender->toArray();
+ $messageArray = $message->toArray();
+ $messageArray['sender'] = $sender;
+ $this->dispatch(new Webcast($messageArray));
+ }
- /**
- * get specific config from talk configurations.
- *
- * @param string
- *
- * @return string|array|int
- */
- public function getConfig($name)
- {
- return $this->config->get(self::CONFIG_PATH.'.'.$name);
- }
+ /**
+ * get specific config from talk configurations.
+ *
+ * @param string
+ *
+ * @return string|array|int
+ */
+ public function getConfig($name)
+ {
+ return $this->config->get(self::CONFIG_PATH . '.' . $name);
+ }
}
diff --git a/src/Live/Webcast.php b/src/Live/Webcast.php
index 33e0fe6..fda899b 100644
--- a/src/Live/Webcast.php
+++ b/src/Live/Webcast.php
@@ -3,50 +3,50 @@
namespace Nahid\Talk\Live;
use Illuminate\Bus\Queueable;
-use Illuminate\Queue\SerializesModels;
-use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
class Webcast implements ShouldQueue
{
- use InteractsWithQueue, Queueable, SerializesModels;
-
- /*
- * Message Model Instance
- *
- * @var object
- * */
- protected $message;
-
- /*
- * Broadcast class instance
- *
- * @var object
- * */
- protected $broadcast;
-
- /**
- * Set message collections to the properties.
- */
- public function __construct($message)
- {
- $this->message = $message;
- }
-
- /*
- * Execute the job and broadcast to the pusher channels
- *
- * @param \Nahid\Talk\Live\Broadcast $broadcast
- * @return void
- */
- public function handle(Broadcast $broadcast)
- {
- $this->broadcast = $broadcast;
- $toUser = ($this->message['sender']['id'] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
-
- $channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
- $channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];
-
- $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
- }
+ use InteractsWithQueue, Queueable, SerializesModels;
+
+ /*
+ * Message Model Instance
+ *
+ * @var object
+ * */
+ protected $message;
+
+ /*
+ * Broadcast class instance
+ *
+ * @var object
+ * */
+ protected $broadcast;
+
+ /**
+ * Set message collections to the properties.
+ */
+ public function __construct($message)
+ {
+ $this->message = $message;
+ }
+
+ /*
+ * Execute the job and broadcast to the pusher channels
+ *
+ * @param \Nahid\Talk\Live\Broadcast $broadcast
+ * @return void
+ */
+ public function handle(Broadcast $broadcast)
+ {
+ $this->broadcast = $broadcast;
+ $toUser = ($this->message['sender']['id'] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
+
+ $channelForUser = $this->broadcast->getConfig('broadcast.app_name') . '-user-' . $toUser;
+ $channelForConversation = $this->broadcast->getConfig('broadcast.app_name') . '-conversation-' . $this->message['conversation_id'];
+
+ $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
+ }
}
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index 28c159e..beccc36 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -6,69 +6,69 @@
class Message extends Model
{
- protected $table = 'messages';
+ protected $table = 'messages';
- public $timestamps = true;
+ public $timestamps = true;
- public $fillable = [
- 'message',
- 'is_seen',
- 'is_read',
- 'deleted_from_sender',
- 'deleted_from_receiver',
- 'user_id',
- 'conversation_id',
- ];
+ public $fillable = [
+ 'message',
+ 'is_seen',
+ 'is_read',
+ 'deleted_from_sender',
+ 'deleted_from_receiver',
+ 'user_id',
+ 'conversation_id',
+ ];
- /*
- * make dynamic attribute for human readable time
- *
- * @return string
- * */
- public function getHumansTimeAttribute()
- {
- $date = $this->created_at;
- $now = $date->now();
+ /*
+ * make dynamic attribute for human readable time
+ *
+ * @return string
+ * */
+ public function getHumansTimeAttribute()
+ {
+ $date = $this->created_at;
+ $now = $date->now();
- if ($date->isToday()) {
- return $date->diffForHumans(null, false, true);
- } else {
- if ($date->isSameYear($now)) {
- return $date->format("M j");
- }
- }
+ if ($date->isToday()) {
+ return $date->diffForHumans(null, false, true);
+ } else {
+ if ($date->isSameYear($now)) {
+ return $date->format("M j");
+ }
+ }
- return $date->format("M j, Y");
- // return $date->diffForHumans(null, true, true) . ' ago';
- }
+ return $date->format("M j, Y");
+ // return $date->diffForHumans(null, true, true) . ' ago';
+ }
- /*
- * make a relation between conversation model
- *
- * @return collection
- * */
- public function conversation()
- {
- return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
- }
+ /*
+ * make a relation between conversation model
+ *
+ * @return collection
+ * */
+ public function conversation()
+ {
+ return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
+ }
- /*
- * make a relation between user model
- *
- * @return collection
- * */
- public function user()
- {
- return $this->belongsTo(config('talk.user.model', 'App\User'));
- }
+ /*
+ * make a relation between user model
+ *
+ * @return collection
+ * */
+ public function user()
+ {
+ return $this->belongsTo(config('talk.user.model', 'App\User'));
+ }
- /*
- * its an alias of user relation
- *
- * @return collection
- * */
- public function sender()
- {
- return $this->user();
- }
+ /*
+ * its an alias of user relation
+ *
+ * @return collection
+ * */
+ public function sender()
+ {
+ return $this->user();
+ }
}
diff --git a/src/Messages/MessageRepository.php b/src/Messages/MessageRepository.php
index 96924ed..bd5bad5 100644
--- a/src/Messages/MessageRepository.php
+++ b/src/Messages/MessageRepository.php
@@ -6,39 +6,39 @@
class MessageRepository extends Repository
{
- public function takeModel()
- {
- return Message::class;
- }
-
- public function deleteMessages($conversationId)
- {
- $delete = Message::where('conversation_id', $conversationId)->delete();
- if ($delete) {
- return true;
- }
-
- return false;
- }
-
- public function softDeleteMessage($messageId, $authUserId)
- {
- $message = $this->with(['conversation' => function ($q) use ($authUserId) {
- $q->where('user_one', $authUserId);
- $q->orWhere('user_two', $authUserId);
- }])->find($messageId);
-
- if (is_null($message->conversation)) {
- return false;
- }
-
- if ($message->user_id == $authUserId) {
- $message->deleted_from_sender = 1;
- } else {
- $message->deleted_from_receiver = 1;
- }
-
- return (boolean) $this->update($message);
-
- }
+ public function takeModel()
+ {
+ return Message::class;
+ }
+
+ public function deleteMessages($conversationId)
+ {
+ $delete = Message::where('conversation_id', $conversationId)->delete();
+ if ($delete) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public function softDeleteMessage($messageId, $authUserId)
+ {
+ $message = $this->with(['conversation' => function ($q) use ($authUserId) {
+ $q->where('user_one', $authUserId);
+ $q->orWhere('user_two', $authUserId);
+ }])->find($messageId);
+
+ if (is_null($message->conversation)) {
+ return false;
+ }
+
+ if ($message->user_id == $authUserId) {
+ $message->deleted_from_sender = 1;
+ } else {
+ $message->deleted_from_receiver = 1;
+ }
+
+ return (boolean) $this->update($message);
+
+ }
}
diff --git a/src/Middleware/TalkMiddleware.php b/src/Middleware/TalkMiddleware.php
index 3fbc8da..f620e8e 100644
--- a/src/Middleware/TalkMiddleware.php
+++ b/src/Middleware/TalkMiddleware.php
@@ -8,20 +8,20 @@
class TalkMiddleware
{
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Illuminate\Http\Request $request
+ * @param \Closure $next
+ * @return mixed
+ */
- public function handle($request, Closure $next, $guard = null)
- {
- if (Auth::guard($guard)->check()) {
- Talk::setAuthUserId(Auth::guard($guard)->user()->id);
- }
+ public function handle($request, Closure $next, $guard = null)
+ {
+ if (Auth::guard($guard)->check()) {
+ Talk::setAuthUserId(Auth::guard($guard)->user()->id);
+ }
- return $next($request);
- }
+ return $next($request);
+ }
}
diff --git a/src/Tags/Tag.php b/src/Tags/Tag.php
index b614769..d166772 100644
--- a/src/Tags/Tag.php
+++ b/src/Tags/Tag.php
@@ -6,22 +6,22 @@
class Tag extends Model
{
- protected $table = 'tags';
- public $timestamps = true;
- public $fillable = [
- 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
- 'name',
- ];
+ protected $table = 'tags';
+ public $timestamps = true;
+ public $fillable = [
+ 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
+ 'name',
+ ];
- /*
- * make a relation with conversations
- *
- * returns collection
- *
- * */
- public function conversations()
- {
- return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation with conversations
+ *
+ * returns collection
+ *
+ * */
+ public function conversations()
+ {
+ return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
+ ->with('sender');
+ }
}
diff --git a/src/Talk.php b/src/Talk.php
index a0fe0b8..4c3b7fe 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -18,726 +18,726 @@
class Talk
{
- /**
- * configurations instance.
- *
- * @var \Illuminate\Contracts\Config\Repository
- */
- protected $config;
-
- /**
- * The ConversationRepository class instance.
- *
- * @var \Nahid\Talk\Conversations\ConversationRepository
- */
- protected $conversation;
-
- /**
- * The MessageRepository class instance.
- *
- * @var \Nahid\Talk\Messages\MessageRepository
- */
- protected $message;
-
- /**
- * Broadcast class instance.
- *
- * @var \Nahid\Talk\Live\Broadcast
- */
- protected $broadcast;
-
- /**
- * Currently loggedin user id.
- *
- * @var int
- */
- protected $authUserId;
-
- /**
- * Initialize and instantiate conversation and message repositories.
- *
- * @param \Nahid\Talk\Conversations\ConversationRepository $conversation
- * @param \Nahid\Talk\Messages\MessageRepository $message
- */
- public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
- {
- $this->config = $config;
- $this->conversation = $conversation;
- $this->message = $message;
- $this->broadcast = $broadcast;
- }
-
- /**
- * make two users as serialize with ascending order.
- *
- * @param int $user1
- * @param int $user2
- *
- * @return array
- */
- protected function getSerializeUser($user1, $user2)
- {
- $user = [];
- $user['one'] = ($user1 < $user2) ? $user1 : $user2;
- $user['two'] = ($user1 < $user2) ? $user2 : $user1;
-
- return $user;
- }
-
- /**
- * create a new message by using conversationId.
- *
- * @param int $conversationId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message
- */
- protected function makeMessage($conversationId, $message)
- {
- $message = $this->message->create([
- 'message' => $message,
- 'conversation_id' => $conversationId,
- 'user_id' => $this->authUserId,
- 'is_seen' => 0,
- ]);
-
- $message->conversation->touch();
- $this->broadcast->transmission($message);
-
- return $message;
- }
-
- /*
- * Make new message collections to response with formatted data
- *
- *@param \Talk\Conversations\Conversation $conversations
- *@return object|bool
- */
- protected function makeMessageCollection($conversations)
- {
- if (!$conversations) {
- return false;
- }
-
- $collection = (object) null;
- if ($conversations->user_one == $this->authUserId || $conversations->user_two == $this->authUserId) {
- $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
- $collection->withUser = $withUser;
- $collection->messages = $conversations->messages;
-
- //mark them as read
- foreach ($collection->messages as $mssg) {
- if ($mssg->sender->id != $this->authUserId) {
- if (!Talk::user($this->authUserId)->markRead($mssg->id)) {
- return false;
- }
- }
- }
-
- return $collection;
- }
-
- return false;
- }
-
- /**
- * make new conversation with the given receiverId with currently loggedin user.
- *
- * @param int $receiverId
- *
- * @return int
- */
- protected function newConversation($receiverId, $title, $tagName = null)
- {
- // $conversationId = $this->isConversationExists($receiverId);
- $user = $this->getSerializeUser($this->authUserId, $receiverId);
-
- // if ($conversationId === false) {
- $conversation = $this->conversation->create([
- 'user_one' => $user['one'],
- 'user_two' => $user['two'],
- 'title' => $title,
- 'status' => 1,
- ]);
-
- if ($conversation) {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
- }
-
- $conversation->addTag($tag);
- }
-
- return $conversation->id;
- }
- // }
-
- return $conversationId;
- }
-
- /**
- * set currently authenticated user id for global usage.
- *
- * @param int $id
- *
- * @return int|bool
- */
- public function setAuthUserId($id = null)
- {
- if (!is_null($id)) {
- return $this->authUserId = $id;
- }
-
- return false;
- }
-
- /*
- * its set user id instantly when you fetch or access data. if you you haven't
- * set authenticated user id globally or you want to fetch work with
- * instant users information, you may use it
- *
- * @param int $id
- * @return \Nahid\Talk\Talk|bool
- * */
- public function user($id = null)
- {
- if ($this->setAuthUserId($id)) {
- return $this;
- }
-
- return false;
- }
-
- /**
- * make sure is this conversation exist for this user with currently loggedin user.
- *
- * @param int $userId
- *
- * @return bool|int
- */
- public function isConversationExists($userId)
- {
- if (empty($userId)) {
- return false;
- }
-
- $user = $this->getSerializeUser($this->authUserId, $userId);
-
- return $this->conversation->isExistsAmongTwoUsers($user['one'], $user['two']);
- }
-
- /**
- * check the given user exist for the given conversation.
- *
- * @param int $conversationId
- * @param int $userId
- *
- * @return bool
- */
- public function isAuthenticUser($conversationId, $userId)
- {
- if ($conversationId && $userId) {
- return $this->conversation->isUserExists($conversationId, $userId);
- }
-
- return false;
- }
-
- /**
- * send a message by using converstionid.
- *
- * @param int $conversationId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function sendMessage($conversatonId, $message)
- {
- if ($conversatonId && $message) {
- if ($this->conversation->existsById($conversatonId)) {
- $message = $this->makeMessage($conversatonId, $message);
-
- return $message;
- }
- }
-
- return false;
- }
-
- /**
- * create a new message by using receiverid.
- *
- * @param int $receiverId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function sendMessageByUserId($receiverId, $message, $title)
- {
- if ($conversationId = $this->isConversationExists($receiverId)) {
- $con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
- if ($con->title == $title) {
- // dd("same: {$con->title} == $title");
- $message = $this->makeMessage($conversationId, $message);
- return $message;
- }
- }
-
- $convId = $this->newConversation($receiverId, $title);
- $message = $this->makeMessage($convId, $message);
-
- return $message;
- }
-
- /**
- * fetch all inbox for currently loggedin user with pagination.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function getInbox($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->conversation->threads($this->authUserId, $order, $offset, $take);
- }
-
- /**
- * fetch all inbox with soft deleted message for currently loggedin user with pagination.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function getInboxAll($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
- }
-
- /**
- * its a alias of getInbox method.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function threads($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->getInbox($order, $offset, $take);
- }
-
- /**
- * its a alias of getInboxAll method.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function threadsAll($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->getInboxAll($order, $offset, $take);
- }
-
- /**
- * fetch all conversation by using conversation id.
- *
- * @param int $conversationId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function getConversationsById($conversationId, $offset = 0, $take = 20)
- {
- // dump($conversationId);
- // dd($this->authUserId);
- $conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
- return $this->makeMessageCollection($conversations);
- }
-
- /**
- * fetch all conversation with soft deleted messages by using conversation id.
- *
- * @param int $conversationId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function getConversationsAllById($conversationId, $offset = 0, $take = 20)
- {
- $conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
-
- return $this->makeMessageCollection($conversations);
- }
-
- /**
- * create a new message by using sender id.
- *
- * @param int $senderId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getConversationsByUserId($senderId, $offset = 0, $take = 20)
- {
- $conversationId = $this->isConversationExists($senderId, $this->authUserId);
- if ($conversationId) {
- return $this->getConversationsById($conversationId, $offset, $take);
- }
-
- return false;
- }
-
- /**
- * create a new message by using sender id.
- *
- * @param int $senderId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
- {
- $conversationId = $this->isConversationExists($senderId, $this->authUserId);
- if ($conversationId) {
- return $this->getConversationsAllById($conversationId, $offset, $take);
- }
-
- return false;
- }
-
- /**
- * gets tags owned/created by this user
- *
- * @return collection
- */
- public function getUserTags()
- {
- return Tags\Tag::where(['user_id' => $this->authUserId])->get();
- }
-
- /**
- * adds a tag to a conversation
- *
- * @param int $conversationId
- *
- * @return bool
- */
- public function addTagToConversation($conversationId, $tagName)
- {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
- }
-
- $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')->findOrFail($conversationId);
- if (!$conversation->tags->pluck('id')->contains($tag->id)) {
- $conversation->addTag($tag);
- }
-
- return true;
- }
-
- return false;
- }
-
- /**
- * its an alias of getConversationById.
- *
- * @param int $conversationId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessages($conversationId, $offset = 0, $take = 20)
- {
- return $this->getConversationsById($conversationId, $offset, $take);
- }
-
- /**
- * its an alias of getConversationAllById.
- *
- * @param int $conversationId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesAll($conversationId, $offset = 0, $take = 20)
- {
- return $this->getConversationsAllById($conversationId, $offset, $take);
- }
-
- /**
- * its an alias by getConversationByUserId.
- *
- * @param int $senderId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesByUserId($userId, $offset = 0, $take = 20)
- {
- return $this->getConversationsByUserId($userId, $offset, $take);
- }
-
- /**
- * its an alias by getConversationAllByUserId.
- *
- * @param int $senderId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesAllByUserId($userId, $offset = 0, $take = 20)
- {
- return $this->getConversationsAllByUserId($userId, $offset, $take);
- }
-
- /**
- * read a single message by message id.
- *
- * @param int $messageId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function readMessage($messageId = null)
- {
- if (!is_null($messageId)) {
- $message = $this->message->with(['sender', 'conversation'])->find($messageId);
-
- if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
- return $message;
- }
- }
-
- return false;
- }
-
- /**
- * marks a message as seen.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function makeSeen($messageId)
- {
- $seen = $this->message->update($messageId, ['is_seen' => 1]);
- if ($seen) {
- return true;
- }
-
- return false;
- }
- /**
- * marks a message as read.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function markRead($messageId)
- {
- if (!is_null($messageId)) {
- $message = $this->message->with(['sender', 'conversation'])->find($messageId);
- if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
-
- //only needful to mark as read if you are the recipient
- if ($message->sender->id != $this->authUserId) {
- $read = $this->message->update($messageId, ['is_read' => 1]);
- if (!$read) {
- return falsse;
- }
- }
-
- return true;
- }
- }
-
- return false;
- }
-
- /**
- * gets messages not yet read in a particular conversation
- *
- * @param int $conversationId
- *
- * @return mixed
- */
- public function getUnreadMessagesInConversation($conversationId)
- {
- if (!is_null($conversationId)) {
- $message = $this->conversation->with(['messages'])->find($conversationId);
- if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
-
- $unread = [];
- $unread = collect($this->conversation->messages)->filter(function ($message) use ($authUserId) {
- return (($message->sender->id != $authUserId) && ($message->is_read != 1));
- });
-
- return $unread;
- }
- }
-
- return false;
- }
-
- /**
- * gets all messages not yet read is all conversations altogether
- *
- * @param int $conversationId
- *
- * @return collection
- */
- public function getAllUnreadMessages()
- {
- $messages = collect();
- $user_id = \Illuminate\Support\Facades\Auth::user();
- $conv = new \Nahid\Talk\Conversations\Conversation();
- $conversations = $conv->with(['messages' => function ($query) use ($user_id) {
- return $query
- ->where('user_id', '!=', $user_id)
- ->where('is_read', '=', '0')
- ;
- }])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->get();
-
- foreach ($conversations as $conversation) {
- $messages = $messages->merge($conversation->messages);
- }
-
- // dump($messages);
- return $messages;
- }
-
- /**
- * gets all latest messages sent to auth'ed user
- *
- * @param int $conversationId
- *
- * @return collection
- */
- public function getLatestMessages()
- {
- // dump($this->authUserId);
- $messages = [];
- $user_id = \Illuminate\Support\Facades\Auth::user()->id;
- // dump($user->id);
- $conv = new \Nahid\Talk\Conversations\Conversation();
- $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
- return $query->where('user_id', '!=', $user_id)->with(['conversation']);
- }])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->orderBy('created_at')
- ->get();
-
- foreach ($msgThread as $thread) {
- $messages = collect($messages)->merge($thread->messages);
- }
-
- return $messages;
- }
-
- /**
- * gets the count of all messages not yet read in all conversations altogether
- *
- * @param int $conversationId
- *
- * @return int
- */
- public function getUnreadMessagesCount()
- {
- return $this->getAllUnreadMessages()->count();
- }
-
- /**
- * get receiver information for this conversation.
- *
- * @param int $conversationId
- *
- * @return UserModel
- *
- * @deprecated since version 2.0.0 Remove it from version 2.0.2
- */
- public function getReceiverInfo($conversationId)
- {
- $conversation = $this->conversation->find($conversationId);
- $receiver = '';
- if ($conversation->user_one == $this->authUserId) {
- $receiver = $conversation->user_two;
- } else {
- $receiver = $conversation->user_one;
- }
-
- $userModel = $this->config('talk.user.model');
- $user = new $userModel();
-
- return $user->find($receiver);
- }
-
- /**
- * delete a specific message, its a softdelete process. All message stored in db.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function deleteMessage($messageId)
- {
- return $this->message->softDeleteMessage($messageId, $this->authUserId);
- }
-
- /**
- * permanently delete message for this id.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function deleteForever($messageId)
- {
- $deleteMessage = $this->message->delete($messageId);
- if ($deleteMessage) {
- return true;
- }
-
- return false;
- }
-
- /**
- * delete message threat or conversation by conversation id.
- *
- * @param int $id
- *
- * @return bool
- */
- public function deleteConversations($id)
- {
- $deleteConversation = $this->conversation->delete($id);
- if ($deleteConversation) {
- return $this->message->deleteMessages($id);
- }
-
- return false;
- }
-
- /**
- * its an alias of deleteConversations.
- *
- * @param int $id
- *
- * @return bool
- */
- public function deleteThread($id = null)
- {
- return $this->deleteConversations($id);
- }
+ /**
+ * configurations instance.
+ *
+ * @var \Illuminate\Contracts\Config\Repository
+ */
+ protected $config;
+
+ /**
+ * The ConversationRepository class instance.
+ *
+ * @var \Nahid\Talk\Conversations\ConversationRepository
+ */
+ protected $conversation;
+
+ /**
+ * The MessageRepository class instance.
+ *
+ * @var \Nahid\Talk\Messages\MessageRepository
+ */
+ protected $message;
+
+ /**
+ * Broadcast class instance.
+ *
+ * @var \Nahid\Talk\Live\Broadcast
+ */
+ protected $broadcast;
+
+ /**
+ * Currently loggedin user id.
+ *
+ * @var int
+ */
+ protected $authUserId;
+
+ /**
+ * Initialize and instantiate conversation and message repositories.
+ *
+ * @param \Nahid\Talk\Conversations\ConversationRepository $conversation
+ * @param \Nahid\Talk\Messages\MessageRepository $message
+ */
+ public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
+ {
+ $this->config = $config;
+ $this->conversation = $conversation;
+ $this->message = $message;
+ $this->broadcast = $broadcast;
+ }
+
+ /**
+ * make two users as serialize with ascending order.
+ *
+ * @param int $user1
+ * @param int $user2
+ *
+ * @return array
+ */
+ protected function getSerializeUser($user1, $user2)
+ {
+ $user = [];
+ $user['one'] = ($user1 < $user2) ? $user1 : $user2;
+ $user['two'] = ($user1 < $user2) ? $user2 : $user1;
+
+ return $user;
+ }
+
+ /**
+ * create a new message by using conversationId.
+ *
+ * @param int $conversationId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ protected function makeMessage($conversationId, $message)
+ {
+ $message = $this->message->create([
+ 'message' => $message,
+ 'conversation_id' => $conversationId,
+ 'user_id' => $this->authUserId,
+ 'is_seen' => 0,
+ ]);
+
+ $message->conversation->touch();
+ $this->broadcast->transmission($message);
+
+ return $message;
+ }
+
+ /*
+ * Make new message collections to response with formatted data
+ *
+ *@param \Talk\Conversations\Conversation $conversations
+ *@return object|bool
+ */
+ protected function makeMessageCollection($conversations)
+ {
+ if (!$conversations) {
+ return false;
+ }
+
+ $collection = (object) null;
+ if ($conversations->user_one == $this->authUserId || $conversations->user_two == $this->authUserId) {
+ $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
+ $collection->withUser = $withUser;
+ $collection->messages = $conversations->messages;
+
+ //mark them as read
+ foreach ($collection->messages as $mssg) {
+ if ($mssg->sender->id != $this->authUserId) {
+ if (!Talk::user($this->authUserId)->markRead($mssg->id)) {
+ return false;
+ }
+ }
+ }
+
+ return $collection;
+ }
+
+ return false;
+ }
+
+ /**
+ * make new conversation with the given receiverId with currently loggedin user.
+ *
+ * @param int $receiverId
+ *
+ * @return int
+ */
+ protected function newConversation($receiverId, $title, $tagName = null)
+ {
+ // $conversationId = $this->isConversationExists($receiverId);
+ $user = $this->getSerializeUser($this->authUserId, $receiverId);
+
+ // if ($conversationId === false) {
+ $conversation = $this->conversation->create([
+ 'user_one' => $user['one'],
+ 'user_two' => $user['two'],
+ 'title' => $title,
+ 'status' => 1,
+ ]);
+
+ if ($conversation) {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
+ }
+
+ $conversation->addTag($tag);
+ }
+
+ return $conversation->id;
+ }
+ // }
+
+ return $conversationId;
+ }
+
+ /**
+ * set currently authenticated user id for global usage.
+ *
+ * @param int $id
+ *
+ * @return int|bool
+ */
+ public function setAuthUserId($id = null)
+ {
+ if (!is_null($id)) {
+ return $this->authUserId = $id;
+ }
+
+ return false;
+ }
+
+ /*
+ * its set user id instantly when you fetch or access data. if you you haven't
+ * set authenticated user id globally or you want to fetch work with
+ * instant users information, you may use it
+ *
+ * @param int $id
+ * @return \Nahid\Talk\Talk|bool
+ * */
+ public function user($id = null)
+ {
+ if ($this->setAuthUserId($id)) {
+ return $this;
+ }
+
+ return false;
+ }
+
+ /**
+ * make sure is this conversation exist for this user with currently loggedin user.
+ *
+ * @param int $userId
+ *
+ * @return bool|int
+ */
+ public function isConversationExists($userId)
+ {
+ if (empty($userId)) {
+ return false;
+ }
+
+ $user = $this->getSerializeUser($this->authUserId, $userId);
+
+ return $this->conversation->isExistsAmongTwoUsers($user['one'], $user['two']);
+ }
+
+ /**
+ * check the given user exist for the given conversation.
+ *
+ * @param int $conversationId
+ * @param int $userId
+ *
+ * @return bool
+ */
+ public function isAuthenticUser($conversationId, $userId)
+ {
+ if ($conversationId && $userId) {
+ return $this->conversation->isUserExists($conversationId, $userId);
+ }
+
+ return false;
+ }
+
+ /**
+ * send a message by using converstionid.
+ *
+ * @param int $conversationId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function sendMessage($conversatonId, $message)
+ {
+ if ($conversatonId && $message) {
+ if ($this->conversation->existsById($conversatonId)) {
+ $message = $this->makeMessage($conversatonId, $message);
+
+ return $message;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * create a new message by using receiverid.
+ *
+ * @param int $receiverId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function sendMessageByUserId($receiverId, $message, $title)
+ {
+ if ($conversationId = $this->isConversationExists($receiverId)) {
+ $con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
+ if ($con->title == $title) {
+ // dd("same: {$con->title} == $title");
+ $message = $this->makeMessage($conversationId, $message);
+ return $message;
+ }
+ }
+
+ $convId = $this->newConversation($receiverId, $title);
+ $message = $this->makeMessage($convId, $message);
+
+ return $message;
+ }
+
+ /**
+ * fetch all inbox for currently loggedin user with pagination.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function getInbox($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->conversation->threads($this->authUserId, $order, $offset, $take);
+ }
+
+ /**
+ * fetch all inbox with soft deleted message for currently loggedin user with pagination.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function getInboxAll($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
+ }
+
+ /**
+ * its a alias of getInbox method.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function threads($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->getInbox($order, $offset, $take);
+ }
+
+ /**
+ * its a alias of getInboxAll method.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function threadsAll($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->getInboxAll($order, $offset, $take);
+ }
+
+ /**
+ * fetch all conversation by using conversation id.
+ *
+ * @param int $conversationId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function getConversationsById($conversationId, $offset = 0, $take = 20)
+ {
+ // dump($conversationId);
+ // dd($this->authUserId);
+ $conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
+ return $this->makeMessageCollection($conversations);
+ }
+
+ /**
+ * fetch all conversation with soft deleted messages by using conversation id.
+ *
+ * @param int $conversationId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function getConversationsAllById($conversationId, $offset = 0, $take = 20)
+ {
+ $conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
+
+ return $this->makeMessageCollection($conversations);
+ }
+
+ /**
+ * create a new message by using sender id.
+ *
+ * @param int $senderId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getConversationsByUserId($senderId, $offset = 0, $take = 20)
+ {
+ $conversationId = $this->isConversationExists($senderId, $this->authUserId);
+ if ($conversationId) {
+ return $this->getConversationsById($conversationId, $offset, $take);
+ }
+
+ return false;
+ }
+
+ /**
+ * create a new message by using sender id.
+ *
+ * @param int $senderId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
+ {
+ $conversationId = $this->isConversationExists($senderId, $this->authUserId);
+ if ($conversationId) {
+ return $this->getConversationsAllById($conversationId, $offset, $take);
+ }
+
+ return false;
+ }
+
+ /**
+ * gets tags owned/created by this user
+ *
+ * @return collection
+ */
+ public function getUserTags()
+ {
+ return Tags\Tag::where(['user_id' => $this->authUserId])->get();
+ }
+
+ /**
+ * adds a tag to a conversation
+ *
+ * @param int $conversationId
+ *
+ * @return bool
+ */
+ public function addTagToConversation($conversationId, $tagName)
+ {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
+ }
+
+ $conversation = \Nahid\Talk\Conversations\Conversation::with('tags')->findOrFail($conversationId);
+ if (!$conversation->tags->pluck('id')->contains($tag->id)) {
+ $conversation->addTag($tag);
+ }
+
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * its an alias of getConversationById.
+ *
+ * @param int $conversationId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessages($conversationId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsById($conversationId, $offset, $take);
+ }
+
+ /**
+ * its an alias of getConversationAllById.
+ *
+ * @param int $conversationId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesAll($conversationId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsAllById($conversationId, $offset, $take);
+ }
+
+ /**
+ * its an alias by getConversationByUserId.
+ *
+ * @param int $senderId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesByUserId($userId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsByUserId($userId, $offset, $take);
+ }
+
+ /**
+ * its an alias by getConversationAllByUserId.
+ *
+ * @param int $senderId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesAllByUserId($userId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsAllByUserId($userId, $offset, $take);
+ }
+
+ /**
+ * read a single message by message id.
+ *
+ * @param int $messageId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function readMessage($messageId = null)
+ {
+ if (!is_null($messageId)) {
+ $message = $this->message->with(['sender', 'conversation'])->find($messageId);
+
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+ return $message;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * marks a message as seen.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function makeSeen($messageId)
+ {
+ $seen = $this->message->update($messageId, ['is_seen' => 1]);
+ if ($seen) {
+ return true;
+ }
+
+ return false;
+ }
+ /**
+ * marks a message as read.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function markRead($messageId)
+ {
+ if (!is_null($messageId)) {
+ $message = $this->message->with(['sender', 'conversation'])->find($messageId);
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+
+ //only needful to mark as read if you are the recipient
+ if ($message->sender->id != $this->authUserId) {
+ $read = $this->message->update($messageId, ['is_read' => 1]);
+ if (!$read) {
+ return falsse;
+ }
+ }
+
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * gets messages not yet read in a particular conversation
+ *
+ * @param int $conversationId
+ *
+ * @return mixed
+ */
+ public function getUnreadMessagesInConversation($conversationId)
+ {
+ if (!is_null($conversationId)) {
+ $message = $this->conversation->with(['messages'])->find($conversationId);
+ if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
+
+ $unread = [];
+ $unread = collect($this->conversation->messages)->filter(function ($message) use ($authUserId) {
+ return (($message->sender->id != $authUserId) && ($message->is_read != 1));
+ });
+
+ return $unread;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * gets all messages not yet read is all conversations altogether
+ *
+ * @param int $conversationId
+ *
+ * @return collection
+ */
+ public function getAllUnreadMessages()
+ {
+ $messages = collect();
+ $user_id = \Illuminate\Support\Facades\Auth::user();
+ $conv = new \Nahid\Talk\Conversations\Conversation();
+ $conversations = $conv->with(['messages' => function ($query) use ($user_id) {
+ return $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0')
+ ;
+ }])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->get();
+
+ foreach ($conversations as $conversation) {
+ $messages = $messages->merge($conversation->messages);
+ }
+
+ // dump($messages);
+ return $messages;
+ }
+
+ /**
+ * gets all latest messages sent to auth'ed user
+ *
+ * @param int $conversationId
+ *
+ * @return collection
+ */
+ public function getLatestMessages()
+ {
+ // dump($this->authUserId);
+ $messages = [];
+ $user_id = \Illuminate\Support\Facades\Auth::user()->id;
+ // dump($user->id);
+ $conv = new \Nahid\Talk\Conversations\Conversation();
+ $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
+ return $query->where('user_id', '!=', $user_id)->with(['conversation']);
+ }])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->orderBy('created_at')
+ ->get();
+
+ foreach ($msgThread as $thread) {
+ $messages = collect($messages)->merge($thread->messages);
+ }
+
+ return $messages;
+ }
+
+ /**
+ * gets the count of all messages not yet read in all conversations altogether
+ *
+ * @param int $conversationId
+ *
+ * @return int
+ */
+ public function getUnreadMessagesCount()
+ {
+ return $this->getAllUnreadMessages()->count();
+ }
+
+ /**
+ * get receiver information for this conversation.
+ *
+ * @param int $conversationId
+ *
+ * @return UserModel
+ *
+ * @deprecated since version 2.0.0 Remove it from version 2.0.2
+ */
+ public function getReceiverInfo($conversationId)
+ {
+ $conversation = $this->conversation->find($conversationId);
+ $receiver = '';
+ if ($conversation->user_one == $this->authUserId) {
+ $receiver = $conversation->user_two;
+ } else {
+ $receiver = $conversation->user_one;
+ }
+
+ $userModel = $this->config('talk.user.model');
+ $user = new $userModel();
+
+ return $user->find($receiver);
+ }
+
+ /**
+ * delete a specific message, its a softdelete process. All message stored in db.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function deleteMessage($messageId)
+ {
+ return $this->message->softDeleteMessage($messageId, $this->authUserId);
+ }
+
+ /**
+ * permanently delete message for this id.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function deleteForever($messageId)
+ {
+ $deleteMessage = $this->message->delete($messageId);
+ if ($deleteMessage) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * delete message threat or conversation by conversation id.
+ *
+ * @param int $id
+ *
+ * @return bool
+ */
+ public function deleteConversations($id)
+ {
+ $deleteConversation = $this->conversation->delete($id);
+ if ($deleteConversation) {
+ return $this->message->deleteMessages($id);
+ }
+
+ return false;
+ }
+
+ /**
+ * its an alias of deleteConversations.
+ *
+ * @param int $id
+ *
+ * @return bool
+ */
+ public function deleteThread($id = null)
+ {
+ return $this->deleteConversations($id);
+ }
}
diff --git a/src/TalkServiceProvider.php b/src/TalkServiceProvider.php
index cd8048b..97fc42a 100644
--- a/src/TalkServiceProvider.php
+++ b/src/TalkServiceProvider.php
@@ -11,83 +11,83 @@
class TalkServiceProvider extends ServiceProvider
{
- /**
- * Bootstrap the application services.
- */
- public function boot()
- {
- $this->setupConfig();
- $this->setupMigrations();
- $this->loadViewsFrom(__DIR__.'/views', 'talk');
- }
- /**
- * Register the application services.
- */
- public function register()
- {
- $this->registerBroadcast();
- $this->registerTalk();
- }
- /**
- * Setup the config.
- */
- protected function setupConfig()
- {
- $source = realpath(__DIR__.'/../config/talk.php');
- // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
- if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
- $this->publishes([$source => config_path('talk.php')]);
- }
-
- if ($this->app instanceof LumenApplication) {
- $this->app->configure('talk');
- }
-
- $this->mergeConfigFrom($source, 'talk');
- }
- /**
- * Publish migrations files.
- */
- protected function setupMigrations()
- {
- $this->publishes([
- realpath(__DIR__.'/../database/migrations/') => database_path('migrations'),
- ], 'migrations');
- }
- /**
- * Register Talk class.
- */
- protected function registerTalk()
- {
- $this->app->singleton('talk', function (Container $app) {
- return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
- });
+ /**
+ * Bootstrap the application services.
+ */
+ public function boot()
+ {
+ $this->setupConfig();
+ $this->setupMigrations();
+ $this->loadViewsFrom(__DIR__ . '/views', 'talk');
+ }
+ /**
+ * Register the application services.
+ */
+ public function register()
+ {
+ $this->registerBroadcast();
+ $this->registerTalk();
+ }
+ /**
+ * Setup the config.
+ */
+ protected function setupConfig()
+ {
+ $source = realpath(__DIR__ . '/../config/talk.php');
+ // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
+ if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
+ $this->publishes([$source => config_path('talk.php')]);
+ }
- $this->app->alias('talk', Talk::class);
- }
+ if ($this->app instanceof LumenApplication) {
+ $this->app->configure('talk');
+ }
- /**
- * Register Talk class.
- */
- protected function registerBroadcast()
- {
- $this->app->singleton('talk.broadcast', function (Container $app) {
- return new Live\Broadcast($app['config']);
- });
+ $this->mergeConfigFrom($source, 'talk');
+ }
+ /**
+ * Publish migrations files.
+ */
+ protected function setupMigrations()
+ {
+ $this->publishes([
+ realpath(__DIR__ . '/../database/migrations/') => database_path('migrations'),
+ ], 'migrations');
+ }
+ /**
+ * Register Talk class.
+ */
+ protected function registerTalk()
+ {
+ $this->app->singleton('talk', function (Container $app) {
+ return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
+ });
- $this->app->alias('talk.broadcast', Live\Broadcast::class);
- }
+ $this->app->alias('talk', Talk::class);
+ }
- /**
- * Get the services provided by the provider.
- *
- * @return string[]
- */
- public function provides()
- {
- return [
- 'talk',
- 'talk.broadcast',
- ];
- }
+ /**
+ * Register Talk class.
+ */
+ protected function registerBroadcast()
+ {
+ $this->app->singleton('talk.broadcast', function (Container $app) {
+ return new Live\Broadcast($app['config']);
+ });
+
+ $this->app->alias('talk.broadcast', Live\Broadcast::class);
+ }
+
+ /**
+ * Get the services provided by the provider.
+ *
+ * @return string[]
+ */
+ public function provides()
+ {
+ return [
+ 'talk',
+ 'talk.broadcast',
+ ];
+ }
}
diff --git a/src/helpers/talk.php b/src/helpers/talk.php
index f2a9738..93399ef 100644
--- a/src/helpers/talk.php
+++ b/src/helpers/talk.php
@@ -6,19 +6,19 @@
* Time: 4:58 PM.
*/
if (!function_exists('talk_live')) {
- function talk_live($options)
- {
- $talk__appKey = config('talk.broadcast.pusher.app_key');
- $talk__appName = config('talk.broadcast.app_name');
- $talk__options = json_encode(config('talk.broadcast.pusher.options'));
+ function talk_live($options)
+ {
+ $talk__appKey = config('talk.broadcast.pusher.app_key');
+ $talk__appName = config('talk.broadcast.app_name');
+ $talk__options = json_encode(config('talk.broadcast.pusher.options'));
- $talk_user_channel = isset($options['user']['id']) ? $talk__appName.'-user-'.$options['user']['id'] : '';
- $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName.'-conversation-'.$options['conversation']['id'] : '';
- $talk__userChannel['name'] = sha1($talk_user_channel);
- $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
- $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
- $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
+ $talk_user_channel = isset($options['user']['id']) ? $talk__appName . '-user-' . $options['user']['id'] : '';
+ $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName . '-conversation-' . $options['conversation']['id'] : '';
+ $talk__userChannel['name'] = sha1($talk_user_channel);
+ $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
+ $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
+ $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
- return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
- }
+ return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
+ }
}
From c28802bc5471a813dde2e73e45c1e9754a963a21 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Tue, 5 Mar 2019 11:58:49 +0100
Subject: [PATCH 21/52] code formatting to ensure consistency in coding style
---
src/Conversations/Conversation.php | 18 +-
src/Conversations/ConversationRepository.php | 406 +++++++++----------
src/Facades/Talk.php | 18 +-
src/Live/Broadcast.php | 153 ++++---
src/Live/Webcast.php | 84 ++--
src/Messages/Message.php | 26 +-
src/Messages/MessageRepository.php | 70 ++--
src/Middleware/TalkMiddleware.php | 28 +-
src/Tags/Tag.php | 34 +-
src/Talk.php | 123 ++++--
src/TalkServiceProvider.php | 150 +++----
src/helpers/talk.php | 26 +-
12 files changed, 594 insertions(+), 542 deletions(-)
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index e763055..bea2953 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -4,7 +4,8 @@
use Illuminate\Database\Eloquent\Model;
-class Conversation extends Model {
+class Conversation extends Model
+{
protected $table = 'conversations';
public $timestamps = true;
public $fillable = [
@@ -19,7 +20,8 @@ class Conversation extends Model {
*
* return relationship
* */
- public function tags() {
+ public function tags()
+ {
return $this->belongsToMany('Nahid\Talk\Tags\Tag');
}
@@ -28,7 +30,8 @@ public function tags() {
*
* return collection
* */
- public function messages() {
+ public function messages()
+ {
return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
->with('sender');
}
@@ -38,7 +41,8 @@ public function messages() {
*
* return collection
* */
- public function userone() {
+ public function userone()
+ {
return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one', config('talk.user.ownerKey'));
}
@@ -47,7 +51,8 @@ public function userone() {
*
* return collection
* */
- public function usertwo() {
+ public function usertwo()
+ {
return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two', config('talk.user.ownerKey'));
}
@@ -56,7 +61,8 @@ public function usertwo() {
*
* return void
* */
- public function addTag(\Nahid\Talk\Tags\Tag $tag) {
+ public function addTag(\Nahid\Talk\Tags\Tag $tag)
+ {
return $this->tags()->attach($tag->id);
}
}
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 240c3a9..6d8a905 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -6,207 +6,207 @@
class ConversationRepository extends Repository
{
- /*
- * this method is default method for repository package
- *
- * @return \Nahid\Talk\Conersations\Conversation
- * */
- public function takeModel()
- {
- return Conversation::class;
- }
-
- /*
- * check this given user is exists
- *
- * @param int $id
- * @return bool
- * */
- public function existsById($id)
- {
- $conversation = $this->find($id);
- if ($conversation) {
- return true;
- }
-
- return false;
- }
-
- /*
- * check this given two users is already make a conversation
- *
- * @param int $user1
- * @param int $user2
- * @return int|bool
- * */
- public function isExistsAmongTwoUsers($user1, $user2)
- {
- $conversation = Conversation::where('user_one', $user1)
- ->where('user_two', $user2);
-
- if ($conversation->exists()) {
- return $conversation->first()->id;
- }
-
- return false;
- }
-
- /*
- * check this given user is involved with this given $conversation
- *
- * @param int $conversationId
- * @param int $userId
- * @return bool
- * */
- public function isUserExists($conversationId, $userId)
- {
- $exists = Conversation::where('id', $conversationId)
- ->where(function ($query) use ($userId) {
- $query->where('user_one', $userId)->orWhere('user_two', $userId);
- })
- ->exists();
-
- return $exists;
- }
-
- /*
- * retrieve all message thread without soft deleted message with latest one message and
- * sender and receiver user model
- *
- * @param int $user_id
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threads($user_id, $order, $offset, $take)
- {
- $conv = new Conversation();
- $conv->authUser = $user_id;
- $conversations = $conv->with(['messages' => function ($q) use ($user_id) {
- return $q->where(function ($q) use ($user_id) {
- $q->where('user_id', $user_id)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($user_id) {
- $q->where('user_id', '!=', $user_id);
- $q->where('deleted_from_receiver', 0);
- })
- ->latest();
- }, 'messages.sender', 'userone', 'usertwo'])
- ->where('user_one', $user_id)
- ->orWhere('user_two', $user_id)
- ->offset($offset)
- ->take($take)
- ->orderBy('updated_at', $order)
- ->get();
-
- $threads = [];
-
- foreach ($conversations as $conversation) {
- $collection = (object) null;
- $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
- $collection->thread = $conversation->messages->first();
- $collection->conversation = $conversation;
- $collection->messages = $conversation->messages;
- $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
- return $query
- ->where('user_id', '!=', $user_id)
- ->where('is_read', '=', '0');
- })->get();
- $collection->withUser = $conversationWith;
- $threads[] = $collection;
- }
-
- return collect($threads);
- }
-
- /*
- * retrieve all message thread with latest one message and sender and receiver user model
- *
- * @param int $user_id
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threadsAll($user_id, $offset, $take)
- {
- $msgThread = Conversation::with(['messages' => function ($q) use ($user_id) {
- return $q->latest();
- }, 'userone', 'usertwo'])
- ->where('user_one', $user_id)->orWhere('user_two', $user_id)->offset($offset)->take($take)->get();
-
- $threads = [];
-
- foreach ($msgThread as $thread) {
- $conversationWith = ($thread->userone->id == $user_id) ? $thread->usertwo : $thread->userone;
- $message = $thread->messages->first();
- $message->user = $conversationWith;
- $threads[] = $message;
- }
-
- return collect($threads);
- }
-
- /*
- * get all conversations by given conversation id
- *
- * @param int $conversationId
- * @param int $userId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesById($conversationId, $userId, $offset, $take)
- {
- return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
-
- $query->offset($offset)->take($take);
-
- }])->with(['userone', 'usertwo'])->find($conversationId);
- }
-
- /*
- * get all conversations by given tag id
- *
- * @param int $conversationId
- * @param int $userId
- * @return collection
- * */
- public function getMessagesByTagId($tagId, $userId)
- {
- return Conversation::with(['messages' => function ($query) use ($userId) {
- $query->where(function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- })
- ->orWhere(function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- });
- }])->with(['userone', 'usertwo', 'tags'])->get();
- }
-
- /*
- * get all conversations with soft deleted message by given conversation id
- *
- * @param int $conversationId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesAllById($conversationId, $offset, $take)
- {
- return $this->with(['messages' => function ($q) use ($offset, $take) {
- return $q->offset($offset)
- ->take($take);
- }, 'userone', 'usertwo'])->find($conversationId);
- }
+ /*
+ * this method is default method for repository package
+ *
+ * @return \Nahid\Talk\Conersations\Conversation
+ * */
+ public function takeModel()
+ {
+ return Conversation::class;
+ }
+
+ /*
+ * check this given user is exists
+ *
+ * @param int $id
+ * @return bool
+ * */
+ public function existsById($id)
+ {
+ $conversation = $this->find($id);
+ if ($conversation) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given two users is already make a conversation
+ *
+ * @param int $user1
+ * @param int $user2
+ * @return int|bool
+ * */
+ public function isExistsAmongTwoUsers($user1, $user2)
+ {
+ $conversation = Conversation::where('user_one', $user1)
+ ->where('user_two', $user2);
+
+ if ($conversation->exists()) {
+ return $conversation->first()->id;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given user is involved with this given $conversation
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return bool
+ * */
+ public function isUserExists($conversationId, $userId)
+ {
+ $exists = Conversation::where('id', $conversationId)
+ ->where(function ($query) use ($userId) {
+ $query->where('user_one', $userId)->orWhere('user_two', $userId);
+ })
+ ->exists();
+
+ return $exists;
+ }
+
+ /*
+ * retrieve all message thread without soft deleted message with latest one message and
+ * sender and receiver user model
+ *
+ * @param int $user_id
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threads($user_id, $order, $offset, $take)
+ {
+ $conv = new Conversation();
+ $conv->authUser = $user_id;
+ $conversations = $conv->with(['messages' => function ($q) use ($user_id) {
+ return $q->where(function ($q) use ($user_id) {
+ $q->where('user_id', $user_id)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($user_id) {
+ $q->where('user_id', '!=', $user_id);
+ $q->where('deleted_from_receiver', 0);
+ })
+ ->latest();
+ }, 'messages.sender', 'userone', 'usertwo'])
+ ->where('user_one', $user_id)
+ ->orWhere('user_two', $user_id)
+ ->offset($offset)
+ ->take($take)
+ ->orderBy('updated_at', $order)
+ ->get();
+
+ $threads = [];
+
+ foreach ($conversations as $conversation) {
+ $collection = (object) null;
+ $conversationWith = ($conversation->userone->id == $user_id) ? $conversation->usertwo : $conversation->userone;
+ $collection->thread = $conversation->messages->first();
+ $collection->conversation = $conversation;
+ $collection->messages = $conversation->messages;
+ $collection->unreadmessages = $conversation->messages()->where(function ($query) use ($user_id) {
+ return $query
+ ->where('user_id', '!=', $user_id)
+ ->where('is_read', '=', '0');
+ })->get();
+ $collection->withUser = $conversationWith;
+ $threads[] = $collection;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * retrieve all message thread with latest one message and sender and receiver user model
+ *
+ * @param int $user_id
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threadsAll($user_id, $offset, $take)
+ {
+ $msgThread = Conversation::with(['messages' => function ($q) use ($user_id) {
+ return $q->latest();
+ }, 'userone', 'usertwo'])
+ ->where('user_one', $user_id)->orWhere('user_two', $user_id)->offset($offset)->take($take)->get();
+
+ $threads = [];
+
+ foreach ($msgThread as $thread) {
+ $conversationWith = ($thread->userone->id == $user_id) ? $thread->usertwo : $thread->userone;
+ $message = $thread->messages->first();
+ $message->user = $conversationWith;
+ $threads[] = $message;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * get all conversations by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesById($conversationId, $userId, $offset, $take)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId, $offset, $take) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+
+ $query->offset($offset)->take($take);
+
+ }])->with(['userone', 'usertwo'])->find($conversationId);
+ }
+
+ /*
+ * get all conversations by given tag id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return collection
+ * */
+ public function getMessagesByTagId($tagId, $userId)
+ {
+ return Conversation::with(['messages' => function ($query) use ($userId) {
+ $query->where(function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ })
+ ->orWhere(function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ });
+ }])->with(['userone', 'usertwo', 'tags'])->get();
+ }
+
+ /*
+ * get all conversations with soft deleted message by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesAllById($conversationId, $offset, $take)
+ {
+ return $this->with(['messages' => function ($q) use ($offset, $take) {
+ return $q->offset($offset)
+ ->take($take);
+ }, 'userone', 'usertwo'])->find($conversationId);
+ }
}
diff --git a/src/Facades/Talk.php b/src/Facades/Talk.php
index 86c531f..ec1394c 100644
--- a/src/Facades/Talk.php
+++ b/src/Facades/Talk.php
@@ -6,13 +6,13 @@
class Talk extends Facade
{
- /**
- * Get the registered name of the component.
- *
- * @return string
- */
- protected static function getFacadeAccessor()
- {
- return 'talk';
- }
+ /**
+ * Get the registered name of the component.
+ *
+ * @return string
+ */
+ protected static function getFacadeAccessor()
+ {
+ return 'talk';
+ }
}
diff --git a/src/Live/Broadcast.php b/src/Live/Broadcast.php
index a786cdd..9ef8b32 100644
--- a/src/Live/Broadcast.php
+++ b/src/Live/Broadcast.php
@@ -2,98 +2,97 @@
namespace Nahid\Talk\Live;
-use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Config\Repository;
+use Illuminate\Foundation\Bus\DispatchesJobs;
use Nahid\Talk\Messages\Message;
use Pusher\Pusher;
class Broadcast
{
- use DispatchesJobs;
-
- /*
- * Constant for talk config prefix
- *
- * @const string
- * */
- const CONFIG_PATH = 'talk';
+ use DispatchesJobs;
- /*
- * Set all configs from talk configurations
- *
- * @var array
- * */
- protected $config;
+ /*
+ * Constant for talk config prefix
+ *
+ * @const string
+ * */
+ const CONFIG_PATH = 'talk';
+ /*
+ * Set all configs from talk configurations
+ *
+ * @var array
+ * */
+ protected $config;
- /*
- * Pusher instance
- *
- * @var object
- * */
- public $pusher;
+ /*
+ * Pusher instance
+ *
+ * @var object
+ * */
+ public $pusher;
- /**
- * Connect pusher and get all credentials from config.
- *
- * @param \Illuminate\Contracts\Config\Repository $config
- */
- public function __construct(Repository $config)
- {
- $this->config = $config;
- $this->pusher = $this->connectPusher();
- }
+ /**
+ * Connect pusher and get all credentials from config.
+ *
+ * @param \Illuminate\Contracts\Config\Repository $config
+ */
+ public function __construct(Repository $config)
+ {
+ $this->config = $config;
+ $this->pusher = $this->connectPusher();
+ }
- /**
- * Make pusher connection.
- *
- * @param array $options
- *
- * @return object | bool
- */
- protected function connectPusher($options = [])
- {
- if ($this->getConfig('broadcast.enable')) {
- $appId = $this->getConfig('broadcast.pusher.app_id');
- $appKey = $this->getConfig('broadcast.pusher.app_key');
- $appSecret = $this->getConfig('broadcast.pusher.app_secret');
- $appOptions = $this->getConfig('broadcast.pusher.options');
+ /**
+ * Make pusher connection.
+ *
+ * @param array $options
+ *
+ * @return object | bool
+ */
+ protected function connectPusher($options = [])
+ {
+ if ($this->getConfig('broadcast.enable')) {
+ $appId = $this->getConfig('broadcast.pusher.app_id');
+ $appKey = $this->getConfig('broadcast.pusher.app_key');
+ $appSecret = $this->getConfig('broadcast.pusher.app_secret');
+ $appOptions = $this->getConfig('broadcast.pusher.options');
- $newOptions = array_merge($appOptions, $options);
- $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
+ $newOptions = array_merge($appOptions, $options);
+ $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
- return $pusher;
- }
+ return $pusher;
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Dispatch the job to the queue.
- *
- * @param \Nahid\Talk\Messages\Message $message
- */
- public function transmission(Message $message)
- {
- if (!$this->pusher) {
- return false;
- }
+ /**
+ * Dispatch the job to the queue.
+ *
+ * @param \Nahid\Talk\Messages\Message $message
+ */
+ public function transmission(Message $message)
+ {
+ if (!$this->pusher) {
+ return false;
+ }
- $sender = $message->sender->toArray();
- $messageArray = $message->toArray();
- $messageArray['sender'] = $sender;
- $this->dispatch(new Webcast($messageArray));
- }
+ $sender = $message->sender->toArray();
+ $messageArray = $message->toArray();
+ $messageArray['sender'] = $sender;
+ $this->dispatch(new Webcast($messageArray));
+ }
- /**
- * get specific config from talk configurations.
- *
- * @param string
- *
- * @return string|array|int
- */
- public function getConfig($name)
- {
- return $this->config->get(self::CONFIG_PATH.'.'.$name);
- }
+ /**
+ * get specific config from talk configurations.
+ *
+ * @param string
+ *
+ * @return string|array|int
+ */
+ public function getConfig($name)
+ {
+ return $this->config->get(self::CONFIG_PATH . '.' . $name);
+ }
}
diff --git a/src/Live/Webcast.php b/src/Live/Webcast.php
index 33e0fe6..fda899b 100644
--- a/src/Live/Webcast.php
+++ b/src/Live/Webcast.php
@@ -3,50 +3,50 @@
namespace Nahid\Talk\Live;
use Illuminate\Bus\Queueable;
-use Illuminate\Queue\SerializesModels;
-use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
class Webcast implements ShouldQueue
{
- use InteractsWithQueue, Queueable, SerializesModels;
-
- /*
- * Message Model Instance
- *
- * @var object
- * */
- protected $message;
-
- /*
- * Broadcast class instance
- *
- * @var object
- * */
- protected $broadcast;
-
- /**
- * Set message collections to the properties.
- */
- public function __construct($message)
- {
- $this->message = $message;
- }
-
- /*
- * Execute the job and broadcast to the pusher channels
- *
- * @param \Nahid\Talk\Live\Broadcast $broadcast
- * @return void
- */
- public function handle(Broadcast $broadcast)
- {
- $this->broadcast = $broadcast;
- $toUser = ($this->message['sender']['id'] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
-
- $channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
- $channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];
-
- $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
- }
+ use InteractsWithQueue, Queueable, SerializesModels;
+
+ /*
+ * Message Model Instance
+ *
+ * @var object
+ * */
+ protected $message;
+
+ /*
+ * Broadcast class instance
+ *
+ * @var object
+ * */
+ protected $broadcast;
+
+ /**
+ * Set message collections to the properties.
+ */
+ public function __construct($message)
+ {
+ $this->message = $message;
+ }
+
+ /*
+ * Execute the job and broadcast to the pusher channels
+ *
+ * @param \Nahid\Talk\Live\Broadcast $broadcast
+ * @return void
+ */
+ public function handle(Broadcast $broadcast)
+ {
+ $this->broadcast = $broadcast;
+ $toUser = ($this->message['sender']['id'] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
+
+ $channelForUser = $this->broadcast->getConfig('broadcast.app_name') . '-user-' . $toUser;
+ $channelForConversation = $this->broadcast->getConfig('broadcast.app_name') . '-conversation-' . $this->message['conversation_id'];
+
+ $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
+ }
}
diff --git a/src/Messages/Message.php b/src/Messages/Message.php
index 022ab7a..f371c83 100644
--- a/src/Messages/Message.php
+++ b/src/Messages/Message.php
@@ -4,7 +4,8 @@
use Illuminate\Database\Eloquent\Model;
-class Message extends Model {
+class Message extends Model
+{
protected $table = 'messages';
public $timestamps = true;
@@ -24,7 +25,8 @@ class Message extends Model {
*
* @return string
* */
- public function getHumansTimeAttribute() {
+ public function getHumansTimeAttribute()
+ {
//laravel sometimes has $this=null but attributes proprty works perfectly well
$date = \Carbon\Carbon::parse($this->attributes['created_at']);
$now = $date->now();
@@ -37,7 +39,8 @@ public function getHumansTimeAttribute() {
*
* @return string
* */
- public function getNaturalHumansTimeAttribute() {
+ public function getNaturalHumansTimeAttribute()
+ {
//laravel sometimes has $this=null but attributes proprty works perfectly well
$date = \Carbon\Carbon::parse($this->attributes['created_at']);
$now = $date->now();
@@ -58,7 +61,8 @@ public function getNaturalHumansTimeAttribute() {
*
* @return collection
* */
- public function conversation() {
+ public function conversation()
+ {
return $this->belongsTo('Nahid\Talk\Conversations\Conversation');
}
@@ -67,12 +71,13 @@ public function conversation() {
*
* @return collection
* */
- public function user() {
+ public function user()
+ {
return $this->belongsTo(
- config('talk.user.model', 'App\User'),
- config('talk.user.foreignKey'),
- config('talk.user.ownerKey')
- );
+ config('talk.user.model', 'App\User'),
+ config('talk.user.foreignKey'),
+ config('talk.user.ownerKey')
+ );
}
/*
@@ -80,7 +85,8 @@ public function user() {
*
* @return collection
* */
- public function sender() {
+ public function sender()
+ {
return $this->user();
}
}
diff --git a/src/Messages/MessageRepository.php b/src/Messages/MessageRepository.php
index 96924ed..bd5bad5 100644
--- a/src/Messages/MessageRepository.php
+++ b/src/Messages/MessageRepository.php
@@ -6,39 +6,39 @@
class MessageRepository extends Repository
{
- public function takeModel()
- {
- return Message::class;
- }
-
- public function deleteMessages($conversationId)
- {
- $delete = Message::where('conversation_id', $conversationId)->delete();
- if ($delete) {
- return true;
- }
-
- return false;
- }
-
- public function softDeleteMessage($messageId, $authUserId)
- {
- $message = $this->with(['conversation' => function ($q) use ($authUserId) {
- $q->where('user_one', $authUserId);
- $q->orWhere('user_two', $authUserId);
- }])->find($messageId);
-
- if (is_null($message->conversation)) {
- return false;
- }
-
- if ($message->user_id == $authUserId) {
- $message->deleted_from_sender = 1;
- } else {
- $message->deleted_from_receiver = 1;
- }
-
- return (boolean) $this->update($message);
-
- }
+ public function takeModel()
+ {
+ return Message::class;
+ }
+
+ public function deleteMessages($conversationId)
+ {
+ $delete = Message::where('conversation_id', $conversationId)->delete();
+ if ($delete) {
+ return true;
+ }
+
+ return false;
+ }
+
+ public function softDeleteMessage($messageId, $authUserId)
+ {
+ $message = $this->with(['conversation' => function ($q) use ($authUserId) {
+ $q->where('user_one', $authUserId);
+ $q->orWhere('user_two', $authUserId);
+ }])->find($messageId);
+
+ if (is_null($message->conversation)) {
+ return false;
+ }
+
+ if ($message->user_id == $authUserId) {
+ $message->deleted_from_sender = 1;
+ } else {
+ $message->deleted_from_receiver = 1;
+ }
+
+ return (boolean) $this->update($message);
+
+ }
}
diff --git a/src/Middleware/TalkMiddleware.php b/src/Middleware/TalkMiddleware.php
index 3fbc8da..f620e8e 100644
--- a/src/Middleware/TalkMiddleware.php
+++ b/src/Middleware/TalkMiddleware.php
@@ -8,20 +8,20 @@
class TalkMiddleware
{
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Illuminate\Http\Request $request
+ * @param \Closure $next
+ * @return mixed
+ */
- public function handle($request, Closure $next, $guard = null)
- {
- if (Auth::guard($guard)->check()) {
- Talk::setAuthUserId(Auth::guard($guard)->user()->id);
- }
+ public function handle($request, Closure $next, $guard = null)
+ {
+ if (Auth::guard($guard)->check()) {
+ Talk::setAuthUserId(Auth::guard($guard)->user()->id);
+ }
- return $next($request);
- }
+ return $next($request);
+ }
}
diff --git a/src/Tags/Tag.php b/src/Tags/Tag.php
index b614769..d166772 100644
--- a/src/Tags/Tag.php
+++ b/src/Tags/Tag.php
@@ -6,22 +6,22 @@
class Tag extends Model
{
- protected $table = 'tags';
- public $timestamps = true;
- public $fillable = [
- 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
- 'name',
- ];
+ protected $table = 'tags';
+ public $timestamps = true;
+ public $fillable = [
+ 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
+ 'name',
+ ];
- /*
- * make a relation with conversations
- *
- * returns collection
- *
- * */
- public function conversations()
- {
- return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation with conversations
+ *
+ * returns collection
+ *
+ * */
+ public function conversations()
+ {
+ return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
+ ->with('sender');
+ }
}
diff --git a/src/Talk.php b/src/Talk.php
index 580c470..4e95fa7 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -16,7 +16,8 @@
use Nahid\Talk\Live\Broadcast;
use Nahid\Talk\Messages\MessageRepository;
-class Talk {
+class Talk
+{
/**
* Now users can attach special importance to conversations by star-ing them.
@@ -72,7 +73,8 @@ class Talk {
* @param \Nahid\Talk\Conversations\ConversationRepository $conversation
* @param \Nahid\Talk\Messages\MessageRepository $message
*/
- public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message) {
+ public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
+ {
// dump('calling meeeeeeeeee');
$this->config = $config;
$this->conversation = $conversation;
@@ -88,7 +90,8 @@ public function __construct(Repository $config, Broadcast $broadcast, Conversati
*
* @return array
*/
- protected function getSerializeUser($user1, $user2) {
+ protected function getSerializeUser($user1, $user2)
+ {
$user = [];
$user['one'] = ($user1 < $user2) ? $user1 : $user2;
$user['two'] = ($user1 < $user2) ? $user2 : $user1;
@@ -104,7 +107,8 @@ protected function getSerializeUser($user1, $user2) {
*
* @return \Nahid\Talk\Messages\Message
*/
- protected function makeMessage($conversationId, $message) {
+ protected function makeMessage($conversationId, $message)
+ {
$message = $this->message->create([
'message' => $message,
'conversation_id' => $conversationId,
@@ -124,7 +128,8 @@ protected function makeMessage($conversationId, $message) {
*@param \Talk\Conversations\Conversation $conversations
*@return object|bool
*/
- protected function makeMessageCollection($conversations) {
+ protected function makeMessageCollection($conversations)
+ {
if (!$conversations) {
return false;
}
@@ -157,7 +162,8 @@ protected function makeMessageCollection($conversations) {
*
* @return int
*/
- protected function newConversation($receiverId, $title, $tagName = null) {
+ protected function newConversation($receiverId, $title, $tagName = null)
+ {
// $conversationId = $this->isConversationExists($receiverId);
$user = $this->getSerializeUser($this->authUserId, $receiverId);
@@ -193,7 +199,8 @@ protected function newConversation($receiverId, $title, $tagName = null) {
*
* @return int|bool
*/
- public function setAuthUserId($id = null) {
+ public function setAuthUserId($id = null)
+ {
if (!is_null($id)) {
return $this->authUserId = $id;
}
@@ -209,7 +216,8 @@ public function setAuthUserId($id = null) {
* @param int $id
* @return \Nahid\Talk\Talk|bool
* */
- public function user($id = null) {
+ public function user($id = null)
+ {
if ($this->setAuthUserId($id)) {
return $this;
}
@@ -224,7 +232,8 @@ public function user($id = null) {
*
* @return bool|int
*/
- public function isConversationExists($userId) {
+ public function isConversationExists($userId)
+ {
if (empty($userId)) {
return false;
}
@@ -242,7 +251,8 @@ public function isConversationExists($userId) {
*
* @return bool
*/
- public function isAuthenticUser($conversationId, $userId) {
+ public function isAuthenticUser($conversationId, $userId)
+ {
if ($conversationId && $userId) {
return $this->conversation->isUserExists($conversationId, $userId);
}
@@ -258,7 +268,8 @@ public function isAuthenticUser($conversationId, $userId) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function sendMessage($conversatonId, $message) {
+ public function sendMessage($conversatonId, $message)
+ {
if ($conversatonId && $message) {
if ($this->conversation->existsById($conversatonId)) {
$message = $this->makeMessage($conversatonId, $message);
@@ -278,7 +289,8 @@ public function sendMessage($conversatonId, $message) {
*
* @return \Nahid\Talk\Messages\Message
*/
- public function sendMessageByUserId($receiverId, $message, $title = null) {
+ public function sendMessageByUserId($receiverId, $message, $title = null)
+ {
if ($conversationId = $this->isConversationExists($receiverId)) {
$con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
if ($con->title == $title) {
@@ -302,7 +314,8 @@ public function sendMessageByUserId($receiverId, $message, $title = null) {
*
* @return array
*/
- public function getInbox($order = 'desc', $offset = 0, $take = 20) {
+ public function getInbox($order = 'desc', $offset = 0, $take = 20)
+ {
// dump($this->authUserId);
return $this->conversation->threads($this->authUserId, $order, $offset, $take);
}
@@ -315,7 +328,8 @@ public function getInbox($order = 'desc', $offset = 0, $take = 20) {
*
* @return array
*/
- public function getInboxAll($order = 'desc', $offset = 0, $take = 20) {
+ public function getInboxAll($order = 'desc', $offset = 0, $take = 20)
+ {
return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
}
@@ -327,7 +341,8 @@ public function getInboxAll($order = 'desc', $offset = 0, $take = 20) {
*
* @return array
*/
- public function threads($order = 'desc', $offset = 0, $take = 20) {
+ public function threads($order = 'desc', $offset = 0, $take = 20)
+ {
return $this->getInbox($order, $offset, $take);
}
@@ -339,7 +354,8 @@ public function threads($order = 'desc', $offset = 0, $take = 20) {
*
* @return array
*/
- public function threadsAll($order = 'desc', $offset = 0, $take = 20) {
+ public function threadsAll($order = 'desc', $offset = 0, $take = 20)
+ {
return $this->getInboxAll($order, $offset, $take);
}
@@ -352,7 +368,8 @@ public function threadsAll($order = 'desc', $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message
*/
- public function getConversationsById($conversationId, $offset = 0, $take = 20) {
+ public function getConversationsById($conversationId, $offset = 0, $take = 20)
+ {
// dump($conversationId);
// dd($this->authUserId);
$conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
@@ -366,7 +383,8 @@ public function getConversationsById($conversationId, $offset = 0, $take = 20) {
*
* @return collection
*/
- public function getConversationsByTagId($tag_id) {
+ public function getConversationsByTagId($tag_id)
+ {
// $threads = $this->conversation->threads($this->authUserId, 'id', 6, 6);
$conversations_ = $this->conversation->getMessagesByTagId($tag_id, $this->authUserId);
$user_id = $this->authUserId;
@@ -407,7 +425,8 @@ public function getConversationsByTagId($tag_id) {
*
* @return \Nahid\Talk\Messages\Message
*/
- public function getConversationsAllById($conversationId, $offset = 0, $take = 20) {
+ public function getConversationsAllById($conversationId, $offset = 0, $take = 20)
+ {
$conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
return $this->makeMessageCollection($conversations);
@@ -422,7 +441,8 @@ public function getConversationsAllById($conversationId, $offset = 0, $take = 20
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getConversationsByUserId($senderId, $offset = 0, $take = 20) {
+ public function getConversationsByUserId($senderId, $offset = 0, $take = 20)
+ {
$conversationId = $this->isConversationExists($senderId, $this->authUserId);
if ($conversationId) {
return $this->getConversationsById($conversationId, $offset, $take);
@@ -440,7 +460,8 @@ public function getConversationsByUserId($senderId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20) {
+ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
+ {
$conversationId = $this->isConversationExists($senderId, $this->authUserId);
if ($conversationId) {
return $this->getConversationsAllById($conversationId, $offset, $take);
@@ -454,7 +475,8 @@ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
*
* @return collection
*/
- public function getUserTags() {
+ public function getUserTags()
+ {
return Tags\Tag::where(['user_id' => $this->authUserId])
->where('name', '!=', Talk::STARTAG)
->get();
@@ -467,7 +489,8 @@ public function getUserTags() {
*
* @return bool
*/
- public function createTagForUser($tagName) {
+ public function createTagForUser($tagName)
+ {
if (!empty($tagName)) {
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
if (is_null($tag)) {
@@ -490,7 +513,8 @@ public function createTagForUser($tagName) {
*
* @return bool
*/
- public function addTagToConversation($conversationId, $tagName, bool $specialTagOnlyOne = null) {
+ public function addTagToConversation($conversationId, $tagName, bool $specialTagOnlyOne = null)
+ {
$specialTagOnlyOne = is_bool($specialTagOnlyOne) ? $specialTagOnlyOne : false;
if (!empty($tagName)) {
//treat star tag specially
@@ -532,7 +556,8 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
*
* @return bool
*/
- public function removeTagFromConversation($conversationId, $tagId) {
+ public function removeTagFromConversation($conversationId, $tagId)
+ {
if (!empty($conversationId) && !empty($tagId)) {
//confirm user owns this tag
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'id' => $tagId])->firstOrFail();
@@ -557,7 +582,8 @@ public function removeTagFromConversation($conversationId, $tagId) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessages($conversationId, $offset = 0, $take = 20) {
+ public function getMessages($conversationId, $offset = 0, $take = 20)
+ {
return $this->getConversationsById($conversationId, $offset, $take);
}
@@ -568,7 +594,8 @@ public function getMessages($conversationId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessagesAll($conversationId, $offset = 0, $take = 20) {
+ public function getMessagesAll($conversationId, $offset = 0, $take = 20)
+ {
return $this->getConversationsAllById($conversationId, $offset, $take);
}
@@ -579,7 +606,8 @@ public function getMessagesAll($conversationId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessagesByUserId($userId, $offset = 0, $take = 20) {
+ public function getMessagesByUserId($userId, $offset = 0, $take = 20)
+ {
return $this->getConversationsByUserId($userId, $offset, $take);
}
@@ -590,7 +618,8 @@ public function getMessagesByUserId($userId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function getMessagesAllByUserId($userId, $offset = 0, $take = 20) {
+ public function getMessagesAllByUserId($userId, $offset = 0, $take = 20)
+ {
return $this->getConversationsAllByUserId($userId, $offset, $take);
}
@@ -601,7 +630,8 @@ public function getMessagesAllByUserId($userId, $offset = 0, $take = 20) {
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function readMessage($messageId = null) {
+ public function readMessage($messageId = null)
+ {
if (!is_null($messageId)) {
$message = $this->message->with(['sender', 'conversation'])->find($messageId);
@@ -620,7 +650,8 @@ public function readMessage($messageId = null) {
*
* @return bool
*/
- public function makeSeen($messageId) {
+ public function makeSeen($messageId)
+ {
$seen = $this->message->update($messageId, ['is_seen' => 1]);
if ($seen) {
return true;
@@ -635,7 +666,8 @@ public function makeSeen($messageId) {
*
* @return bool
*/
- public function markRead($messageId) {
+ public function markRead($messageId)
+ {
if (!is_null($messageId)) {
$message = $this->message->with(['sender', 'conversation'])->find($messageId);
if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
@@ -662,7 +694,8 @@ public function markRead($messageId) {
*
* @return mixed
*/
- public function getUnreadMessagesInConversation($conversationId) {
+ public function getUnreadMessagesInConversation($conversationId)
+ {
if (!is_null($conversationId)) {
$message = $this->conversation->with(['messages'])->find($conversationId);
if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
@@ -690,7 +723,8 @@ public function getUnreadMessagesInConversation($conversationId) {
*
* @return collection
*/
- public function getAllUnreadMessages($removeSpecialMessages = false) {
+ public function getAllUnreadMessages($removeSpecialMessages = false)
+ {
$messages = collect();
$user_id = $this->authUserId;
$conv = new \Nahid\Talk\Conversations\Conversation();
@@ -731,7 +765,8 @@ public function getAllUnreadMessages($removeSpecialMessages = false) {
*
* @return collection
*/
- public function getLatestMessages() {
+ public function getLatestMessages()
+ {
if ($this->latestMessages == null) {
$messages = collect();
@@ -763,7 +798,8 @@ public function getLatestMessages() {
*
* @return int
*/
- public function getUnreadMessagesCount($removeSpecialMessages = false) {
+ public function getUnreadMessagesCount($removeSpecialMessages = false)
+ {
return $this->getAllUnreadMessages($removeSpecialMessages)->count();
}
@@ -776,7 +812,8 @@ public function getUnreadMessagesCount($removeSpecialMessages = false) {
*
* @deprecated since version 2.0.0 Remove it from version 2.0.2
*/
- public function getReceiverInfo($conversationId) {
+ public function getReceiverInfo($conversationId)
+ {
$conversation = $this->conversation->find($conversationId);
$receiver = '';
if ($conversation->user_one == $this->authUserId) {
@@ -798,7 +835,8 @@ public function getReceiverInfo($conversationId) {
*
* @return bool
*/
- public function deleteMessage($messageId) {
+ public function deleteMessage($messageId)
+ {
return $this->message->softDeleteMessage($messageId, $this->authUserId);
}
@@ -809,7 +847,8 @@ public function deleteMessage($messageId) {
*
* @return bool
*/
- public function deleteForever($messageId) {
+ public function deleteForever($messageId)
+ {
$deleteMessage = $this->message->delete($messageId);
if ($deleteMessage) {
return true;
@@ -825,7 +864,8 @@ public function deleteForever($messageId) {
*
* @return bool
*/
- public function deleteConversations($id) {
+ public function deleteConversations($id)
+ {
$deleteConversation = $this->conversation->delete($id);
if ($deleteConversation) {
return $this->message->deleteMessages($id);
@@ -841,7 +881,8 @@ public function deleteConversations($id) {
*
* @return bool
*/
- public function deleteThread($id = null) {
+ public function deleteThread($id = null)
+ {
return $this->deleteConversations($id);
}
}
diff --git a/src/TalkServiceProvider.php b/src/TalkServiceProvider.php
index cd8048b..97fc42a 100644
--- a/src/TalkServiceProvider.php
+++ b/src/TalkServiceProvider.php
@@ -11,83 +11,83 @@
class TalkServiceProvider extends ServiceProvider
{
- /**
- * Bootstrap the application services.
- */
- public function boot()
- {
- $this->setupConfig();
- $this->setupMigrations();
- $this->loadViewsFrom(__DIR__.'/views', 'talk');
- }
- /**
- * Register the application services.
- */
- public function register()
- {
- $this->registerBroadcast();
- $this->registerTalk();
- }
- /**
- * Setup the config.
- */
- protected function setupConfig()
- {
- $source = realpath(__DIR__.'/../config/talk.php');
- // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
- if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
- $this->publishes([$source => config_path('talk.php')]);
- }
-
- if ($this->app instanceof LumenApplication) {
- $this->app->configure('talk');
- }
-
- $this->mergeConfigFrom($source, 'talk');
- }
- /**
- * Publish migrations files.
- */
- protected function setupMigrations()
- {
- $this->publishes([
- realpath(__DIR__.'/../database/migrations/') => database_path('migrations'),
- ], 'migrations');
- }
- /**
- * Register Talk class.
- */
- protected function registerTalk()
- {
- $this->app->singleton('talk', function (Container $app) {
- return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
- });
+ /**
+ * Bootstrap the application services.
+ */
+ public function boot()
+ {
+ $this->setupConfig();
+ $this->setupMigrations();
+ $this->loadViewsFrom(__DIR__ . '/views', 'talk');
+ }
+ /**
+ * Register the application services.
+ */
+ public function register()
+ {
+ $this->registerBroadcast();
+ $this->registerTalk();
+ }
+ /**
+ * Setup the config.
+ */
+ protected function setupConfig()
+ {
+ $source = realpath(__DIR__ . '/../config/talk.php');
+ // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
+ if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
+ $this->publishes([$source => config_path('talk.php')]);
+ }
- $this->app->alias('talk', Talk::class);
- }
+ if ($this->app instanceof LumenApplication) {
+ $this->app->configure('talk');
+ }
- /**
- * Register Talk class.
- */
- protected function registerBroadcast()
- {
- $this->app->singleton('talk.broadcast', function (Container $app) {
- return new Live\Broadcast($app['config']);
- });
+ $this->mergeConfigFrom($source, 'talk');
+ }
+ /**
+ * Publish migrations files.
+ */
+ protected function setupMigrations()
+ {
+ $this->publishes([
+ realpath(__DIR__ . '/../database/migrations/') => database_path('migrations'),
+ ], 'migrations');
+ }
+ /**
+ * Register Talk class.
+ */
+ protected function registerTalk()
+ {
+ $this->app->singleton('talk', function (Container $app) {
+ return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
+ });
- $this->app->alias('talk.broadcast', Live\Broadcast::class);
- }
+ $this->app->alias('talk', Talk::class);
+ }
- /**
- * Get the services provided by the provider.
- *
- * @return string[]
- */
- public function provides()
- {
- return [
- 'talk',
- 'talk.broadcast',
- ];
- }
+ /**
+ * Register Talk class.
+ */
+ protected function registerBroadcast()
+ {
+ $this->app->singleton('talk.broadcast', function (Container $app) {
+ return new Live\Broadcast($app['config']);
+ });
+
+ $this->app->alias('talk.broadcast', Live\Broadcast::class);
+ }
+
+ /**
+ * Get the services provided by the provider.
+ *
+ * @return string[]
+ */
+ public function provides()
+ {
+ return [
+ 'talk',
+ 'talk.broadcast',
+ ];
+ }
}
diff --git a/src/helpers/talk.php b/src/helpers/talk.php
index f2a9738..93399ef 100644
--- a/src/helpers/talk.php
+++ b/src/helpers/talk.php
@@ -6,19 +6,19 @@
* Time: 4:58 PM.
*/
if (!function_exists('talk_live')) {
- function talk_live($options)
- {
- $talk__appKey = config('talk.broadcast.pusher.app_key');
- $talk__appName = config('talk.broadcast.app_name');
- $talk__options = json_encode(config('talk.broadcast.pusher.options'));
+ function talk_live($options)
+ {
+ $talk__appKey = config('talk.broadcast.pusher.app_key');
+ $talk__appName = config('talk.broadcast.app_name');
+ $talk__options = json_encode(config('talk.broadcast.pusher.options'));
- $talk_user_channel = isset($options['user']['id']) ? $talk__appName.'-user-'.$options['user']['id'] : '';
- $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName.'-conversation-'.$options['conversation']['id'] : '';
- $talk__userChannel['name'] = sha1($talk_user_channel);
- $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
- $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
- $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
+ $talk_user_channel = isset($options['user']['id']) ? $talk__appName . '-user-' . $options['user']['id'] : '';
+ $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName . '-conversation-' . $options['conversation']['id'] : '';
+ $talk__userChannel['name'] = sha1($talk_user_channel);
+ $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
+ $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
+ $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
- return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
- }
+ return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
+ }
}
From ffd74cfbebc5184632cf51c9b396b25bf965d18c Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Tue, 5 Mar 2019 11:59:36 +0100
Subject: [PATCH 22/52] code formatting to ensure consistency in coding style
---
src/Conversations/Conversation.php | 18 +-
src/Conversations/ConversationRepository.php | 432 +++----
src/Embera/Adapter.php | 57 +-
src/Facades/Talk.php | 18 +-
src/Html/HtmlString.php | 133 ++-
src/Html/HtmlStringInterface.php | 9 +-
src/Live/Broadcast.php | 153 ++-
src/Live/Webcast.php | 86 +-
src/Messages/MessageRepository.php | 60 +-
src/Middleware/TalkMiddleware.php | 28 +-
src/Tags/Tag.php | 34 +-
src/Talk.php | 1110 +++++++++---------
src/TalkServiceProvider.php | 150 +--
src/helpers/talk.php | 26 +-
14 files changed, 1158 insertions(+), 1156 deletions(-)
diff --git a/src/Conversations/Conversation.php b/src/Conversations/Conversation.php
index 5d108fc..823a93f 100644
--- a/src/Conversations/Conversation.php
+++ b/src/Conversations/Conversation.php
@@ -4,7 +4,8 @@
use Illuminate\Database\Eloquent\Model;
-class Conversation extends Model {
+class Conversation extends Model
+{
protected $table = 'conversations';
public $timestamps = true;
public $fillable = [
@@ -19,7 +20,8 @@ class Conversation extends Model {
*
* return collection
* */
- public function tags() {
+ public function tags()
+ {
return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
->with('sender');
}
@@ -29,7 +31,8 @@ public function tags() {
*
* return collection
* */
- public function messages() {
+ public function messages()
+ {
return $this->hasMany('Nahid\Talk\Messages\Message', 'conversation_id')
->with('sender');
}
@@ -39,7 +42,8 @@ public function messages() {
*
* return collection
* */
- public function userone() {
+ public function userone()
+ {
return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_one', config('talk.user.ownerKey'));
}
@@ -48,7 +52,8 @@ public function userone() {
*
* return collection
* */
- public function usertwo() {
+ public function usertwo()
+ {
return $this->belongsTo(config('talk.user.model', 'App\User'), 'user_two', config('talk.user.ownerKey'));
}
@@ -57,7 +62,8 @@ public function usertwo() {
*
* return bool
* */
- public function addTag(\Nahid\Talk\Tags $tag) {
+ public function addTag(\Nahid\Talk\Tags $tag)
+ {
$this->tags()->attach($tag->id);
return true;
}
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index 80645b6..930ad88 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -6,220 +6,220 @@
class ConversationRepository extends Repository
{
- /*
- * this method is default method for repository package
- *
- * @return \Nahid\Talk\Conersations\Conversation
- * */
- public function takeModel()
- {
- return Conversation::class;
- }
-
- /*
- * check this given user is exists
- *
- * @param int $id
- * @return bool
- * */
- public function existsById($id)
- {
- $conversation = $this->find($id);
- if ($conversation) {
- return true;
- }
-
- return false;
- }
-
- /*
- * check this given two users is already make a conversation
- *
- * @param int $user1
- * @param int $user2
- * @return int|bool
- * */
- public function isExistsAmongTwoUsers($user1, $user2)
- {
- $conversation = Conversation::where(
- function ($query) use ($user1, $user2) {
- $query->where(
- function ($q) use ($user1, $user2) {
- $q->where('user_one', $user1)
- ->where('user_two', $user2);
- }
- )
- ->orWhere(
- function ($q) use ($user1, $user2) {
- $q->where('user_one', $user2)
- ->where('user_two', $user1);
- }
- );
- }
- );
-
- if ($conversation->exists()) {
- return $conversation->first()->id;
- }
-
- return false;
- }
-
- /*
- * check this given user is involved with this given $conversation
- *
- * @param int $conversationId
- * @param int $userId
- * @return bool
- * */
- public function isUserExists($conversationId, $userId)
- {
- $exists = Conversation::where('id', $conversationId)
- ->where(
- function ($query) use ($userId) {
- $query->where('user_one', $userId)->orWhere('user_two', $userId);
- }
- )
- ->exists();
-
- return $exists;
- }
-
- /*
- * retrieve all message thread without soft deleted message with latest one message and
- * sender and receiver user model
- *
- * @param int $user
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threads($user, $order, $offset, $take)
- {
- $conv = new Conversation();
- $conv->authUser = $user;
- $msgThread = $conv->with(
- [
- 'messages' => function ($q) use ($user) {
- return $q->where(
- function ($q) use ($user) {
- $q->where('user_id', $user)
- ->where('deleted_from_sender', 0);
- }
- )
- ->orWhere(
- function ($q) use ($user) {
- $q->where('user_id', '!=', $user);
- $q->where('deleted_from_receiver', 0);
- }
- )
- ->latest();
- }, 'messages.sender', 'userone', 'usertwo'
- ]
- )
- ->where('user_one', $user)
- ->orWhere('user_two', $user)
- ->offset($offset)
- ->take($take)
- ->orderBy('updated_at', $order)
- ->get();
-
- $threads = [];
-
- foreach ($msgThread as $thread) {
- $collection = (object)null;
- $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
- $collection->thread = $thread->messages->first();
- $collection->withUser = $conversationWith;
- $threads[] = $collection;
- }
-
- return collect($threads);
- }
-
- /*
- * retrieve all message thread with latest one message and sender and receiver user model
- *
- * @param int $user
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function threadsAll($user, $offset, $take)
- {
- $msgThread = Conversation::with(
- [
- 'messages' => function ($q) use ($user) {
- return $q->latest();
- }, 'userone', 'usertwo'
- ]
- )
- ->where('user_one', $user)->orWhere('user_two', $user)->offset($offset)->take($take)->get();
-
- $threads = [];
-
- foreach ($msgThread as $thread) {
- $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
- $message = $thread->messages->first();
- $message->user = $conversationWith;
- $threads[] = $message;
- }
-
- return collect($threads);
- }
-
- /*
- * get all conversations by given conversation id
- *
- * @param int $conversationId
- * @param int $userId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesById($conversationId, $userId, $offset, $take)
- {
- return Conversation::with(
- [
- 'messages' => function ($query) use ($userId, $offset, $take) {
- $query->where(
- function ($qr) use ($userId) {
- $qr->where('user_id', '=', $userId)
- ->where('deleted_from_sender', 0);
- }
- )
- ->orWhere(
- function ($q) use ($userId) {
- $q->where('user_id', '!=', $userId)
- ->where('deleted_from_receiver', 0);
- }
- );
-
- $query->offset($offset)->take($take);
-
- }
- ]
- )->with(['userone', 'usertwo'])->find($conversationId);
-
- }
-
- /*
- * get all conversations with soft deleted message by given conversation id
- *
- * @param int $conversationId
- * @param int $offset
- * @param int $take
- * @return collection
- * */
- public function getMessagesAllById($conversationId, $offset, $take)
- {
- return $this->with(
- [
- 'messages' => function ($q) use ($offset, $take) {
- return $q->offset($offset)
- ->take($take);
- }, 'userone', 'usertwo'
- ]
- )->find($conversationId);
- }
+ /*
+ * this method is default method for repository package
+ *
+ * @return \Nahid\Talk\Conersations\Conversation
+ * */
+ public function takeModel()
+ {
+ return Conversation::class;
+ }
+
+ /*
+ * check this given user is exists
+ *
+ * @param int $id
+ * @return bool
+ * */
+ public function existsById($id)
+ {
+ $conversation = $this->find($id);
+ if ($conversation) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given two users is already make a conversation
+ *
+ * @param int $user1
+ * @param int $user2
+ * @return int|bool
+ * */
+ public function isExistsAmongTwoUsers($user1, $user2)
+ {
+ $conversation = Conversation::where(
+ function ($query) use ($user1, $user2) {
+ $query->where(
+ function ($q) use ($user1, $user2) {
+ $q->where('user_one', $user1)
+ ->where('user_two', $user2);
+ }
+ )
+ ->orWhere(
+ function ($q) use ($user1, $user2) {
+ $q->where('user_one', $user2)
+ ->where('user_two', $user1);
+ }
+ );
+ }
+ );
+
+ if ($conversation->exists()) {
+ return $conversation->first()->id;
+ }
+
+ return false;
+ }
+
+ /*
+ * check this given user is involved with this given $conversation
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @return bool
+ * */
+ public function isUserExists($conversationId, $userId)
+ {
+ $exists = Conversation::where('id', $conversationId)
+ ->where(
+ function ($query) use ($userId) {
+ $query->where('user_one', $userId)->orWhere('user_two', $userId);
+ }
+ )
+ ->exists();
+
+ return $exists;
+ }
+
+ /*
+ * retrieve all message thread without soft deleted message with latest one message and
+ * sender and receiver user model
+ *
+ * @param int $user
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threads($user, $order, $offset, $take)
+ {
+ $conv = new Conversation();
+ $conv->authUser = $user;
+ $msgThread = $conv->with(
+ [
+ 'messages' => function ($q) use ($user) {
+ return $q->where(
+ function ($q) use ($user) {
+ $q->where('user_id', $user)
+ ->where('deleted_from_sender', 0);
+ }
+ )
+ ->orWhere(
+ function ($q) use ($user) {
+ $q->where('user_id', '!=', $user);
+ $q->where('deleted_from_receiver', 0);
+ }
+ )
+ ->latest();
+ }, 'messages.sender', 'userone', 'usertwo',
+ ]
+ )
+ ->where('user_one', $user)
+ ->orWhere('user_two', $user)
+ ->offset($offset)
+ ->take($take)
+ ->orderBy('updated_at', $order)
+ ->get();
+
+ $threads = [];
+
+ foreach ($msgThread as $thread) {
+ $collection = (object) null;
+ $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
+ $collection->thread = $thread->messages->first();
+ $collection->withUser = $conversationWith;
+ $threads[] = $collection;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * retrieve all message thread with latest one message and sender and receiver user model
+ *
+ * @param int $user
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function threadsAll($user, $offset, $take)
+ {
+ $msgThread = Conversation::with(
+ [
+ 'messages' => function ($q) use ($user) {
+ return $q->latest();
+ }, 'userone', 'usertwo',
+ ]
+ )
+ ->where('user_one', $user)->orWhere('user_two', $user)->offset($offset)->take($take)->get();
+
+ $threads = [];
+
+ foreach ($msgThread as $thread) {
+ $conversationWith = ($thread->userone->id == $user) ? $thread->usertwo : $thread->userone;
+ $message = $thread->messages->first();
+ $message->user = $conversationWith;
+ $threads[] = $message;
+ }
+
+ return collect($threads);
+ }
+
+ /*
+ * get all conversations by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $userId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesById($conversationId, $userId, $offset, $take)
+ {
+ return Conversation::with(
+ [
+ 'messages' => function ($query) use ($userId, $offset, $take) {
+ $query->where(
+ function ($qr) use ($userId) {
+ $qr->where('user_id', '=', $userId)
+ ->where('deleted_from_sender', 0);
+ }
+ )
+ ->orWhere(
+ function ($q) use ($userId) {
+ $q->where('user_id', '!=', $userId)
+ ->where('deleted_from_receiver', 0);
+ }
+ );
+
+ $query->offset($offset)->take($take);
+
+ },
+ ]
+ )->with(['userone', 'usertwo'])->find($conversationId);
+
+ }
+
+ /*
+ * get all conversations with soft deleted message by given conversation id
+ *
+ * @param int $conversationId
+ * @param int $offset
+ * @param int $take
+ * @return collection
+ * */
+ public function getMessagesAllById($conversationId, $offset, $take)
+ {
+ return $this->with(
+ [
+ 'messages' => function ($q) use ($offset, $take) {
+ return $q->offset($offset)
+ ->take($take);
+ }, 'userone', 'usertwo',
+ ]
+ )->find($conversationId);
+ }
}
diff --git a/src/Embera/Adapter.php b/src/Embera/Adapter.php
index f464595..f6afda5 100644
--- a/src/Embera/Adapter.php
+++ b/src/Embera/Adapter.php
@@ -8,7 +8,6 @@
namespace Nahid\Talk\Embera;
-
use Embera\Adapters\Service;
/**
@@ -18,35 +17,35 @@
*/
class Adapter extends Service
{
- protected $config;
+ protected $config;
- /**
- * Adapter constructor.
- *
- * @param string $url
- * @param array $config
- * @param $oembed
- */
- public function __construct($url, array $config = array(), $oembed)
- {
- $this->apiUrl = config('talk.oembed.url');
- parent::__construct($url, $config, $oembed);
- }
+ /**
+ * Adapter constructor.
+ *
+ * @param string $url
+ * @param array $config
+ * @param $oembed
+ */
+ public function __construct($url, array $config = array(), $oembed)
+ {
+ $this->apiUrl = config('talk.oembed.url');
+ parent::__construct($url, $config, $oembed);
+ }
- /**
- * Validates that the url belongs to this service.
- * Should be implemented on all children and should
- * return a boolean (preg_match returns 0 or 1 that
- * is why I'm also allowing 'int' as a return type).
- *
- * The current url is made available via $this->url
- *
- * @return bool|int
- */
- protected function validateUrl()
- {
- $parsedUrl = parse_url($this->apiUrl);
+ /**
+ * Validates that the url belongs to this service.
+ * Should be implemented on all children and should
+ * return a boolean (preg_match returns 0 or 1 that
+ * is why I'm also allowing 'int' as a return type).
+ *
+ * The current url is made available via $this->url
+ *
+ * @return bool|int
+ */
+ protected function validateUrl()
+ {
+ $parsedUrl = parse_url($this->apiUrl);
- return preg_match('#' . $parsedUrl['host'] . '#i', (string)$this->url);
- }
+ return preg_match('#' . $parsedUrl['host'] . '#i', (string) $this->url);
+ }
}
diff --git a/src/Facades/Talk.php b/src/Facades/Talk.php
index 86c531f..ec1394c 100644
--- a/src/Facades/Talk.php
+++ b/src/Facades/Talk.php
@@ -6,13 +6,13 @@
class Talk extends Facade
{
- /**
- * Get the registered name of the component.
- *
- * @return string
- */
- protected static function getFacadeAccessor()
- {
- return 'talk';
- }
+ /**
+ * Get the registered name of the component.
+ *
+ * @return string
+ */
+ protected static function getFacadeAccessor()
+ {
+ return 'talk';
+ }
}
diff --git a/src/Html/HtmlString.php b/src/Html/HtmlString.php
index 734b0f0..8630fbd 100644
--- a/src/Html/HtmlString.php
+++ b/src/Html/HtmlString.php
@@ -2,87 +2,86 @@
namespace Nahid\Talk\Html;
-
use Embera\Embera;
use Illuminate\Contracts\Support\Htmlable;
use Nahid\Talk\Embera\Adapter;
class HtmlString implements Htmlable
{
- /**
- * @var string
- */
- protected $string;
+ /**
+ * @var string
+ */
+ protected $string;
- /**
- * @var Embera
- */
- protected $driver;
+ /**
+ * @var Embera
+ */
+ protected $driver;
- /**
- * HtmlString constructor.
- *
- * @param $string
- */
- public function __construct($string, Embera $driver)
- {
- $this->setDriver($driver);
- $this->setString($string);
- }
+ /**
+ * HtmlString constructor.
+ *
+ * @param $string
+ */
+ public function __construct($string, Embera $driver)
+ {
+ $this->setDriver($driver);
+ $this->setString($string);
+ }
- /**
- * Get content as a string of HTML.
- *
- * @return string
- */
- public function toHtml()
- {
- if (config('talk.oembed.enabled')) {
- $parsedUrl = parse_url(config('talk.oembed.url'));
- $this->driver->addProvider(
- $parsedUrl['host'],
- Adapter::class,
- [
- 'api_key' => config('talk.oembed.key'),
- ]
- );
- }
+ /**
+ * Get content as a string of HTML.
+ *
+ * @return string
+ */
+ public function toHtml()
+ {
+ if (config('talk.oembed.enabled')) {
+ $parsedUrl = parse_url(config('talk.oembed.url'));
+ $this->driver->addProvider(
+ $parsedUrl['host'],
+ Adapter::class,
+ [
+ 'api_key' => config('talk.oembed.key'),
+ ]
+ );
+ }
- $result = $this->driver->autoEmbed($this->string);
+ $result = $this->driver->autoEmbed($this->string);
- return $result;
- }
+ return $result;
+ }
- /**
- * @return string
- */
- public function getString()
- {
- return $this->string;
- }
+ /**
+ * @return string
+ */
+ public function getString()
+ {
+ return $this->string;
+ }
- /**
- * @param string $string
- */
- public function setString($string)
- {
- $this->string = $string;
- }
+ /**
+ * @param string $string
+ */
+ public function setString($string)
+ {
+ $this->string = $string;
+ }
- /**
- * @return Embera
- */
- public function getDriver()
- {
- return $this->driver;
- }
+ /**
+ * @return Embera
+ */
+ public function getDriver()
+ {
+ return $this->driver;
+ }
- /**
- * @param Embera $driver
- */
- public function setDriver($driver)
- {
- $this->driver = $driver;
- }
+ /**
+ * @param Embera $driver
+ */
+ public function setDriver($driver)
+ {
+ $this->driver = $driver;
+ }
}
diff --git a/src/Html/HtmlStringInterface.php b/src/Html/HtmlStringInterface.php
index 01eb3cb..0ec8a69 100644
--- a/src/Html/HtmlStringInterface.php
+++ b/src/Html/HtmlStringInterface.php
@@ -8,13 +8,12 @@
namespace Nahid\Talk\Html;
-
use Illuminate\Contracts\Support\Htmlable;
interface HtmlStringInterface
{
- /**
- * @return Htmlable
- */
- public function toHtmlString();
+ /**
+ * @return Htmlable
+ */
+ public function toHtmlString();
}
diff --git a/src/Live/Broadcast.php b/src/Live/Broadcast.php
index a786cdd..9ef8b32 100644
--- a/src/Live/Broadcast.php
+++ b/src/Live/Broadcast.php
@@ -2,98 +2,97 @@
namespace Nahid\Talk\Live;
-use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Contracts\Config\Repository;
+use Illuminate\Foundation\Bus\DispatchesJobs;
use Nahid\Talk\Messages\Message;
use Pusher\Pusher;
class Broadcast
{
- use DispatchesJobs;
-
- /*
- * Constant for talk config prefix
- *
- * @const string
- * */
- const CONFIG_PATH = 'talk';
+ use DispatchesJobs;
- /*
- * Set all configs from talk configurations
- *
- * @var array
- * */
- protected $config;
+ /*
+ * Constant for talk config prefix
+ *
+ * @const string
+ * */
+ const CONFIG_PATH = 'talk';
+ /*
+ * Set all configs from talk configurations
+ *
+ * @var array
+ * */
+ protected $config;
- /*
- * Pusher instance
- *
- * @var object
- * */
- public $pusher;
+ /*
+ * Pusher instance
+ *
+ * @var object
+ * */
+ public $pusher;
- /**
- * Connect pusher and get all credentials from config.
- *
- * @param \Illuminate\Contracts\Config\Repository $config
- */
- public function __construct(Repository $config)
- {
- $this->config = $config;
- $this->pusher = $this->connectPusher();
- }
+ /**
+ * Connect pusher and get all credentials from config.
+ *
+ * @param \Illuminate\Contracts\Config\Repository $config
+ */
+ public function __construct(Repository $config)
+ {
+ $this->config = $config;
+ $this->pusher = $this->connectPusher();
+ }
- /**
- * Make pusher connection.
- *
- * @param array $options
- *
- * @return object | bool
- */
- protected function connectPusher($options = [])
- {
- if ($this->getConfig('broadcast.enable')) {
- $appId = $this->getConfig('broadcast.pusher.app_id');
- $appKey = $this->getConfig('broadcast.pusher.app_key');
- $appSecret = $this->getConfig('broadcast.pusher.app_secret');
- $appOptions = $this->getConfig('broadcast.pusher.options');
+ /**
+ * Make pusher connection.
+ *
+ * @param array $options
+ *
+ * @return object | bool
+ */
+ protected function connectPusher($options = [])
+ {
+ if ($this->getConfig('broadcast.enable')) {
+ $appId = $this->getConfig('broadcast.pusher.app_id');
+ $appKey = $this->getConfig('broadcast.pusher.app_key');
+ $appSecret = $this->getConfig('broadcast.pusher.app_secret');
+ $appOptions = $this->getConfig('broadcast.pusher.options');
- $newOptions = array_merge($appOptions, $options);
- $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
+ $newOptions = array_merge($appOptions, $options);
+ $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
- return $pusher;
- }
+ return $pusher;
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Dispatch the job to the queue.
- *
- * @param \Nahid\Talk\Messages\Message $message
- */
- public function transmission(Message $message)
- {
- if (!$this->pusher) {
- return false;
- }
+ /**
+ * Dispatch the job to the queue.
+ *
+ * @param \Nahid\Talk\Messages\Message $message
+ */
+ public function transmission(Message $message)
+ {
+ if (!$this->pusher) {
+ return false;
+ }
- $sender = $message->sender->toArray();
- $messageArray = $message->toArray();
- $messageArray['sender'] = $sender;
- $this->dispatch(new Webcast($messageArray));
- }
+ $sender = $message->sender->toArray();
+ $messageArray = $message->toArray();
+ $messageArray['sender'] = $sender;
+ $this->dispatch(new Webcast($messageArray));
+ }
- /**
- * get specific config from talk configurations.
- *
- * @param string
- *
- * @return string|array|int
- */
- public function getConfig($name)
- {
- return $this->config->get(self::CONFIG_PATH.'.'.$name);
- }
+ /**
+ * get specific config from talk configurations.
+ *
+ * @param string
+ *
+ * @return string|array|int
+ */
+ public function getConfig($name)
+ {
+ return $this->config->get(self::CONFIG_PATH . '.' . $name);
+ }
}
diff --git a/src/Live/Webcast.php b/src/Live/Webcast.php
index a7ffdce..2c3c0c7 100644
--- a/src/Live/Webcast.php
+++ b/src/Live/Webcast.php
@@ -3,51 +3,51 @@
namespace Nahid\Talk\Live;
use Illuminate\Bus\Queueable;
-use Illuminate\Queue\SerializesModels;
-use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Queue\SerializesModels;
class Webcast implements ShouldQueue
{
- use InteractsWithQueue, Queueable, SerializesModels;
-
- /*
- * Message Model Instance
- *
- * @var object
- * */
- protected $message;
-
- /*
- * Broadcast class instance
- *
- * @var object
- * */
- protected $broadcast;
-
- /**
- * Set message collections to the properties.
- */
- public function __construct($message)
- {
- $this->message = $message;
- }
-
- /*
- * Execute the job and broadcast to the pusher channels
- *
- * @param \Nahid\Talk\Live\Broadcast $broadcast
- * @return void
- */
- public function handle(Broadcast $broadcast)
- {
- $this->broadcast = $broadcast;
- $senderIdKey = $this->broadcast->getConfig('user.ownerKey') ? $this->broadcast->getConfig('user.ownerKey') : 'id';
- $toUser = ($this->message['sender'][ $senderIdKey] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
-
- $channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
- $channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];
-
- $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
- }
+ use InteractsWithQueue, Queueable, SerializesModels;
+
+ /*
+ * Message Model Instance
+ *
+ * @var object
+ * */
+ protected $message;
+
+ /*
+ * Broadcast class instance
+ *
+ * @var object
+ * */
+ protected $broadcast;
+
+ /**
+ * Set message collections to the properties.
+ */
+ public function __construct($message)
+ {
+ $this->message = $message;
+ }
+
+ /*
+ * Execute the job and broadcast to the pusher channels
+ *
+ * @param \Nahid\Talk\Live\Broadcast $broadcast
+ * @return void
+ */
+ public function handle(Broadcast $broadcast)
+ {
+ $this->broadcast = $broadcast;
+ $senderIdKey = $this->broadcast->getConfig('user.ownerKey') ? $this->broadcast->getConfig('user.ownerKey') : 'id';
+ $toUser = ($this->message['sender'][$senderIdKey] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
+
+ $channelForUser = $this->broadcast->getConfig('broadcast.app_name') . '-user-' . $toUser;
+ $channelForConversation = $this->broadcast->getConfig('broadcast.app_name') . '-conversation-' . $this->message['conversation_id'];
+
+ $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
+ }
}
diff --git a/src/Messages/MessageRepository.php b/src/Messages/MessageRepository.php
index 6b24026..fe88e7a 100644
--- a/src/Messages/MessageRepository.php
+++ b/src/Messages/MessageRepository.php
@@ -6,34 +6,34 @@
class MessageRepository extends Repository
{
- public function takeModel()
- {
- return Message::class;
- }
-
- public function deleteMessages($conversationId)
- {
- return (boolean) Message::where('conversation_id', $conversationId)->delete();
- }
-
- public function softDeleteMessage($messageId, $authUserId)
- {
- $message = $this->with(['conversation' => function ($q) use ($authUserId) {
- $q->where('user_one', $authUserId);
- $q->orWhere('user_two', $authUserId);
- }])->find($messageId);
-
- if (is_null($message->conversation)) {
- return false;
- }
-
- if ($message->user_id == $authUserId) {
- $message->deleted_from_sender = 1;
- } else {
- $message->deleted_from_receiver = 1;
- }
-
- return (boolean) $this->update($message);
-
- }
+ public function takeModel()
+ {
+ return Message::class;
+ }
+
+ public function deleteMessages($conversationId)
+ {
+ return (boolean) Message::where('conversation_id', $conversationId)->delete();
+ }
+
+ public function softDeleteMessage($messageId, $authUserId)
+ {
+ $message = $this->with(['conversation' => function ($q) use ($authUserId) {
+ $q->where('user_one', $authUserId);
+ $q->orWhere('user_two', $authUserId);
+ }])->find($messageId);
+
+ if (is_null($message->conversation)) {
+ return false;
+ }
+
+ if ($message->user_id == $authUserId) {
+ $message->deleted_from_sender = 1;
+ } else {
+ $message->deleted_from_receiver = 1;
+ }
+
+ return (boolean) $this->update($message);
+
+ }
}
diff --git a/src/Middleware/TalkMiddleware.php b/src/Middleware/TalkMiddleware.php
index 3fbc8da..f620e8e 100644
--- a/src/Middleware/TalkMiddleware.php
+++ b/src/Middleware/TalkMiddleware.php
@@ -8,20 +8,20 @@
class TalkMiddleware
{
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Illuminate\Http\Request $request
+ * @param \Closure $next
+ * @return mixed
+ */
- public function handle($request, Closure $next, $guard = null)
- {
- if (Auth::guard($guard)->check()) {
- Talk::setAuthUserId(Auth::guard($guard)->user()->id);
- }
+ public function handle($request, Closure $next, $guard = null)
+ {
+ if (Auth::guard($guard)->check()) {
+ Talk::setAuthUserId(Auth::guard($guard)->user()->id);
+ }
- return $next($request);
- }
+ return $next($request);
+ }
}
diff --git a/src/Tags/Tag.php b/src/Tags/Tag.php
index b614769..d166772 100644
--- a/src/Tags/Tag.php
+++ b/src/Tags/Tag.php
@@ -6,22 +6,22 @@
class Tag extends Model
{
- protected $table = 'tags';
- public $timestamps = true;
- public $fillable = [
- 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
- 'name',
- ];
+ protected $table = 'tags';
+ public $timestamps = true;
+ public $fillable = [
+ 'user_id', //the owner/creator of this tag. Very helpful when using with other packages like https://github.com/the-control-group/voyager
+ 'name',
+ ];
- /*
- * make a relation with conversations
- *
- * returns collection
- *
- * */
- public function conversations()
- {
- return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
- ->with('sender');
- }
+ /*
+ * make a relation with conversations
+ *
+ * returns collection
+ *
+ * */
+ public function conversations()
+ {
+ return $this->hasMany('Nahid\Talk\Messages\Conversations', 'conversation_id')
+ ->with('sender');
+ }
}
diff --git a/src/Talk.php b/src/Talk.php
index 8d0066f..8651366 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -18,559 +18,559 @@
class Talk
{
- /**
- * configurations instance.
- *
- * @var \Illuminate\Contracts\Config\Repository
- */
- protected $config;
-
- /**
- * The ConversationRepository class instance.
- *
- * @var \Nahid\Talk\Conversations\ConversationRepository
- */
- protected $conversation;
-
- /**
- * The MessageRepository class instance.
- *
- * @var \Nahid\Talk\Messages\MessageRepository
- */
- protected $message;
-
- /**
- * Broadcast class instance.
- *
- * @var \Nahid\Talk\Live\Broadcast
- */
- protected $broadcast;
-
- /**
- * Currently loggedin user id.
- *
- * @var int
- */
- protected $authUserId;
-
- /**
- * Initialize and instantiate conversation and message repositories.
- *
- * @param \Nahid\Talk\Conversations\ConversationRepository $conversation
- * @param \Nahid\Talk\Messages\MessageRepository $message
- */
- public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
- {
- $this->config = $config;
- $this->conversation = $conversation;
- $this->message = $message;
- $this->broadcast = $broadcast;
- }
-
- /**
- * make two users as serialize with ascending order.
- *
- * @param int $user1
- * @param int $user2
- *
- * @return array
- */
- protected function getSerializeUser($user1, $user2)
- {
- $user = [];
- $user['one'] = ($user1 < $user2) ? $user1 : $user2;
- $user['two'] = ($user1 < $user2) ? $user2 : $user1;
-
- return $user;
- }
-
- /**
- * create a new message by using conversationId.
- *
- * @param int $conversationId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message
- */
- protected function makeMessage($conversationId, $message)
- {
- $message = $this->message->create([
- 'message' => $message,
- 'conversation_id' => $conversationId,
- 'user_id' => $this->authUserId,
- 'is_seen' => 0,
- ]);
-
- $message->conversation->touch();
- $this->broadcast->transmission($message);
-
- return $message;
- }
-
- /*
- * Make new message collections to response with formatted data
- *
- *@param \Talk\Conversations\Conversation $conversations
- *@return object|bool
- */
- protected function makeMessageCollection($conversations)
- {
- if (!$conversations) {
- return false;
- }
-
- $collection = (object) null;
- if ($conversations->user_one == $this->authUserId || $conversations->user_two == $this->authUserId) {
- $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
- $collection->withUser = $withUser;
- $collection->messages = $conversations->messages;
-
- return $collection;
- }
-
- return false;
- }
-
- /**
- * make new conversation with the given receiverId with currently loggedin user.
- *
- * @param int $receiverId
- *
- * @return int
- */
- protected function newConversation($receiverId, $title, $tagName = null)
- {
- // $conversationId = $this->isConversationExists($receiverId);
- $user = $this->getSerializeUser($this->authUserId, $receiverId);
-
- // if ($conversationId === false) {
- $conversation = $this->conversation->create([
- 'user_one' => $user['one'],
- 'user_two' => $user['two'],
- 'title' => $title,
- 'status' => 1,
- ]);
-
- if ($conversation) {
- if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
- if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
- }
-
- $conversation->addTag($tag);
- }
-
- return $conversation->id;
- }
- // }
-
- return $conversationId;
- }
-
- /**
- * set currently authenticated user id for global usage.
- *
- * @param int $id
- *
- * @return int|bool
- */
- public function setAuthUserId($id = null)
- {
- if (!is_null($id)) {
- return $this->authUserId = $id;
- }
-
- return false;
- }
-
- /*
- * its set user id instantly when you fetch or access data. if you you haven't
- * set authenticated user id globally or you want to fetch work with
- * instant users information, you may use it
- *
- * @param int $id
- * @return \Nahid\Talk\Talk|bool
- * */
- public function user($id = null)
- {
- if ($this->setAuthUserId($id)) {
- return $this;
- }
-
- return false;
- }
-
- /**
- * make sure is this conversation exist for this user with currently loggedin user.
- *
- * @param int $userId
- *
- * @return bool|int
- */
- public function isConversationExists($userId)
- {
- if (empty($userId)) {
- return false;
- }
-
- $user = $this->getSerializeUser($this->authUserId, $userId);
-
- return $this->conversation->isExistsAmongTwoUsers($user['one'], $user['two']);
- }
-
- /**
- * check the given user exist for the given conversation.
- *
- * @param int $conversationId
- * @param int $userId
- *
- * @return bool
- */
- public function isAuthenticUser($conversationId, $userId)
- {
- if ($conversationId && $userId) {
- return $this->conversation->isUserExists($conversationId, $userId);
- }
-
- return false;
- }
-
- /**
- * send a message by using converstionid.
- *
- * @param int $conversationId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function sendMessage($conversatonId, $message)
- {
- if ($conversatonId && $message) {
- if ($this->conversation->existsById($conversatonId)) {
- $message = $this->makeMessage($conversatonId, $message);
-
- return $message;
- }
- }
-
- return false;
- }
-
- /**
- * create a new message by using receiverid.
- *
- * @param int $receiverId
- * @param string $message
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function sendMessageByUserId($receiverId, $message, $title, $tag = null)
- {
- if ($conversationId = $this->isConversationExists($receiverId)) {
- $con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
- if ($con->title == $title) {
- // dd("same: {$con->title} == $title");
- $message = $this->makeMessage($conversationId, $message);
- return $message;
- }
- }
-
- $convId = $this->newConversation($receiverId, $title);
- $message = $this->makeMessage($convId, $message);
-
- return $message;
- }
-
- /**
- * fetch all inbox for currently loggedin user with pagination.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function getInbox($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->conversation->threads($this->authUserId, $order, $offset, $take);
- }
-
- /**
- * fetch all inbox with soft deleted message for currently loggedin user with pagination.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function getInboxAll($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
- }
-
- /**
- * its a alias of getInbox method.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function threads($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->getInbox($order, $offset, $take);
- }
-
- /**
- * its a alias of getInboxAll method.
- *
- * @param int $offset
- * @param int $take
- *
- * @return array
- */
- public function threadsAll($order = 'desc', $offset = 0, $take = 20)
- {
- return $this->getInboxAll($order, $offset, $take);
- }
-
- /**
- * fetch all conversation by using coversation id.
- *
- * @param int $conversationId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function getConversationsById($conversationId, $offset = 0, $take = 20)
- {
- // dump($conversationId);
- // dd($this->authUserId);
- $conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
-
- return $this->makeMessageCollection($conversations);
- }
-
- /**
- * fetch all conversation with soft deleted messages by using coversation id.
- *
- * @param int $conversationId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message
- */
- public function getConversationsAllById($conversationId, $offset = 0, $take = 20)
- {
- $conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
-
- return $this->makeMessageCollection($conversations);
- }
-
- /**
- * create a new message by using sender id.
- *
- * @param int $senderId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getConversationsByUserId($senderId, $offset = 0, $take = 20)
- {
- $conversationId = $this->isConversationExists($senderId, $this->authUserId);
- if ($conversationId) {
- return $this->getConversationsById($conversationId, $offset, $take);
- }
-
- return false;
- }
-
- /**
- * create a new message by using sender id.
- *
- * @param int $senderId
- * @param int $offset = 0
- * @param int $take = 20
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
- {
- $conversationId = $this->isConversationExists($senderId, $this->authUserId);
- if ($conversationId) {
- return $this->getConversationsAllById($conversationId, $offset, $take);
- }
-
- return false;
- }
-
- /**
- * its an alias of getConversationById.
- *
- * @param int $conversationId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessages($conversationId, $offset = 0, $take = 20)
- {
- return $this->getConversationsById($conversationId, $offset, $take);
- }
-
- /**
- * its an alias of getConversationAllById.
- *
- * @param int $conversationId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesAll($conversationId, $offset = 0, $take = 20)
- {
- return $this->getConversationsAllById($conversationId, $offset, $take);
- }
-
- /**
- * its an alias by getConversationByUserId.
- *
- * @param int $senderId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesByUserId($userId, $offset = 0, $take = 20)
- {
- return $this->getConversationsByUserId($userId, $offset, $take);
- }
-
- /**
- * its an alias by getConversationAllByUserId.
- *
- * @param int $senderId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function getMessagesAllByUserId($userId, $offset = 0, $take = 20)
- {
- return $this->getConversationsAllByUserId($userId, $offset, $take);
- }
-
- /**
- * read a single message by message id.
- *
- * @param int $messageId
- *
- * @return \Nahid\Talk\Messages\Message|bool
- */
- public function readMessage($messageId = null)
- {
- if (!is_null($messageId)) {
- $message = $this->message->with(['sender', 'conversation'])->find($messageId);
-
- if ($message->coversation->user_one == $this->authUserId || $message->coversation->user_two == $this->authUserId) {
- return $message;
- }
- }
-
- return false;
- }
-
- /**
- * make a message as seen.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function makeSeen($messageId)
- {
- $seen = $this->message->update($messageId, ['is_seen' => 1]);
- if ($seen) {
- return true;
- }
-
- return false;
- }
-
- /**
- * get receiver information for this conversation.
- *
- * @param int $conversationId
- *
- * @return UserModel
- *
- * @deprecated since version 2.0.0 Remove it from version 2.0.2
- */
- public function getReceiverInfo($conversationId)
- {
- $conversation = $this->conversation->find($conversationId);
- $receiver = '';
- if ($conversation->user_one == $this->authUserId) {
- $receiver = $conversation->user_two;
- } else {
- $receiver = $conversation->user_one;
- }
-
- $userModel = $this->config('talk.user.model');
- $user = new $userModel();
-
- return $user->find($receiver);
- }
-
- /**
- * delete a specific message, its a softdelete process. All message stored in db.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function deleteMessage($messageId)
- {
- return $this->message->softDeleteMessage($messageId, $this->authUserId);
- }
-
- /**
- * permanently delete message for this id.
- *
- * @param int $messageId
- *
- * @return bool
- */
- public function deleteForever($messageId)
- {
- $deleteMessage = $this->message->delete($messageId);
- if ($deleteMessage) {
- return true;
- }
-
- return false;
- }
-
- /**
- * delete message threat or conversation by conversation id.
- *
- * @param int $id
- *
- * @return bool
- */
- public function deleteConversations($id)
- {
- $deleteConversation = $this->conversation->delete($id);
- if ($deleteConversation) {
- return $this->message->deleteMessages($id);
- }
-
- return false;
- }
-
- /**
- * its an alias of deleteConversations.
- *
- * @param int $id
- *
- * @return bool
- */
- public function deleteThread($id = null)
- {
- return $this->deleteConversations($id);
- }
+ /**
+ * configurations instance.
+ *
+ * @var \Illuminate\Contracts\Config\Repository
+ */
+ protected $config;
+
+ /**
+ * The ConversationRepository class instance.
+ *
+ * @var \Nahid\Talk\Conversations\ConversationRepository
+ */
+ protected $conversation;
+
+ /**
+ * The MessageRepository class instance.
+ *
+ * @var \Nahid\Talk\Messages\MessageRepository
+ */
+ protected $message;
+
+ /**
+ * Broadcast class instance.
+ *
+ * @var \Nahid\Talk\Live\Broadcast
+ */
+ protected $broadcast;
+
+ /**
+ * Currently loggedin user id.
+ *
+ * @var int
+ */
+ protected $authUserId;
+
+ /**
+ * Initialize and instantiate conversation and message repositories.
+ *
+ * @param \Nahid\Talk\Conversations\ConversationRepository $conversation
+ * @param \Nahid\Talk\Messages\MessageRepository $message
+ */
+ public function __construct(Repository $config, Broadcast $broadcast, ConversationRepository $conversation, MessageRepository $message)
+ {
+ $this->config = $config;
+ $this->conversation = $conversation;
+ $this->message = $message;
+ $this->broadcast = $broadcast;
+ }
+
+ /**
+ * make two users as serialize with ascending order.
+ *
+ * @param int $user1
+ * @param int $user2
+ *
+ * @return array
+ */
+ protected function getSerializeUser($user1, $user2)
+ {
+ $user = [];
+ $user['one'] = ($user1 < $user2) ? $user1 : $user2;
+ $user['two'] = ($user1 < $user2) ? $user2 : $user1;
+
+ return $user;
+ }
+
+ /**
+ * create a new message by using conversationId.
+ *
+ * @param int $conversationId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ protected function makeMessage($conversationId, $message)
+ {
+ $message = $this->message->create([
+ 'message' => $message,
+ 'conversation_id' => $conversationId,
+ 'user_id' => $this->authUserId,
+ 'is_seen' => 0,
+ ]);
+
+ $message->conversation->touch();
+ $this->broadcast->transmission($message);
+
+ return $message;
+ }
+
+ /*
+ * Make new message collections to response with formatted data
+ *
+ *@param \Talk\Conversations\Conversation $conversations
+ *@return object|bool
+ */
+ protected function makeMessageCollection($conversations)
+ {
+ if (!$conversations) {
+ return false;
+ }
+
+ $collection = (object) null;
+ if ($conversations->user_one == $this->authUserId || $conversations->user_two == $this->authUserId) {
+ $withUser = ($conversations->userone->id === $this->authUserId) ? $conversations->usertwo : $conversations->userone;
+ $collection->withUser = $withUser;
+ $collection->messages = $conversations->messages;
+
+ return $collection;
+ }
+
+ return false;
+ }
+
+ /**
+ * make new conversation with the given receiverId with currently loggedin user.
+ *
+ * @param int $receiverId
+ *
+ * @return int
+ */
+ protected function newConversation($receiverId, $title, $tagName = null)
+ {
+ // $conversationId = $this->isConversationExists($receiverId);
+ $user = $this->getSerializeUser($this->authUserId, $receiverId);
+
+ // if ($conversationId === false) {
+ $conversation = $this->conversation->create([
+ 'user_one' => $user['one'],
+ 'user_two' => $user['two'],
+ 'title' => $title,
+ 'status' => 1,
+ ]);
+
+ if ($conversation) {
+ if (!empty($tagName)) {
+ $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ if (is_null($tag)) {
+ $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
+ }
+
+ $conversation->addTag($tag);
+ }
+
+ return $conversation->id;
+ }
+ // }
+
+ return $conversationId;
+ }
+
+ /**
+ * set currently authenticated user id for global usage.
+ *
+ * @param int $id
+ *
+ * @return int|bool
+ */
+ public function setAuthUserId($id = null)
+ {
+ if (!is_null($id)) {
+ return $this->authUserId = $id;
+ }
+
+ return false;
+ }
+
+ /*
+ * its set user id instantly when you fetch or access data. if you you haven't
+ * set authenticated user id globally or you want to fetch work with
+ * instant users information, you may use it
+ *
+ * @param int $id
+ * @return \Nahid\Talk\Talk|bool
+ * */
+ public function user($id = null)
+ {
+ if ($this->setAuthUserId($id)) {
+ return $this;
+ }
+
+ return false;
+ }
+
+ /**
+ * make sure is this conversation exist for this user with currently loggedin user.
+ *
+ * @param int $userId
+ *
+ * @return bool|int
+ */
+ public function isConversationExists($userId)
+ {
+ if (empty($userId)) {
+ return false;
+ }
+
+ $user = $this->getSerializeUser($this->authUserId, $userId);
+
+ return $this->conversation->isExistsAmongTwoUsers($user['one'], $user['two']);
+ }
+
+ /**
+ * check the given user exist for the given conversation.
+ *
+ * @param int $conversationId
+ * @param int $userId
+ *
+ * @return bool
+ */
+ public function isAuthenticUser($conversationId, $userId)
+ {
+ if ($conversationId && $userId) {
+ return $this->conversation->isUserExists($conversationId, $userId);
+ }
+
+ return false;
+ }
+
+ /**
+ * send a message by using converstionid.
+ *
+ * @param int $conversationId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function sendMessage($conversatonId, $message)
+ {
+ if ($conversatonId && $message) {
+ if ($this->conversation->existsById($conversatonId)) {
+ $message = $this->makeMessage($conversatonId, $message);
+
+ return $message;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * create a new message by using receiverid.
+ *
+ * @param int $receiverId
+ * @param string $message
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function sendMessageByUserId($receiverId, $message, $title, $tag = null)
+ {
+ if ($conversationId = $this->isConversationExists($receiverId)) {
+ $con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
+ if ($con->title == $title) {
+ // dd("same: {$con->title} == $title");
+ $message = $this->makeMessage($conversationId, $message);
+ return $message;
+ }
+ }
+
+ $convId = $this->newConversation($receiverId, $title);
+ $message = $this->makeMessage($convId, $message);
+
+ return $message;
+ }
+
+ /**
+ * fetch all inbox for currently loggedin user with pagination.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function getInbox($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->conversation->threads($this->authUserId, $order, $offset, $take);
+ }
+
+ /**
+ * fetch all inbox with soft deleted message for currently loggedin user with pagination.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function getInboxAll($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->conversation->threadsAll($this->authUserId, $order, $offset, $take);
+ }
+
+ /**
+ * its a alias of getInbox method.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function threads($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->getInbox($order, $offset, $take);
+ }
+
+ /**
+ * its a alias of getInboxAll method.
+ *
+ * @param int $offset
+ * @param int $take
+ *
+ * @return array
+ */
+ public function threadsAll($order = 'desc', $offset = 0, $take = 20)
+ {
+ return $this->getInboxAll($order, $offset, $take);
+ }
+
+ /**
+ * fetch all conversation by using coversation id.
+ *
+ * @param int $conversationId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function getConversationsById($conversationId, $offset = 0, $take = 20)
+ {
+ // dump($conversationId);
+ // dd($this->authUserId);
+ $conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
+
+ return $this->makeMessageCollection($conversations);
+ }
+
+ /**
+ * fetch all conversation with soft deleted messages by using coversation id.
+ *
+ * @param int $conversationId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message
+ */
+ public function getConversationsAllById($conversationId, $offset = 0, $take = 20)
+ {
+ $conversations = $this->conversation->getMessagesAllById($conversationId, $offset, $take);
+
+ return $this->makeMessageCollection($conversations);
+ }
+
+ /**
+ * create a new message by using sender id.
+ *
+ * @param int $senderId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getConversationsByUserId($senderId, $offset = 0, $take = 20)
+ {
+ $conversationId = $this->isConversationExists($senderId, $this->authUserId);
+ if ($conversationId) {
+ return $this->getConversationsById($conversationId, $offset, $take);
+ }
+
+ return false;
+ }
+
+ /**
+ * create a new message by using sender id.
+ *
+ * @param int $senderId
+ * @param int $offset = 0
+ * @param int $take = 20
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
+ {
+ $conversationId = $this->isConversationExists($senderId, $this->authUserId);
+ if ($conversationId) {
+ return $this->getConversationsAllById($conversationId, $offset, $take);
+ }
+
+ return false;
+ }
+
+ /**
+ * its an alias of getConversationById.
+ *
+ * @param int $conversationId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessages($conversationId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsById($conversationId, $offset, $take);
+ }
+
+ /**
+ * its an alias of getConversationAllById.
+ *
+ * @param int $conversationId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesAll($conversationId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsAllById($conversationId, $offset, $take);
+ }
+
+ /**
+ * its an alias by getConversationByUserId.
+ *
+ * @param int $senderId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesByUserId($userId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsByUserId($userId, $offset, $take);
+ }
+
+ /**
+ * its an alias by getConversationAllByUserId.
+ *
+ * @param int $senderId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function getMessagesAllByUserId($userId, $offset = 0, $take = 20)
+ {
+ return $this->getConversationsAllByUserId($userId, $offset, $take);
+ }
+
+ /**
+ * read a single message by message id.
+ *
+ * @param int $messageId
+ *
+ * @return \Nahid\Talk\Messages\Message|bool
+ */
+ public function readMessage($messageId = null)
+ {
+ if (!is_null($messageId)) {
+ $message = $this->message->with(['sender', 'conversation'])->find($messageId);
+
+ if ($message->coversation->user_one == $this->authUserId || $message->coversation->user_two == $this->authUserId) {
+ return $message;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * make a message as seen.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function makeSeen($messageId)
+ {
+ $seen = $this->message->update($messageId, ['is_seen' => 1]);
+ if ($seen) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * get receiver information for this conversation.
+ *
+ * @param int $conversationId
+ *
+ * @return UserModel
+ *
+ * @deprecated since version 2.0.0 Remove it from version 2.0.2
+ */
+ public function getReceiverInfo($conversationId)
+ {
+ $conversation = $this->conversation->find($conversationId);
+ $receiver = '';
+ if ($conversation->user_one == $this->authUserId) {
+ $receiver = $conversation->user_two;
+ } else {
+ $receiver = $conversation->user_one;
+ }
+
+ $userModel = $this->config('talk.user.model');
+ $user = new $userModel();
+
+ return $user->find($receiver);
+ }
+
+ /**
+ * delete a specific message, its a softdelete process. All message stored in db.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function deleteMessage($messageId)
+ {
+ return $this->message->softDeleteMessage($messageId, $this->authUserId);
+ }
+
+ /**
+ * permanently delete message for this id.
+ *
+ * @param int $messageId
+ *
+ * @return bool
+ */
+ public function deleteForever($messageId)
+ {
+ $deleteMessage = $this->message->delete($messageId);
+ if ($deleteMessage) {
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * delete message threat or conversation by conversation id.
+ *
+ * @param int $id
+ *
+ * @return bool
+ */
+ public function deleteConversations($id)
+ {
+ $deleteConversation = $this->conversation->delete($id);
+ if ($deleteConversation) {
+ return $this->message->deleteMessages($id);
+ }
+
+ return false;
+ }
+
+ /**
+ * its an alias of deleteConversations.
+ *
+ * @param int $id
+ *
+ * @return bool
+ */
+ public function deleteThread($id = null)
+ {
+ return $this->deleteConversations($id);
+ }
}
diff --git a/src/TalkServiceProvider.php b/src/TalkServiceProvider.php
index cd8048b..97fc42a 100644
--- a/src/TalkServiceProvider.php
+++ b/src/TalkServiceProvider.php
@@ -11,83 +11,83 @@
class TalkServiceProvider extends ServiceProvider
{
- /**
- * Bootstrap the application services.
- */
- public function boot()
- {
- $this->setupConfig();
- $this->setupMigrations();
- $this->loadViewsFrom(__DIR__.'/views', 'talk');
- }
- /**
- * Register the application services.
- */
- public function register()
- {
- $this->registerBroadcast();
- $this->registerTalk();
- }
- /**
- * Setup the config.
- */
- protected function setupConfig()
- {
- $source = realpath(__DIR__.'/../config/talk.php');
- // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
- if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
- $this->publishes([$source => config_path('talk.php')]);
- }
-
- if ($this->app instanceof LumenApplication) {
- $this->app->configure('talk');
- }
-
- $this->mergeConfigFrom($source, 'talk');
- }
- /**
- * Publish migrations files.
- */
- protected function setupMigrations()
- {
- $this->publishes([
- realpath(__DIR__.'/../database/migrations/') => database_path('migrations'),
- ], 'migrations');
- }
- /**
- * Register Talk class.
- */
- protected function registerTalk()
- {
- $this->app->singleton('talk', function (Container $app) {
- return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
- });
+ /**
+ * Bootstrap the application services.
+ */
+ public function boot()
+ {
+ $this->setupConfig();
+ $this->setupMigrations();
+ $this->loadViewsFrom(__DIR__ . '/views', 'talk');
+ }
+ /**
+ * Register the application services.
+ */
+ public function register()
+ {
+ $this->registerBroadcast();
+ $this->registerTalk();
+ }
+ /**
+ * Setup the config.
+ */
+ protected function setupConfig()
+ {
+ $source = realpath(__DIR__ . '/../config/talk.php');
+ // Check if the application is a Laravel OR Lumen instance to properly merge the configuration file.
+ if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
+ $this->publishes([$source => config_path('talk.php')]);
+ }
- $this->app->alias('talk', Talk::class);
- }
+ if ($this->app instanceof LumenApplication) {
+ $this->app->configure('talk');
+ }
- /**
- * Register Talk class.
- */
- protected function registerBroadcast()
- {
- $this->app->singleton('talk.broadcast', function (Container $app) {
- return new Live\Broadcast($app['config']);
- });
+ $this->mergeConfigFrom($source, 'talk');
+ }
+ /**
+ * Publish migrations files.
+ */
+ protected function setupMigrations()
+ {
+ $this->publishes([
+ realpath(__DIR__ . '/../database/migrations/') => database_path('migrations'),
+ ], 'migrations');
+ }
+ /**
+ * Register Talk class.
+ */
+ protected function registerTalk()
+ {
+ $this->app->singleton('talk', function (Container $app) {
+ return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
+ });
- $this->app->alias('talk.broadcast', Live\Broadcast::class);
- }
+ $this->app->alias('talk', Talk::class);
+ }
- /**
- * Get the services provided by the provider.
- *
- * @return string[]
- */
- public function provides()
- {
- return [
- 'talk',
- 'talk.broadcast',
- ];
- }
+ /**
+ * Register Talk class.
+ */
+ protected function registerBroadcast()
+ {
+ $this->app->singleton('talk.broadcast', function (Container $app) {
+ return new Live\Broadcast($app['config']);
+ });
+
+ $this->app->alias('talk.broadcast', Live\Broadcast::class);
+ }
+
+ /**
+ * Get the services provided by the provider.
+ *
+ * @return string[]
+ */
+ public function provides()
+ {
+ return [
+ 'talk',
+ 'talk.broadcast',
+ ];
+ }
}
diff --git a/src/helpers/talk.php b/src/helpers/talk.php
index f2a9738..93399ef 100644
--- a/src/helpers/talk.php
+++ b/src/helpers/talk.php
@@ -6,19 +6,19 @@
* Time: 4:58 PM.
*/
if (!function_exists('talk_live')) {
- function talk_live($options)
- {
- $talk__appKey = config('talk.broadcast.pusher.app_key');
- $talk__appName = config('talk.broadcast.app_name');
- $talk__options = json_encode(config('talk.broadcast.pusher.options'));
+ function talk_live($options)
+ {
+ $talk__appKey = config('talk.broadcast.pusher.app_key');
+ $talk__appName = config('talk.broadcast.app_name');
+ $talk__options = json_encode(config('talk.broadcast.pusher.options'));
- $talk_user_channel = isset($options['user']['id']) ? $talk__appName.'-user-'.$options['user']['id'] : '';
- $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName.'-conversation-'.$options['conversation']['id'] : '';
- $talk__userChannel['name'] = sha1($talk_user_channel);
- $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
- $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
- $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
+ $talk_user_channel = isset($options['user']['id']) ? $talk__appName . '-user-' . $options['user']['id'] : '';
+ $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName . '-conversation-' . $options['conversation']['id'] : '';
+ $talk__userChannel['name'] = sha1($talk_user_channel);
+ $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
+ $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
+ $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
- return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
- }
+ return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
+ }
}
From eb5d39d151401c049541086ac21e6e3b015e3436 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Tue, 5 Mar 2019 13:31:40 +0100
Subject: [PATCH 23/52] Renamed migration class appropriately
(https://github.com/nahid/talk/pull/135#discussion_r225612832)
---
.../2015_10_05_110622_create_conversation_tag_table.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/database/migrations/2015_10_05_110622_create_conversation_tag_table.php b/database/migrations/2015_10_05_110622_create_conversation_tag_table.php
index 0a08ddd..bdc760e 100644
--- a/database/migrations/2015_10_05_110622_create_conversation_tag_table.php
+++ b/database/migrations/2015_10_05_110622_create_conversation_tag_table.php
@@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
-class CreateConversationsTable extends Migration
+class CreateConversationTagsTable extends Migration
{
/**
* Run the migrations.
From a222dd5e43c255dc6b5aefe8ad7b99636545b745 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Thu, 7 Mar 2019 21:37:16 +0100
Subject: [PATCH 24/52] fix 'Tags\Tag' namespace
---
src/Tags/Tag.php | 2 +-
src/Talk.php | 1 +
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/src/Tags/Tag.php b/src/Tags/Tag.php
index 5ccab62..d166772 100644
--- a/src/Tags/Tag.php
+++ b/src/Tags/Tag.php
@@ -1,6 +1,6 @@
Date: Thu, 7 Mar 2019 20:38:12 +0000
Subject: [PATCH 25/52] Initial WhiteSource configuration file
---
.whitesource | 8 ++++++++
1 file changed, 8 insertions(+)
create mode 100644 .whitesource
diff --git a/.whitesource b/.whitesource
new file mode 100644
index 0000000..f340c5d
--- /dev/null
+++ b/.whitesource
@@ -0,0 +1,8 @@
+##########################################################
+#### WhiteSource Integration configuration file ####
+##########################################################
+
+# Configuration #
+#---------------#
+ws.repo.scan=true
+vulnerable.check.run.conclusion.level=failure
From ebc2d1f717fcd31dab8d96056e6801b7b2d54375 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Wed, 26 Jun 2019 22:14:11 +0100
Subject: [PATCH 26/52] fixed name of migration file to match migration class
name
https://github.com/nahid/talk/pull/135#pullrequestreview-165251122
---
...e.php => 2015_10_05_110622_create_conversation_tags_table.php} | 0
1 file changed, 0 insertions(+), 0 deletions(-)
rename database/migrations/{2015_10_05_110622_create_conversation_tag_table.php => 2015_10_05_110622_create_conversation_tags_table.php} (100%)
diff --git a/database/migrations/2015_10_05_110622_create_conversation_tag_table.php b/database/migrations/2015_10_05_110622_create_conversation_tags_table.php
similarity index 100%
rename from database/migrations/2015_10_05_110622_create_conversation_tag_table.php
rename to database/migrations/2015_10_05_110622_create_conversation_tags_table.php
From 099448722afbe29bf65362ff445b9c37c42918d0 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 09:13:10 +0100
Subject: [PATCH 27/52] refactors and minor code cleanups
---
src/Talk.php | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/src/Talk.php b/src/Talk.php
index 46ccffe..ee418e1 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -14,19 +14,21 @@
use Illuminate\Contracts\Config\Repository;
use Nahid\Talk\Conversations\ConversationRepository;
use Nahid\Talk\Live\Broadcast;
-use Nahid\Talk\Tag;
use Nahid\Talk\Messages\MessageRepository;
+use Nahid\Talk\Tag;
class Talk
{
/**
- * Now users can attach special importance to conversations by star-ing them.
- * This is the name of the special 'star' tag
- * special tags do not have creator. i.e. their user_id is null. Useful to ensure when getting tags for a user, special tags are not retuened
+ * With this constant, conversations can be marked as being specially important by "star-ing" them.
+ * This is powered by this new, special tag name constant.
+ * Special tags do not have creator. i.e. their user_id is null. This is useful because it ensures that
+ * special tags are not returned when getting the tags for any user
*
+ * @var string
*/
- const STARTAG = "talk_special_tag_star";
+ const STAR_TAG = "talk_special_tag_star";
/**
* configurations instance.
@@ -477,7 +479,7 @@ public function getConversationsAllByUserId($senderId, $offset = 0, $take = 20)
public function getUserTags()
{
return Tags\Tag::where(['user_id' => $this->authUserId])
- ->where('name', '!=', Talk::STARTAG)
+ ->where('name', '!=', Talk::STAR_TAG)
->get();
}
@@ -518,7 +520,7 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
if (!empty($tagName)) {
//treat star tag specially
$tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
- if ($tagName == \Nahid\Talk\Talk::STARTAG || $specialTagOnlyOne) {
+ if ($tagName == \Nahid\Talk\Talk::STAR_TAG || $specialTagOnlyOne) {
//at any time, we want to always have only one star tag, irrespective of who created it
//Therefore, this will ensure that we have only one star tag in our db table
$tag = Tags\Tag::where(['name' => $tagName])->first();
@@ -526,7 +528,7 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
if (is_null($tag)) {
//special tags dn't have owners
- if ($tagName == \Nahid\Talk\Talk::STARTAG || $specialTagOnlyOne) {
+ if ($tagName == \Nahid\Talk\Talk::STAR_TAG || $specialTagOnlyOne) {
$tag = Tags\Tag::create([
'name' => $tagName,
'is_special_tag' => 1,
From 0d742f8f9c41c4f888d5fb5951ab3996c1605d04 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 09:29:44 +0100
Subject: [PATCH 28/52] some bug fixes
---
src/Talk.php | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/Talk.php b/src/Talk.php
index ee418e1..a23fce7 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -179,9 +179,9 @@ protected function newConversation($receiverId, $title, $tagName = null)
if ($conversation) {
if (!empty($tagName)) {
- $tag = Tags\Tag::where(['user_id' => $authUserId, 'name' => $tagName])->first();
+ $tag = Tags\Tag::where(['user_id' => $this->authUserId, 'name' => $tagName])->first();
if (is_null($tag)) {
- $tag = Tags\Tag::create(['user_id' => $authUserId, 'name' => $tagName]);
+ $tag = Tags\Tag::create(['user_id' => $this->authUserId, 'name' => $tagName]);
}
$conversation->addTag($tag);
@@ -677,7 +677,7 @@ public function markRead($messageId)
if ($message->sender->id != $this->authUserId) {
$read = $this->message->update($messageId, ['is_read' => 1]);
if (!$read) {
- return falsse;
+ return false;
}
}
@@ -702,8 +702,8 @@ public function getUnreadMessagesInConversation($conversationId)
if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
$unread = [];
- $unread = collect($this->conversation->messages)->filter(function ($message) use ($authUserId) {
- return (($message->sender->id != $authUserId) && ($message->is_read != 1));
+ $unread = collect($this->conversation->messages)->filter(function ($message) {
+ return (($message->sender->id != $this->authUserId) && ($message->is_read != 1));
});
return $unread;
From f2c5867a3a9615009185c8d1781288fedb53dca9 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 09:42:32 +0100
Subject: [PATCH 29/52] more refactors and minor code cleanups
---
.vscode/settings.json | 2 ++
src/Talk.php | 60 +++++++++++++++++++------------------------
2 files changed, 29 insertions(+), 33 deletions(-)
create mode 100644 .vscode/settings.json
diff --git a/.vscode/settings.json b/.vscode/settings.json
new file mode 100644
index 0000000..7a73a41
--- /dev/null
+++ b/.vscode/settings.json
@@ -0,0 +1,2 @@
+{
+}
\ No newline at end of file
diff --git a/src/Talk.php b/src/Talk.php
index a23fce7..408109c 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -143,9 +143,9 @@ protected function makeMessageCollection($conversations)
$collection->messages = $conversations->messages;
//mark them as read
- foreach ($collection->messages as $mssg) {
- if ($mssg->sender->id != $this->authUserId) {
- if (!Talk::user($this->authUserId)->markRead($mssg->id)) {
+ foreach ($collection->messages as $message) {
+ if ($message->sender->id != $this->authUserId) {
+ if (!Talk::user($this->authUserId)->markRead($message->id)) {
return false;
}
}
@@ -191,7 +191,7 @@ protected function newConversation($receiverId, $title, $tagName = null)
}
// }
- return $conversationId;
+ return $conversation->id;
}
/**
@@ -263,18 +263,18 @@ public function isAuthenticUser($conversationId, $userId)
}
/**
- * send a message by using converstionid.
+ * send a message by using conversationId.
*
* @param int $conversationId
* @param string $message
*
* @return \Nahid\Talk\Messages\Message|bool
*/
- public function sendMessage($conversatonId, $message)
+ public function sendMessage($conversationId, $message)
{
- if ($conversatonId && $message) {
- if ($this->conversation->existsById($conversatonId)) {
- $message = $this->makeMessage($conversatonId, $message);
+ if ($conversationId && $message) {
+ if ($this->conversation->existsById($conversationId)) {
+ $message = $this->makeMessage($conversationId, $message);
return $message;
}
@@ -284,7 +284,7 @@ public function sendMessage($conversatonId, $message)
}
/**
- * create a new message by using receiverid.
+ * create a new message by using receiverId.
*
* @param int $receiverId
* @param string $message
@@ -296,14 +296,13 @@ public function sendMessageByUserId($receiverId, $message, $title = null, $tag =
if ($conversationId = $this->isConversationExists($receiverId)) {
$con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
if ($con->title == $title) {
- // dd("same: {$con->title} == $title");
$message = $this->makeMessage($conversationId, $message);
return $message;
}
}
- $convId = $this->newConversation($receiverId, $title);
- $message = $this->makeMessage($convId, $message);
+ $conversationId = $this->newConversation($receiverId, $title);
+ $message = $this->makeMessage($conversationId, $message);
return $message;
}
@@ -371,8 +370,6 @@ public function threadsAll($order = 'desc', $offset = 0, $take = 20)
*/
public function getConversationsById($conversationId, $offset = 0, $take = 20)
{
- // dump($conversationId);
- // dd($this->authUserId);
$conversations = $this->conversation->getMessagesById($conversationId, $this->authUserId, $offset, $take);
return $this->makeMessageCollection($conversations);
}
@@ -390,7 +387,6 @@ public function getConversationsByTagId($tag_id)
$conversations_ = $this->conversation->getMessagesByTagId($tag_id, $this->authUserId);
$user_id = $this->authUserId;
$conversations = collect($conversations_)->filter(function ($item) use ($tag_id) {
- // dd($item);
return ($item->tags->pluck('id')->contains($tag_id));
});
@@ -408,8 +404,6 @@ public function getConversationsByTagId($tag_id)
->where('user_id', '!=', $user_id)
->where('is_read', '=', '0');
})->get();
- // dump($conversation->id);
- // dump($collection->unreadmessages);
$collection->withUser = $conversationWith;
$threads[] = $collection;
}
@@ -484,7 +478,7 @@ public function getUserTags()
}
/**
- * creares tag for user
+ * creates tag for user
*
* @param string $tagName
*
@@ -527,7 +521,7 @@ public function addTagToConversation($conversationId, $tagName, bool $specialTag
}
if (is_null($tag)) {
- //special tags dn't have owners
+ //special tags do not have owners
if ($tagName == \Nahid\Talk\Talk::STAR_TAG || $specialTagOnlyOne) {
$tag = Tags\Tag::create([
'name' => $tagName,
@@ -702,7 +696,7 @@ public function getUnreadMessagesInConversation($conversationId)
if ($message->conversation->user_one == $this->authUserId || $message->conversation->user_two == $this->authUserId) {
$unread = [];
- $unread = collect($this->conversation->messages)->filter(function ($message) {
+ $unread = collect($this->conversation->messages)->filter(function ($message) {
return (($message->sender->id != $this->authUserId) && ($message->is_read != 1));
});
@@ -718,9 +712,9 @@ public function getUnreadMessagesInConversation($conversationId)
*
*
* @param int $removeSpecialMessages : allows to use Talk also to send system notifications
- * to users by allowing some conversations to be "tagged" as "special".
- * As a rule of thumb, any conversation that is not "normal" message/converation should simply be tagged as special, so
- * that it is easy to get unread messages..etcwitout confusing message contexts
+ * to users by allowing some conversations to be tagged as "special".
+ * As a rule of thumb, any conversation that is not "normal" message/conversation should simply be tagged as special, so
+ * that it is easy to get unread messages without confusing message contexts
*
* @return collection
*/
@@ -728,8 +722,8 @@ public function getAllUnreadMessages($removeSpecialMessages = false)
{
$messages = collect();
$user_id = $this->authUserId;
- $conv = new \Nahid\Talk\Conversations\Conversation();
- $conversations = $conv->with(
+ $conversation = new \Nahid\Talk\Conversations\Conversation();
+ $conversations = $conversation->with(
[
'messages' => function ($query) use ($user_id) {
$query
@@ -760,7 +754,7 @@ public function getAllUnreadMessages($removeSpecialMessages = false)
}
/**
- * gets all latest messages sent to auth'ed user
+ * gets all latest messages sent to the authenticated user
*
* @param int $conversationId
*
@@ -770,10 +764,10 @@ public function getLatestMessages()
{
if ($this->latestMessages == null) {
- $messages = collect();
- $user_id = $this->authUserId;
- $conv = new \Nahid\Talk\Conversations\Conversation();
- $msgThread = $conv->with(['messages' => function ($query) use ($user_id) {
+ $messages = collect();
+ $user_id = $this->authUserId;
+ $conversation = new \Nahid\Talk\Conversations\Conversation();
+ $msgThread = $conversation->with(['messages' => function ($query) use ($user_id) {
$query->where('user_id', '!=', $user_id)->with(['conversation']);
}])
->where('user_one', $user_id)
@@ -795,7 +789,7 @@ public function getLatestMessages()
/**
* gets the count of all messages not yet read in all conversations altogether
*
- * @param int $removeSpecialMessages whether to remove special tag messages: This allows for Nahid to be useful for sending custom system notifcation messages to users
+ * @param int $removeSpecialMessages whether to remove special tag messages: This allows for Nahid to be useful for sending custom system notification messages to users
*
* @return int
*/
@@ -830,7 +824,7 @@ public function getReceiverInfo($conversationId)
}
/**
- * delete a specific message, its a softdelete process. All message stored in db.
+ * delete a specific message, it is a softDelete process. All message stored in db.
*
* @param int $messageId
*
From 53394aff18a71eab1ae80c53dd7118263ab79ef1 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 09:56:56 +0100
Subject: [PATCH 30/52] fix migration files for messages table and minor
changes
---
...8_095235_add_is_read_to_messages_table.php | 32 +++++++++++++++++++
src/Talk.php | 2 +-
2 files changed, 33 insertions(+), 1 deletion(-)
create mode 100644 database/migrations/2020_03_28_095235_add_is_read_to_messages_table.php
diff --git a/database/migrations/2020_03_28_095235_add_is_read_to_messages_table.php b/database/migrations/2020_03_28_095235_add_is_read_to_messages_table.php
new file mode 100644
index 0000000..15e28ba
--- /dev/null
+++ b/database/migrations/2020_03_28_095235_add_is_read_to_messages_table.php
@@ -0,0 +1,32 @@
+boolean('is_read')->default(0);
+ });
+ }
+
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ Schema::table('messages', function (Blueprint $table) {
+ $table->dropColumn('is_read');
+ });
+ }
+}
diff --git a/src/Talk.php b/src/Talk.php
index 408109c..77be9a2 100644
--- a/src/Talk.php
+++ b/src/Talk.php
@@ -504,7 +504,7 @@ public function createTagForUser($tagName)
*
* @param int $conversationId
* @param string $tagName
- * @param bool $specialTagOnlyOne indicates if only one tag should b maintained, thus supporting use of custom "system tags" e.g. for notifications
+ * @param bool $specialTagOnlyOne indicates if only one tag should be maintained, thus supporting use of custom "system tags" e.g. for notifications
*
* @return bool
*/
From 527cb5b517e52c9a4913753208163c335249fc81 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 10:01:28 +0100
Subject: [PATCH 31/52] fix src/Embera/Adapter.php according to
https://github.com/nahid/talk/pull/135#discussion_r290658470
---
src/Embera/Adapter.php | 37 -------------------------------------
1 file changed, 37 deletions(-)
diff --git a/src/Embera/Adapter.php b/src/Embera/Adapter.php
index e4fac12..f464595 100644
--- a/src/Embera/Adapter.php
+++ b/src/Embera/Adapter.php
@@ -8,10 +8,7 @@
namespace Nahid\Talk\Embera;
-<<<<<<< HEAD
-=======
->>>>>>> remotes/upstream/master
use Embera\Adapters\Service;
/**
@@ -21,39 +18,6 @@
*/
class Adapter extends Service
{
-<<<<<<< HEAD
- protected $config;
-
- /**
- * Adapter constructor.
- *
- * @param string $url
- * @param array $config
- * @param $oembed
- */
- public function __construct($url, array $config = array(), $oembed)
- {
- $this->apiUrl = config('talk.oembed.url');
- parent::__construct($url, $config, $oembed);
- }
-
- /**
- * Validates that the url belongs to this service.
- * Should be implemented on all children and should
- * return a boolean (preg_match returns 0 or 1 that
- * is why I'm also allowing 'int' as a return type).
- *
- * The current url is made available via $this->url
- *
- * @return bool|int
- */
- protected function validateUrl()
- {
- $parsedUrl = parse_url($this->apiUrl);
-
- return preg_match('#' . $parsedUrl['host'] . '#i', (string) $this->url);
- }
-=======
protected $config;
/**
@@ -85,5 +49,4 @@ protected function validateUrl()
return preg_match('#' . $parsedUrl['host'] . '#i', (string)$this->url);
}
->>>>>>> remotes/upstream/master
}
From 67a816de9378eedf4cf79a5cae263ac27ba4669c Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 10:40:56 +0100
Subject: [PATCH 32/52] fix migration errors
---
database/migrations/2015_10_05_110608_create_messages_table.php | 1 -
.../migrations/2015_10_05_110622_create_conversations_table.php | 2 +-
2 files changed, 1 insertion(+), 2 deletions(-)
diff --git a/database/migrations/2015_10_05_110608_create_messages_table.php b/database/migrations/2015_10_05_110608_create_messages_table.php
index fd867e2..fd75886 100644
--- a/database/migrations/2015_10_05_110608_create_messages_table.php
+++ b/database/migrations/2015_10_05_110608_create_messages_table.php
@@ -17,7 +17,6 @@ public function up()
$table->increments('id');
$table->text('message');
$table->boolean('is_seen')->default(0);
- $table->boolean('is_read')->default(0);
$table->boolean('deleted_from_sender')->default(0);
$table->boolean('deleted_from_receiver')->default(0);
$table->integer('user_id');
diff --git a/database/migrations/2015_10_05_110622_create_conversations_table.php b/database/migrations/2015_10_05_110622_create_conversations_table.php
index c69e792..c4c19e6 100644
--- a/database/migrations/2015_10_05_110622_create_conversations_table.php
+++ b/database/migrations/2015_10_05_110622_create_conversations_table.php
@@ -4,7 +4,7 @@
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
-class CreateConversationTagsTable extends Migration
+class CreateConversationsTable extends Migration
{
/**
* Run the migrations.
From b2b09f1631c965e1813ba81d57773aa9975dd031 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 11:04:03 +0100
Subject: [PATCH 33/52] remove formats done to non-concerned filed
---
src/Conversations/ConversationRepository.php | 2 +-
src/Facades/Talk.php | 18 +--
src/Html/HtmlString.php | 1 +
src/Html/HtmlStringInterface.php | 1 +
src/Live/Broadcast.php | 153 ++++++++++---------
src/Live/Webcast.php | 86 +++++------
src/Messages/MessageRepository.php | 2 +-
src/Middleware/TalkMiddleware.php | 28 ++--
src/TalkServiceProvider.php | 76 ++++-----
src/helpers/talk.php | 26 ++--
10 files changed, 186 insertions(+), 207 deletions(-)
diff --git a/src/Conversations/ConversationRepository.php b/src/Conversations/ConversationRepository.php
index dc35a65..6ba9531 100644
--- a/src/Conversations/ConversationRepository.php
+++ b/src/Conversations/ConversationRepository.php
@@ -9,7 +9,7 @@ class ConversationRepository extends Repository
/*
* this method is default method for repository package
*
- * @return \Nahid\Talk\Conersations\Conversation
+ * @return \Nahid\Talk\Conversations\Conversation
* */
public function takeModel()
{
diff --git a/src/Facades/Talk.php b/src/Facades/Talk.php
index ec1394c..86c531f 100644
--- a/src/Facades/Talk.php
+++ b/src/Facades/Talk.php
@@ -6,13 +6,13 @@
class Talk extends Facade
{
- /**
- * Get the registered name of the component.
- *
- * @return string
- */
- protected static function getFacadeAccessor()
- {
- return 'talk';
- }
+ /**
+ * Get the registered name of the component.
+ *
+ * @return string
+ */
+ protected static function getFacadeAccessor()
+ {
+ return 'talk';
+ }
}
diff --git a/src/Html/HtmlString.php b/src/Html/HtmlString.php
index 3e59874..734b0f0 100644
--- a/src/Html/HtmlString.php
+++ b/src/Html/HtmlString.php
@@ -2,6 +2,7 @@
namespace Nahid\Talk\Html;
+
use Embera\Embera;
use Illuminate\Contracts\Support\Htmlable;
use Nahid\Talk\Embera\Adapter;
diff --git a/src/Html/HtmlStringInterface.php b/src/Html/HtmlStringInterface.php
index 9d1a668..01eb3cb 100644
--- a/src/Html/HtmlStringInterface.php
+++ b/src/Html/HtmlStringInterface.php
@@ -8,6 +8,7 @@
namespace Nahid\Talk\Html;
+
use Illuminate\Contracts\Support\Htmlable;
interface HtmlStringInterface
diff --git a/src/Live/Broadcast.php b/src/Live/Broadcast.php
index 9ef8b32..a786cdd 100644
--- a/src/Live/Broadcast.php
+++ b/src/Live/Broadcast.php
@@ -2,97 +2,98 @@
namespace Nahid\Talk\Live;
-use Illuminate\Contracts\Config\Repository;
use Illuminate\Foundation\Bus\DispatchesJobs;
+use Illuminate\Contracts\Config\Repository;
use Nahid\Talk\Messages\Message;
use Pusher\Pusher;
class Broadcast
{
- use DispatchesJobs;
+ use DispatchesJobs;
+
+ /*
+ * Constant for talk config prefix
+ *
+ * @const string
+ * */
+ const CONFIG_PATH = 'talk';
- /*
- * Constant for talk config prefix
- *
- * @const string
- * */
- const CONFIG_PATH = 'talk';
+ /*
+ * Set all configs from talk configurations
+ *
+ * @var array
+ * */
+ protected $config;
- /*
- * Set all configs from talk configurations
- *
- * @var array
- * */
- protected $config;
- /*
- * Pusher instance
- *
- * @var object
- * */
- public $pusher;
+ /*
+ * Pusher instance
+ *
+ * @var object
+ * */
+ public $pusher;
- /**
- * Connect pusher and get all credentials from config.
- *
- * @param \Illuminate\Contracts\Config\Repository $config
- */
- public function __construct(Repository $config)
- {
- $this->config = $config;
- $this->pusher = $this->connectPusher();
- }
+ /**
+ * Connect pusher and get all credentials from config.
+ *
+ * @param \Illuminate\Contracts\Config\Repository $config
+ */
+ public function __construct(Repository $config)
+ {
+ $this->config = $config;
+ $this->pusher = $this->connectPusher();
+ }
- /**
- * Make pusher connection.
- *
- * @param array $options
- *
- * @return object | bool
- */
- protected function connectPusher($options = [])
- {
- if ($this->getConfig('broadcast.enable')) {
- $appId = $this->getConfig('broadcast.pusher.app_id');
- $appKey = $this->getConfig('broadcast.pusher.app_key');
- $appSecret = $this->getConfig('broadcast.pusher.app_secret');
- $appOptions = $this->getConfig('broadcast.pusher.options');
+ /**
+ * Make pusher connection.
+ *
+ * @param array $options
+ *
+ * @return object | bool
+ */
+ protected function connectPusher($options = [])
+ {
+ if ($this->getConfig('broadcast.enable')) {
+ $appId = $this->getConfig('broadcast.pusher.app_id');
+ $appKey = $this->getConfig('broadcast.pusher.app_key');
+ $appSecret = $this->getConfig('broadcast.pusher.app_secret');
+ $appOptions = $this->getConfig('broadcast.pusher.options');
- $newOptions = array_merge($appOptions, $options);
- $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
+ $newOptions = array_merge($appOptions, $options);
+ $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);
- return $pusher;
- }
+ return $pusher;
+ }
- return false;
- }
+ return false;
+ }
- /**
- * Dispatch the job to the queue.
- *
- * @param \Nahid\Talk\Messages\Message $message
- */
- public function transmission(Message $message)
- {
- if (!$this->pusher) {
- return false;
- }
+ /**
+ * Dispatch the job to the queue.
+ *
+ * @param \Nahid\Talk\Messages\Message $message
+ */
+ public function transmission(Message $message)
+ {
+ if (!$this->pusher) {
+ return false;
+ }
- $sender = $message->sender->toArray();
- $messageArray = $message->toArray();
- $messageArray['sender'] = $sender;
- $this->dispatch(new Webcast($messageArray));
- }
+ $sender = $message->sender->toArray();
+ $messageArray = $message->toArray();
+ $messageArray['sender'] = $sender;
+ $this->dispatch(new Webcast($messageArray));
+ }
- /**
- * get specific config from talk configurations.
- *
- * @param string
- *
- * @return string|array|int
- */
- public function getConfig($name)
- {
- return $this->config->get(self::CONFIG_PATH . '.' . $name);
- }
+ /**
+ * get specific config from talk configurations.
+ *
+ * @param string
+ *
+ * @return string|array|int
+ */
+ public function getConfig($name)
+ {
+ return $this->config->get(self::CONFIG_PATH.'.'.$name);
+ }
}
diff --git a/src/Live/Webcast.php b/src/Live/Webcast.php
index 2c3c0c7..a7ffdce 100644
--- a/src/Live/Webcast.php
+++ b/src/Live/Webcast.php
@@ -3,51 +3,51 @@
namespace Nahid\Talk\Live;
use Illuminate\Bus\Queueable;
-use Illuminate\Contracts\Queue\ShouldQueue;
-use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
+use Illuminate\Queue\InteractsWithQueue;
+use Illuminate\Contracts\Queue\ShouldQueue;
class Webcast implements ShouldQueue
{
- use InteractsWithQueue, Queueable, SerializesModels;
-
- /*
- * Message Model Instance
- *
- * @var object
- * */
- protected $message;
-
- /*
- * Broadcast class instance
- *
- * @var object
- * */
- protected $broadcast;
-
- /**
- * Set message collections to the properties.
- */
- public function __construct($message)
- {
- $this->message = $message;
- }
-
- /*
- * Execute the job and broadcast to the pusher channels
- *
- * @param \Nahid\Talk\Live\Broadcast $broadcast
- * @return void
- */
- public function handle(Broadcast $broadcast)
- {
- $this->broadcast = $broadcast;
- $senderIdKey = $this->broadcast->getConfig('user.ownerKey') ? $this->broadcast->getConfig('user.ownerKey') : 'id';
- $toUser = ($this->message['sender'][$senderIdKey] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
-
- $channelForUser = $this->broadcast->getConfig('broadcast.app_name') . '-user-' . $toUser;
- $channelForConversation = $this->broadcast->getConfig('broadcast.app_name') . '-conversation-' . $this->message['conversation_id'];
-
- $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
- }
+ use InteractsWithQueue, Queueable, SerializesModels;
+
+ /*
+ * Message Model Instance
+ *
+ * @var object
+ * */
+ protected $message;
+
+ /*
+ * Broadcast class instance
+ *
+ * @var object
+ * */
+ protected $broadcast;
+
+ /**
+ * Set message collections to the properties.
+ */
+ public function __construct($message)
+ {
+ $this->message = $message;
+ }
+
+ /*
+ * Execute the job and broadcast to the pusher channels
+ *
+ * @param \Nahid\Talk\Live\Broadcast $broadcast
+ * @return void
+ */
+ public function handle(Broadcast $broadcast)
+ {
+ $this->broadcast = $broadcast;
+ $senderIdKey = $this->broadcast->getConfig('user.ownerKey') ? $this->broadcast->getConfig('user.ownerKey') : 'id';
+ $toUser = ($this->message['sender'][ $senderIdKey] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];
+
+ $channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
+ $channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];
+
+ $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
+ }
}
diff --git a/src/Messages/MessageRepository.php b/src/Messages/MessageRepository.php
index 1b14308..6b24026 100644
--- a/src/Messages/MessageRepository.php
+++ b/src/Messages/MessageRepository.php
@@ -34,6 +34,6 @@ public function softDeleteMessage($messageId, $authUserId)
}
return (boolean) $this->update($message);
-
+
}
}
diff --git a/src/Middleware/TalkMiddleware.php b/src/Middleware/TalkMiddleware.php
index f620e8e..3fbc8da 100644
--- a/src/Middleware/TalkMiddleware.php
+++ b/src/Middleware/TalkMiddleware.php
@@ -8,20 +8,20 @@
class TalkMiddleware
{
- /**
- * Handle an incoming request.
- *
- * @param \Illuminate\Http\Request $request
- * @param \Closure $next
- * @return mixed
- */
+ /**
+ * Handle an incoming request.
+ *
+ * @param \Illuminate\Http\Request $request
+ * @param \Closure $next
+ * @return mixed
+ */
- public function handle($request, Closure $next, $guard = null)
- {
- if (Auth::guard($guard)->check()) {
- Talk::setAuthUserId(Auth::guard($guard)->user()->id);
- }
+ public function handle($request, Closure $next, $guard = null)
+ {
+ if (Auth::guard($guard)->check()) {
+ Talk::setAuthUserId(Auth::guard($guard)->user()->id);
+ }
- return $next($request);
- }
+ return $next($request);
+ }
}
diff --git a/src/TalkServiceProvider.php b/src/TalkServiceProvider.php
index 3ab075e..4b24577 100644
--- a/src/TalkServiceProvider.php
+++ b/src/TalkServiceProvider.php
@@ -38,11 +38,11 @@ protected function setupConfig()
if ($this->app instanceof LaravelApplication && $this->app->runningInConsole()) {
$this->publishes([$source => config_path('talk.php')]);
}
-
+
if ($this->app instanceof LumenApplication) {
$this->app->configure('talk');
}
-
+
$this->mergeConfigFrom($source, 'talk');
}
/**
@@ -63,55 +63,31 @@ protected function registerTalk()
return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
});
- if ($this->app instanceof LumenApplication) {
- $this->app->configure('talk');
- }
-
- $this->mergeConfigFrom($source, 'talk');
- }
- /**
- * Publish migrations files.
- */
- protected function setupMigrations()
- {
- $this->publishes([
- realpath(__DIR__ . '/../database/migrations/') => database_path('migrations'),
- ], 'migrations');
- }
- /**
- * Register Talk class.
- */
- protected function registerTalk()
- {
- $this->app->singleton('talk', function (Container $app) {
- return new Talk($app['config'], $app['talk.broadcast'], $app[ConversationRepository::class], $app[MessageRepository::class]);
- });
-
- $this->app->alias('talk', Talk::class);
- }
+ $this->app->alias('talk', Talk::class);
+ }
- /**
- * Register Talk class.
- */
- protected function registerBroadcast()
- {
- $this->app->singleton('talk.broadcast', function (Container $app) {
- return new Live\Broadcast($app['config']);
- });
+ /**
+ * Register Talk class.
+ */
+ protected function registerBroadcast()
+ {
+ $this->app->singleton('talk.broadcast', function (Container $app) {
+ return new Live\Broadcast($app['config']);
+ });
- $this->app->alias('talk.broadcast', Live\Broadcast::class);
- }
+ $this->app->alias('talk.broadcast', Live\Broadcast::class);
+ }
- /**
- * Get the services provided by the provider.
- *
- * @return string[]
- */
- public function provides()
- {
- return [
- 'talk',
- 'talk.broadcast',
- ];
- }
+ /**
+ * Get the services provided by the provider.
+ *
+ * @return string[]
+ */
+ public function provides()
+ {
+ return [
+ 'talk',
+ 'talk.broadcast',
+ ];
+ }
}
diff --git a/src/helpers/talk.php b/src/helpers/talk.php
index 93399ef..f2a9738 100644
--- a/src/helpers/talk.php
+++ b/src/helpers/talk.php
@@ -6,19 +6,19 @@
* Time: 4:58 PM.
*/
if (!function_exists('talk_live')) {
- function talk_live($options)
- {
- $talk__appKey = config('talk.broadcast.pusher.app_key');
- $talk__appName = config('talk.broadcast.app_name');
- $talk__options = json_encode(config('talk.broadcast.pusher.options'));
+ function talk_live($options)
+ {
+ $talk__appKey = config('talk.broadcast.pusher.app_key');
+ $talk__appName = config('talk.broadcast.app_name');
+ $talk__options = json_encode(config('talk.broadcast.pusher.options'));
- $talk_user_channel = isset($options['user']['id']) ? $talk__appName . '-user-' . $options['user']['id'] : '';
- $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName . '-conversation-' . $options['conversation']['id'] : '';
- $talk__userChannel['name'] = sha1($talk_user_channel);
- $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
- $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
- $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
+ $talk_user_channel = isset($options['user']['id']) ? $talk__appName.'-user-'.$options['user']['id'] : '';
+ $talk_conversation_channel = isset($options['conversation']['id']) ? $talk__appName.'-conversation-'.$options['conversation']['id'] : '';
+ $talk__userChannel['name'] = sha1($talk_user_channel);
+ $talk__conversationChannel['name'] = sha1($talk_conversation_channel);
+ $talk__userChannel['callback'] = isset($options['user']['callback']) ? $options['user']['callback'] : [];
+ $talk__conversationChannel['callback'] = isset($options['conversation']['callback']) ? $options['conversation']['callback'] : [];
- return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
- }
+ return view('talk::pusherjs', compact('talk__appKey', 'talk__options', 'talk__userChannel', 'talk__conversationChannel'))->render();
+ }
}
From 964996a1bfd63b062877a05b83ee9db6c7347215 Mon Sep 17 00:00:00 2001
From: Damilola Olowookere
Date: Sat, 28 Mar 2020 22:56:02 +0100
Subject: [PATCH 34/52] updat readme, improve code readability and bug fixes
---
README.md | 213 ++++++++++--------
..._is_read_column_to_conversations_table.php | 32 +++
...dd_title_column_to_conversations_table.php | 33 ---
...2_add_is_read_column_to_messages_table.php | 32 ---
...802_add_title_column_to_messages_table.php | 32 +++
..._add_is_read_column_to_messages_table.php} | 2 +-
src/Conversations/ConversationRepository.php | 86 ++++---
src/Talk.php | 145 +++++++-----
8 files changed, 321 insertions(+), 254 deletions(-)
create mode 100644 database/migrations/2018_11_10_160722_add_is_read_column_to_conversations_table.php
delete mode 100644 database/migrations/2018_11_10_160722_add_title_column_to_conversations_table.php
delete mode 100644 database/migrations/2018_11_10_164802_add_is_read_column_to_messages_table.php
create mode 100644 database/migrations/2018_11_10_164802_add_title_column_to_messages_table.php
rename database/migrations/{2020_03_28_095235_add_is_read_to_messages_table.php => 2020_03_28_095235_add_is_read_column_to_messages_table.php} (90%)
diff --git a/README.md b/README.md
index 8333826..f723387 100644
--- a/README.md
+++ b/README.md
@@ -1,14 +1,11 @@
# Laravel-Talk
-[](https://github.com/nahid/talk)
-[](https://raw.githubusercontent.com/nahid/talk/master/LICENSE)
-[](https://travis-ci.org/nahid/talk)
+[](https://github.com/nahid/talk) [](https://raw.githubusercontent.com/nahid/talk/master/LICENSE) [](https://travis-ci.org/nahid/talk)
Talk is a Laravel 5 based user conversation (inbox) system with realtime messaging. You can easily integrate this package with any Laravel based project. It helps you to develop a messaging system in just few minutes. Here is a project screenshot that was developed by Talk.
Talk v2.1.0 supports realtime messaging. Learn more about [Talk Live Messaging](https://github.com/nahid/talk#realtime-messaging)
-
#### Feedback
If you already used Talk, please share your experience with us. It will make the project better.
@@ -25,7 +22,6 @@ See which project was [Built with Talk](https://github.com/nahid/talk/issues/42)
> Do not migrate 1.1.7 from its higher version directly. Please try our [sample project](https://github.com/nahid/talk-example) first and then apply it on your project.
-

You may try [Talk-Example](https://github.com/nahid/talk-example) project.
@@ -37,23 +33,23 @@ username: admin
password: admin
```
-
-
So let's start your tour :)
### Features
-* Head to head messaging
-* Realtime messaging
-* Creating new conversation with titles
-* Message threads with latest one
-* View conversations by user id or conversation id
-* Support pagination in threads and messages
-* Delete (soft delete) message from both end. Sender and receiver can delete their message from their end
-* Permanent delete message
-* Mark message as seen
-* Only participant can view or access there message or message threads
-* Inline url render using oembed specifications
+- Head to head messaging
+- Realtime messaging
+- Creating new conversation with titles
+- Adding [tags](https://github.com/nahid/talk#tags) to conversations
+- Message threads (i.e. conversations) with latest messages
+- View conversations by user id or conversation id
+- Support pagination in threads and messages
+- Delete (soft delete) message from both end. Sender and receiver can delete their message from their end
+- Permanent delete message
+- Mark message as seen
+- Mark message as read
+- Only participant can view or access there message or message threads
+- Inline url render using oembed specifications
### Installation
@@ -121,40 +117,40 @@ return [
];
```
-
### Usage
-Its very easy to use. If you want to set authenticate user id globally then you have to set a middleware first. Go to `app/Http/Kernel.php` and set it in `$routeMiddleware` array:
-
- ```php
- 'talk' => \Nahid\Talk\Middleware\TalkMiddleware::class,
- ```
+It is very easy to use. If you want to set authenticate user id globally then you have to set a middleware first. Go to `app/Http/Kernel.php` and set it in `$routeMiddleware` array:
- And now you can use it from anywhere with middleware. Suppose you have a Controller and you want to set authenticate user id globally then write this in controller constructor:
+```php
+'talk' => \Nahid\Talk\Middleware\TalkMiddleware::class,
+```
+And now you can use it from anywhere with middleware. Suppose you have a Controller and you want to set authenticate user id globally then write this in controller constructor:
- ```php
- $this->middleware('talk');
- ```
+```php
+$this->middleware('talk');
+```
But instead of set id globally you can use these procedure from any method in controller:
-
```php
Talk::setAuthUserId(auth()->user()->id);
```
-
Now you may use any method what you need. But if want pass authentic id instantly, this method may help you:
```php
Talk::user(auth()->user()->id)->anyMethodHere();
```
+
+### Tags
+
+With Talk, you can "tag" conversations. This makes it possible for your users to uniquely identify conversations and categorized them. This is usually a very useful and invaluable tool when you want your users to add labels to their conversations. When creating tags, you can mark a tag as being "special". Special tags are tags that do not belong to any user. Hence, only one copy of a special tag can exist. With this, Talk can effectively replace the default [Laravel notification system](https://laravel.com/docs/6.x/notifications), because you can simply send a Talk message to the concerned user and labelling the conversation with a special tag (e.g. "SYSTEM_NOTIFICATION"). Then in your frontend, you can simply fetch all conversations with that label and you've got yourself a system notification! The applications of Talk tags are limitless!
+
Please see the API Doc.
### API List
-
- [setAuthUserId](https://github.com/nahid/talk#setauthuserid)
- [user](https://github.com/nahid/talk#user)
- [isConversationExists](https://github.com/nahid/talk#isconversationexists)
@@ -165,12 +161,16 @@ Please see the API Doc.
- [getInboxAll](https://github.com/nahid/talk#getinboxAll)
- [threads](https://github.com/nahid/talk#threads)
- [threadsAll](https://github.com/nahid/talk#threadsall)
+- [addTagToConversation](https://github.com/nahid/talk#addtagtoconversation)
+- [getUserTags](https://github.com/nahid/talk#getusertags)
- [getConversationsById](https://github.com/nahid/talk#getconversationbyid)
+- [getConversationsByTagId](https://github.com/nahid/talk#getconversationsbytagid)
- [getConversationsAllById](https://github.com/nahid/talk#getconversationallbyid)
- [getConversationsByUserId](https://github.com/nahid/talk#getconversationbyuserid)
- [getConversationsAllByUserId](https://github.com/nahid/talk#getconversationallbyuserid)
- [getMessages](https://github.com/nahid/talk#getmessages)
- [getMessagesByUserId](https://github.com/nahid/talk#getmessagesbyuserid)
+- [getMessagesByTagId](https://github.com/nahid/talk#getMessagesByTagId)
- [getMessagesAll](https://github.com/nahid/talk#getmessagesall)
- [getMessagesAllByUserId](https://github.com/nahid/talk#getmessagesallbyuserid)
- [readMessage](https://github.com/nahid/talk#readmessage)
@@ -180,7 +180,6 @@ Please see the API Doc.
- [deleteForever](https://github.com/nahid/talk#deleteforever)
- [deleteConversations](https://github.com/nahid/talk#deleteconversations)
-
### setAuthUserId
`setAuthUserId` method sets the currently loggedin user id, which you pass through parameter. If you pass `null` or `empty` value then it returns false.
@@ -207,13 +206,14 @@ When you pass logged in user ID, Talk will know who is currently authenticated f
### user
You may use this method instead of `setAuthUserId()` method. When you have to instantly access users conversations then you may use it.
+
**Syntax**
```php
object user($id)
```
-**Example**
-When you haven't set authenticated user id globally, then you just use this method directly with others method.
+
+**Example** When you haven't set authenticated user id globally, then you just use this method directly with others method.
```php
$inboxes = Talk::user(auth()->user()->id)->threads();
@@ -240,7 +240,7 @@ if ($conversationId = Talk::isConversationExists($userId)) {
### isAuthenticUser
-isAuthenticUser checks if the given user exists in given conversation.
+isAuthenticUser checks if the given user exists in given conversation.
**Syntax**
@@ -282,12 +282,12 @@ You can send message via receiver id by using this method. If the message is suc
**Syntax**
```php
-object|false sendMessageByUserId($userId, $message)
+object|false sendMessageByUserId($receiverId, $message)
```
### getInbox
-If you want to get all the inboxes except soft deleted message , this method may help you. This method gets all the inboxes via previously assigned authenticated user id. It returns collections of message thread with latest message.
+If you want to get all the inboxes except soft deleted message , this method may help you. This method gets all the inboxes via previously assigned authenticated user id. It returns collections of message threads with latest messages in each of the returned threads. Specifically, it retrieves all message threads (i.e. conversations) for the authenticated user without soft deleted message, including latest messages, sender and receiver user model. This method differs from the `getInboxAll()` method in that each conversation returned contains a lot more data (each conversation contains the conversation model itself, the messages in the conversation, a collection of only unread messages in the conversation, the corresponding user, etc.)
**Syntax**
@@ -295,7 +295,6 @@ If you want to get all the inboxes except soft deleted message , this method may
array getInbox([$order = 'desc'[,$offset = 0[, $take = 20]]])
```
-
**Example**
```php
@@ -307,19 +306,19 @@ return view('message.threads', compact('inboxes'));
```html
```
### getInboxAll
-Its similar as `getInbox()` method. If you want to get all the inboxes with soft deleted messages, this method may help you. This method gets all the inboxes via given user id.
+It is similar as `getInbox()` method. If you want to get all the inboxes with soft deleted messages, this method may help you. This method gets all the inboxes for the authenticated user. Specifically, it retrieves all message threads (i.e. conversations) for the authenticated user with the latest message and the corresponding user model. This method defers from the `getInbox()` method in that it is more light-weight and thus more efficient, because it only includes just a handful of information for each conversation (each conversation only contains the first message in the conversation and the corresponding user).
**Syntax**
@@ -337,7 +336,6 @@ This method is an alias of `getInbox()` method.
array threads([$order = 'desc'[,$offset = 0[, $take = 20]]])
```
-
### threadsAll
This method is an alias of `getInboxAll()` method.
@@ -348,8 +346,27 @@ This method is an alias of `getInboxAll()` method.
array threadsAll([$order = 'desc'[,$offset = 0[, $take = 20]]])
```
-### getConversationsById
+### addTagToConversation
+
+This method adds a tag to a conversation. This is useful when you need to label a conversation. There is also an included `starThisConversation()` function to help you "star" a conversation. Internally, the method simply assigns a special tag to the conversation.
+**Syntax**
+
+```php
+bool addTagToConversation($conversationId, string $tagName, bool $makeItASpecialTag = null)
+```
+
+### getUserTags
+
+Get all the tags that belongs to the currently authenticated user (i.e. the tags that the user created)
+
+**Syntax**
+
+```php
+bool getUserTags()
+```
+
+### getConversationsById
When you want to get all the conversations using your desire conversation id, you can try this method. This method returns all the conversations (except soft deleted) with `sender` and `withUser` objects
@@ -369,6 +386,7 @@ $withUser = $conversations->withUser;
return view('messages.conversations', compact('messages', 'withUser'));
```
+
This method returns two objects `messages` and `withUser`. `messages` object contains messages collection and `withUser` object contains participant User collections.
Let's see how to use it with your views
@@ -376,28 +394,29 @@ Let's see how to use it with your views
```html
-
Chat with {{$withUser->name}}
- @foreach ($messages as $msg)
-
-
{{$msg->sender->name}}
-
{{$msg->humans_time}}
-
- {{$msg->message}}
-
-
- @endforeach
+
Chat with {{$withUser->name}}
+ @foreach ($messages as $msg)
+
+
{{$msg->sender->name}}
+
{{$msg->humans_time}}
+
+ {{$msg->message}}
+
+
+ @endforeach
```
### getConversationsAllById
-This method is similar as `getConversationsById()`. The only difference between this method is its return all messages with soft deleted items.
+This method is similar as `getConversationsById()`. The only difference is that this method returns all messages with soft deleted items.
**Syntax**
```php
array getConversationsAllById($conversationId[, $offset = 0[, $take = 20]])
```
+
### getConversationsByUserId
When you want to get all the conversations using your desire receiver id, you can try this method. This method returns all the conversations (except soft deleted message) with user's objects
@@ -410,7 +429,7 @@ object getConversationsByUserId($receiverId [, $offset = 0[, $take = 20]])
### getConversationsAllByUserId
-This method is similar as `getConversationsByUserId()`. The only difference between this method is it returns all messages with soft deleted items.
+This method is similar as `getConversationsByUserId()`. The only difference is that this method returns all messages with soft deleted items.
**Syntax**
@@ -418,9 +437,13 @@ This method is similar as `getConversationsByUserId()`. The only difference betw
array getConversationsAllByUserId($receiverId[, $offset = 0[, $take = 20]])
```
+### getConversationsByTagId
+
+This method is similar to `getMessagesByTagId()`
+
### getMessages
-This is a alias of `getConversationsById()` method.
+This is a alias of `getConversationsById()` method.
**Syntax**
@@ -430,7 +453,7 @@ array messages($conversationId[, $offset = 0[, $take = 20]])
### getMessagesAll
-This is a alias of `getConversationsAllById()` method.
+This is a alias of `getConversationsAllById()` method.
**Syntax**
@@ -440,7 +463,7 @@ array messagesAll($conversationId[, $offset = 0[, $take = 20]])
### getMessagesByUserId
-This is a alias of `getConversationsByUserId()` method.
+This is a alias of `getConversationsByUserId()` method.
**Syntax**
@@ -448,10 +471,19 @@ This is a alias of `getConversationsByUserId()` method.
array messagesByUserId($receiverId[, $offset = 0[, $take = 20]])
```
+### getMessagesByTagId
+
+Gets the conversations that has the specified tag id.
+
+**Syntax**
+
+```php
+Collection getMessagesByTagId($tagId)
+```
### getMessagesAllByUserId
-This is a alias of `getConversationsAllByUserId()` method.
+This is a alias of `getConversationsAllByUserId()` method.
**Syntax**
@@ -546,11 +578,7 @@ return [
];
```
-in this new version broadcast section was added with talk config. Here broadcast is disabled by default.
-If you want to enable live (realtime) messaging then you have to enable it first. Then add pusher credentials to your .env file and you must add a new line called PUSHER_APP_NAME in the .env file to specify your application pusher name. Thats it. Everytime
-when you send message then talk will automatically fire two event, one for specific user and second for specific conversation. So
-you may listen or subscribe one or both as per your wish. Finally you have to subscribe these events by using `talk_live()` helper function.
-Go to where you want to subscribe to work with message data follow this code.
+In this new version, broadcast section was added with Talk config. Here broadcast is disabled by default. If you want to enable live (realtime) messaging then you have to enable it first. Then add pusher credentials to your .env file and you must add a new line called PUSHER_APP_NAME in the .env file to specify your application pusher name. Thats it. Whenever you send message, Talk will automatically fire two event, one for specific user and second for specific conversation. So you may listen or subscribe one or both as per your wish. Finally you have to subscribe these events by using `talk_live()` helper function. Go to where you want to subscribe to work with message data follow this code.
```