Skip to content

Commit

Permalink
feat(feed): 增加发布动态 At 好友功能
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Aug 13, 2018
1 parent aeeb37f commit 41df582
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 6 deletions.
38 changes: 38 additions & 0 deletions app/AtMessage/AtMessageHelperTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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;
use Zhiyi\Plus\AtMessage\Message;

trait AtMessageHelperTrait
{
public function sendAtMessage(string $content, UserModel $sender, $resource): void
{
preg_match_all('/\x{00ad}@((?:[^\/]+?))\x{00ad}/iu', $content, $matches);
$users = UserModel::whereIn('name', $matches[1])->get();
$message = app(Message::class);

foreach ($users as $user) {
$message->send($sender, $user, $resource);
}
}
}
8 changes: 7 additions & 1 deletion app/AtMessage/Resources/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,13 @@ public function __construct(CommentModel $comment, UserModel $sender)

public function type(): string
{
return ModelTypes::get(CommentModel::class, ModelTypes::KEY_BY_CLASS_ALIAS);
$alise = ModelTypes::$types[CommentModel::class] ?? null;

if (is_null($alise)) {
throw new InvalidArgumentException('不支持的资源');
}

return $alise;
}

public function id(): int
Expand Down
12 changes: 8 additions & 4 deletions app/AtMessage/Resources/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

namespace Zhiyi\Plus\AtMessage\Resources;

use InvalidArgumentException;
use Zhiyi\Plus\Models\User as UserModel;
use Zhiyi\Plus\Types\Models as ModelTypes;
use Zhiyi\Plus\AtMessage\ResourceInterface;
Expand All @@ -38,10 +39,13 @@ public function __construct(FeedModel $feed, UserModel $sender)

public function type(): string
{
return ModelTypes::get(
FeedModel::class,
ModelTypes::KEY_BY_CLASS_ALIAS
);
$alise = ModelTypes::$types[FeedModel::class] ?? null;

if (is_null($alise)) {
throw new InvalidArgumentException('不支持的资源');
}

return $alise;
}

public function id(): int
Expand Down
12 changes: 12 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@ public function register()
$this->app->singleton(TargetTypeManager::class, function ($app) {
return new TargetTypeManager($app);
});

$this->app->singleton('at-message', function ($app) {
$manager = $app->make(\Zhiyi\Plus\AtMessage\ResourceManagerInterface::class);
$pusher = $app->make(\Zhiyi\Plus\Services\Push::class);
$model = new \Zhiyi\Plus\Models\AtMessage();

return new \Zhiyi\Plus\AtMessage\Message($manager, $model, $pusher);
});

$this->app->singleton('at-resource-manager', function ($app) {
return new \Zhiyi\Plus\AtMessage\ResourceManager($app);
});
}

/**
Expand Down
8 changes: 7 additions & 1 deletion packages/slimkit-plus-feed/src/API2/FeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\Repository\Feed as FeedRepository;
use Zhiyi\Component\ZhiyiPlus\PlusComponentFeed\FormRequest\API2\StoreFeedPost as StoreFeedPostRequest;
use Zhiyi\Plus\AtMessage\AtMessageHelperTrait;

class FeedController extends Controller
{
use AtMessageHelperTrait;
/**
* 分享列表.
*
Expand Down Expand Up @@ -449,7 +451,7 @@ public function store(StoreFeedPostRequest $request)
$videoWith = $this->makeVideoWith($request);
$videoCoverWith = $this->makeVideoCoverWith($request);

return $user->getConnection()->transaction(function () use ($request, $feed, $topics, $paidNodes, $fileWiths, $videoWith, $videoCoverWith, $user) {
$response = $user->getConnection()->transaction(function () use ($request, $feed, $topics, $paidNodes, $fileWiths, $videoWith, $videoCoverWith, $user) {
$feed->save();
$this->saveFeedPaidNode($request, $feed);
$this->saveFeedFilePaidNode($paidNodes, $feed);
Expand All @@ -465,6 +467,10 @@ public function store(StoreFeedPostRequest $request)

return response()->json(['message' => '发布成功', 'id' => $feed->id])->setStatusCode(201);
});

$this->sendAtMessage($feed->feed_content, $user, $feed);

return $response;
}

/**
Expand Down

0 comments on commit 41df582

Please sign in to comment.