From cf787ad7fdb8e6273fdc35d7b5cc164b400207e9 Mon Sep 17 00:00:00 2001
From: fluzz <fluzz@freedroid.org>
Date: Thu, 20 Jul 2023 11:35:10 +0200
Subject: [PATCH] Add an updated_at field to the API call for issue's comment
 edition

The update date is used as the comment update date, and is applied to
the issue as an update date.
---
 models/issues/comment.go             |  9 ++++++---
 modules/structs/issue_comment.go     |  2 ++
 routers/api/v1/repo/issue_comment.go | 11 +++++++++++
 services/issue/comments.go           |  6 +++++-
 templates/swagger/v1_json.tmpl       |  5 +++++
 5 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/models/issues/comment.go b/models/issues/comment.go
index 643956670d..0b46f40589 100644
--- a/models/issues/comment.go
+++ b/models/issues/comment.go
@@ -1106,9 +1106,12 @@ func UpdateComment(c *Comment, doer *user_model.User) error {
 		return err
 	}
 	defer committer.Close()
-	sess := db.GetEngine(ctx)
-
-	if _, err := sess.ID(c.ID).AllCols().Update(c); err != nil {
+	sess := db.GetEngine(ctx).ID(c.ID).AllCols()
+	if c.Issue.NoAutoTime {
+		c.UpdatedUnix = c.Issue.UpdatedUnix
+		sess = sess.NoAutoTime()
+	}
+	if _, err := sess.Update(c); err != nil {
 		return err
 	}
 	if err := c.LoadIssue(ctx); err != nil {
diff --git a/modules/structs/issue_comment.go b/modules/structs/issue_comment.go
index 8961b70ad3..9ecb4a1789 100644
--- a/modules/structs/issue_comment.go
+++ b/modules/structs/issue_comment.go
@@ -36,6 +36,8 @@ type CreateIssueCommentOption struct {
 type EditIssueCommentOption struct {
 	// required: true
 	Body string `json:"body" binding:"Required"`
+	// swagger:strfmt date-time
+	Updated *time.Time `json:"updated_at"`
 }
 
 // TimelineComment represents a timeline comment (comment of any type) on a commit or issue
diff --git a/routers/api/v1/repo/issue_comment.go b/routers/api/v1/repo/issue_comment.go
index 6eed71fc74..a73f5c7a94 100644
--- a/routers/api/v1/repo/issue_comment.go
+++ b/routers/api/v1/repo/issue_comment.go
@@ -560,6 +560,17 @@ func editIssueComment(ctx *context.APIContext, form api.EditIssueCommentOption)
 		return
 	}
 
+	err = comment.LoadIssue(ctx)
+	if err != nil {
+		ctx.Error(http.StatusInternalServerError, "LoadIssue", err)
+		return
+	}
+	err = issue_service.SetIssueUpdateDate(ctx, comment.Issue, form.Updated, ctx.Doer)
+	if err != nil {
+		ctx.Error(http.StatusForbidden, "SetIssueUpdateDate", err)
+		return
+	}
+
 	oldContent := comment.Content
 	comment.Content = form.Body
 	if err := issue_service.UpdateComment(ctx, comment, ctx.Doer, oldContent); err != nil {
diff --git a/services/issue/comments.go b/services/issue/comments.go
index 2c5ef0f5dc..c89031a1d8 100644
--- a/services/issue/comments.go
+++ b/services/issue/comments.go
@@ -89,7 +89,11 @@ func UpdateComment(ctx context.Context, c *issues_model.Comment, doer *user_mode
 	}
 
 	if needsContentHistory {
-		err := issues_model.SaveIssueContentHistory(ctx, doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
+		historyDate := timeutil.TimeStampNow()
+		if c.Issue.NoAutoTime {
+			historyDate = c.Issue.UpdatedUnix
+		}
+		err := issues_model.SaveIssueContentHistory(ctx, doer.ID, c.IssueID, c.ID, historyDate, c.Content, false)
 		if err != nil {
 			return err
 		}
diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl
index 3b9d49dd92..ca7672be19 100644
--- a/templates/swagger/v1_json.tmpl
+++ b/templates/swagger/v1_json.tmpl
@@ -18075,6 +18075,11 @@
         "body": {
           "type": "string",
           "x-go-name": "Body"
+        },
+        "updated_at": {
+          "type": "string",
+          "format": "date-time",
+          "x-go-name": "Updated"
         }
       },
       "x-go-package": "code.gitea.io/gitea/modules/structs"