webfinger.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package apmodels
  2. import (
  3. "fmt"
  4. )
  5. // WebfingerResponse represents a Webfinger response.
  6. type WebfingerResponse struct {
  7. Aliases []string `json:"aliases"`
  8. Subject string `json:"subject"`
  9. Links []Link `json:"links"`
  10. }
  11. // WebfingerProfileRequestResponse represents a Webfinger profile request response.
  12. type WebfingerProfileRequestResponse struct {
  13. Self string
  14. }
  15. // Link represents a Webfinger response Link entity.
  16. type Link struct {
  17. Rel string `json:"rel"`
  18. Type string `json:"type"`
  19. Href string `json:"href"`
  20. }
  21. // MakeWebfingerResponse will create a new Webfinger response.
  22. func MakeWebfingerResponse(account string, inbox string, host string) WebfingerResponse {
  23. accountIRI := MakeLocalIRIForAccount(account)
  24. streamIRI := MakeLocalIRIForStreamURL()
  25. logoIRI := MakeLocalIRIforLogo()
  26. logoType := GetLogoType()
  27. return WebfingerResponse{
  28. Subject: fmt.Sprintf("acct:%s@%s", account, host),
  29. Aliases: []string{
  30. accountIRI.String(),
  31. },
  32. Links: []Link{
  33. {
  34. Rel: "self",
  35. Type: "application/activity+json",
  36. Href: accountIRI.String(),
  37. },
  38. {
  39. Rel: "http://webfinger.net/rel/profile-page",
  40. Type: "text/html",
  41. Href: accountIRI.String(),
  42. },
  43. {
  44. Rel: "http://webfinger.net/rel/avatar",
  45. Type: logoType,
  46. Href: logoIRI.String(),
  47. },
  48. {
  49. Rel: "alternate",
  50. Type: "application/x-mpegURL",
  51. Href: streamIRI.String(),
  52. },
  53. },
  54. }
  55. }
  56. // MakeWebFingerRequestResponseFromData converts WebFinger data to an easier
  57. // to use model.
  58. func MakeWebFingerRequestResponseFromData(data []map[string]interface{}) WebfingerProfileRequestResponse {
  59. response := WebfingerProfileRequestResponse{}
  60. for _, link := range data {
  61. if link["rel"] == "self" {
  62. return WebfingerProfileRequestResponse{
  63. Self: link["href"].(string),
  64. }
  65. }
  66. }
  67. return response
  68. }