mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-15 19:02:11 +00:00
fc037b4b82
closes #13585 fixes #9067 fixes #2386 ref #6226 ref #6219 fixes #745 This PR adds support to process incoming emails to perform actions. Currently I added handling of replies and unsubscribing from issues/pulls. In contrast to #13585 the IMAP IDLE command is used instead of polling which results (in my opinion 😉) in cleaner code. Procedure: - When sending an issue/pull reply email, a token is generated which is present in the Reply-To and References header. - IMAP IDLE waits until a new email arrives - The token tells which action should be performed A possible signature and/or reply gets stripped from the content. I added a new service to the drone pipeline to test the receiving of incoming mails. If we keep this in, we may test our outgoing emails too in future. Co-authored-by: silverwind <me@silverwind.io> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
71 lines
1.7 KiB
Go
71 lines
1.7 KiB
Go
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package payload
|
|
|
|
import (
|
|
"context"
|
|
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
"code.gitea.io/gitea/modules/util"
|
|
)
|
|
|
|
const replyPayloadVersion1 byte = 1
|
|
|
|
type payloadReferenceType byte
|
|
|
|
const (
|
|
payloadReferenceIssue payloadReferenceType = iota
|
|
payloadReferenceComment
|
|
)
|
|
|
|
// CreateReferencePayload creates data which GetReferenceFromPayload resolves to the reference again.
|
|
func CreateReferencePayload(reference interface{}) ([]byte, error) {
|
|
var refType payloadReferenceType
|
|
var refID int64
|
|
|
|
switch r := reference.(type) {
|
|
case *issues_model.Issue:
|
|
refType = payloadReferenceIssue
|
|
refID = r.ID
|
|
case *issues_model.Comment:
|
|
refType = payloadReferenceComment
|
|
refID = r.ID
|
|
default:
|
|
return nil, util.NewInvalidArgumentErrorf("unsupported reference type: %T", r)
|
|
}
|
|
|
|
payload, err := util.PackData(refType, refID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return append([]byte{replyPayloadVersion1}, payload...), nil
|
|
}
|
|
|
|
// GetReferenceFromPayload resolves the reference from the payload
|
|
func GetReferenceFromPayload(ctx context.Context, payload []byte) (interface{}, error) {
|
|
if len(payload) < 1 {
|
|
return nil, util.NewInvalidArgumentErrorf("payload to small")
|
|
}
|
|
|
|
if payload[0] != replyPayloadVersion1 {
|
|
return nil, util.NewInvalidArgumentErrorf("unsupported payload version")
|
|
}
|
|
|
|
var ref payloadReferenceType
|
|
var id int64
|
|
if err := util.UnpackData(payload[1:], &ref, &id); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
switch ref {
|
|
case payloadReferenceIssue:
|
|
return issues_model.GetIssueByID(ctx, id)
|
|
case payloadReferenceComment:
|
|
return issues_model.GetCommentByID(ctx, id)
|
|
default:
|
|
return nil, util.NewInvalidArgumentErrorf("unsupported reference type: %T", ref)
|
|
}
|
|
}
|