Skip to content

Commit

Permalink
warnings: показ под топиком
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Nov 1, 2024
1 parent 4b54f85 commit 14b9a30
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 17 deletions.
4 changes: 3 additions & 1 deletion src/main/scala/ru/org/linux/topic/PreparedTopic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ru.org.linux.section.Section
import ru.org.linux.site.DeleteInfo
import ru.org.linux.tag.TagRef
import ru.org.linux.user.{Remark, User}
import ru.org.linux.warning.{PreparedWarning, Warning}

import javax.annotation.Nullable
import scala.beans.BeanProperty
Expand All @@ -34,6 +35,7 @@ case class PreparedTopic(@BeanProperty message: Topic, @BeanProperty author: Use
@BeanProperty markupType: MarkupType, @Nullable @BeanProperty image: PreparedImage,
@BeanProperty postscoreInfo: String, @Nullable @BeanProperty remark: Remark,
@BeanProperty showRegisterInvite: Boolean, @Nullable @BeanProperty userAgent: String,
@BeanProperty reactions: PreparedReactions) {
@BeanProperty reactions: PreparedReactions,
@BeanProperty warnings: java.util.List[PreparedWarning]) {
def getId: Int = message.id
}
12 changes: 10 additions & 2 deletions src/main/scala/ru/org/linux/topic/TopicController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import ru.org.linux.section.{Section, SectionScrollModeEnum, SectionService}
import ru.org.linux.site.{MessageNotFoundException, Template}
import ru.org.linux.spring.dao.MsgbaseDao
import ru.org.linux.user.{IgnoreListDao, MemoriesDao, User}
import ru.org.linux.warning.WarningService

