diff options
Diffstat (limited to 'routers/api/v1/activitypub/repository.go')
-rw-r--r-- | routers/api/v1/activitypub/repository.go | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/routers/api/v1/activitypub/repository.go b/routers/api/v1/activitypub/repository.go new file mode 100644 index 000000000..b1aab70d0 --- /dev/null +++ b/routers/api/v1/activitypub/repository.go @@ -0,0 +1,90 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package activitypub + +import ( + "net/http" + "net/url" + "strings" + + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/modules/context" + "code.gitea.io/gitea/modules/setting" + projectbase_service "code.gitea.io/gitea/services/projectbase" + "github.com/go-fed/activity/streams" +) + +// Repository function +func Repository(ctx *context.APIContext) { + // swagger:operation GET /activitypub/repos/{username}/{reponame} activitypub activitypubRepository + // --- + // summary: Returns the repository + // produces: + // - application/json + // parameters: + // - name: username + // in: path + // description: username of the user + // type: string + // required: true + // - name: reponame + // in: path + // description: name of the repo + // type: string + // required: true + // responses: + // "200": + // "$ref": "#/responses/ActivityPub" + + repository := ctx.Repo.Repository + if err := projectbase_service.InitProjectBase(ctx, repository); err != nil { + ctx.Error(http.StatusInternalServerError, "InitProjectBase", err) + } + + // + // Transient token + // + token := &models.AccessToken{ + UID: repository.Owner.ID, + Name: "projectbase", + } + if err := models.NewAccessToken(token); err != nil { + ctx.Error(http.StatusInternalServerError, "NewAccessToken", err) + } + defer func() { + if err := models.DeleteAccessTokenByID(token.ID, token.UID); err != nil { + ctx.Error(http.StatusInternalServerError, "DeleteAccessTokenByID", err) + } + }() + + if err := projectbase_service.UpdateProjectBase(ctx, token.TokenHash, repository); err != nil { + ctx.Error(http.StatusInternalServerError, "UpdateProjectBase", err) + } + + r := streams.NewForgeFedRepository() + + id := streams.NewJSONLDIdProperty() + link := strings.TrimSuffix(setting.AppURL, "/") + strings.TrimSuffix(ctx.Req.URL.EscapedPath(), "/") + idIRI, _ := url.Parse(link) + id.SetIRI(idIRI) + r.SetJSONLDId(id) + + name := streams.NewActivityStreamsNameProperty() + name.AppendXMLSchemaString(ctx.Params("reponame")) + r.SetActivityStreamsName(name) + + projectbase := repository.ProjectBaseCloneLink() + + urlProp := streams.NewActivityStreamsUrlProperty() + urlObject, _ := url.Parse(projectbase.SSH) + urlProp.AppendIRI(urlObject) + urlObject, _ = url.Parse(projectbase.HTTPS) + urlProp.AppendIRI(urlObject) + r.SetActivityStreamsUrl(urlProp) + + var jsonmap map[string]interface{} + jsonmap, _ = streams.Serialize(r) + ctx.JSON(http.StatusOK, jsonmap) +} |