-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
436 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\AtMessage; | ||
|
||
use Closure; | ||
use Zhiyi\Plus\Services\Push; | ||
use Zhiyi\Plus\Models\User as UserModel; | ||
use Zhiyi\Plus\Models\AtMessage as Model; | ||
use Zhiyi\Plus\Models\UserCount as UserCountModel; | ||
|
||
class Message implements MessageInterface | ||
{ | ||
protected $manager; | ||
protected $model; | ||
protected $pusher; | ||
|
||
public function __construct(ResourceManagerInterface $manager, Model $pusher, Push $push) | ||
{ | ||
$this->manager = $manager; | ||
$this->model = $model; | ||
$this->pusher = $pusher; | ||
} | ||
|
||
public function send(UserModel $sender, UserModel $user, $resource): void | ||
{ | ||
$resource = $this->manager->resource($resource, $sender); | ||
$atMessage = $this->message($resource, $user); | ||
$this->save(function () use ($resource, $atMessage, $user) { | ||
$atMessage->save(); | ||
$this->updateAtMessageCount($user); | ||
}); | ||
|
||
$this->notice($user, $resource); | ||
} | ||
|
||
public function message(ResourceInterface $resource, UserModel $user): Model | ||
{ | ||
$message = $this->model->newInstance(); | ||
$message->resourceable_type = $resource->type(); | ||
$message->resourceable_id = $resource->id(); | ||
$message->user_id = $user->id; | ||
|
||
return $message; | ||
} | ||
|
||
protected function notice(UserModel $user, ResourceInterface $resource): void | ||
{ | ||
$this->pusher->push( | ||
$resource->message(), | ||
$user->id, | ||
[ | ||
'resourceable_type' => $resource->type(), | ||
'resourceable_id' => $resource->id(), | ||
] | ||
); | ||
} | ||
|
||
protected function updateAtMessageCount(UserModel $user): void | ||
{ | ||
$model = new UserCountModel(); | ||
$count = $model->newQuery() | ||
->where('user_id', $user->id) | ||
->where('type', 'at') | ||
->first(); | ||
if (! $count) { | ||
$count = $model->newInstance(); | ||
$count->type = 'at'; | ||
$count->user_id = $user->id; | ||
$count->total = 0; | ||
} | ||
|
||
$count->total += 1; | ||
$count->save(); | ||
} | ||
|
||
protected function save(Closure $closure) | ||
{ | ||
return $this->model->getConnection()->transaction($closure); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\AtMessage; | ||
|
||
use Zhiyi\Plus\Models\User as UserModel; | ||
|
||
interface MessageInterface | ||
{ | ||
/** | ||
* Send at message | ||
* @param \Zhiyi\Plus\Models\User $sender | ||
* @param \Zhiyi\Plus\Models\User $user | ||
* @param mixed $resource | ||
* @return void | ||
*/ | ||
public function send(UserModel $sender, UserModel $user, $resource): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\AtMessage; | ||
|
||
interface MessageInterface | ||
{ | ||
public function type(): string; | ||
|
||
public function id(): int; | ||
|
||
public function message(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\AtMessage; | ||
|
||
use InvalidArgumentException; | ||
use Zhiyi\Plus\Models\User as UserModel; | ||
|
||
class ResourceManager implements ResourceManagerInterface | ||
{ | ||
/** | ||
* Resource map | ||
* @var array | ||
*/ | ||
public static $map = [ | ||
\Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed::class => Resources\Feed::class, | ||
]; | ||
|
||
/** | ||
* Get resource. | ||
* @param mixed $resource | ||
* @param \Zhiyi\Plus\Models\User $sender | ||
* @return \Zhiyi\Plus\AtMessage\ResourceInterface | ||
* @throws \InvalidArgumentException | ||
*/ | ||
public function resource($resource, UserModel $sender): ResourceInterface | ||
{ | ||
$className = $this->getClassName($resource); | ||
$resourceClass = static::$map[$className] ?? null; | ||
if (! $resourceClass) { | ||
throw new InvalidArgumentException(sprintf( | ||
'Resource [%s] not supported.', $className | ||
)); | ||
} | ||
|
||
return new $resourceClass($resource, $sender); | ||
} | ||
|
||
/** | ||
* Get resource class name. | ||
* @param mixed $resource | ||
* @return string | ||
*/ | ||
public function getClassName($resource): string | ||
{ | ||
return get_class($resource); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\AtMessage; | ||
|
||
use Zhiyi\Plus\Models\User as UserModel; | ||
|
||
interface ResourceManagerInterface | ||
{ | ||
/** | ||
* Get resource. | ||
* @param mixed $resource | ||
* @param \Zhiyi\Plus\Models\User $sender | ||
* @return \Zhiyi\Plus\AtMessage\ResourceInterface | ||
*/ | ||
public function resource($resource, UserModel $sender): ResourceInterface; | ||
|
||
/** | ||
* Get resource class name. | ||
* @param mixed $resource | ||
* @return string | ||
*/ | ||
public function getClassName($resource): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\AtMessage\Resources; | ||
|
||
use Zhiyi\Plus\Models\User as UserModel; | ||
use Zhiyi\Plus\Types\Models as ModelTypes; | ||
use Zhiyi\Plus\AtMessage\ResourceInterface; | ||
use Zhiyi\Plus\Models\Comment as CommentModel; | ||
|
||
class Feed implements ResourceInterface | ||
{ | ||
protected $comment; | ||
protected $sender; | ||
|
||
public function __construct(CommentModel $comment, UserModel $sender) | ||
{ | ||
$this->comment = $comment; | ||
$this->sender = $sender; | ||
} | ||
|
||
public function type(): string | ||
{ | ||
return ModelTypes::get(CommentModel::class, ModelTypes::KEY_BY_CLASS_ALIAS); | ||
} | ||
|
||
public function id(): int | ||
{ | ||
return $this->comment->id; | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return sprintf('%s在评论中@了你', $this->sender->name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <master@zhiyicx.com> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\AtMessage\Resources; | ||
|
||
use Zhiyi\Plus\Models\User as UserModel; | ||
use Zhiyi\Plus\Types\Models as ModelTypes; | ||
use Zhiyi\Plus\AtMessage\ResourceInterface; | ||
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed as FeedModel; | ||
|
||
class Feed implements ResourceInterface | ||
{ | ||
protected $feed; | ||
protected $sender; | ||
|
||
public function __construct(FeedModel $feed, UserModel $sender) | ||
{ | ||
$this->feed = $feed; | ||
$this->sender = $sender; | ||
} | ||
|
||
public function type(): string | ||
{ | ||
return ModelTypes::get( | ||
FeedModel::class, | ||
ModelTypes::KEY_BY_CLASS_ALIAS | ||
); | ||
} | ||
|
||
public function id(): int | ||
{ | ||
return $this->feed->id; | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return sprintf('%s在动态中@了你', $this->sender->name); | ||
} | ||
} |
Oops, something went wrong.