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.
 
 
 
 
 

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