Skip to content

Commit

Permalink
WarningController (WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Oct 27, 2024
1 parent 2e310e5 commit 9c73082
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
133 changes: 133 additions & 0 deletions src/main/scala/ru/org/linux/warning/WarningController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* 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.
*/

package ru.org.linux.warning

import org.springframework.stereotype.Controller
import org.springframework.validation.Errors
import org.springframework.web.bind.WebDataBinder
import org.springframework.web.bind.annotation.{InitBinder, ModelAttribute, RequestMapping, RequestMethod}
import org.springframework.web.servlet.ModelAndView
import org.springframework.web.servlet.view.RedirectView
import ru.org.linux.auth.{AccessViolationException, AuthUtil}
import ru.org.linux.comment.{Comment, CommentReadService}
import ru.org.linux.site.MessageNotFoundException
import ru.org.linux.topic.{Topic, TopicDao, TopicLinkBuilder}

import java.beans.PropertyEditorSupport
import scala.beans.BeanProperty

class PostWarningRequest(@BeanProperty var topic: Topic, @BeanProperty var comment: Comment,
@BeanProperty var text: String)

@Controller
class WarningController(warningService: WarningService, topicDao: TopicDao, commentReadService: CommentReadService) {
@RequestMapping(value = Array("/post-warning"), method = Array(RequestMethod.GET))
def showForm(@ModelAttribute request: PostWarningRequest): ModelAndView = AuthUtil.AuthorizedOnly { currentUser =>
if (!warningService.canPostWarning(currentUser)) {
throw new AccessViolationException("Вы не можете отправить уведомление")
}

if (request.topic.isDeleted) {
throw new AccessViolationException("Топик удален")
}

if (request.topic.isExpired) {
throw new AccessViolationException("Топик перемещен в архив")
}

if (request.comment!=null && request.comment.isDeleted) {
throw new AccessViolationException("Комментарий удален")
}

// TODO rate limit warning
// TODO show topic / comment

val mv = new ModelAndView("post-warning")

mv
}

@RequestMapping(value = Array("/post-warning"), method = Array(RequestMethod.POST))
def post(@ModelAttribute request: PostWarningRequest, errors: Errors): ModelAndView = AuthUtil.AuthorizedOnly { currentUser =>
if (!warningService.canPostWarning(currentUser)) {
throw new AccessViolationException("Вы не можете отправить уведомление")
}

if (request.topic.isDeleted) {
errors.reject("Топик удален")
}

if (request.topic.isExpired) {
errors.reject("Топик перемещен в архив")
}

if (request.comment!=null && request.comment.isDeleted) {
errors.reject("Комментарий удален")
}

// TODO rate limit warning
// TODO show topic / comment
// TODO check text length

if (errors.hasErrors) {
val mv = new ModelAndView("post-warning")

mv
} else {
warningService.postWarning(request.topic, Option(request.comment), currentUser.user, request.text)

// TODO action-done view
val builder = TopicLinkBuilder.baseLink(request.topic)

if (request.comment != null) {
new ModelAndView(new RedirectView(builder.comment(request.comment.id).build()))
} else {
new ModelAndView(new RedirectView(builder.build()))
}
}
}

@InitBinder
def initBinder(binder: WebDataBinder): Unit = {
binder.registerCustomEditor(classOf[Topic], new PropertyEditorSupport() {
@throws[IllegalArgumentException]
override def setAsText(text: String): Unit = {
try {
setValue(topicDao.getById(text.toInt))
} catch {
case e: MessageNotFoundException =>
throw new IllegalArgumentException(e)
}
}
})

binder.registerCustomEditor(classOf[Comment], new PropertyEditorSupport() {
@throws[IllegalArgumentException]
override def setAsText(text: String): Unit = {
if (text.isEmpty || "0" == text) {
setValue(null)
} else {
try {
setValue(commentReadService.getById(text.toInt))
} catch {
case e: MessageNotFoundException =>
throw new IllegalArgumentException(e)
}
}
}
})
}
}
4 changes: 4 additions & 0 deletions src/main/scala/ru/org/linux/warning/WarningService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package ru.org.linux.warning
import org.springframework.scala.transaction.support.TransactionManagement
import org.springframework.stereotype.Service
import org.springframework.transaction.PlatformTransactionManager
import ru.org.linux.auth.CurrentUser
import ru.org.linux.comment.Comment
import ru.org.linux.topic.Topic
import ru.org.linux.user.{User, UserEventService, UserService}
Expand All @@ -31,6 +32,9 @@ class WarningService(warningDao: WarningDao, eventService: UserEventService, use
eventService.addWarningEvent(author, moderators, topic, comment, message)

warningDao.postWarning(topicId = topic.id, commentId = comment.map(_.id), authorId = author.getId, message = message)
}

def canPostWarning(user: CurrentUser): Boolean = {
user.moderator
}
}

0 comments on commit 9c73082

Please sign in to comment.