2017-01-20 07:58:46 +01:00
// Copyright 2017 The Gitea Authors. All rights reserved.
2022-11-27 19:20:29 +01:00
// SPDX-License-Identifier: MIT
2017-01-20 07:58:46 +01:00
2021-11-19 14:39:57 +01:00
package repo
2017-01-20 07:58:46 +01:00
import (
2021-09-23 17:45:36 +02:00
"context"
2017-01-20 07:58:46 +01:00
"fmt"
2021-11-16 19:18:25 +01:00
"net/url"
2017-01-20 07:58:46 +01:00
"path"
2021-09-19 13:49:59 +02:00
"code.gitea.io/gitea/models/db"
2017-01-20 07:58:46 +01:00
"code.gitea.io/gitea/modules/setting"
2020-08-18 06:23:45 +02:00
"code.gitea.io/gitea/modules/storage"
2019-08-15 16:46:21 +02:00
"code.gitea.io/gitea/modules/timeutil"
2022-10-18 07:50:37 +02:00
"code.gitea.io/gitea/modules/util"
2017-01-20 07:58:46 +01:00
)
// Attachment represent a attachment of issue/comment/release.
type Attachment struct {
2023-04-12 11:05:23 +02:00
ID int64 ` xorm:"pk autoincr" `
UUID string ` xorm:"uuid UNIQUE" `
RepoID int64 ` xorm:"INDEX" ` // this should not be zero
IssueID int64 ` xorm:"INDEX" ` // maybe zero when creating
ReleaseID int64 ` xorm:"INDEX" ` // maybe zero when creating
UploaderID int64 ` xorm:"INDEX DEFAULT 0" ` // Notice: will be zero before this column added
CommentID int64
Name string
DownloadCount int64 ` xorm:"DEFAULT 0" `
Size int64 ` xorm:"DEFAULT 0" `
[v1.21] [FEAT] allow setting the update date on issues and comments
This field adds the possibility to set the update date when modifying
an issue through the API.
A 'NoAutoDate' in-memory field is added in the Issue struct.
If the update_at field is set, NoAutoDate is set to true and the
Issue's UpdatedUnix field is filled.
That information is passed down to the functions that actually updates
the database, which have been modified to not auto update dates if
requested.
A guard is added to the 'EditIssue' API call, to checks that the
udpate_at date is between the issue's creation date and the current
date (to avoid 'malicious' changes). It also limits the new feature
to project's owners and admins.
(cherry picked from commit c524d33402c76bc4cccea2806f289e08a009baae)
Add a SetIssueUpdateDate() function in services/issue.go
That function is used by some API calls to set the NoAutoDate and
UpdatedUnix fields of an Issue if an updated_at date is provided.
(cherry picked from commit f061caa6555e0c9e922ee1e73dd2e4337360e9fe)
Add an updated_at field to the API calls related to Issue's Labels.
The update date is applied to the issue's comment created to inform
about the modification of the issue's labels.
(cherry picked from commit ea36cf80f58f0ab20c565a8f5d063b90fd741f97)
Add an updated_at field to the API call for issue's attachment creation
The update date is applied to the issue's comment created to inform
about the modification of the issue's content, and is set as the
asset creation date.
(cherry picked from commit 96150971ca31b97e97e84d5f5eb95a177cc44e2e)
Checking Issue changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPIEditIssueWithAutoDate
- TestAPIEditIssueWithNoAutoDate
- TestAPIAddIssueLabelsWithAutoDate
- TestAPIAddIssueLabelsWithNoAutoDate
- TestAPICreateIssueAttachmentWithAutoDate
- TestAPICreateIssueAttachmentWithNoAutoDate
(cherry picked from commit 4926a5d7a28581003545256632213bf4136b193d)
Add an updated_at field to the API call for issue's comment creation
The update date is used as the comment creation date, and is applied to
the issue as the update creation date.
(cherry picked from commit 76c8faecdc6cba48ca4fe07d1a916d1f1a4b37b4)
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.
(cherry picked from commit cf787ad7fdb8e6273fdc35d7b5cc164b400207e9)
Add an updated_at field to the API call for comment's attachment creation
The update date is applied to the comment, and is set as the asset
creation date.
(cherry picked from commit 1e4ff424d39db7a4256cd9abf9c58b8d3e1b5c14)
Checking Comment changes, with and without providing an updated_at date
Those unit tests are added:
- TestAPICreateCommentWithAutoDate
- TestAPICreateCommentWithNoAutoDate
- TestAPIEditCommentWithAutoDate
- TestAPIEditCommentWithNoAutoDate
- TestAPICreateCommentAttachmentWithAutoDate
- TestAPICreateCommentAttachmentWithNoAutoDate
(cherry picked from commit da932152f1deb3039a399516a51c8b6757059c91)
Pettier code to set the update time of comments
Now uses sess.AllCols().NoAutoToime().SetExpr("updated_unix", ...)
XORM is smart enough to compose one single SQL UPDATE which all
columns + updated_unix.
(cherry picked from commit 1f6a42808dd739c0c2e49e6b7ae2967f120f43c2)
Issue edition: Keep the max of the milestone and issue update dates.
When editing an issue via the API, an updated_at date can be provided.
If the EditIssue call changes the issue's milestone, the milestone's
update date is to be changed accordingly, but only with a greater
value.
This ensures that a milestone's update date is the max of all issue's
update dates.
(cherry picked from commit 8f22ea182e6b49e933dc6534040160dd739ff18a)
Rewrite the 'AutoDate' tests using subtests
Also add a test to check the permissions to set a date, and a test
to check update dates on milestones.
The tests related to 'AutoDate' are:
- TestAPIEditIssueAutoDate
- TestAPIAddIssueLabelsAutoDate
- TestAPIEditIssueMilestoneAutoDate
- TestAPICreateIssueAttachmentAutoDate
- TestAPICreateCommentAutoDate
- TestAPIEditCommentWithDate
- TestAPICreateCommentAttachmentAutoDate
(cherry picked from commit 961fd13c551b3e50040acb7c914a00ead92de63f)
(cherry picked from commit d52f4eea44692ee773010cb66a69a603663947d5)
(cherry picked from commit 3540ea2a43155ca8cf5ab1a4a246babfb829db16)
Conflicts:
services/issue/issue.go
https://codeberg.org/forgejo/forgejo/pulls/1415
(cherry picked from commit 56720ade008c09122d825959171aa5346d645987)
Conflicts:
routers/api/v1/repo/issue_label.go
https://codeberg.org/forgejo/forgejo/pulls/1462
(cherry picked from commit 47c78927d6c7e7a50298fa67efad1e73723a0981)
(cherry picked from commit 2030f3b965cde401976821083c3250b404954ecc)
(cherry picked from commit f02aeb76981cd688ceaf6613f142a8a725be1437)
(cherry picked from commit 2e43e49961c1cd5791744fa4e7994ce929c31837)
(cherry picked from commit 3bfb6cc1c085a1ae11885d0eb138d7e977fa1a16)
2023-05-30 18:42:58 +02:00
NoAutoTime bool ` xorm:"-" `
2023-04-12 11:05:23 +02:00
CreatedUnix timeutil . TimeStamp ` xorm:"created" `
CustomDownloadURL string ` xorm:"-" `
2017-01-20 07:58:46 +01:00
}
2021-09-19 13:49:59 +02:00
func init ( ) {
db . RegisterModel ( new ( Attachment ) )
}
2017-04-20 04:31:31 +02:00
// IncreaseDownloadCount is update download count + 1
2023-09-15 08:13:19 +02:00
func ( a * Attachment ) IncreaseDownloadCount ( ctx context . Context ) error {
2017-04-20 04:31:31 +02:00
// Update download count.
2023-09-15 08:13:19 +02:00
if _ , err := db . GetEngine ( ctx ) . Exec ( "UPDATE `attachment` SET download_count=download_count+1 WHERE id=?" , a . ID ) ; err != nil {
2022-10-24 21:29:17 +02:00
return fmt . Errorf ( "increase attachment count: %w" , err )
2017-04-20 04:31:31 +02:00
}
return nil
}
2020-08-18 06:23:45 +02:00
// AttachmentRelativePath returns the relative path
func AttachmentRelativePath ( uuid string ) string {
return path . Join ( uuid [ 0 : 1 ] , uuid [ 1 : 2 ] , uuid )
2017-01-20 07:58:46 +01:00
}
2020-08-18 06:23:45 +02:00
// RelativePath returns the relative path of the attachment
func ( a * Attachment ) RelativePath ( ) string {
return AttachmentRelativePath ( a . UUID )
2017-01-20 07:58:46 +01:00
}
2018-03-06 02:22:16 +01:00
// DownloadURL returns the download url of the attached file
func ( a * Attachment ) DownloadURL ( ) string {
2023-04-12 11:05:23 +02:00
if a . CustomDownloadURL != "" {
return a . CustomDownloadURL
}
2021-11-16 19:18:25 +01:00
return setting . AppURL + "attachments/" + url . PathEscape ( a . UUID )
2018-03-06 02:22:16 +01:00
}
2021-11-19 14:39:57 +01:00
// ErrAttachmentNotExist represents a "AttachmentNotExist" kind of error.
type ErrAttachmentNotExist struct {
ID int64
UUID string
}
// IsErrAttachmentNotExist checks if an error is a ErrAttachmentNotExist.
func IsErrAttachmentNotExist ( err error ) bool {
_ , ok := err . ( ErrAttachmentNotExist )
return ok
}
func ( err ErrAttachmentNotExist ) Error ( ) string {
return fmt . Sprintf ( "attachment does not exist [id: %d, uuid: %s]" , err . ID , err . UUID )
}
2022-10-18 07:50:37 +02:00
func ( err ErrAttachmentNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2022-05-20 16:08:52 +02:00
// GetAttachmentByID returns attachment by given id
func GetAttachmentByID ( ctx context . Context , id int64 ) ( * Attachment , error ) {
2020-06-17 19:50:11 +02:00
attach := & Attachment { }
2022-05-20 16:08:52 +02:00
if has , err := db . GetEngine ( ctx ) . ID ( id ) . Get ( attach ) ; err != nil {
2018-03-06 02:22:16 +01:00
return nil , err
} else if ! has {
return nil , ErrAttachmentNotExist { ID : id , UUID : "" }
}
return attach , nil
}
2022-05-20 16:08:52 +02:00
// GetAttachmentByUUID returns attachment by given UUID.
func GetAttachmentByUUID ( ctx context . Context , uuid string ) ( * Attachment , error ) {
2020-06-17 19:50:11 +02:00
attach := & Attachment { }
2022-05-20 16:08:52 +02:00
has , err := db . GetEngine ( ctx ) . Where ( "uuid=?" , uuid ) . Get ( attach )
2017-01-20 07:58:46 +01:00
if err != nil {
return nil , err
} else if ! has {
return nil , ErrAttachmentNotExist { 0 , uuid }
}
return attach , nil
}
2019-12-11 01:01:52 +01:00
// GetAttachmentsByUUIDs returns attachment by given UUID list.
2021-09-23 17:45:36 +02:00
func GetAttachmentsByUUIDs ( ctx context . Context , uuids [ ] string ) ( [ ] * Attachment , error ) {
2017-01-20 07:58:46 +01:00
if len ( uuids ) == 0 {
return [ ] * Attachment { } , nil
}
// Silently drop invalid uuids.
attachments := make ( [ ] * Attachment , 0 , len ( uuids ) )
2022-05-20 16:08:52 +02:00
return attachments , db . GetEngine ( ctx ) . In ( "uuid" , uuids ) . Find ( & attachments )
2017-01-20 07:58:46 +01:00
}
2022-11-15 09:08:59 +01:00
// ExistAttachmentsByUUID returns true if attachment exists with the given UUID
func ExistAttachmentsByUUID ( ctx context . Context , uuid string ) ( bool , error ) {
return db . GetEngine ( ctx ) . Where ( "`uuid`=?" , uuid ) . Exist ( new ( Attachment ) )
2021-09-06 16:46:20 +02:00
}
2022-05-20 16:08:52 +02:00
// GetAttachmentsByIssueID returns all attachments of an issue.
func GetAttachmentsByIssueID ( ctx context . Context , issueID int64 ) ( [ ] * Attachment , error ) {
2017-01-20 07:58:46 +01:00
attachments := make ( [ ] * Attachment , 0 , 10 )
2021-11-19 14:39:57 +01:00
return attachments , db . GetEngine ( ctx ) . Where ( "issue_id = ? AND comment_id = 0" , issueID ) . Find ( & attachments )
2017-01-20 07:58:46 +01:00
}
2023-02-11 09:12:41 +01:00
// GetAttachmentsByIssueIDImagesLatest returns the latest image attachments of an issue.
func GetAttachmentsByIssueIDImagesLatest ( ctx context . Context , issueID int64 ) ( [ ] * Attachment , error ) {
attachments := make ( [ ] * Attachment , 0 , 5 )
return attachments , db . GetEngine ( ctx ) . Where ( ` issue_id = ? AND ( name like ' % . apng '
OR name like ' % . avif '
OR name like ' % . bmp '
OR name like ' % . gif '
OR name like ' % . jpg '
OR name like ' % . jpeg '
OR name like ' % . jxl '
OR name like ' % . png '
OR name like ' % . svg '
OR name like ' % . webp ' ) ` , issueID ) . Desc ( "comment_id" ) . Limit ( 5 ) . Find ( & attachments )
}
2017-01-20 07:58:46 +01:00
// GetAttachmentsByCommentID returns all attachments if comment by given ID.
2022-05-20 16:08:52 +02:00
func GetAttachmentsByCommentID ( ctx context . Context , commentID int64 ) ( [ ] * Attachment , error ) {
2017-01-20 07:58:46 +01:00
attachments := make ( [ ] * Attachment , 0 , 10 )
2021-11-19 14:39:57 +01:00
return attachments , db . GetEngine ( ctx ) . Where ( "comment_id=?" , commentID ) . Find ( & attachments )
2017-01-20 07:58:46 +01:00
}
2022-05-20 16:08:52 +02:00
// GetAttachmentByReleaseIDFileName returns attachment by given releaseId and fileName.
func GetAttachmentByReleaseIDFileName ( ctx context . Context , releaseID int64 , fileName string ) ( * Attachment , error ) {
2019-01-06 23:37:30 +01:00
attach := & Attachment { ReleaseID : releaseID , Name : fileName }
2022-05-20 16:08:52 +02:00
has , err := db . GetEngine ( ctx ) . Get ( attach )
2019-01-06 23:37:30 +01:00
if err != nil {
return nil , err
} else if ! has {
return nil , err
}
return attach , nil
}
2017-01-20 07:58:46 +01:00
// DeleteAttachment deletes the given attachment and optionally the associated file.
2023-09-15 08:13:19 +02:00
func DeleteAttachment ( ctx context . Context , a * Attachment , remove bool ) error {
_ , err := DeleteAttachments ( ctx , [ ] * Attachment { a } , remove )
2017-01-20 07:58:46 +01:00
return err
}
// DeleteAttachments deletes the given attachments and optionally the associated files.
2021-09-23 17:45:36 +02:00
func DeleteAttachments ( ctx context . Context , attachments [ ] * Attachment , remove bool ) ( int , error ) {
2017-12-24 22:04:22 +01:00
if len ( attachments ) == 0 {
return 0 , nil
}
2021-03-14 19:52:12 +01:00
ids := make ( [ ] int64 , 0 , len ( attachments ) )
2017-12-24 22:04:22 +01:00
for _ , a := range attachments {
ids = append ( ids , a . ID )
}
2021-09-23 17:45:36 +02:00
cnt , err := db . GetEngine ( ctx ) . In ( "id" , ids ) . NoAutoCondition ( ) . Delete ( attachments [ 0 ] )
2017-12-24 22:04:22 +01:00
if err != nil {
return 0 , err
}
if remove {
for i , a := range attachments {
2020-08-18 06:23:45 +02:00
if err := storage . Attachments . Delete ( a . RelativePath ( ) ) ; err != nil {
2017-01-20 07:58:46 +01:00
return i , err
}
}
}
2017-12-24 22:04:22 +01:00
return int ( cnt ) , nil
2017-01-20 07:58:46 +01:00
}
// DeleteAttachmentsByIssue deletes all attachments associated with the given issue.
2023-09-15 08:13:19 +02:00
func DeleteAttachmentsByIssue ( ctx context . Context , issueID int64 , remove bool ) ( int , error ) {
attachments , err := GetAttachmentsByIssueID ( ctx , issueID )
2017-01-20 07:58:46 +01:00
if err != nil {
return 0 , err
}
2023-09-15 08:13:19 +02:00
return DeleteAttachments ( ctx , attachments , remove )
2017-01-20 07:58:46 +01:00
}
// DeleteAttachmentsByComment deletes all attachments associated with the given comment.
2023-09-15 08:13:19 +02:00
func DeleteAttachmentsByComment ( ctx context . Context , commentID int64 , remove bool ) ( int , error ) {
attachments , err := GetAttachmentsByCommentID ( ctx , commentID )
2017-01-20 07:58:46 +01:00
if err != nil {
return 0 , err
}
2023-09-15 08:13:19 +02:00
return DeleteAttachments ( ctx , attachments , remove )
2017-01-20 07:58:46 +01:00
}
2018-03-06 02:22:16 +01:00
2021-03-22 17:09:51 +01:00
// UpdateAttachmentByUUID Updates attachment via uuid
2021-09-23 17:45:36 +02:00
func UpdateAttachmentByUUID ( ctx context . Context , attach * Attachment , cols ... string ) error {
2021-03-22 17:09:51 +01:00
if attach . UUID == "" {
2021-09-23 17:45:36 +02:00
return fmt . Errorf ( "attachment uuid should be not blank" )
2021-03-22 17:09:51 +01:00
}
2021-09-23 17:45:36 +02:00
_ , err := db . GetEngine ( ctx ) . Where ( "uuid=?" , attach . UUID ) . Cols ( cols ... ) . Update ( attach )
2021-03-22 17:09:51 +01:00
return err
}
2022-05-20 16:08:52 +02:00
// UpdateAttachment updates the given attachment in database
func UpdateAttachment ( ctx context . Context , atta * Attachment ) error {
2022-01-20 18:46:10 +01:00
sess := db . GetEngine ( ctx ) . Cols ( "name" , "issue_id" , "release_id" , "comment_id" , "download_count" )
2018-03-06 02:22:16 +01:00
if atta . ID != 0 && atta . UUID == "" {
2021-11-19 14:39:57 +01:00
sess = sess . ID ( atta . ID )
2018-03-06 02:22:16 +01:00
} else {
// Use uuid only if id is not set and uuid is set
2021-11-19 14:39:57 +01:00
sess = sess . Where ( "uuid = ?" , atta . UUID )
2018-03-06 02:22:16 +01:00
}
2021-11-19 14:39:57 +01:00
_ , err := sess . Update ( atta )
2018-03-06 02:22:16 +01:00
return err
}
2019-09-30 18:10:00 +02:00
// DeleteAttachmentsByRelease deletes all attachments associated with the given release.
2022-11-19 09:12:33 +01:00
func DeleteAttachmentsByRelease ( ctx context . Context , releaseID int64 ) error {
_ , err := db . GetEngine ( ctx ) . Where ( "release_id = ?" , releaseID ) . Delete ( & Attachment { } )
2019-09-30 18:10:00 +02:00
return err
}
2020-08-18 06:23:45 +02:00
2021-09-14 21:41:40 +02:00
// CountOrphanedAttachments returns the number of bad attachments
2022-11-19 09:12:33 +01:00
func CountOrphanedAttachments ( ctx context . Context ) ( int64 , error ) {
return db . GetEngine ( ctx ) . Where ( "(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ) .
2021-09-14 21:41:40 +02:00
Count ( new ( Attachment ) )
}
// DeleteOrphanedAttachments delete all bad attachments
2022-11-19 09:12:33 +01:00
func DeleteOrphanedAttachments ( ctx context . Context ) error {
_ , err := db . GetEngine ( ctx ) . Where ( "(issue_id > 0 and issue_id not in (select id from issue)) or (release_id > 0 and release_id not in (select id from `release`))" ) .
2021-09-14 21:41:40 +02:00
Delete ( new ( Attachment ) )
return err
}