Skip to content

Commit

Permalink
Merge pull request #2 from mylxsw/master
Browse files Browse the repository at this point in the history
merge latest code
  • Loading branch information
ro4 committed May 10, 2018
2 parents 41e22ee + 4ff3457 commit bb0702b
Show file tree
Hide file tree
Showing 23 changed files with 1,930 additions and 63 deletions.
40 changes: 40 additions & 0 deletions .env.local
@@ -0,0 +1,40 @@
APP_NAME="Wizard 文档库"
APP_ENV=local
APP_KEY=base64:9ZqHD9ABVYkfN1il53mBYAqN/ZqEeSvUz3zPt2z3KFM=
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=wizard
DB_USERNAME=root
DB_PASSWORD=

BROADCAST_DRIVER=log
CACHE_DRIVER=file
SESSION_DRIVER=file
QUEUE_DRIVER=sync

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_DRIVER=smtp
MAIL_HOST=smtp.qq.com
MAIL_PORT=465
MAIL_USERNAME=wizard@aicode.cc
MAIL_PASSWORD=zewhczghsmbabceb
MAIL_ENCRYPTION=ssl
MAIL_FROM_NAME=Wizard

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=

APP_TIMEZONE=Asia/Shanghai
WIZARD_NEED_ACTIVATE=false
WIZARD_JWT_SECRET=5Dck4izVEXsP52AOcVxaFUsTzA8uwUiP

