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

83 行
2.0 KiB

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