Skip to content

Commit 51be475

Browse files
committed
add method to make sending notifications easy
1 parent 964996a commit 51be475

File tree

3 files changed

+93
-13
lines changed

3 files changed

+93
-13
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,12 @@ Now run this command in your terminal to publish this package resources:
8181
php artisan vendor:publish --provider="Nahid\Talk\TalkServiceProvider"
8282
```
8383

84+
Note that you can publish only the migrations with the command below:
85+
86+
```
87+
php artisan vendor:publish --provider="Nahid\Talk\TalkServiceProvider" --tag=migrations
88+
```
89+
8490
After running this command, all necessary file will be included in your project. This package has two default migrations. So you have to run migrate command like this. (But make sure your database configuration is configured correctly.)
8591

8692
```shell
@@ -157,6 +163,7 @@ Please see the API Doc.
157163
- [isAuthenticUser](https://github.com/nahid/talk#isauthenticuser)
158164
- [sendMessage](https://github.com/nahid/talk#sendmessage)
159165
- [sendMessageByUserId](https://github.com/nahid/talk#sendmessagebyuserid)
166+
- [sendNotificationToUser](https://github.com/nahid/talk#sendnotificationtouser)
160167
- [getInbox](https://github.com/nahid/talk#getinbox)
161168
- [getInboxAll](https://github.com/nahid/talk#getinboxAll)
162169
- [threads](https://github.com/nahid/talk#threads)
@@ -285,6 +292,17 @@ You can send message via receiver id by using this method. If the message is suc
285292
object|false sendMessageByUserId($receiverId, $message)
286293
```
287294

295+
### sendNotificationToUser
296+
297+
This allows you to quickly send notification to a user. Notifications usually will not have a sender (user), because the idea is that notifications are system messages (actions completed, alerts, etc.). However, Talk still allows you to specifier a sender if you must.
298+
299+
**Syntax**
300+
301+
```php
302+
object|false sendNotificationToUser($receiverId, $message, $title = null, $customNotificationTag = null, \$optionalSenderId = null)
303+
304+
```
305+
288306
### getInbox
289307

290308
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.)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateConversationsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('conversations', function (Blueprint $table) {
17+
$table->integer('user_one')->nullable();
18+
});
19+
}
20+
21+
/**
22+
* Reverse the migrations.
23+
*
24+
* @return void
25+
*/
26+
public function down()
27+
{
28+
Schema::table('conversations', function (Blueprint $table) {
29+
$table->integer('user_one');
30+
});
31+
}
32+
}

src/Talk.php

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class Talk
3232
*/
3333
const STAR_TAG = "talk_special_tag_star";
3434

35+
/**
36+
* Just like STAR_TAG, this constant is also courtesy of the tag feature of Talk.
37+
*
38+
* @var string
39+
*/
40+
const NOTIFICATION_TAG = "talk_special_tag_notification";
41+
3542
/**
3643
* configurations instance.
3744
*
@@ -88,19 +95,20 @@ public function __construct(Repository $config, Broadcast $broadcast, Conversati
8895

8996
/**
9097
* make two users as serialize with ascending order.
98+
* It returns an array whose elements are $user1 and $user2
9199
*
92100
* @param int $user1
93101
* @param int $user2
94102
*
95103
* @return array
96104
*/
97-
protected function getSerializeUser($user1, $user2)
105+
protected function getSerializeUser($user1_id, $user2_id)
98106
{
99-
$user = [];
100-
$user['one'] = ($user1 < $user2) ? $user1 : $user2;
101-
$user['two'] = ($user1 < $user2) ? $user2 : $user1;
107+
$users = [];
108+
$user['one'] = ($user1_id < $user2_id) ? $user1_id : $user2_id;
109+
$user['two'] = ($user1_id < $user2_id) ? $user2_id : $user1_id;
102110

103-
return $user;
111+
return $users;
104112
}
105113

106114
/**
@@ -172,12 +180,12 @@ protected function makeMessageCollection($conversations)
172180
protected function newConversation($receiverId, $title, $tagName = null)
173181
{
174182
// $conversationId = $this->isConversationExists($receiverId);
175-
$user = $this->getSerializeUser($this->authUserId, $receiverId);
183+
$users = $this->getSerializeUser($this->authUserId, $receiverId);
176184

177185
// if ($conversationId === false) {
178186
$conversation = $this->conversation->create([
179-
'user_one' => $user['one'],
180-
'user_two' => $user['two'],
187+
'user_one' => $users['one'],
188+
'user_two' => $users['two'],
181189
'title' => $title,
182190
'status' => 1,
183191
]);
@@ -293,7 +301,7 @@ public function sendMessage($conversationId, $message)
293301
*
294302
* @return \Nahid\Talk\Messages\Message
295303
*/
296-
public function sendMessageByUserId($receiverId, $message, $title = null, $tag = null)
304+
public function sendMessageByUserId($receiverId, $message, $title = null, $tagName = null)
297305
{
298306
if ($conversationId = $this->isConversationExists($receiverId)) {
299307
$con = \Nahid\Talk\Conversations\Conversation::find($conversationId);
@@ -303,12 +311,34 @@ public function sendMessageByUserId($receiverId, $message, $title = null, $tag =
303311
}
304312
}
305313

306-
$conversationId = $this->newConversation($receiverId, $title, $tag);
314+
$conversationId = $this->newConversation($receiverId, $title, $tagName);
307315
$message = $this->makeMessage($conversationId, $message);
308316

309317
return $message;
310318
}
311319

320+
/**
321+
* Undocumented function
322+
*
323+
* @param int $receiverId
324+
* @param string $message
325+
* @param string $title
326+
* @param string $customNotificationTagName
327+
* @param int $optionalSenderId
328+
*
329+
* @return \Nahid\Talk\Messages\Message
330+
*/
331+
public function sendNotificationToUser($receiverId, $message, $title = null, $customNotificationTagName = null, $optionalSenderId = null)
332+
{
333+
//when sending notifications, there is no user_one, because
334+
//notifications should ideally be sent by the "system"
335+
$this->authUserId = $optionalSenderId;
336+
337+
$customNotificationTagName = empty($customNotificationTagName) ? self::NOTIFICATION_TAG : $customNotificationTagName;
338+
339+
return $this->sendMessageByUserId($receiverId, $message, $title, $customNotificationTagName);
340+
}
341+
312342
/**
313343
* fetch all inbox (i.e. conversations) for currently loggedin user with pagination.
314344
*
@@ -515,7 +545,7 @@ public function createTagForUser($tagName)
515545
}
516546

517547
/**
518-
* adds a tag to a conversation. Creates the tag if it does not exist for the user
548+
* adds a tag to an existing conversation. Creates the tag if it does not exist for the user
519549
* This allows for several users to maintain same tag name conveniently without any conflicts/issues
520550
*
521551
* @param int $conversationId
@@ -734,8 +764,8 @@ public function getUnreadMessagesInConversation($conversationId)
734764
* gets all messages not yet read in all conversations altogether
735765
*
736766
*
737-
* @param int $removeSpecialMessages : allows to use Talk also to send system notifications
738-
* to users by allowing some conversations to be tagged as "special".
767+
* @param int $removeSpecialMessages When true will remove conversations that have special tags.
768+
* Special tags is one of those features of Talk that makes it easy to mimic sending of system notifications
739769
* As a rule of thumb, any conversation that is not "normal" message/conversation should simply be tagged as special, so
740770
* that it is easy to get unread messages without confusing message contexts
741771
*

0 commit comments

Comments
 (0)