Skip to content

Commit

Permalink
Merge branch 'master' into v2
Browse files Browse the repository at this point in the history
  • Loading branch information
cmgmyr committed Jun 9, 2018
2 parents 3da74c4 + 2534abe commit 717a27e
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 34 deletions.
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ php:
- 5.6
- 7.0
- 7.1
- 7.2
- nightly

matrix:
allow_failures:
- php: 7.2
- php: nightly

# This triggers builds to run on the new TravisCI infrastructure.
# See: http://docs.travis-ci.com/user/workers/container-based-infrastructure/
Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2014-2017 Chris Gmyr <[email protected]>
Copyright (c) 2014-2018 Chris Gmyr <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
5 changes: 3 additions & 2 deletions nitpick.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"ignore": [
"tests/*",
"src/migrations/*"
"examples/*",
"migrations/*",
"tests/*"
]
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ If you discover any security related issues, please email [Chris Gmyr](mailto:cm
## Credits

- [Chris Gmyr](https://github.com/cmgmyr)
- [Anton Komarev](https://github.com/a-komarev)
- [All Contributors](../../contributors)

### Special Thanks
Expand Down
14 changes: 12 additions & 2 deletions src/MessengerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ protected function offerPublishing()
}
}

/**
* Define Messenger's models in registry.
*
* @return void
*/
protected function setMessengerModels()
{
$config = $this->app->make('config');
Expand All @@ -78,12 +83,17 @@ protected function setMessengerModels()
]);
}

/**
* Define User model in Messenger's model registry.
*
* @return void
*/
protected function setUserModel()
{
$config = $this->app->make('config');

$model = $config->get('auth.providers.users.model', function () use ($config) {
return $config->get('auth.model', $config->get('messenger.user_model'));
$model = $config->get('messenger.user_model', function () use ($config) {
return $config->get('auth.providers.users.model', $config->get('auth.model'));
});

Models::setUserModel($model);
Expand Down
9 changes: 4 additions & 5 deletions src/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\DB;

class Message extends Eloquent
{
Expand Down Expand Up @@ -98,9 +97,9 @@ public function recipients()
/**
* Returns unread messages given the userId.
*
* @param Builder $query
* @param $userId
* @return Builder
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $userId
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeUnreadForUser(Builder $query, $userId)
{
Expand All @@ -109,7 +108,7 @@ public function scopeUnreadForUser(Builder $query, $userId)
->whereHas('participants', function (Builder $query) use ($userId) {
$query->where('user_id', $userId)
->where(function (Builder $q) {
$q->where('last_read', '<', DB::raw($this->getTable() . '.created_at'))
$q->where('last_read', '<', $this->getConnection()->raw($this->getConnection()->getTablePrefix() . $this->getTable() . '.created_at'))
->orWhereNull('last_read');
});
});
Expand Down
57 changes: 33 additions & 24 deletions src/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ class Thread extends Eloquent
* @var array
*/
protected $dates = ['deleted_at'];

/**
* Internal cache for creator.
*
* @var null|Models::user()
*/
* Internal cache for creator.
*
* @var null|Models::user()
*/
protected $creatorCache = null;

/**
Expand Down Expand Up @@ -65,7 +65,7 @@ public function messages()
/**
* Returns the latest message from a thread.
*
* @return \Cmgmyr\Messenger\Models\Message
* @return null|\Cmgmyr\Messenger\Models\Message
*/
public function getLatestMessageAttribute()
{
Expand Down Expand Up @@ -114,22 +114,23 @@ public function creator()
/**
* Returns all of the latest threads by updated_at date.
*
* @return self
* @return \Illuminate\Database\Query\Builder|static
*/
public static function getAllLatest()
{
return self::latest('updated_at');
return static::latest('updated_at');
}

/**
* Returns all threads by subject.
*
* @param string $subject
* @return self
*
* @return \Illuminate\Database\Eloquent\Collection|static[]
*/
public static function getBySubject($subject)
{
return self::where('subject', 'like', $subject)->get();
return static::where('subject', 'like', $subject)->get();
}

/**
Expand All @@ -155,10 +156,10 @@ public function participantsUserIds($userId = null)
/**
* Returns threads that the user is associated with.
*
* @param Builder $query
* @param $userId
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $userId
*
* @return Builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeForUser(Builder $query, $userId)
{
Expand All @@ -174,10 +175,10 @@ public function scopeForUser(Builder $query, $userId)
/**
* Returns threads with new messages that the user is associated with.
*
* @param Builder $query
* @param $userId
* @param \Illuminate\Database\Eloquent\Builder $query
* @param int $userId
*
* @return Builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeForUserWithNewMessages(Builder $query, $userId)
{
Expand All @@ -197,10 +198,10 @@ public function scopeForUserWithNewMessages(Builder $query, $userId)
/**
* Returns threads between given user ids.
*
* @param Builder $query
* @param \Illuminate\Database\Eloquent\Builder $query
* @param array $participants
*
* @return Builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeBetween(Builder $query, array $participants)
{
Expand All @@ -216,6 +217,8 @@ public function scopeBetween(Builder $query, array $participants)
* Add users to thread as participants.
*
* @param array|mixed $userId
*
* @return void
*/
public function addParticipant($userId)
{
Expand All @@ -233,6 +236,8 @@ public function addParticipant($userId)
* Remove participants from thread.
*
* @param array|mixed $userId
*
* @return void
*/
public function removeParticipant($userId)
{
Expand All @@ -245,6 +250,8 @@ public function removeParticipant($userId)
* Mark a thread as read for a user.
*
* @param int $userId
*
* @return void
*/
public function markAsRead($userId)
{
Expand Down Expand Up @@ -286,7 +293,7 @@ public function isUnread($userId)
*
* @return mixed
*
* @throws ModelNotFoundException
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function getParticipantFromUser($userId)
{
Expand All @@ -295,6 +302,8 @@ public function getParticipantFromUser($userId)

/**
* Restores all participants within a thread that has a new message.
*
* @return void
*/
public function activateAllParticipants()
{
Expand All @@ -307,7 +316,7 @@ public function activateAllParticipants()
/**
* Generates a string of participant information.
*
* @param null $userId
* @param null|int $userId
* @param array $columns
*
* @return string
Expand Down Expand Up @@ -335,7 +344,7 @@ public function participantsString($userId = null, $columns = ['name'])
/**
* Checks to see if a user is a current participant of the thread.
*
* @param $userId
* @param int $userId
*
* @return bool
*/
Expand All @@ -352,7 +361,7 @@ public function hasParticipant($userId)
/**
* Generates a select string used in participantsString().
*
* @param $columns
* @param array $columns
*
* @return string
*/
Expand Down Expand Up @@ -383,7 +392,7 @@ protected function createSelectString($columns)
/**
* Returns array of unread messages in thread for given user.
*
* @param $userId
* @param int $userId
*
* @return \Illuminate\Support\Collection
*/
Expand All @@ -409,7 +418,7 @@ public function userUnreadMessages($userId)
/**
* Returns count of unread messages in thread for given user.
*
* @param $userId
* @param int $userId
*
* @return int
*/
Expand Down

0 comments on commit 717a27e

Please sign in to comment.