Skip to content

Commit

Permalink
feat(core): 增加 At 消息基本代码
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Aug 13, 2018
1 parent 1bd41e5 commit 6056ba7
Show file tree
Hide file tree
Showing 10 changed files with 436 additions and 0 deletions.
8 changes: 8 additions & 0 deletions app/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ public function registerCoreContainerAliases()
\Zhiyi\Plus\Contracts\Cdn\UrlFactory::class,
\Zhiyi\Plus\Cdn\UrlManager::class,
],
'at-message' => [
\Zhiyi\Plus\AtMessage\MessageInterface::class,
\Zhiyi\Plus\AtMessage\Message::class,
],
'at-resource-manager' => [
\Zhiyi\Plus\AtMessage\ResourceManagerInterface::class,
\Zhiyi\Plus\AtMessage\ResourceManager::class,
],
] as $abstract => $aliases) {
foreach ($aliases as $alias) {
$this->alias($abstract, $alias);
Expand Down
98 changes: 98 additions & 0 deletions app/AtMessage/Message.php
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);
}
}
35 changes: 35 additions & 0 deletions app/AtMessage/MessageInterface.php
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;
}
30 changes: 30 additions & 0 deletions app/AtMessage/ResourceInterface.php
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;
}
65 changes: 65 additions & 0 deletions app/AtMessage/ResourceManager.php
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);
}
}
41 changes: 41 additions & 0 deletions app/AtMessage/ResourceManagerInterface.php
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;
}
53 changes: 53 additions & 0 deletions app/AtMessage/Resources/Comment.php
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);
}
}
56 changes: 56 additions & 0 deletions app/AtMessage/Resources/Feed.php
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);
}
}

0 comments on commit 6056ba7

Please sign in to comment.