Skip to content

Commit 6056ba7

Browse files
committed
feat(core): 增加 At 消息基本代码
1 parent 1bd41e5 commit 6056ba7

File tree

10 files changed

+436
-0
lines changed

10 files changed

+436
-0
lines changed

app/Application.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ public function registerCoreContainerAliases()
7979
\Zhiyi\Plus\Contracts\Cdn\UrlFactory::class,
8080
\Zhiyi\Plus\Cdn\UrlManager::class,
8181
],
82+
'at-message' => [
83+
\Zhiyi\Plus\AtMessage\MessageInterface::class,
84+
\Zhiyi\Plus\AtMessage\Message::class,
85+
],
86+
'at-resource-manager' => [
87+
\Zhiyi\Plus\AtMessage\ResourceManagerInterface::class,
88+
\Zhiyi\Plus\AtMessage\ResourceManager::class,
89+
],
8290
] as $abstract => $aliases) {
8391
foreach ($aliases as $alias) {
8492
$this->alias($abstract, $alias);

app/AtMessage/Message.php

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\AtMessage;
22+
23+
use Closure;
24+
use Zhiyi\Plus\Services\Push;
25+
use Zhiyi\Plus\Models\User as UserModel;
26+
use Zhiyi\Plus\Models\AtMessage as Model;
27+
use Zhiyi\Plus\Models\UserCount as UserCountModel;
28+
29+
class Message implements MessageInterface
30+
{
31+
protected $manager;
32+
protected $model;
33+
protected $pusher;
34+
35+
public function __construct(ResourceManagerInterface $manager, Model $pusher, Push $push)
36+
{
37+
$this->manager = $manager;
38+
$this->model = $model;
39+
$this->pusher = $pusher;
40+
}
41+
42+
public function send(UserModel $sender, UserModel $user, $resource): void
43+
{
44+
$resource = $this->manager->resource($resource, $sender);
45+
$atMessage = $this->message($resource, $user);
46+
$this->save(function () use ($resource, $atMessage, $user) {
47+
$atMessage->save();
48+
$this->updateAtMessageCount($user);
49+
});
50+
51+
$this->notice($user, $resource);
52+
}
53+
54+
public function message(ResourceInterface $resource, UserModel $user): Model
55+
{
56+
$message = $this->model->newInstance();
57+
$message->resourceable_type = $resource->type();
58+
$message->resourceable_id = $resource->id();
59+
$message->user_id = $user->id;
60+
61+
return $message;
62+
}
63+
64+
protected function notice(UserModel $user, ResourceInterface $resource): void
65+
{
66+
$this->pusher->push(
67+
$resource->message(),
68+
$user->id,
69+
[
70+
'resourceable_type' => $resource->type(),
71+
'resourceable_id' => $resource->id(),
72+
]
73+
);
74+
}
75+
76+
protected function updateAtMessageCount(UserModel $user): void
77+
{
78+
$model = new UserCountModel();
79+
$count = $model->newQuery()
80+
->where('user_id', $user->id)
81+
->where('type', 'at')
82+
->first();
83+
if (! $count) {
84+
$count = $model->newInstance();
85+
$count->type = 'at';
86+
$count->user_id = $user->id;
87+
$count->total = 0;
88+
}
89+
90+
$count->total += 1;
91+
$count->save();
92+
}
93+
94+
protected function save(Closure $closure)
95+
{
96+
return $this->model->getConnection()->transaction($closure);
97+
}
98+
}

app/AtMessage/MessageInterface.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\AtMessage;
22+
23+
use Zhiyi\Plus\Models\User as UserModel;
24+
25+
interface MessageInterface
26+
{
27+
/**
28+
* Send at message
29+
* @param \Zhiyi\Plus\Models\User $sender
30+
* @param \Zhiyi\Plus\Models\User $user
31+
* @param mixed $resource
32+
* @return void
33+
*/
34+
public function send(UserModel $sender, UserModel $user, $resource): void;
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\AtMessage;
22+
23+
interface MessageInterface
24+
{
25+
public function type(): string;
26+
27+
public function id(): int;
28+
29+
public function message(): string;
30+
}

app/AtMessage/ResourceManager.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\AtMessage;
22+
23+
use InvalidArgumentException;
24+
use Zhiyi\Plus\Models\User as UserModel;
25+
26+
class ResourceManager implements ResourceManagerInterface
27+
{
28+
/**
29+
* Resource map
30+
* @var array
31+
*/
32+
public static $map = [
33+
\Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed::class => Resources\Feed::class,
34+
];
35+
36+
/**
37+
* Get resource.
38+
* @param mixed $resource
39+
* @param \Zhiyi\Plus\Models\User $sender
40+
* @return \Zhiyi\Plus\AtMessage\ResourceInterface
41+
* @throws \InvalidArgumentException
42+
*/
43+
public function resource($resource, UserModel $sender): ResourceInterface
44+
{
45+
$className = $this->getClassName($resource);
46+
$resourceClass = static::$map[$className] ?? null;
47+
if (! $resourceClass) {
48+
throw new InvalidArgumentException(sprintf(
49+
'Resource [%s] not supported.', $className
50+
));
51+
}
52+
53+
return new $resourceClass($resource, $sender);
54+
}
55+
56+
/**
57+
* Get resource class name.
58+
* @param mixed $resource
59+
* @return string
60+
*/
61+
public function getClassName($resource): string
62+
{
63+
return get_class($resource);
64+
}
65+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\AtMessage;
22+
23+
use Zhiyi\Plus\Models\User as UserModel;
24+
25+
interface ResourceManagerInterface
26+
{
27+
/**
28+
* Get resource.
29+
* @param mixed $resource
30+
* @param \Zhiyi\Plus\Models\User $sender
31+
* @return \Zhiyi\Plus\AtMessage\ResourceInterface
32+
*/
33+
public function resource($resource, UserModel $sender): ResourceInterface;
34+
35+
/**
36+
* Get resource class name.
37+
* @param mixed $resource
38+
* @return string
39+
*/
40+
public function getClassName($resource): string;
41+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\AtMessage\Resources;
22+
23+
use Zhiyi\Plus\Models\User as UserModel;
24+
use Zhiyi\Plus\Types\Models as ModelTypes;
25+
use Zhiyi\Plus\AtMessage\ResourceInterface;
26+
use Zhiyi\Plus\Models\Comment as CommentModel;
27+
28+
class Feed implements ResourceInterface
29+
{
30+
protected $comment;
31+
protected $sender;
32+
33+
public function __construct(CommentModel $comment, UserModel $sender)
34+
{
35+
$this->comment = $comment;
36+
$this->sender = $sender;
37+
}
38+
39+
public function type(): string
40+
{
41+
return ModelTypes::get(CommentModel::class, ModelTypes::KEY_BY_CLASS_ALIAS);
42+
}
43+
44+
public function id(): int
45+
{
46+
return $this->comment->id;
47+
}
48+
49+
public function message(): string
50+
{
51+
return sprintf('%s在评论中@了你', $this->sender->name);
52+
}
53+
}

app/AtMessage/Resources/Feed.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\AtMessage\Resources;
22+
23+
use Zhiyi\Plus\Models\User as UserModel;
24+
use Zhiyi\Plus\Types\Models as ModelTypes;
25+
use Zhiyi\Plus\AtMessage\ResourceInterface;
26+
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Models\Feed as FeedModel;
27+
28+
class Feed implements ResourceInterface
29+
{
30+
protected $feed;
31+
protected $sender;
32+
33+
public function __construct(FeedModel $feed, UserModel $sender)
34+
{
35+
$this->feed = $feed;
36+
$this->sender = $sender;
37+
}
38+
39+
public function type(): string
40+
{
41+
return ModelTypes::get(
42+
FeedModel::class,
43+
ModelTypes::KEY_BY_CLASS_ALIAS
44+
);
45+
}
46+
47+
public function id(): int
48+
{
49+
return $this->feed->id;
50+
}
51+
52+
public function message(): string
53+
{
54+
return sprintf('%s在动态中@了你', $this->sender->name);
55+
}
56+
}

0 commit comments

Comments
 (0)