DEBUGBAR_ENABLED=true
40 changes: 34 additions & 6 deletions app/Http/Controllers/AttachmentController.php
Expand Up @@ -50,8 +50,11 @@ class AttachmentController extends Controller
* 上传文件
*
* @param Request $request
* @param $id
* @param $page_id
*
* @return array
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function upload(Request $request, $id, $page_id)
{
Expand All @@ -71,7 +74,7 @@ public function upload(Request $request, $id, $page_id)

$this->validateParameters(
[
'extension' => $extension,
'extension' => strtolower($extension),
'project_id' => $id,
'page_id' => $page_id,
],
Expand All @@ -87,7 +90,10 @@ public function upload(Request $request, $id, $page_id)

$this->authorize('page-edit', $page_id);

$path = $file->storePublicly(sprintf('public/%s', date('Y/m-d')));
$path = $file->storePubliclyAs(
sprintf('public/%s', date('Y/m-d')),
$this->getSaveAsName($file, $extension)
);
$name = $request->input('name');

Attachment::create([
Expand All @@ -105,13 +111,35 @@ public function upload(Request $request, $id, $page_id)
/**
* 获取文件扩展名
*
* 有些文件,通过mime类型无法获取类型,所以直接读取文件扩展名
* 直接读取文件扩展名
*
* @param \Illuminate\Http\UploadedFile $file
* @param $file
*
* @return string
*/
private function getFileExtension($file)
{
return $file->extension() ?? $file->getClientOriginalExtension();
return $file->getClientOriginalExtension();
}

/**
* 获取上传后保存的文件名
*
* 解决上传文件无法获取mime类型而存储为默认的zip格式的问题
*
* @param \Illuminate\Http\UploadedFile $file
* @param string $ext
*
* @return string
*/
private function getSaveAsName($file, $ext): string
{
if (ends_with($file, $ext)) {
return $file->hashName();
}

$guessedName = $file->hashName();
return substr($guessedName, 0, strrpos($guessedName, '.')) . '.' . $ext;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/CommentController.php
Expand Up @@ -54,7 +54,7 @@ public function publish(Request $request, $id, $page_id)
}

$comment = Comment::create([
'content' => $content,// TODO XSS过滤
'content' => comment_filter($content),// TODO XSS过滤
'user_id' => \Auth::user()->id,
'reply_to_id' => 0,
'page_id' => $page_id,
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/FileController.php
Expand Up @@ -28,7 +28,7 @@ public function imageUpload(Request $request)
__('common.upload.failed', ['reason' => $file->getErrorMessage()]));
}

if (!in_array($file->extension(), ["jpg", "jpeg", "gif", "png", "bmp"])) {
if (!in_array(strtolower($file->extension()), ["jpg", "jpeg", "gif", "png", "bmp"])) {
return $this->response(false, __('common.upload.invalid_type'));
}

Expand Down
5 changes: 3 additions & 2 deletions app/Http/Controllers/HomeController.php
Expand Up @@ -46,6 +46,7 @@ public function home(Request $request, $catalog = 0)

/** @var Project $projectModel */
$projectModel = Project::query();
$projectModel->withCount('pages');
if (!empty($name)) {
$projectModel->where('name', 'like', "%{$name}%");
} else {
Expand Down Expand Up @@ -95,9 +96,9 @@ public function home(Request $request, $catalog = 0)
// 3. 页码为第一页
if (!empty($user) && empty($name) && $page === 1) {
if (!empty($catalogId)) {
$favorites = $user->favoriteProjects()->where('catalog_id', $catalogId)->get();
$favorites = $user->favoriteProjects()->where('catalog_id', $catalogId)->withCount('pages')->get();
} else {
$favorites = $user->favoriteProjects;
$favorites = $user->favoriteProjects()->withCount('pages')->get();
}
}
return view('index', [
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/ProjectController.php
Expand Up @@ -37,6 +37,7 @@ public function home(Request $request)

/** @var Project $projectModel */
$projectModel = Project::query();
$projectModel->withCount('pages');
if (!empty($name)) {
$projectModel->where('name', 'like', "%{$name}%");
}
Expand Down
19 changes: 17 additions & 2 deletions app/Listeners/CommentCreatedListener.php
Expand Up @@ -13,6 +13,7 @@
use App\Notifications\CommentReplied;
use App\Notifications\DocumentCommented;
use App\Repositories\Comment;
use App\Repositories\OperationLogs;
use App\Repositories\User;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -28,8 +29,22 @@ class CommentCreatedListener
*/
public function handle(CommentCreated $event)
{
// 通知相关用户
$comment = $event->getComment();

OperationLogs::log(
\Auth::user()->id,
'comment_created',
[
'username' => $comment->user->name,
'user_id' => $comment->user_id,
'project_name' => $comment->document->project->name,
'project_id' => $comment->document->project->id,
'doc_title' => $comment->document->title,
'doc_id' => $comment->document->id
]
);

// 通知相关用户
if ($comment->user_id != $comment->document->user_id) {
$comment->document->user->notify(new DocumentCommented($comment->document, $comment));
}
Expand All @@ -44,7 +59,7 @@ public function handle(CommentCreated $event)
}

// 解析文本内容,如果包含@user,则通知该用户
$users = parseUsersFromContent($comment->content);
$users = comment_filter_users($comment->content);
if (!empty($users)) {
$users->map(function (User $user) use ($comment) {
$user->notify(new CommentMentioned($comment->document, $comment));
Expand Down
106 changes: 89 additions & 17 deletions app/helpers.php
Expand Up @@ -248,23 +248,6 @@ function resourceVersion()
return $version;
}

/**
* 从内容中解析出用户
*
* @param string $content
*
* @return \Illuminate\Database\Eloquent\Collection|null|static[]
*/
function parseUsersFromContent($content)
{
preg_match_all('/@(.*?)(?:\s|$)/', $content, $matches);
if (!empty($matches[1])) {
$users = User::whereIn('name', $matches[1])->select('id', 'name', 'email')->get();
return $users;
}

return null;
}

/**
* 创建一个JWT Token
Expand Down Expand Up @@ -321,4 +304,93 @@ function user_face($id)
{
$identicon = new Identicon\Identicon();
return $identicon->getImageDataUri($id);
}

/**
* 获取所有用户列表
*
* @return \Illuminate\Database\Eloquent\Collection
*/
function users()
{
static $users = null;
if (is_null($users)) {
$users = User::all();
}

return $users;
}

/**
* 用户名列表(js数组)
*
* @param \Illuminate\Database\Eloquent\Collection $users
* @param bool $actived
*
* @return string
*/
function ui_usernames(\Illuminate\Database\Eloquent\Collection $users, $actived = true)
{
return $users->filter(function (User $user) use ($actived) {
return $actived ? $user->isActivated() : true;
})->map(function (User $user) {
return "'{$user->name}'";
})->implode(',');
}

/**
* 从内容中解析出用户
*
* @param string $content
*
* @return \Illuminate\Database\Eloquent\Collection|null
*/
function comment_filter_users($content)
{
preg_match_all('/@{uid:(\d+)}/', $content, $matches);
if (!empty($matches[1])) {
$users = User::whereIn('id', $matches[1])->select('id', 'name', 'email')->get();
return $users;
}

return null;
}


/**
* 对评论信息预处理
*
* @param string $comment
*
* @return string
*/
function comment_filter(string $comment): string
{
$matchRegexp = '/@(.*?)(?:\s|$)/';

$users = (function ($content) use ($matchRegexp) {
preg_match_all($matchRegexp, $content, $matches);
if (!empty($matches[1])) {
$users = User::whereIn('name', $matches[1])->select('id', 'name', 'email')->get();
return $users;
}

return null;
})($comment);
if (is_null($users) || $users->isEmpty()) {
return $comment;
}

return preg_replace_callback($matchRegexp, function ($matches) use ($users) {
if (count($matches) < 2) {
return $matches[0];
}

$user = $users->firstWhere('name', '=', $matches[1]);
if (!empty($user)) {
return "@{uid:{$user->id}} ";
}

return $matches[0];
}, $comment);
}

0 comments on commit bb0702b

Please sign in to comment.