-
-
Notifications
You must be signed in to change notification settings - Fork 78
/
Messageable.php
143 lines (130 loc) · 3.71 KB
/
Messageable.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
<?php
namespace RTippin\Messenger\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use RTippin\Messenger\Contracts\MessengerProvider;
use RTippin\Messenger\Facades\Messenger;
use RTippin\Messenger\Support\Helpers;
/**
* @mixin Model
*
* @method static getProviderSearchableBuilder(Builder $query, string $search, array $searchItems)
*/
trait Messageable
{
/**
* When calling for isOnline, we cache on the model as it is common
* for the method to be called multiple times in collections.
*
* @var null|int
*/
public ?int $isOnlineCache = null;
/**
* When calling for onlineStatus, we cache on the model as it is common
* for the method to be called multiple times in collections.
*
* @var null|string
*/
public ?string $onlineStatusCache = null;
/**
* Get the provider settings and alias override, if set.
*
* @return array
*/
public static function getProviderSettings(): array
{
return [
'alias' => null,
'searchable' => true,
'friendable' => true,
'devices' => true,
'default_avatar' => public_path('vendor/messenger/images/users.png'),
'cant_message_first' => [],
'cant_search' => [],
'cant_friend' => [],
];
}
/**
* If your provider has a route/slug for a profile page,
* return that route here.
*
* @return string|null
*/
public function getProviderProfileRoute(): ?string
{
return null;
}
/**
* Format and return your provider name here.
* ex: $this->first . ' ' . $this->last.
*
* @return string
*/
public function getProviderName(): string
{
return strip_tags(ucwords($this->name));
}
/**
* The column name your providers avatar is stored in the database as.
*
* @return string
*/
public function getProviderAvatarColumn(): string
{
return 'picture';
}
/**
* The column name your provider has in the database that we will use to
* show last active, and touch / update timestamp when using our online
* heartbeat. This should be a timestamp column.
*
* @return string
*/
public function getProviderLastActiveColumn(): string
{
return 'updated_at';
}
/**
* Get the route of the avatar for your provider. We will call this
* from our resource classes using sm/md/lg .
*
* @param string $size
* @return string|null
*/
public function getProviderAvatarRoute(string $size = 'sm'): ?string
{
return Helpers::route('assets.messenger.provider.avatar.render',
[
'alias' => Messenger::findProviderAlias($this),
'id' => $this->getKey(),
'size' => $size,
'image' => $this->{$this->getProviderAvatarColumn()} ?: 'default.png',
]
);
}
/**
* Returns online status of your provider.
* 0 - offline, 1 - online, 2 - away.
*
* @return int
*/
public function getProviderOnlineStatus(): int
{
if (! is_null($this->isOnlineCache)) {
return $this->isOnlineCache;
}
return $this->isOnlineCache = Messenger::getProviderOnlineStatus($this);
}
/**
* Verbose meaning of the online status number.
*
* @deprecated To be removed in v2. No longer used in the backend.
*
* @return string
*/
public function getProviderOnlineStatusVerbose(): string
{
return Str::lower(MessengerProvider::ONLINE_STATUS[$this->getProviderOnlineStatus()]);
}
}