From c64d3fa19544ab9aeaa82568b7eae1ee966ac8e6 Mon Sep 17 00:00:00 2001 From: Anthony Wang Date: Sun, 27 Nov 2022 22:30:00 +0000 Subject: [PATCH] Implement commenting on issues from Mastodon --- models/issues/comment.go | 16 ++++++++++++++++ routers/api/v1/activitypub/create.go | 9 +++++---- routers/api/v1/activitypub/person.go | 8 ++++++++ routers/api/v1/activitypub/repo.go | 2 +- services/activitypub/objects.go | 2 ++ services/comments/comments.go | 2 +- services/issue/issue.go | 2 +- 7 files changed, 34 insertions(+), 7 deletions(-) diff --git a/models/issues/comment.go b/models/issues/comment.go index 6877991a9..fe692bd58 100644 --- a/models/issues/comment.go +++ b/models/issues/comment.go @@ -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) +} diff --git a/routers/api/v1/activitypub/create.go b/routers/api/v1/activitypub/create.go index eb4f86d59..8a2f77398 100644 --- a/routers/api/v1/activitypub/create.go +++ b/routers/api/v1/activitypub/create.go @@ -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 } diff --git a/routers/api/v1/activitypub/person.go b/routers/api/v1/activitypub/person.go index 4fbc7f736..4068dfcf6 100644 --- a/routers/api/v1/activitypub/person.go +++ b/routers/api/v1/activitypub/person.go @@ -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") diff --git a/routers/api/v1/activitypub/repo.go b/routers/api/v1/activitypub/repo.go index 9f7d93f22..df1e5e233 100644 --- a/routers/api/v1/activitypub/repo.go +++ b/routers/api/v1/activitypub/repo.go @@ -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 } diff --git a/services/activitypub/objects.go b/services/activitypub/objects.go index ef4e7f3e2..0169194cd 100644 --- a/services/activitypub/objects.go +++ b/services/activitypub/objects.go @@ -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)) diff --git a/services/comments/comments.go b/services/comments/comments.go index 8f478c018..d2406ba07 100644 --- a/services/comments/comments.go +++ b/services/comments/comments.go @@ -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 diff --git a/services/issue/issue.go b/services/issue/issue.go index e18e65b5a..e9b44d405 100644 --- a/services/issue/issue.go +++ b/services/issue/issue.go @@ -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