src/main/kotlin/de/uapcore/lightpit/servlet/FeedServlet.kt

Thu, 29 Dec 2022 15:04:21 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 29 Dec 2022 15:04:21 +0100
changeset 260
fb2ae2d63a56
parent 254
55ca6cafc3dd
permissions
-rw-r--r--

some minor style fixes

universe@195 1 /*
universe@195 2 * Copyright 2021 Mike Becker. All rights reserved.
universe@195 3 *
universe@195 4 * Redistribution and use in source and binary forms, with or without
universe@195 5 * modification, are permitted provided that the following conditions are met:
universe@195 6 *
universe@195 7 * 1. Redistributions of source code must retain the above copyright
universe@195 8 * notice, this list of conditions and the following disclaimer.
universe@195 9 *
universe@195 10 * 2. Redistributions in binary form must reproduce the above copyright
universe@195 11 * notice, this list of conditions and the following disclaimer in the
universe@195 12 * documentation and/or other materials provided with the distribution.
universe@195 13 *
universe@195 14 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@195 15 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@195 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
universe@195 17 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
universe@195 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
universe@195 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
universe@195 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
universe@195 21 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
universe@195 22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
universe@195 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
universe@195 24 */
universe@195 25
universe@195 26 package de.uapcore.lightpit.servlet
universe@195 27
universe@236 28 import com.github.difflib.text.DiffRow
universe@236 29 import com.github.difflib.text.DiffRowGenerator
universe@195 30 import de.uapcore.lightpit.AbstractServlet
universe@195 31 import de.uapcore.lightpit.HttpRequest
universe@195 32 import de.uapcore.lightpit.dao.DataAccessObject
universe@242 33 import de.uapcore.lightpit.entities.IssueCommentHistoryEntry
universe@235 34 import de.uapcore.lightpit.entities.IssueHistoryEntry
universe@242 35 import de.uapcore.lightpit.types.IssueHistoryType
universe@242 36 import de.uapcore.lightpit.viewmodel.CommentDiff
universe@235 37 import de.uapcore.lightpit.viewmodel.IssueDiff
universe@195 38 import de.uapcore.lightpit.viewmodel.IssueFeed
universe@235 39 import de.uapcore.lightpit.viewmodel.IssueFeedEntry
universe@254 40 import jakarta.servlet.annotation.WebServlet
universe@235 41 import java.text.SimpleDateFormat
universe@195 42
universe@236 43
universe@195 44 @WebServlet(urlPatterns = ["/feed/*"])
universe@195 45 class FeedServlet : AbstractServlet() {
universe@195 46
universe@195 47 init {
universe@198 48 get("/%project/issues.rss", this::issues)
universe@198 49 }
universe@198 50
universe@260 51 private val diffGenerator: DiffRowGenerator by lazyOf(DiffRowGenerator.create()
universe@242 52 .showInlineDiffs(true)
universe@242 53 .mergeOriginalRevised(true)
universe@242 54 .inlineDiffByWord(true)
universe@242 55 .oldTag { start -> if (start) "<strike style=\"color:red\">" else "</strike>" }
universe@242 56 .newTag { start -> if (start) "<i style=\"color: green\">" else "</i>" }
universe@242 57 .build()
universe@242 58 )
universe@242 59
universe@242 60 private fun fullContent(data: IssueCommentHistoryEntry) =
universe@242 61 CommentDiff(
universe@242 62 data.issueid,
universe@242 63 data.commentid,
universe@242 64 data.subject,
universe@242 65 data.comment.replace("\r", "")
universe@242 66 )
universe@242 67
universe@242 68 private fun diffContent(cur: IssueCommentHistoryEntry, next: IssueCommentHistoryEntry) =
universe@242 69 CommentDiff(
universe@242 70 cur.issueid,
universe@242 71 cur.commentid,
universe@242 72 cur.subject,
universe@242 73 diffGenerator.generateDiffRows(
universe@242 74 next.comment.replace("\r", "").split('\n'),
universe@242 75 cur.comment.replace("\r", "").split('\n')
universe@242 76 ).joinToString("\n", transform = DiffRow::getOldLine)
universe@242 77 )
universe@242 78
universe@242 79 private fun fullContent(issue: IssueHistoryEntry) = IssueDiff(
universe@242 80 issue.issueid,
universe@235 81 issue.subject,
universe@235 82 issue.component,
universe@235 83 issue.status.name,
universe@235 84 issue.category.name,
universe@235 85 issue.subject,
universe@236 86 issue.description.replace("\r", ""),
universe@235 87 issue.assignee,
universe@235 88 issue.eta?.let { SimpleDateFormat("dd.MM.yyyy").format(it) } ?: "",
universe@235 89 issue.affected,
universe@235 90 issue.resolved
universe@235 91 )
universe@235 92
universe@242 93 private fun diffContent(cur: IssueHistoryEntry, next: IssueHistoryEntry): IssueDiff {
universe@235 94 val prev = fullContent(next)
universe@235 95 val diff = fullContent(cur)
universe@242 96 val result = diffGenerator.generateDiffRows(
universe@238 97 listOf(
universe@238 98 prev.subject, prev.component, prev.status,
universe@238 99 prev.category, prev.assignee, prev.eta, prev.affected, prev.resolved
universe@238 100 ),
universe@238 101 listOf(
universe@238 102 diff.subject, diff.component, diff.status,
universe@238 103 diff.category, diff.assignee, diff.eta, diff.affected, diff.resolved
universe@238 104 )
universe@236 105 )
universe@236 106
universe@236 107 diff.subject = result[0].oldLine
universe@236 108 diff.component = result[1].oldLine
universe@236 109 diff.status = result[2].oldLine
universe@236 110 diff.category = result[3].oldLine
universe@236 111 diff.assignee = result[4].oldLine
universe@236 112 diff.eta = result[5].oldLine
universe@236 113 diff.affected = result[6].oldLine
universe@236 114 diff.resolved = result[7].oldLine
universe@236 115
universe@242 116 diff.description = diffGenerator.generateDiffRows(
universe@236 117 prev.description.split('\n'),
universe@236 118 diff.description.split('\n')
universe@240 119 ).joinToString("\n", transform = DiffRow::getOldLine)
universe@236 120
universe@235 121 return diff
universe@235 122 }
universe@235 123
universe@235 124 /**
universe@235 125 * Generates the feed entries.
universe@242 126 * Assumes that [issueEntries] and [commentEntries] are already sorted by timestamp (descending).
universe@235 127 */
universe@242 128 private fun generateFeedEntries(
universe@242 129 issueEntries: List<IssueHistoryEntry>,
universe@242 130 commentEntries: List<IssueCommentHistoryEntry>
universe@242 131 ): List<IssueFeedEntry> =
universe@242 132 (generateIssueFeedEntries(issueEntries) + generateCommentFeedEntries(commentEntries)).sortedByDescending { it.time }
universe@242 133
universe@242 134 private fun generateIssueFeedEntries(entries: List<IssueHistoryEntry>): List<IssueFeedEntry> =
universe@242 135 if (entries.isEmpty()) {
universe@238 136 emptyList()
universe@238 137 } else {
universe@242 138 entries.groupBy { it.issueid }.mapValues { (_, history) ->
universe@238 139 history.zipWithNext().map { (cur, next) ->
universe@238 140 IssueFeedEntry(
universe@242 141 cur.time, cur.type, issue = diffContent(cur, next)
universe@238 142 )
universe@238 143 }.plus(
universe@242 144 history.last().let { IssueFeedEntry(it.time, it.type, issue = fullContent(it)) }
universe@238 145 )
universe@242 146 }.flatMap { it.value }
universe@242 147 }
universe@242 148
universe@242 149 private fun generateCommentFeedEntries(entries: List<IssueCommentHistoryEntry>): List<IssueFeedEntry> =
universe@242 150 if (entries.isEmpty()) {
universe@242 151 emptyList()
universe@242 152 } else {
universe@242 153 entries.groupBy { it.commentid }.mapValues { (_, history) ->
universe@242 154 history.zipWithNext().map { (cur, next) ->
universe@242 155 IssueFeedEntry(
universe@242 156 cur.time, cur.type, comment = diffContent(cur, next)
universe@242 157 )
universe@242 158 }.plus(
universe@242 159 history.last().let { IssueFeedEntry(it.time, it.type, comment = fullContent(it)) }
universe@242 160 )
universe@242 161 }.flatMap { it.value }
universe@238 162 }
universe@235 163
universe@195 164 private fun issues(http: HttpRequest, dao: DataAccessObject) {
universe@198 165 val project = http.pathParams["project"]?.let { dao.findProjectByNode(it) }
universe@198 166 if (project == null) {
universe@198 167 http.response.sendError(404)
universe@198 168 return
universe@198 169 }
universe@239 170 val assignees = http.param("assignee")?.split(',')
universe@242 171 val comments = http.param("comments") ?: "all"
universe@195 172
universe@235 173 val days = http.param("days")?.toIntOrNull() ?: 30
universe@195 174
universe@239 175 val issuesFromDb = dao.listIssueHistory(project.id, days)
universe@239 176 val issueHistory = if (assignees == null) issuesFromDb else
universe@241 177 issuesFromDb.filter { assignees.contains(it.currentAssignee) }
universe@239 178
universe@242 179 val commentsFromDb = dao.listIssueCommentHistory(project.id, days)
universe@242 180 val commentHistory = when (comments) {
universe@242 181 "all" -> commentsFromDb
universe@242 182 "new" -> commentsFromDb.filter { it.type == IssueHistoryType.NewComment }
universe@242 183 else -> emptyList()
universe@242 184 }
universe@235 185
universe@242 186 http.view = IssueFeed(project, generateFeedEntries(issueHistory, commentHistory))
universe@198 187 http.renderFeed("issues-feed")
universe@195 188 }
universe@195 189 }

mercurial