You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

person.go 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package activitypub
  4. import (
  5. "fmt"
  6. "net/http"
  7. "strings"
  8. "code.gitea.io/gitea/modules/activitypub"
  9. "code.gitea.io/gitea/modules/context"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. ap "github.com/go-ap/activitypub"
  13. "github.com/go-ap/jsonld"
  14. )
  15. // Person function returns the Person actor for a user
  16. func Person(ctx *context.APIContext) {
  17. // swagger:operation GET /activitypub/user-id/{user-id} activitypub activitypubPerson
  18. // ---
  19. // summary: Returns the Person actor for a user
  20. // produces:
  21. // - application/json
  22. // parameters:
  23. // - name: user-id
  24. // in: path
  25. // description: user ID of the user
  26. // type: integer
  27. // required: true
  28. // responses:
  29. // "200":
  30. // "$ref": "#/responses/ActivityPub"
  31. // TODO: the setting.AppURL during the test doesn't follow the definition: "It always has a '/' suffix"
  32. link := fmt.Sprintf("%s/api/v1/activitypub/user-id/%d", strings.TrimSuffix(setting.AppURL, "/"), ctx.ContextUser.ID)
  33. person := ap.PersonNew(ap.IRI(link))
  34. person.Name = ap.NaturalLanguageValuesNew()
  35. err := person.Name.Set("en", ap.Content(ctx.ContextUser.FullName))
  36. if err != nil {
  37. ctx.ServerError("Set Name", err)
  38. return
  39. }
  40. person.PreferredUsername = ap.NaturalLanguageValuesNew()
  41. err = person.PreferredUsername.Set("en", ap.Content(ctx.ContextUser.Name))
  42. if err != nil {
  43. ctx.ServerError("Set PreferredUsername", err)
  44. return
  45. }
  46. person.URL = ap.IRI(ctx.ContextUser.HTMLURL())
  47. person.Icon = ap.Image{
  48. Type: ap.ImageType,
  49. MediaType: "image/png",
  50. URL: ap.IRI(ctx.ContextUser.AvatarLink(ctx)),
  51. }
  52. person.Inbox = ap.IRI(link + "/inbox")
  53. person.Outbox = ap.IRI(link + "/outbox")
  54. person.PublicKey.ID = ap.IRI(link + "#main-key")
  55. person.PublicKey.Owner = ap.IRI(link)
  56. publicKeyPem, err := activitypub.GetPublicKey(ctx.ContextUser)
  57. if err != nil {
  58. ctx.ServerError("GetPublicKey", err)
  59. return
  60. }
  61. person.PublicKey.PublicKeyPem = publicKeyPem
  62. binary, err := jsonld.WithContext(jsonld.IRI(ap.ActivityBaseURI), jsonld.IRI(ap.SecurityContextURI)).Marshal(person)
  63. if err != nil {
  64. ctx.ServerError("MarshalJSON", err)
  65. return
  66. }
  67. ctx.Resp.Header().Add("Content-Type", activitypub.ActivityStreamsContentType)
  68. ctx.Resp.WriteHeader(http.StatusOK)
  69. if _, err = ctx.Resp.Write(binary); err != nil {
  70. log.Error("write to resp err: %v", err)
  71. }
  72. }
  73. // PersonInbox function handles the incoming data for a user inbox
  74. func PersonInbox(ctx *context.APIContext) {
  75. // swagger:operation POST /activitypub/user-id/{user-id}/inbox activitypub activitypubPersonInbox
  76. // ---
  77. // summary: Send to the inbox
  78. // produces:
  79. // - application/json
  80. // parameters:
  81. // - name: user-id
  82. // in: path
  83. // description: user ID of the user
  84. // type: integer
  85. // required: true
  86. // responses:
  87. // "204":
  88. // "$ref": "#/responses/empty"
  89. ctx.Status(http.StatusNoContent)
  90. }