A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

72 行
1.7 KiB

  1. package writefreely
  2. import (
  3. "github.com/writeas/go-webfinger"
  4. "github.com/writeas/impart"
  5. "github.com/writeas/web-core/log"
  6. "github.com/writeas/writefreely/config"
  7. "net/http"
  8. )
  9. type wfResolver struct {
  10. db *datastore
  11. cfg *config.Config
  12. }
  13. var wfUserNotFoundErr = impart.HTTPError{http.StatusNotFound, "User not found."}
  14. func (wfr wfResolver) FindUser(username string, host, requestHost string, r []webfinger.Rel) (*webfinger.Resource, error) {
  15. var c *Collection
  16. var err error
  17. if wfr.cfg.App.SingleUser {
  18. c, err = wfr.db.GetCollectionByID(1)
  19. } else {
  20. c, err = wfr.db.GetCollection(username)
  21. }
  22. if err != nil {
  23. log.Error("Unable to get blog: %v", err)
  24. return nil, err
  25. }
  26. if wfr.cfg.App.SingleUser {
  27. // Ensure handle matches user-chosen one on single-user blogs
  28. if username != c.Alias {
  29. log.Info("Username '%s' is not handle '%s'", username, c.Alias)
  30. return nil, wfUserNotFoundErr
  31. }
  32. }
  33. // Only return information if site has federation enabled.
  34. // TODO: enable two levels of federation? Unlisted or Public on timelines?
  35. if !wfr.cfg.App.Federation {
  36. return nil, wfUserNotFoundErr
  37. }
  38. res := webfinger.Resource{
  39. Subject: "acct:" + username + "@" + host,
  40. Aliases: []string{
  41. c.CanonicalURL(),
  42. c.FederatedAccount(),
  43. },
  44. Links: []webfinger.Link{
  45. {
  46. HRef: c.CanonicalURL(),
  47. Type: "text/html",
  48. Rel: "https://webfinger.net/rel/profile-page",
  49. },
  50. {
  51. HRef: c.FederatedAccount(),
  52. Type: "application/activity+json",
  53. Rel: "self",
  54. },
  55. },
  56. }
  57. return &res, nil
  58. }
  59. func (wfr wfResolver) DummyUser(username string, hostname string, r []webfinger.Rel) (*webfinger.Resource, error) {
  60. return nil, wfUserNotFoundErr
  61. }
  62. func (wfr wfResolver) IsNotFoundError(err error) bool {
  63. return err == wfUserNotFoundErr
  64. }