Skip to content

Commit

Permalink
история правок доп изображений
Browse files Browse the repository at this point in the history
  • Loading branch information
maxcom committed Dec 24, 2024
1 parent 4d87829 commit d03c85d
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 38 deletions.
1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,6 @@
<configuration>
<propertyFile>sql/liquibase.config</propertyFile>
<verbose>true</verbose>
<promptOnNonLocalDatabase>false</promptOnNonLocalDatabase>
</configuration>
<executions>
<execution>
Expand Down
26 changes: 26 additions & 0 deletions sql/updates/2024-12-24-image-history.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!--
~ 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.
-->
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd">

<changeSet id="2024122401" author="Maxim Valyanskiy">
<addColumn tableName="edit_info">
<column name="oldaddimages" type="integer[]"/>
</addColumn>
</changeSet>
</databaseChangeLog>
17 changes: 14 additions & 3 deletions src/main/java/ru/org/linux/edithistory/PreparedEditHistory.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 1998-2021 Linux.org.ru
* 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
Expand Down Expand Up @@ -27,7 +27,6 @@
import javax.annotation.Nullable;
import java.util.Date;
import java.util.List;
import java.util.SortedMap;

public class PreparedEditHistory {
private final boolean original;
Expand All @@ -44,6 +43,8 @@ public class PreparedEditHistory {
private final boolean imageDeleted;
private final Poll poll;
private final Integer restoreFrom;
private final List<PreparedImage> addedImages;
private final List<PreparedImage> removedImages;

public PreparedEditHistory(
MessageTextService lorCodeService,
Expand All @@ -61,13 +62,15 @@ public PreparedEditHistory(
Boolean imageDeleted,
MarkupType markup,
Poll poll,
Integer restoreFrom) {
Integer restoreFrom, List<PreparedImage> addedImages, List<PreparedImage> removedImages) {
this.original = original;

this.editor = editor;
this.image = image;
this.imageDeleted = imageDeleted;
this.poll = poll;
this.addedImages = addedImages;
this.removedImages = removedImages;

if (message!=null) {
this.restoreFrom = restoreFrom;
Expand Down Expand Up @@ -142,4 +145,12 @@ public Poll getPoll() {
public Integer getRestoreFrom() {
return restoreFrom;
}

public List<PreparedImage> getAddedImages() {
return addedImages;
}

public List<PreparedImage> getRemovedImages() {
return removedImages;
}
}
13 changes: 9 additions & 4 deletions src/main/scala/ru/org/linux/edithistory/EditHistoryDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ case class EditHistoryRecord(
oldurl: Option[String] = None,
oldminor: Option[Boolean] = None,
oldimage: Option[Int] = None,
oldPoll: Option[Poll] = None)
oldPoll: Option[Poll] = None,
oldaddimages: Option[Seq[Int]] = None)

@Repository
class EditHistoryDao(dataSource: DataSource) {
Expand All @@ -49,7 +50,7 @@ class EditHistoryDao(dataSource: DataSource) {
new SimpleJdbcInsert(dataSource)
.withTableName("edit_info")
.usingColumns("msgid", "editor", "oldmessage", "oldtitle", "oldtags", "oldlinktext", "oldurl",
"object_type", "oldminor", "oldimage", "oldpoll")
"object_type", "oldminor", "oldimage", "oldpoll", "oldaddimages")

private def parseEditHistoryRecord(resultSet: ResultSet) = {
EditHistoryRecord(
Expand All @@ -75,7 +76,10 @@ class EditHistoryDao(dataSource: DataSource) {
},
oldPoll = Option(resultSet.getString("oldpoll")).map { json =>
parse(json).toTry.flatMap(_.as[Poll].toTry).get
})
},
oldaddimages = Option(resultSet.getArray("oldaddimages"))
.map(_.getArray.asInstanceOf[Array[Integer]].toSeq.map(_.toInt))
)
}

/**
Expand Down Expand Up @@ -109,7 +113,8 @@ class EditHistoryDao(dataSource: DataSource) {
"object_type" -> record.objectType,
"oldminor" -> record.oldminor.orNull,
"oldimage" -> record.oldimage.orNull,
"oldpoll" -> record.oldPoll.map(_.asJson).orNull
"oldpoll" -> record.oldPoll.map(_.asJson).orNull,
"oldaddimages" -> record.oldaddimages.map(_.toArray).orNull
).asJava)
}
}
42 changes: 35 additions & 7 deletions src/main/scala/ru/org/linux/edithistory/EditHistoryService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
private case class TopicEditHistoryState(message: String, markup: MarkupType, title: String,
url: String, linktext: String, tags: util.List[TagRef],
minor: Boolean, image: PreparedImage, lastId: Integer,
poll: Poll, first: Boolean) {
poll: Poll, first: Boolean, additionalImages: Seq[PreparedImage]) {
def next(dto: EditHistoryRecord): TopicEditHistoryState = {
val image = dto.oldimage.map { oldimage =>
if (oldimage == 0) {
Expand All @@ -47,6 +47,10 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
}
}.getOrElse(this.image)

val additionalImages = dto.oldaddimages.map { additionalImages =>
additionalImages.map(imageDao.getImage).flatMap(imageService.prepareImage)
}.getOrElse(this.additionalImages)

val (message, lastId) = dto.oldmessage.map { oldmessage =>
(oldmessage, Integer.valueOf(dto.id))
}.getOrElse((this.message, this.lastId))
Expand All @@ -59,10 +63,24 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
val poll = dto.oldPoll.getOrElse(this.poll)

this.copy(image = image, message = message, lastId = lastId, title = title, url = url, linktext = linktext,
tags = tags, minor = minor, poll = poll, first = false)
tags = tags, minor = minor, poll = poll, first = false, additionalImages = additionalImages)
}

def build(dto: EditHistoryRecord): PreparedEditHistory = {
val imageDeleted = this.image == null && dto.oldimage.isDefined

val addedImages = if (dto.oldaddimages.isDefined) {
this.additionalImages.filterNot(img => dto.oldaddimages.get.contains(img.getImage.id)).asJava
} else {
null
}

val removedImages = if (dto.oldaddimages.isDefined) {
dto.oldaddimages.get.filterNot(this.additionalImages.map(_.getImage.id).contains).map(imageDao.getImage).flatMap(imageService.prepareImage).asJava
} else {
null
}

new PreparedEditHistory(
textService,
userService.getUserCached(dto.editor),
Expand All @@ -76,10 +94,12 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
false,
if (dto.oldminor.isDefined) this.minor else null,
if (dto.oldimage.isDefined && this.image != null) this.image else null,
this.image == null && dto.oldimage.isDefined,
imageDeleted,
this.markup,
if (dto.oldPoll.isDefined) this.poll else null,
this.lastId)
this.lastId,
addedImages,
removedImages)
}

def buildLast(topic: Topic): PreparedEditHistory = {
Expand All @@ -99,14 +119,17 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
false,
this.markup,
this.poll,
this.lastId)
this.lastId,
this.additionalImages.asJava,
null)
}
}

private object TopicEditHistoryState {
def fromTopic(topic: Topic): TopicEditHistoryState = {
val messageText: MessageText = msgbaseDao.getMessageText(topic.id)
val maybeImage = imageDao.imageForTopic(topic)
val images = imageService.allImagesForTopic(topic)
val maybeImage = images.find(_.main)

new TopicEditHistoryState(
message = messageText.text,
Expand All @@ -124,7 +147,8 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
case _: PollNotFoundException =>
null
},
first = true)
first = true,
additionalImages = images.filterNot(_.main).flatMap(imageService.prepareImage))
}
}

Expand Down Expand Up @@ -153,6 +177,8 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
false,
this.markup,
null,
null,
null,
null)
}

Expand All @@ -173,6 +199,8 @@ class EditHistoryService(topicTagService: TopicTagService, userService: UserServ
false,
this.markup,
null,
null,
null,
null)
}
}
Expand Down
12 changes: 2 additions & 10 deletions src/main/scala/ru/org/linux/gallery/ImageDao.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ import org.springframework.stereotype.Repository
import ru.org.linux.gallery.ImageDao.galleryItemRowMapper
import ru.org.linux.section.Section
import ru.org.linux.section.SectionService
import ru.org.linux.topic.Topic

import javax.annotation.Nullable
import javax.sql.DataSource
import java.sql.ResultSet
import scala.jdk.CollectionConverters.MapHasAsJava
Expand Down Expand Up @@ -102,15 +100,9 @@ class ImageDao(private val sectionService: SectionService, dataSource: DataSourc
jdbcTemplate.queryAndMap(sql, tagId, countItems)(galleryItemRowMapper(gallery))
}

@Nullable
def imageForTopic(topic: Topic): Option[Image] =
def allImagesForTopic(topicId: Int): Seq[Image] =
jdbcTemplate.queryAndMap(
"SELECT id, topic, extension, deleted, main FROM images WHERE topic=? AND NOT deleted AND main", topic.id
)(ImageDao.imageRowMapper).headOption

def allImagesForTopic(topic: Topic): Seq[Image] =
jdbcTemplate.queryAndMap(
"SELECT id, topic, extension, deleted, main FROM images WHERE topic=? AND NOT deleted ORDER BY id", topic.id
"SELECT id, topic, extension, deleted, main FROM images WHERE topic=? AND NOT deleted ORDER BY id", topicId
)(ImageDao.imageRowMapper)

def getImage(id: Int): Image =
Expand Down
22 changes: 16 additions & 6 deletions src/main/scala/ru/org/linux/gallery/ImageService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,21 @@ class ImageService(imageDao: ImageDao, editHistoryDao: EditHistoryDao,

def deleteImage(image: Image)(implicit session: AuthorizedSession): Unit = {
transactional() { _ =>
val info = EditHistoryRecord(
editor = session.user.getId,
msgid = image.topicId,
oldimage = Some(image.id),
objectType = EditHistoryObjectTypeEnum.TOPIC)
val info = if (image.main) {
EditHistoryRecord(
editor = session.user.getId,
msgid = image.topicId,
oldimage = Some(image.id),
objectType = EditHistoryObjectTypeEnum.TOPIC)
} else {
val oldImages = imageDao.allImagesForTopic(image.topicId).filterNot(_.main)

EditHistoryRecord(
editor = session.user.getId,
msgid = image.topicId,
oldaddimages = Some(oldImages.map(_.id)),
objectType = EditHistoryObjectTypeEnum.TOPIC)
}

imageDao.deleteImage(image)
editHistoryDao.insert(info)
Expand Down Expand Up @@ -95,7 +105,7 @@ class ImageService(imageDao: ImageDao, editHistoryDao: EditHistoryDao,

def getGalleryItems(countItems: Int): java.util.List[GalleryItem] = imageDao.getGalleryItems(countItems).asJava

def allImagesForTopic(topic: Topic): Seq[Image] = imageDao.allImagesForTopic(topic)
def allImagesForTopic(topic: Topic): Seq[Image] = imageDao.allImagesForTopic(topic.id)

@throws(classOf[IOException])
@throws(classOf[BadImageException])
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/ru/org/linux/topic/TopicService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.springframework.transaction.PlatformTransactionManager
import org.springframework.validation.Errors
import ru.org.linux.auth.AuthorizedSession
import ru.org.linux.edithistory.{EditHistoryDao, EditHistoryObjectTypeEnum, EditHistoryRecord}
import ru.org.linux.gallery.{ImageDao, ImageService, UploadedImagePreview}
import ru.org.linux.gallery.{Image, ImageDao, ImageService, UploadedImagePreview}
import ru.org.linux.group.{Group, GroupPermissionService}
import ru.org.linux.markup.MessageTextService
import ru.org.linux.poll.{PollDao, PollVariant}
Expand Down Expand Up @@ -138,6 +138,7 @@ class TopicService(topicDao: TopicDao, msgbaseDao: MsgbaseDao, sectionService: S
objectType = EditHistoryObjectTypeEnum.TOPIC)

val oldText = msgbaseDao.getMessageText(oldMsg.id).text
val oldImages = imageService.allImagesForTopic(oldMsg)

var modified = false

Expand Down Expand Up @@ -187,13 +188,14 @@ class TopicService(topicDao: TopicDao, msgbaseDao: MsgbaseDao, sectionService: S
}

imagePreview.foreach { imagePreview =>
editHistoryRecord = replaceImage(oldMsg, imagePreview, editHistoryRecord)
editHistoryRecord = replaceImage(oldMsg, oldImages.find(_.main), imagePreview, editHistoryRecord)

modified = true
}

additionalImages.foreach { imagePreview =>
imageService.saveImage(imagePreview, oldMsg.id, main = false)
editHistoryRecord = editHistoryRecord.copy(oldaddimages = Some(oldImages.filterNot(_.main).map(_.id)))

modified = true
}
Expand Down Expand Up @@ -253,9 +255,7 @@ class TopicService(topicDao: TopicDao, msgbaseDao: MsgbaseDao, sectionService: S
(modified, notified)
}

private def replaceImage(oldMsg: Topic, imagePreview: UploadedImagePreview, editHistoryRecord: EditHistoryRecord): EditHistoryRecord = {
val oldImage = imageDao.imageForTopic(oldMsg)

private def replaceImage(oldMsg: Topic, oldImage: Option[Image], imagePreview: UploadedImagePreview, editHistoryRecord: EditHistoryRecord): EditHistoryRecord = {
oldImage.foreach { oldImage =>
imageDao.deleteImage(oldImage)
}
Expand Down
18 changes: 16 additions & 2 deletions src/main/webapp/WEB-INF/jsp/history.jsp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="lor" %>
<%--
~ Copyright 1998-2023 Linux.org.ru
~ 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
Expand Down Expand Up @@ -64,7 +64,21 @@
</c:if>

<c:if test="${editHistory.imageDeleted}">
<p>Изображение удалено</p>
<p>Основное изображение удалено</p>
</c:if>

<c:if test="${not empty editHistory.addedImages}">
Добавлены дополнительные изображения:
<c:forEach var="image" items="${editHistory.addedImages}">
<lor:image title="additional image" image="${image}" enableSchema="true" showImage="true" enableEdit="false"/>
</c:forEach>
</c:if>

<c:if test="${not empty editHistory.removedImages}">
Удалены дополнительные изображения:
<c:forEach var="image" items="${editHistory.removedImages}">
<lor:image title="additional image" image="${image}" enableSchema="true" showImage="true" enableEdit="false"/>
</c:forEach>
</c:if>

${editHistory.message}
Expand Down

0 comments on commit d03c85d

Please sign in to comment.