-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
133 changes: 133 additions & 0 deletions
133
src/main/scala/ru/org/linux/warning/WarningController.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters