Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
IEQEFCR
halo
提交
1c9f561f
H
halo
项目概览
IEQEFCR
/
halo
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
H
halo
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
1c9f561f
编写于
4月 24, 2019
作者:
J
johnniang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Refactor comment creation
上级
85ba526a
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
90 addition
and
123 deletion
+90
-123
src/main/java/run/halo/app/model/params/CommentParam.java
src/main/java/run/halo/app/model/params/CommentParam.java
+1
-0
src/main/java/run/halo/app/service/CommentService.java
src/main/java/run/halo/app/service/CommentService.java
+9
-10
src/main/java/run/halo/app/service/impl/CommentServiceImpl.java
...in/java/run/halo/app/service/impl/CommentServiceImpl.java
+48
-34
src/main/java/run/halo/app/utils/ServiceUtils.java
src/main/java/run/halo/app/utils/ServiceUtils.java
+11
-0
src/main/java/run/halo/app/utils/ServletUtils.java
src/main/java/run/halo/app/utils/ServletUtils.java
+11
-0
src/main/java/run/halo/app/web/controller/admin/api/CommentController.java
.../halo/app/web/controller/admin/api/CommentController.java
+4
-34
src/main/java/run/halo/app/web/controller/content/api/CommentController.java
...alo/app/web/controller/content/api/CommentController.java
+6
-45
未找到文件。
src/main/java/run/halo/app/model/params/CommentParam.java
浏览文件 @
1c9f561f
package
run.halo.app.model.params
;
import
org.hibernate.validator.constraints.URL
;
import
run.halo.app.model.dto.base.InputConverter
;
import
run.halo.app.model.entity.Comment
;
import
lombok.Data
;
...
...
src/main/java/run/halo/app/service/CommentService.java
浏览文件 @
1c9f561f
package
run.halo.app.service
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.lang.NonNull
;
import
org.springframework.lang.Nullable
;
import
run.halo.app.model.entity.Comment
;
import
run.halo.app.model.enums.CommentStatus
;
import
run.halo.app.model.params.CommentParam
;
import
run.halo.app.model.params.CommentQuery
;
import
run.halo.app.model.vo.CommentVO
;
import
run.halo.app.model.vo.CommentWithParentVO
;
import
run.halo.app.model.vo.CommentWithPostVO
;
import
run.halo.app.model.vo.CommentVO
;
import
run.halo.app.service.base.CrudService
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.lang.NonNull
;
import
org.springframework.lang.Nullable
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.Collection
;
import
java.util.List
;
import
java.util.Map
;
...
...
@@ -54,7 +54,7 @@ public interface CommentService extends CrudService<Comment, Long> {
* Pages comments.
*
* @param commentQuery comment query must not be null
* @param pageable page info must not be null
* @param pageable
page info must not be null
* @return a page of comment
*/
@NonNull
...
...
@@ -81,12 +81,11 @@ public interface CommentService extends CrudService<Comment, Long> {
/**
* Creates a comment by comment param.
*
* @param comment comment must not be null
* @param request http servlet request must not be null
* @param commentParam comment param must not be null
* @return created comment
*/
@NonNull
Comment
createBy
(
@NonNull
Comment
comment
,
@NonNull
HttpServletRequest
request
);
Comment
createBy
(
@NonNull
Comment
Param
commentParam
);
/**
* Lists comment vos by post id.
...
...
src/main/java/run/halo/app/service/impl/CommentServiceImpl.java
浏览文件 @
1c9f561f
package
run.halo.app.service.impl
;
import
cn.hutool.core.util.URLUtil
;
import
cn.hutool.crypto.SecureUtil
;
import
cn.hutool.extra.servlet.ServletUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.apache.commons.codec.digest.DigestUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.context.ApplicationEventPublisher
;
import
org.springframework.data.domain.*
;
...
...
@@ -19,12 +17,16 @@ import org.springframework.web.util.HtmlUtils;
import
run.halo.app.event.comment.CommentNewEvent
;
import
run.halo.app.event.comment.CommentPassEvent
;
import
run.halo.app.event.comment.CommentReplyEvent
;
import
run.halo.app.exception.NotFoundException
;
import
run.halo.app.model.dto.post.PostMinimalOutputDTO
;
import
run.halo.app.model.entity.Comment
;
import
run.halo.app.model.entity.Post
;
import
run.halo.app.model.entity.User
;
import
run.halo.app.model.enums.CommentStatus
;
import
run.halo.app.model.params.CommentParam
;
import
run.halo.app.model.params.CommentQuery
;
import
run.halo.app.model.projection.CommentCountProjection
;
import
run.halo.app.model.properties.BlogProperties
;
import
run.halo.app.model.properties.CommentProperties
;
import
run.halo.app.model.support.CommentPage
;
import
run.halo.app.model.vo.CommentVO
;
...
...
@@ -37,11 +39,11 @@ import run.halo.app.security.context.SecurityContextHolder;
import
run.halo.app.service.CommentService
;
import
run.halo.app.service.OptionService
;
import
run.halo.app.service.base.AbstractCrudService
;
import
run.halo.app.utils.OwoUtil
;
import
run.halo.app.utils.ServiceUtils
;
import
run.halo.app.utils.ServletUtils
;
import
run.halo.app.utils.ValidationUtils
;
import
javax.persistence.criteria.Predicate
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.*
;
import
java.util.stream.Collectors
;
...
...
@@ -149,53 +151,65 @@ public class CommentServiceImpl extends AbstractCrudService<Comment, Long> imple
}
@Override
public
Comment
createBy
(
Comment
comment
,
HttpServletRequest
request
)
{
Assert
.
notNull
(
comment
,
"Comment must not be null"
);
Assert
.
notNull
(
request
,
"Http servlet request must not be null"
);
// Set some default value
comment
.
setContent
(
OwoUtil
.
parseOwo
(
formatContent
(
comment
.
getContent
())));
comment
.
setIpAddress
(
ServletUtil
.
getClientIP
(
request
));
comment
.
setUserAgent
(
ServletUtil
.
getHeaderIgnoreCase
(
request
,
HttpHeaders
.
USER_AGENT
));
public
Comment
createBy
(
CommentParam
commentParam
)
{
Assert
.
notNull
(
commentParam
,
"Comment param must not be null"
);
// Check user login status and set this field
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
if
(
authentication
!=
null
)
{
User
user
=
authentication
.
getDetail
().
getUser
();
commentParam
.
setAuthor
(
StringUtils
.
isEmpty
(
user
.
getNickname
())
?
user
.
getUsername
()
:
user
.
getNickname
());
commentParam
.
setEmail
(
user
.
getEmail
());
commentParam
.
setAuthorUrl
(
optionService
.
getByPropertyOfNullable
(
BlogProperties
.
BLOG_URL
));
}
// Validate the comment param manually
ValidationUtils
.
validate
(
commentParam
);
// Check post id
boolean
isPostExist
=
postRepository
.
existsById
(
commentParam
.
getPostId
());
if
(!
isPostExist
)
{
throw
new
NotFoundException
(
"The post with id "
+
commentParam
.
getPostId
()
+
" was not found"
);
}
// Check parent id
if
(!
ServiceUtils
.
isEmptyId
(
commentParam
.
getParentId
()))
{
mustExistById
(
commentParam
.
getParentId
());
}
// Convert to comment
Comment
comment
=
commentParam
.
convertTo
();
// Set some default values
comment
.
setIpAddress
(
ServletUtils
.
getRequestIp
());
comment
.
setUserAgent
(
ServletUtils
.
getHeaderIgnoreCase
(
HttpHeaders
.
USER_AGENT
));
comment
.
setGavatarMd5
(
DigestUtils
.
md5Hex
(
comment
.
getEmail
()));
if
(
authentication
!=
null
)
{
//
If the user is login
//
Comment of blogger
comment
.
setIsAdmin
(
true
);
comment
.
setStatus
(
CommentStatus
.
PUBLISHED
);
}
else
{
// Comment of guest
// Handle comment status
Boolean
needAudit
=
optionService
.
getByPropertyOrDefault
(
CommentProperties
.
NEW_NEED_CHECK
,
Boolean
.
class
,
true
);
if
(
needAudit
)
{
comment
.
setStatus
(
CommentStatus
.
AUDITING
);
}
else
{
comment
.
setStatus
(
CommentStatus
.
PUBLISHED
);
}
}
comment
.
setAuthor
(
HtmlUtils
.
htmlEscape
(
comment
.
getAuthor
()));
comment
.
setGavatarMd5
(
SecureUtil
.
md5
(
comment
.
getEmail
()));
if
(
StringUtils
.
isNotBlank
(
comment
.
getAuthorUrl
()))
{
// Normalize the author url and set it
comment
.
setAuthorUrl
(
URLUtil
.
normalize
(
comment
.
getAuthorUrl
()));
comment
.
setStatus
(
needAudit
?
CommentStatus
.
AUDITING
:
CommentStatus
.
PUBLISHED
);
}
// Create comment
Comment
createdComment
=
create
(
comment
);
// TODO Handle email sending
if
(
createdComment
.
getParentId
()
==
null
||
createdComment
.
getParentId
()
==
0
)
{
// New comment
eventPublisher
.
publishEvent
(
new
CommentNewEvent
(
this
,
createdComment
.
getId
()));
if
(
ServiceUtils
.
isEmptyId
(
createdComment
.
getParentId
()))
{
if
(
authentication
==
null
)
{
// New comment of guest
eventPublisher
.
publishEvent
(
new
CommentNewEvent
(
this
,
createdComment
.
getId
()));
}
}
else
{
// Reply comment
eventPublisher
.
publishEvent
(
new
CommentReplyEvent
(
this
,
createdComment
.
getId
()));
}
return
createdComment
;
}
...
...
src/main/java/run/halo/app/utils/ServiceUtils.java
浏览文件 @
1c9f561f
package
run.halo.app.utils
;
import
org.springframework.lang.Nullable
;
import
org.springframework.util.Assert
;
import
org.springframework.util.CollectionUtils
;
...
...
@@ -105,4 +106,14 @@ public class ServiceUtils {
return
resultMap
;
}
/**
* Checks if the given number id is empty id.
*
* @param id the given number id
* @return true if the given number id is empty id; false otherwise
*/
public
static
boolean
isEmptyId
(
@Nullable
Number
id
)
{
return
id
==
null
||
id
.
longValue
()
<=
0
;
}
}
src/main/java/run/halo/app/utils/ServletUtils.java
浏览文件 @
1c9f561f
...
...
@@ -43,4 +43,15 @@ public class ServletUtils {
return
getCurrentRequest
().
map
(
ServletUtil:
:
getClientIP
).
orElse
(
null
);
}
/**
* Gets request header.
*
* @param header http header name
* @return http header of null
*/
@Nullable
public
static
String
getHeaderIgnoreCase
(
String
header
)
{
return
getCurrentRequest
().
map
(
request
->
ServletUtil
.
getHeaderIgnoreCase
(
request
,
header
)).
orElse
(
null
);
}
}
src/main/java/run/halo/app/web/controller/admin/api/CommentController.java
浏览文件 @
1c9f561f
package
run.halo.app.web.controller.admin.api
;
import
io.swagger.annotations.ApiOperation
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.data.domain.Page
;
import
org.springframework.data.domain.Pageable
;
import
org.springframework.data.web.PageableDefault
;
import
org.springframework.web.bind.annotation.*
;
import
run.halo.app.model.dto.CommentDTO
;
import
run.halo.app.model.entity.Comment
;
import
run.halo.app.model.entity.User
;
import
run.halo.app.model.enums.CommentStatus
;
import
run.halo.app.model.params.CommentParam
;
import
run.halo.app.model.params.CommentQuery
;
import
run.halo.app.model.properties.BlogProperties
;
import
run.halo.app.model.vo.CommentWithPostVO
;
import
run.halo.app.service.CommentService
;
import
run.halo.app.service.OptionService
;
import
run.halo.app.service.PostService
;
import
run.halo.app.utils.ValidationUtils
;
import
javax.servlet.http.HttpServletRequest
;
import
java.util.List
;
import
static
org
.
springframework
.
data
.
domain
.
Sort
.
Direction
.
DESC
;
...
...
@@ -36,16 +29,8 @@ public class CommentController {
private
final
CommentService
commentService
;
private
final
PostService
postService
;
private
final
OptionService
optionService
;
public
CommentController
(
CommentService
commentService
,
PostService
postService
,
OptionService
optionService
)
{
public
CommentController
(
CommentService
commentService
)
{
this
.
commentService
=
commentService
;
this
.
postService
=
postService
;
this
.
optionService
=
optionService
;
}
@GetMapping
...
...
@@ -68,24 +53,9 @@ public class CommentController {
}
@PostMapping
public
CommentDTO
createBy
(
@RequestBody
CommentParam
commentParam
,
HttpServletRequest
request
,
User
user
)
{
// Set some default info
commentParam
.
setAuthor
(
StringUtils
.
isEmpty
(
user
.
getNickname
())
?
user
.
getUsername
()
:
user
.
getNickname
());
commentParam
.
setEmail
(
user
.
getEmail
());
commentParam
.
setAuthorUrl
(
optionService
.
getByPropertyOfNullable
(
BlogProperties
.
BLOG_URL
));
// Validate the comment param manually
ValidationUtils
.
validate
(
commentParam
);
// Check post id
postService
.
mustExistById
(
commentParam
.
getPostId
());
// Check parent id
if
(
commentParam
.
getParentId
()
!=
null
&&
commentParam
.
getParentId
()
>
0
)
{
commentService
.
mustExistById
(
commentParam
.
getParentId
());
}
return
new
CommentDTO
().
convertFrom
(
commentService
.
createBy
(
commentParam
.
convertTo
(),
request
));
@ApiOperation
(
"Creates a comment (new or reply)"
)
public
CommentDTO
createBy
(
@RequestBody
CommentParam
commentParam
)
{
return
new
CommentDTO
().
convertFrom
(
commentService
.
createBy
(
commentParam
));
}
@PutMapping
(
"{commentId:\\d+}/status/{status}"
)
...
...
src/main/java/run/halo/app/web/controller/content/api/CommentController.java
浏览文件 @
1c9f561f
package
run.halo.app.web.controller.content.api
;
import
io.swagger.annotations.ApiOperation
;
import
run.halo.app.model.dto.CommentDTO
;
import
run.halo.app.model.entity.User
;
import
run.halo.app.model.params.CommentParam
;
import
run.halo.app.model.properties.BlogProperties
;
import
run.halo.app.security.authentication.Authentication
;
import
run.halo.app.security.context.SecurityContextHolder
;
import
run.halo.app.service.CommentService
;
import
run.halo.app.service.OptionService
;
import
run.halo.app.service.PostService
;
import
run.halo.app.utils.ValidationUtils
;
import
org.apache.commons.lang3.StringUtils
;
import
org.springframework.web.bind.annotation.PostMapping
;
import
org.springframework.web.bind.annotation.RequestBody
;
import
org.springframework.web.bind.annotation.RequestMapping
;
import
org.springframework.web.bind.annotation.RestController
;
import
javax.servlet.http.HttpServletRequest
;
import
run.halo.app.model.dto.CommentDTO
;
import
run.halo.app.model.params.CommentParam
;
import
run.halo.app.service.CommentService
;
/**
* Portal comment controller.
...
...
@@ -31,42 +21,13 @@ public class CommentController {
private
final
CommentService
commentService
;
private
final
OptionService
optionService
;
private
final
PostService
postService
;
public
CommentController
(
CommentService
commentService
,
OptionService
optionService
,
PostService
postService
)
{
public
CommentController
(
CommentService
commentService
)
{
this
.
commentService
=
commentService
;
this
.
optionService
=
optionService
;
this
.
postService
=
postService
;
}
@PostMapping
@ApiOperation
(
"Comments a post"
)
public
CommentDTO
comment
(
@RequestBody
CommentParam
commentParam
,
HttpServletRequest
request
)
{
// Get authentication
Authentication
authentication
=
SecurityContextHolder
.
getContext
().
getAuthentication
();
if
(
authentication
!=
null
)
{
User
user
=
authentication
.
getDetail
().
getUser
();
// If the admin is login
commentParam
.
setAuthor
(
StringUtils
.
isEmpty
(
user
.
getNickname
())
?
user
.
getUsername
()
:
user
.
getNickname
());
commentParam
.
setEmail
(
user
.
getEmail
());
commentParam
.
setAuthorUrl
(
optionService
.
getByPropertyOfNullable
(
BlogProperties
.
BLOG_URL
));
}
// Validate the comment param manually
ValidationUtils
.
validate
(
commentParam
);
// Check post id
postService
.
mustExistById
(
commentParam
.
getPostId
());
// Check parent id
if
(
commentParam
.
getParentId
()
!=
null
&&
commentParam
.
getParentId
()
>
0
)
{
commentService
.
mustExistById
(
commentParam
.
getParentId
());
}
return
new
CommentDTO
().
convertFrom
(
commentService
.
createBy
(
commentParam
.
convertTo
(),
request
));
public
CommentDTO
comment
(
@RequestBody
CommentParam
commentParam
)
{
return
new
CommentDTO
().
convertFrom
(
commentService
.
createBy
(
commentParam
));
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录