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
1 parent
7b3f388
commit c8ea0ed
Showing
10 changed files
with
304 additions
and
1 deletion.
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
16 changes: 16 additions & 0 deletions
16
app/Plugins/Topic/src/Annotation/Topic/UpdateFirstMiddleware.php
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,16 @@ | ||
<?php | ||
|
||
namespace App\Plugins\Topic\src\Annotation\Topic; | ||
|
||
use Attribute; | ||
use Hyperf\Di\Annotation\AbstractAnnotation; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"CLASS"}) | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class UpdateFirstMiddleware extends AbstractAnnotation | ||
{ | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
app/Plugins/Topic/src/Annotation/Topic/UpdateLastMiddleware.php
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,16 @@ | ||
<?php | ||
|
||
namespace App\Plugins\Topic\src\Annotation\Topic; | ||
|
||
use Attribute; | ||
use Hyperf\Di\Annotation\AbstractAnnotation; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"CLASS"}) | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class UpdateLastMiddleware extends AbstractAnnotation | ||
{ | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
app/Plugins/Topic/src/Annotation/Topic/UpdateMiddleware.php
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,16 @@ | ||
<?php | ||
|
||
namespace App\Plugins\Topic\src\Annotation\Topic; | ||
|
||
use Attribute; | ||
use Hyperf\Di\Annotation\AbstractAnnotation; | ||
|
||
/** | ||
* @Annotation | ||
* @Target({"CLASS"}) | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class UpdateMiddleware extends AbstractAnnotation | ||
{ | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
app/Plugins/Topic/src/Handler/Topic/Middleware/Update/PostsOptionsMiddleware.php
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,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of zhuchunshu. | ||
* @link https://github.com/zhuchunshu | ||
* @document https://github.com/zhuchunshu/super-forum | ||
* @contact laravel@88.com | ||
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE | ||
*/ | ||
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update; | ||
|
||
use App\Plugins\Core\src\Models\PostsOption; | ||
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface; | ||
|
||
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateLastMiddleware] | ||
class PostsOptionsMiddleware implements MiddlewareInterface | ||
{ | ||
public function handler($data, \Closure $next) | ||
{ | ||
$post_id = $data['post_id']; | ||
if (! PostsOption::query()->where('post_id', $post_id)->exists()) { | ||
$post_options = PostsOption::create([ | ||
'post_id' => $post_id, | ||
]); | ||
|
||
} else { | ||
$post_options = PostsOption::query()->where('post_id', $post_id)->first(); | ||
} | ||
$data['posts_options'] = $post_options; | ||
return $next($data); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
app/Plugins/Topic/src/Handler/Topic/Middleware/Update/SetDisableCommentMiddleware.php
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of zhuchunshu. | ||
* @link https://github.com/zhuchunshu | ||
* @document https://github.com/zhuchunshu/super-forum | ||
* @contact laravel@88.com | ||
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE | ||
*/ | ||
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update; | ||
|
||
use App\Plugins\Core\src\Models\PostsOption; | ||
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface; | ||
|
||
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateLastMiddleware] | ||
class SetDisableCommentMiddleware implements MiddlewareInterface | ||
{ | ||
public function handler($data, \Closure $next) | ||
{ | ||
$disable_comment = (bool) request()->input('options.disable_comment', false); | ||
$posts_options_id = $data['posts_options']['id']; | ||
PostsOption::query()->where('id', $posts_options_id)->update([ | ||
'disable_comment' => $disable_comment, | ||
]); | ||
return $next($data); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/Plugins/Topic/src/Handler/Topic/Middleware/Update/UpdateEndMiddleware.php
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,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of zhuchunshu. | ||
* @link https://github.com/zhuchunshu | ||
* @document https://github.com/zhuchunshu/super-forum | ||
* @contact laravel@88.com | ||
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE | ||
*/ | ||
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update; | ||
|
||
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface; | ||
|
||
class UpdateEndMiddleware implements MiddlewareInterface | ||
{ | ||
public function handler($data, \Closure $next) | ||
{ | ||
return redirect()->url('/' . $data['topic_id'] . '.html')->with('success', '更新成功!')->go(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
app/Plugins/Topic/src/Handler/Topic/Middleware/Update/UpdateFirstMiddleware.php
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of zhuchunshu. | ||
* @link https://github.com/zhuchunshu | ||
* @document https://github.com/zhuchunshu/super-forum | ||
* @contact laravel@88.com | ||
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE | ||
*/ | ||
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update; | ||
|
||
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface; | ||
use App\Plugins\Topic\src\Models\TopicTag; | ||
use App\Plugins\User\src\Models\UserClass; | ||
|
||
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateFirstMiddleware] | ||
class UpdateFirstMiddleware implements MiddlewareInterface | ||
{ | ||
public function handler($data, \Closure $next) | ||
{ | ||
$class_name = UserClass::query()->where('id', auth()->data()->class_id)->first()->name; | ||
$tag_value = TopicTag::query()->where('id', $data['basis']['tag'])->first(); | ||
if (! user_TopicTagQuanxianCheck($tag_value, $class_name)) { | ||
return redirect()->with('danger', '无权使用此标签')->url('topic/create')->go(); | ||
} | ||
return $next($data); | ||
} | ||
} |
143 changes: 143 additions & 0 deletions
143
app/Plugins/Topic/src/Handler/Topic/Middleware/Update/UpdateMiddleware.php
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,143 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of zhuchunshu. | ||
* @link https://github.com/zhuchunshu | ||
* @document https://github.com/zhuchunshu/super-forum | ||
* @contact laravel@88.com | ||
* @license https://github.com/zhuchunshu/super-forum/blob/master/LICENSE | ||
*/ | ||
namespace App\Plugins\Topic\src\Handler\Topic\Middleware\Update; | ||
|
||
use App\Plugins\Core\src\Models\Post; | ||
use App\Plugins\Topic\src\Handler\Topic\Middleware\MiddlewareInterface; | ||
use App\Plugins\Topic\src\Models\Topic; | ||
use App\Plugins\Topic\src\Models\TopicKeyword; | ||
use App\Plugins\Topic\src\Models\TopicKeywordsWith; | ||
use App\Plugins\Topic\src\Models\TopicUpdated; | ||
use App\Plugins\User\src\Models\User; | ||
use Hyperf\Di\Annotation\Inject; | ||
use Hyperf\Validation\Contract\ValidatorFactoryInterface; | ||
|
||
#[\App\Plugins\Topic\src\Annotation\Topic\UpdateMiddleware] | ||
class UpdateMiddleware implements MiddlewareInterface | ||
{ | ||
/** | ||
* @Inject | ||
*/ | ||
protected ValidatorFactoryInterface $validationFactory; | ||
|
||
public function handler($data, \Closure $next) | ||
{ | ||
$validator = $this->validationFactory->make( | ||
$data['basis'], | ||
[ | ||
'topic_id' => 'required|exists:topic,id', | ||
'content' => 'required|string|min:' . get_options('topic_create_content_min', 10), | ||
'title' => 'required|string|min:' . get_options('topic_create_title_min', 1) . '|max:' . get_options('topic_create_title_max', 200), | ||
'tag' => 'required|exists:topic_tag,id', | ||
], | ||
[ | ||
'topic_id' => '帖子id', | ||
'content' => '正文内容', | ||
'title' => '标题', | ||
'tag' => '标签id', | ||
] | ||
); | ||
|
||
if ($validator->fails()) { | ||
return redirect()->with('danger', $validator->errors()->first())->url('topic/' . $data['basis']['topic_id'] . '/edit')->go(); | ||
} | ||
$data['topic_id'] = $data['basis']['topic_id']; | ||
$data = $this->update($data); | ||
return $next($data); | ||
} | ||
|
||
public function update($data) | ||
{ | ||
// 帖子id | ||
$topic_id = $data['topic_id']; | ||
// 帖子标题 | ||
$title = $data['basis']['title']; | ||
// 帖子标签 | ||
$tag = $data['basis']['tag']; | ||
// 帖子html内容 | ||
$content = $data['basis']['content']; | ||
$content = xss()->clean($content); | ||
|
||
// 解析标签 | ||
$_content = $content; | ||
$content = $this->tag($content); | ||
// 解析艾特 | ||
$content = $this->at($content); | ||
|
||
$post_id = Topic::query()->find($topic_id)->post_id; | ||
Post::query()->where('id', $post_id)->update([ | ||
'content' => $content, | ||
]); | ||
Topic::query()->where('id', $topic_id)->update([ | ||
'title' => $title, | ||
'tag_id' => $tag, | ||
]); | ||
$topic = Topic::query()->where('id', $topic_id)->first(); | ||
TopicUpdated::create([ | ||
'topic_id' => $topic_id, | ||
'user_id' => auth()->id(), | ||
'user_ip' => get_client_ip(), | ||
'user_agent' => get_user_agent(), | ||
]); | ||
$this->topic_keywords($topic, $_content); | ||
$topic_data = Topic::query()->find($topic_id); | ||
$this->at_user($topic_data, $_content); | ||
cache()->delete('topic.data.' . $topic_id); | ||
$data['topic_id'] = $topic_id; | ||
$data['post_id'] = $post_id; | ||
return $data; | ||
} | ||
|
||
public function tag(string $html) | ||
{ | ||
return replace_all_keywords($html); | ||
} | ||
|
||
public function at(string $html): string | ||
{ | ||
return replace_all_at($html); | ||
} | ||
|
||
public function topic_keywords($data, string $html): void | ||
{ | ||
foreach (get_all_keywords($html) as $tag) { | ||
if (! TopicKeyword::query()->where('name', $tag)->exists()) { | ||
TopicKeyword::query()->create([ | ||
'name' => $tag, | ||
'user_id' => auth()->id(), | ||
]); | ||
} | ||
$tk = TopicKeyword::query()->where('name', $tag)->first(); | ||
if (! TopicKeywordsWith::query()->where(['topic_id' => $data->id, 'with_id' => $tk->id])->exists()) { | ||
TopicKeywordsWith::query()->create([ | ||
'topic_id' => $data->id, | ||
'with_id' => $tk->id, | ||
'user_id' => auth()->id(), | ||
]); | ||
} | ||
} | ||
} | ||
|
||
private function at_user(\Hyperf\Database\Model\Model | \Hyperf\Database\Model\Builder $data, string $html): void | ||
{ | ||
$at_user = get_all_at($html); | ||
foreach ($at_user as $value) { | ||
go(function () use ($value, $data) { | ||
if (User::query()->where('username', $value)->exists()) { | ||
$user = User::query()->where('username', $value)->first(); | ||
if ($user->id != $data->user_id) { | ||
user_notice()->send($user->id, '有人在帖子中提到了你', $user->username . '在帖子<b>' . $data->title . '</b>中提到了你', '/' . $data->id . '.html'); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
} |