A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
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.
 
 
 
 
 

82 lines
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. if wfr.cfg.App.SingleUser {
  36. // Ensure handle matches user-chosen one on single-user blogs
  37. if username != c.Alias {
  38. log.Info("Username '%s' is not handle '%s'", username, c.Alias)
  39. return nil, wfUserNotFoundErr
  40. }
  41. }
  42. // Only return information if site has federation enabled.
  43. // TODO: enable two levels of federation? Unlisted or Public on timelines?
  44. if !wfr.cfg.App.Federation {
  45. return nil, wfUserNotFoundErr
  46. }
  47. res := webfinger.Resource{
  48. Subject: "acct:" + username + "@" + host,
  49. Aliases: []string{
  50. c.CanonicalURL(),
  51. c.FederatedAccount(),
  52. },
  53. Links: []webfinger.Link{
  54. {
  55. HRef: c.CanonicalURL(),
  56. Type: "text/html",
  57. Rel: "https://webfinger.net/rel/profile-page",
  58. },
  59. {
  60. HRef: c.FederatedAccount(),
  61. Type: "application/activity+json",
  62. Rel: "self",
  63. },
  64. },
  65. }
  66. return &res, nil
  67. }
  68. func (wfr wfResolver) DummyUser(username string, hostname string, r []webfinger.Rel) (*webfinger.Resource, error) {
  69. return nil, wfUserNotFoundErr
  70. }
  71. func (wfr wfResolver) IsNotFoundError(err error) bool {
  72. return err == wfUserNotFoundErr
  73. }