import java.time.Instant
import java.util
Expand Down Expand Up @@ -92,7 +93,8 @@ class TopicController(sectionService: SectionService, topicDao: TopicDao, prepar
ignoreListDao: IgnoreListDao, ipBlockDao: IPBlockDao, editHistoryService: EditHistoryService,
memoriesDao: MemoriesDao, permissionService: TopicPermissionService,
moreLikeThisService: MoreLikeThisService, topicTagService: TopicTagService,
msgbaseDao: MsgbaseDao, textService: MessageTextService, groupDao: GroupDao) extends StrictLogging {
msgbaseDao: MsgbaseDao, textService: MessageTextService, groupDao: GroupDao,
warningService: WarningService) extends StrictLogging {
@RequestMapping(value = Array("/{section:(?:forum)|(?:news)|(?:polls)|(?:articles)|(?:gallery)}/{group}/{id}"))
def getMessageNewMain(webRequest: WebRequest, request: HttpServletRequest, response: HttpServletResponse,
@RequestParam(value = "filter", required = false) filter: String,
Expand Down Expand Up @@ -159,7 +161,13 @@ class TopicController(sectionService: SectionService, topicDao: TopicDao, prepar
val messageText = msgbaseDao.getMessageText(topic.id)
val plainText = textService.extractPlainText(messageText)

val preparedMessage = topicPrepareService.prepareTopic(topic, tags, currentUserOpt.map(_.user), messageText)
val warnings = if (currentUserOpt.exists(_.moderator) || currentUserOpt.exists(_.corrector)) {
warningService.load(topic, currentUserOpt.exists(_.moderator))
} else {
Seq.empty
}

val preparedMessage = topicPrepareService.prepareTopic(topic, tags, currentUserOpt.map(_.user), messageText, warnings)

val group = preparedMessage.group

Expand Down
15 changes: 10 additions & 5 deletions src/main/scala/ru/org/linux/topic/TopicPrepareService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import ru.org.linux.spring.dao.{DeleteInfoDao, MessageText, MsgbaseDao, UserAgen
import ru.org.linux.tag.TagRef
import ru.org.linux.user.*
import ru.org.linux.util.StringUtil
import ru.org.linux.warning.{Warning, WarningService}

import javax.annotation.Nullable
import scala.jdk.CollectionConverters.*
Expand All @@ -40,13 +41,15 @@ class TopicPrepareService(sectionService: SectionService, groupDao: GroupDao, de
topicPermissionService: TopicPermissionService,
groupPermissionService: GroupPermissionService, topicTagService: TopicTagService,
msgbaseDao: MsgbaseDao, imageService: ImageService, userAgentDao: UserAgentDao,
reactionPrepareService: ReactionService, ignoreListDao: IgnoreListDao) {
reactionPrepareService: ReactionService, ignoreListDao: IgnoreListDao,
warningService: WarningService) {
def prepareTopic(message: Topic, user: User): PreparedTopic =
prepareTopic(message, topicTagService.getTagRefs(message).asScala, minimizeCut = false, None, user,
msgbaseDao.getMessageText(message.id), None)

def prepareTopic(message: Topic, tags: java.util.List[TagRef], user: Option[User], text: MessageText): PreparedTopic =
prepareTopic(message, tags.asScala, minimizeCut = false, None, user.orNull, text, None)
def prepareTopic(message: Topic, tags: java.util.List[TagRef], user: Option[User], text: MessageText,
warnings: Seq[Warning]): PreparedTopic =
prepareTopic(message, tags.asScala, minimizeCut = false, None, user.orNull, text, None, warnings)

def prepareTopicPreview(message: Topic, tags: Seq[TagRef], newPoll: Option[Poll], text: MessageText,
image: Option[Image]): PreparedTopic =
Expand All @@ -73,7 +76,8 @@ class TopicPrepareService(sectionService: SectionService, groupDao: GroupDao, de
* @return подготовленный топик
*/
private def prepareTopic(topic: Topic, tags: collection.Seq[TagRef], minimizeCut: Boolean, poll: Option[PreparedPoll],
@Nullable currentUser: User, text: MessageText, image: Option[Image]): PreparedTopic = try {
@Nullable currentUser: User, text: MessageText, image: Option[Image],
warnings: Seq[Warning] = Seq.empty): PreparedTopic = try {
val group = groupDao.getGroup(topic.groupId)
val author = userService.getUserCached(topic.authorUserId)
val section = sectionService.getSection(topic.sectionId)
Expand Down Expand Up @@ -143,7 +147,8 @@ class TopicPrepareService(sectionService: SectionService, groupDao: GroupDao, de
PreparedTopic(topic, author, deleteInfo.orNull, deleteUser.orNull, processedMessage, preparedPoll.orNull,
commiter.orNull, tags.asJava, group, section, text.markup, preparedImage.orNull,
TopicPermissionService.getPostScoreInfo(postscore), remark.orNull, showRegisterInvite, userAgent.orNull,
reactionPrepareService.prepare(topic.reactions, ignoreList, Option(currentUser), topic, None))
reactionPrepareService.prepare(topic.reactions, ignoreList, Option(currentUser), topic, None),
warningService.prepareWarning(warnings).asJava)
} catch {
case e: PollNotFoundException =>
throw new RuntimeException(e)
Expand Down
6 changes: 6 additions & 0 deletions src/main/scala/ru/org/linux/warning/Warning.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@

package ru.org.linux.warning

import ru.org.linux.user.User

import java.time.Instant
import java.util.Date
import scala.beans.BeanProperty

sealed trait WarningType {
def id: String
Expand Down Expand Up @@ -48,3 +52,5 @@ object SpellingWarning extends WarningType {

case class Warning(id: Int, topicId: Int, commentId: Option[Int], postdate: Instant, authorId: Int, message: String,
warningType: WarningType)

case class PreparedWarning(@BeanProperty postdate: Date, @BeanProperty author: User, @BeanProperty message: String)
14 changes: 10 additions & 4 deletions src/main/scala/ru/org/linux/warning/WarningDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,16 @@ class WarningDao(ds: DataSource) {
warningType = WarningType.idToType(rs.getString("warning_type")))
}

def loadForTopic(topicId: Int): Option[Warning] = {
namedJdbcTemplate.query("select id, topic, comment, postdate, author, message, warning_type from message_warnings " +
"where topic=:topic and comment is null " +
"order by postdate", Map("topic" -> topicId).asJava, mapper).asScala.headOption
def loadForTopic(topicId: Int, forModerator: Boolean): collection.Seq[Warning] = {
val filter = if (forModerator) {
""
} else {
"and warning_type IN ('tag', 'spelling') "
}

namedJdbcTemplate.query("select id, topic, comment, postdate, author, message, warning_type from message_warnings " +
"where topic=:topic and comment is null " + filter +
"order by postdate", Map("topic" -> topicId).asJava, mapper).asScala
}

def loadForComments(topicId: Int, comments: Set[Int]): Map[Int, Warning] = {
Expand Down
13 changes: 13 additions & 0 deletions src/main/scala/ru/org/linux/warning/WarningService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import ru.org.linux.comment.Comment
import ru.org.linux.topic.Topic
import ru.org.linux.user.{User, UserEventService, UserService}

import java.util.Date

object WarningService {
val MaxWarningsPerHour = 5
}
Expand All @@ -46,4 +48,15 @@ class WarningService(warningDao: WarningDao, eventService: UserEventService, use
}

def lastWarningsCount(user: CurrentUser): Int = warningDao.lastWarningsCount(user.user.getId)

def prepareWarning(warnings: Seq[Warning]): Seq[PreparedWarning] =
warnings.map { warning =>
PreparedWarning(
postdate = new Date(warning.postdate.toEpochMilli),
author = userService.getUserCached(warning.authorId),
message = s"[${warning.warningType.name}] ${warning.message}")
}

def load(topic: Topic, forModerator: Boolean): Seq[Warning] =
warningDao.loadForTopic(topic.id, forModerator).toVector
}
8 changes: 5 additions & 3 deletions src/main/webapp/WEB-INF/tags/topic.tag
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@
</p>
</c:if>

<c:if test="${preparedMessage.image != null}">
<lor:image title="${preparedMessage.message.title}" image="${preparedMessage.image}" preparedMessage="${preparedMessage}" showInfo="true"/>
</c:if>
<lor:warnings warnings="${preparedMessage.warnings}"/>

<c:if test="${preparedMessage.image != null}">
<lor:image title="${preparedMessage.message.title}" image="${preparedMessage.image}" preparedMessage="${preparedMessage}" showInfo="true"/>
</c:if>
</div>
<footer>

Expand Down
30 changes: 30 additions & 0 deletions src/main/webapp/WEB-INF/tags/warnings.tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<%--
~ Copyright 1998-2024 Linux.org.ru
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
--%>
<%@ tag pageEncoding="UTF-8" trimDirectiveWhitespaces="true"%>
<%@ attribute name="warnings" required="true" type="java.util.List<ru.org.linux.warning.PreparedWarning>" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="lor" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<c:if test="${not empty warnings}">
<div class="infoblock">
<c:forEach var="warning" items="${warnings}">
<div>
⚠️${' '} <lor:dateonly date="${warning.postdate}"/> ${' '} <lor:user user="${warning.author}" link="true"/>:
<c:out value="${warning.message}" escapeXml="true"/>
</div>
</c:forEach>
</div>
</c:if>

4 changes: 4 additions & 0 deletions src/main/webapp/sass/_common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ h1 {
}
}

.msg_body .infoblock {
border: 1px solid;
}

.infoblock-small {
font-size: smaller;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import ru.org.linux.spring.dao.MsgbaseDao;
import ru.org.linux.user.IgnoreListDao;
import ru.org.linux.user.MemoriesDao;
import ru.org.linux.warning.WarningService;
import sttp.client3.SttpBackend;

import java.io.FileInputStream;
Expand Down Expand Up @@ -78,10 +79,10 @@ public TopicController topicController(SectionService sectionService, TopicDao m
EditHistoryService editHistoryService, MemoriesDao memoriesDao,
TopicPermissionService permissionService, MoreLikeThisService moreLikeThisService,
TopicTagService topicTagService, MsgbaseDao msgbaseDao, MessageTextService textService,
GroupDao groupDao) {
GroupDao groupDao, WarningService warningService) {
return new TopicController(sectionService, messageDao, prepareService, topicPrepareService, commentService,
ignoreListDao, ipBlockDao, editHistoryService, memoriesDao, permissionService,
moreLikeThisService, topicTagService, msgbaseDao, textService, groupDao);
moreLikeThisService, topicTagService, msgbaseDao, textService, groupDao, warningService);
}

@Bean
Expand Down

0 comments on commit 14b9a30

Please sign in to comment.