|
| 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 | +} |
0 commit comments