Implement commenting on issues from Mastodon

This commit is contained in:
Anthony Wang 2022-11-27 22:30:00 +00:00
parent f5a50ce457
commit c64d3fa195
Signed by: a
GPG key ID: 42A5B952E6DD8D38
7 changed files with 34 additions and 7 deletions

View file

@ -26,6 +26,7 @@ import (
"code.gitea.io/gitea/modules/markup"
"code.gitea.io/gitea/modules/markup/markdown"
"code.gitea.io/gitea/modules/references"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
@ -1549,3 +1550,18 @@ func FixCommentTypeLabelWithOutsideLabels() (int64, error) {
return res.RowsAffected()
}
func (c *Comment) GetIRI() string {
err := c.LoadIssue()
if err != nil {
return ""
}
err = c.Issue.LoadRepo(db.DefaultContext)
if err != nil {
return ""
}
if strings.Contains(c.Issue.Repo.OwnerName, "@") {
return c.OldTitle
}
return setting.AppURL + "api/v1/activitypub/note/" + c.Issue.Repo.OwnerName + "/" + c.Issue.Repo.Name + "/" + strconv.FormatInt(c.Issue.Index, 10) + "/" + strconv.FormatInt(c.ID, 10)
}

View file

@ -308,10 +308,11 @@ func createComment(ctx context.Context, note *ap.Note) error {
return err
}
_, err = issues_model.CreateCommentCtx(ctx, &issues_model.CreateCommentOptions{
Doer: user,
Repo: repo,
Issue: issue,
Content: note.Content.String(),
Doer: user,
Repo: repo,
Issue: issue,
OldTitle: note.GetLink().String(),
Content: note.Content.String(),
})
return err
}

View file

@ -134,6 +134,14 @@ func PersonInbox(ctx *context.APIContext) {
err = follow(ctx, activity)
case ap.UndoType:
err = unfollow(ctx, activity)
case ap.CreateType:
// TODO: this is kinda a hack
err = ap.OnObject(activity.Object, func(n *ap.Note) error {
noteIRI := n.InReplyTo.GetLink().String()
noteIRISplit := strings.Split(noteIRI, "/")
n.Context = ap.IRI(strings.TrimSuffix(noteIRI, "/"+noteIRISplit[len(noteIRISplit)-1]))
return createComment(ctx, n)
})
default:
log.Info("Incoming unsupported ActivityStreams type: %s", activity.GetType())
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams type not supported")

View file

@ -140,7 +140,7 @@ func RepoInbox(ctx *context.APIContext) {
return createComment(ctx, n)
})
default:
log.Info("Incoming unsupported ActivityStreams object type: %s", activity.Object.GetType())
log.Info("Incoming unsupported ActivityStreams object type: %s", activity.Object.GetType())
ctx.PlainText(http.StatusNotImplemented, "ActivityStreams object type not supported")
return
}

View file

@ -26,8 +26,10 @@ func Note(comment *issues_model.Comment) (*ap.Note, error) {
}
note := ap.Note{
Type: ap.NoteType,
ID: ap.IRI(comment.GetIRI()),
AttributedTo: ap.IRI(comment.Poster.GetIRI()),
Context: ap.IRI(comment.Issue.GetIRI()),
To: ap.ItemCollection{ap.IRI("https://www.w3.org/ns/activitystreams#Public")},
}
note.Content = ap.NaturalLanguageValuesNew()
err = note.Content.Set("en", ap.Content(comment.Content))

View file

@ -36,7 +36,7 @@ func CreateIssueComment(doer *user_model.User, repo *repo_model.Repository, issu
if err != nil {
return nil, err
}
create := activitypub.Create(repo.OriginalURL + "/inbox", note)
create := activitypub.Create(repo.OriginalURL+"/inbox", note)
err = activitypub.Send(doer, create)
if err != nil {
return nil, err

View file

@ -35,7 +35,7 @@ func NewIssue(repo *repo_model.Repository, issue *issues_model.Issue, labelIDs [
if err != nil {
return err
}
create := activitypub.Create(repo.OriginalURL + "/inbox", ticket)
create := activitypub.Create(repo.OriginalURL+"/inbox", ticket)
err = activitypub.Send(issue.Poster, create)
if err != nil {
return err