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.
 
 
 
 
 

100 lines
2.4 KiB

  1. package writefreely
  2. import (
  3. "github.com/writeas/go-nodeinfo"
  4. "github.com/writeas/web-core/log"
  5. "github.com/writeas/writefreely/config"
  6. "strings"
  7. )
  8. type nodeInfoResolver struct {
  9. cfg *config.Config
  10. db *datastore
  11. }
  12. func nodeInfoConfig(db *datastore, cfg *config.Config) *nodeinfo.Config {
  13. name := cfg.App.SiteName
  14. desc := "Minimal, federated blogging platform."
  15. if cfg.App.SingleUser {
  16. // Fetch blog information, instead
  17. coll, err := db.GetCollectionByID(1)
  18. if err == nil {
  19. desc = coll.Description
  20. }
  21. }
  22. return &nodeinfo.Config{
  23. BaseURL: cfg.App.Host,
  24. InfoURL: "/api/nodeinfo",
  25. Metadata: nodeinfo.Metadata{
  26. NodeName: name,
  27. NodeDescription: desc,
  28. Private: cfg.App.Private,
  29. Software: nodeinfo.SoftwareMeta{
  30. HomePage: softwareURL,
  31. GitHub: "https://github.com/writeas/writefreely",
  32. Follow: "https://writing.exchange/@write_as",
  33. },
  34. },
  35. Protocols: []nodeinfo.NodeProtocol{
  36. nodeinfo.ProtocolActivityPub,
  37. },
  38. Services: nodeinfo.Services{
  39. Inbound: []nodeinfo.NodeService{},
  40. Outbound: []nodeinfo.NodeService{
  41. nodeinfo.ServiceRSS,
  42. },
  43. },
  44. Software: nodeinfo.SoftwareInfo{
  45. Name: strings.ToLower(serverSoftware),
  46. Version: softwareVer,
  47. },
  48. }
  49. }
  50. func (r nodeInfoResolver) IsOpenRegistration() (bool, error) {
  51. return r.cfg.App.OpenRegistration, nil
  52. }
  53. func (r nodeInfoResolver) Usage() (nodeinfo.Usage, error) {
  54. var collCount, postCount int64
  55. var activeHalfYear, activeMonth int
  56. var err error
  57. collCount, err = r.db.GetTotalCollections()
  58. if err != nil {
  59. collCount = 0
  60. }
  61. postCount, err = r.db.GetTotalPosts()
  62. if err != nil {
  63. log.Error("Unable to fetch post counts: %v", err)
  64. }
  65. if r.cfg.App.PublicStats {
  66. // Display bi-yearly / monthly stats
  67. err = r.db.QueryRow(`SELECT COUNT(*) FROM (
  68. SELECT DISTINCT collection_id
  69. FROM posts
  70. INNER JOIN collections c
  71. ON collection_id = c.id
  72. WHERE collection_id IS NOT NULL
  73. AND updated > DATE_SUB(NOW(), INTERVAL 6 MONTH)) co`).Scan(&activeHalfYear)
  74. err = r.db.QueryRow(`SELECT COUNT(*) FROM (
  75. SELECT DISTINCT collection_id
  76. FROM posts
  77. INNER JOIN FROM collections c
  78. ON collection_id = c.id
  79. WHERE collection_id IS NOT NULL
  80. AND updated > DATE_SUB(NOW(), INTERVAL 1 MONTH)) co`).Scan(&activeMonth)
  81. }
  82. return nodeinfo.Usage{
  83. Users: nodeinfo.UsageUsers{
  84. Total: int(collCount),
  85. ActiveHalfYear: activeHalfYear,
  86. ActiveMonth: activeMonth,
  87. },
  88. LocalPosts: int(postCount),
  89. }, nil
  90. }