Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Oct 30, 2024
1 parent 211183b commit 9e12045
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class AddCommentController(ipBlockDao: IPBlockDao, commentPrepareService: Commen

val postscore = topicPermissionService.getPostscore(add.getTopic)

new ModelAndView("add_comment", (commentService.prepareReplyto(add, currentUser.map(_.user), tmpl.getProf, add.getTopic) + (
new ModelAndView("add_comment", (commentService.prepareReplyto(add, currentUser, tmpl.getProf, add.getTopic) + (
"postscoreInfo" -> TopicPermissionService.getPostScoreInfo(postscore)
)).asJava)
}
Expand Down Expand Up @@ -124,7 +124,7 @@ class AddCommentController(ipBlockDao: IPBlockDao, commentPrepareService: Commen

add.setMsg(StringUtil.escapeForceHtml(add.getMsg))

new ModelAndView("add_comment", (commentService.prepareReplyto(add, sessionUserOpt.map(_.user), tmpl.getProf, add.getTopic) ++ info).asJava)
new ModelAndView("add_comment", (commentService.prepareReplyto(add, sessionUserOpt, tmpl.getProf, add.getTopic) ++ info).asJava)
} else {
val (msgid, mentions) = commentService.create(user, comment, msg, remoteAddress = request.getRemoteAddr,
xForwardedFor = Option(request.getHeader("X-Forwarded-For")), userAgent = Option(request.getHeader("user-agent")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,11 +212,11 @@ class CommentCreateService(commentDao: CommentDao, topicDao: TopicDao, userServi
}

@throws[UserNotFoundException]
def prepareReplyto(add: CommentRequest, currentUser: Option[User], profile: Profile, topic: Topic): Map[String, AnyRef] = {
def prepareReplyto(add: CommentRequest, currentUser: Option[CurrentUser], profile: Profile, topic: Topic): Map[String, AnyRef] = {
if (add.getReplyto != null) {
val ignoreList = currentUser.map(user => ignoreListDao.get(user.getId)).getOrElse(Set.empty)
val ignoreList = currentUser.map(user => ignoreListDao.get(user.user.getId)).getOrElse(Set.empty)

val preparedComment = commentPrepareService.prepareCommentOnly(add.getReplyto, currentUser.orNull, profile, topic, ignoreList)
val preparedComment = commentPrepareService.prepareCommentOnly(add.getReplyto, currentUser, profile, topic, ignoreList)

Map("onComment" -> preparedComment)
} else {
Expand Down
22 changes: 11 additions & 11 deletions src/main/scala/ru/org/linux/comment/CommentPrepareService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ru.org.linux.comment
import com.google.common.base.Strings
import org.joda.time.{DateTime, Duration}
import org.springframework.stereotype.Service
import ru.org.linux.auth.CurrentUser
import ru.org.linux.group.{Group, GroupDao}
import ru.org.linux.markup.MessageTextService
import ru.org.linux.reaction.{PreparedReactions, ReactionService}
Expand All @@ -25,7 +26,6 @@ import ru.org.linux.spring.dao.{DeleteInfoDao, MessageText, MsgbaseDao, UserAgen
import ru.org.linux.topic.{Topic, TopicPermissionService}
import ru.org.linux.user.*

import javax.annotation.Nullable
import scala.jdk.CollectionConverters.*
import scala.jdk.OptionConverters.*

Expand All @@ -37,7 +37,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase

private def prepareComment(messageText: MessageText, author: User, remark: Option[String], comment: Comment,
comments: Option[CommentList], profile: Profile, topic: Topic,
hideSet: Set[Int], samePageComments: Set[Int], currentUser: Option[User],
hideSet: Set[Int], samePageComments: Set[Int], currentUser: Option[CurrentUser],
group: Group, ignoreList: Set[Int], filterShow: Boolean) = {
val processedMessage = textService.renderCommentText(messageText, !topicPermissionService.followAuthorLinks(author))

Expand Down Expand Up @@ -96,15 +96,15 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
val apiDeleteInfo = deleteInfo.map(i => new ApiDeleteInfo(userService.getUserCached(i.userid).getNick, i.getReason))
val editSummary = loadEditSummary(comment)

val (postIP, userAgent) = if (currentUser != null && currentUser.exists(_.isModerator)) {
val (postIP, userAgent) = if (currentUser != null && currentUser.exists(_.moderator)) {
(Option(comment.postIP), userAgentDao.getUserAgentById(comment.userAgentId).toScala)
} else {
(None, None)
}

val undeletable = topicPermissionService.isUndeletable(topic, comment, currentUser.orNull, deleteInfo)
val deletable = topicPermissionService.isCommentDeletableNow(comment, currentUser.orNull, topic, hasAnswers)
val editable = topicPermissionService.isCommentEditableNow(comment, currentUser.orNull, hasAnswers, topic, messageText.markup)
val undeletable = topicPermissionService.isUndeletable(topic, comment, currentUser.map(_.user).orNull, deleteInfo)
val deletable = topicPermissionService.isCommentDeletableNow(comment, currentUser.map(_.user).orNull, topic, hasAnswers)
val editable = topicPermissionService.isCommentEditableNow(comment, currentUser.map(_.user).orNull, hasAnswers, topic, messageText.markup)

val authorReadonly = !topicPermissionService.isCommentsAllowed(group, topic, Some(author), ignoreFrozen = true)

Expand All @@ -113,7 +113,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
editSummary = editSummary, postIP = postIP, userAgent = userAgent, undeletable = undeletable,
answerCount = answerCount, answerLink = answerLink, answerSamepage = answerSamepage,
authorReadonly = authorReadonly,
reactions = reactionPrepareService.prepare(comment.reactions, ignoreList, currentUser, topic, Some(comment)))
reactions = reactionPrepareService.prepare(comment.reactions, ignoreList, currentUser.map(_.user), topic, Some(comment)))
}

private def loadDeleteInfo(comment: Comment) = {
Expand All @@ -132,14 +132,14 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
}
}

def prepareCommentOnly(comment: Comment, @Nullable currentUser: User, profile: Profile,
def prepareCommentOnly(comment: Comment, currentUser: Option[CurrentUser], profile: Profile,
topic: Topic, ignoreList: Set[Int]): PreparedComment = {
val messageText = msgbaseDao.getMessageText(comment.id)
val author = userService.getUserCached(comment.userid)
val group = groupDao.getGroup(topic.groupId)

prepareComment(messageText = messageText, author = author, remark = None, comment = comment, comments = None,
profile = profile, topic = topic, hideSet = Set.empty, samePageComments = Set.empty, currentUser = Option(currentUser),
profile = profile, topic = topic, hideSet = Set.empty, samePageComments = Set.empty, currentUser = currentUser,
group = group, ignoreList = ignoreList, filterShow = false)
}

Expand All @@ -162,7 +162,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
}

def prepareCommentList(comments: CommentList, list: Seq[Comment], topic: Topic, hideSet: Set[Int],
currentUser: Option[User], profile: Profile, ignoreList: Set[Int],
currentUser: Option[CurrentUser], profile: Profile, ignoreList: Set[Int],
filterShow: Boolean): Seq[PreparedComment] = {
if (list.isEmpty) {
Seq.empty
Expand All @@ -172,7 +172,7 @@ class CommentPrepareService(textService: MessageTextService, msgbaseDao: Msgbase
val group = groupDao.getGroup(topic.groupId)

val remarks = currentUser.map { user =>
remarkDao.getRemarks(user, users.values)
remarkDao.getRemarks(user.user, users.values)
}.getOrElse(Map.empty[Int, Remark])

val samePageComments = list.map(_.id).toSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class DeleteCommentController(searchQueueSender: SearchQueueSender, commentServi
"comments" -> comments,
"topic" -> topic,
"commentsPrepared" -> prepareService.prepareCommentList(comments, list, topic, Set.empty[Int],
Some(currentUser.user), tmpl.getProf, ignoreList, filterShow = false).asJava
Some(currentUser), tmpl.getProf, ignoreList, filterShow = false).asJava
).asJava)
}

Expand Down Expand Up @@ -191,7 +191,7 @@ class DeleteCommentController(searchQueueSender: SearchQueueSender, commentServi
val ignoreList = ignoreListDao.get(currentUser.user.getId)

new ModelAndView("undelete_comment", Map[String, Any](
"comment" -> prepareService.prepareCommentOnly(comment, currentUser.user, tmpl.getProf, topic, ignoreList),
"comment" -> prepareService.prepareCommentOnly(comment, Some(currentUser), tmpl.getProf, topic, ignoreList),
"topic" -> topic
).asJava)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class EditCommentController(commentService: CommentCreateService, msgbaseDao: Ms

val ignoreList = ignoreListDao.get(currentUser.user.getId)

formParams.put("comment", commentPrepareService.prepareCommentOnly(comment, currentUser.user, tmpl.getProf,
formParams.put("comment", commentPrepareService.prepareCommentOnly(comment, Some(currentUser), tmpl.getProf,
topic, ignoreList))

topicPermissionService.getEditDeadline(comment).foreach(value => formParams.put("deadline", value.toDate))
Expand Down Expand Up @@ -107,7 +107,8 @@ class EditCommentController(commentService: CommentCreateService, msgbaseDao: Ms
val comment = commentService.getComment(commentRequest, user, request)
val tmpl = Template.getTemplate

val formParams = new util.HashMap[String, AnyRef](commentService.prepareReplyto(commentRequest, Some(currentUser.user), tmpl.getProf, commentRequest.getTopic).asJava)
val formParams = new util.HashMap[String, AnyRef](commentService.prepareReplyto(commentRequest, Some(currentUser),
tmpl.getProf, commentRequest.getTopic).asJava)

val originalMessageText = msgbaseDao.getMessageText(commentRequest.getOriginal.id)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class ReactionController(topicDao: TopicDao, commentDao: CommentDao, permissionS
new ModelAndView("reaction-comment", Map[String, Any](
"topic" -> topic,
"preparedComment" ->
commentPrepareService.prepareCommentOnly(comment, currentUser.user, tmpl.getProf, topic, ignoreList),
commentPrepareService.prepareCommentOnly(comment, Some(currentUser), tmpl.getProf, topic, ignoreList),
"reactionList" -> reactionService.prepareReactionList(comment.reactions, reactionLog, ignoreList)
).asJava)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/ru/org/linux/topic/TopicController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class TopicController(sectionService: SectionService, topicDao: TopicDao, prepar
list = commentsFiltered,
topic = topic,
hideSet = hideSet,
currentUser = currentUserOpt.map(_.user),
currentUser = currentUserOpt,
profile = tmpl.getProf,
ignoreList = ignoreList,
filterShow = filterModeShow)
Expand Down

0 comments on commit 9e12045

Please sign in to comment.