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.
 
 
 
 
 

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