// Copyright 2023 The Forgejo Authors. All rights reserved. // SPDX-License-Identifier: MIT package activitypub import ( "fmt" "io" "net/http" "strings" "code.gitea.io/gitea/modules/context" "code.gitea.io/gitea/modules/forgefed" "code.gitea.io/gitea/modules/setting" ap "github.com/go-ap/activitypub" ) // Repo function returns the Repository actor of a repo func Repo(ctx *context.APIContext) { // swagger:operation GET /activitypub/repo/{username}/{reponame} activitypub activitypubRepo // --- // summary: Returns the repository // produces: // - application/activity+json // parameters: // - name: username // in: path // description: username of the user // type: string // required: true // - name: reponame // in: path // description: name of the repository // type: string // required: true // responses: // "200": // "$ref": "#/responses/ActivityPub" iri := ctx.Repo.Repository.GetIRI() repo := forgefed.RepositoryNew(ap.IRI(iri)) repo.Name = ap.DefaultNaturalLanguageValue(ctx.Repo.Repository.Name) repo.AttributedTo = ap.IRI(ctx.Repo.Owner.GetIRI()) repo.Summary = ap.DefaultNaturalLanguageValue(ctx.Repo.Repository.Description) repo.Inbox = ap.IRI(iri + "/inbox") repo.Outbox = ap.IRI(iri + "/outbox") repo.Followers = ap.IRI(iri + "/followers") repo.Team = ap.IRI(iri + "/team") if ctx.Repo.Repository.IsFork { _ = ctx.Repo.Repository.GetBaseRepo(ctx) repo.ForkedFrom = ap.IRI(ctx.Repo.Repository.BaseRepo.GetIRI()) } response(ctx, repo) } // RepoInbox function handles the incoming data for a repo inbox func RepoInbox(ctx *context.APIContext) { // swagger:operation POST /activitypub/repo/{username}/{reponame}/inbox activitypub activitypubRepoInbox // --- // summary: Send to the inbox // produces: // - application/activity+json // parameters: // - name: username // in: path // description: username of the user // type: string // required: true // - name: reponame // in: path // description: name of the repository // type: string // required: true // responses: // "202": // "$ref": "#/responses/empty" body, _ := io.ReadAll(io.LimitReader(ctx.Req.Body, setting.Federation.MaxSize)) // body := []byte("{\"type\":\"Ticket\",\"context\":\"https://test.exozy.me/api/v1/activitypub/repo/test/Hello-world\",\"summary\":\"Update 'README.md'\",\"content\":\"WIIJLJk\",\"attributedTo\":\"https://test.exozy.me/api/v1/activitypub/user/test\",\"isResolved\":\"false\",\"origin\":\"https://test.exozy.me/branch/test/Hello-world/main\",\"target\":\"https://git.exozy.me/branch/a/Hello-world/main\",\"name\":\"#1\"}") s := string(body) fmt.Println(s) // HUGE HACK // VERY BAD IDEA x := strings.Index(s, "ct\":{") body = []byte(strings.Replace(s[x+4:len(s)-1], "Object", "Ticket", 1)) fmt.Println(string(body)) var ticket forgefed.Ticket fmt.Println(ticket) err := ticket.UnmarshalJSON(body) if err != nil { ctx.ServerError("UnmarshalJSON", err) return } fmt.Println(ticket) fmt.Println(createTicket(ctx, &ticket)) ctx.Status(http.StatusNoContent) } // RepoOutbox function returns the repo's Outbox OrderedCollection func RepoOutbox(ctx *context.APIContext) { // swagger:operation GET /activitypub/repo/{username}/{reponame}/outbox activitypub activitypubRepoOutbox // --- // summary: Returns the outbox // produces: // - application/activity+json // parameters: // - name: username // in: path // description: username of the user // type: string // required: true // - name: reponame // in: path // description: name of the repository // type: string // required: true // responses: // "501": // "$ref": "#/responses/empty" ctx.Status(http.StatusNotImplemented) } // RepoFollowers function returns the repo's Followers OrderedCollection func RepoFollowers(ctx *context.APIContext) { // swagger:operation GET /activitypub/repo/{username}/{reponame}/followers activitypub activitypubRepoFollowers // --- // summary: Returns the followers collection // produces: // - application/activity+json // parameters: // - name: username // in: path // description: username of the user // type: string // required: true // - name: reponame // in: path // description: name of the repository // type: string // required: true // responses: // "200": // "$ref": "#/responses/ActivityPub" // TODO ctx.Status(http.StatusNotImplemented) }