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.
 
 
 
 
 

114 lines
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. },
  48. Protocols: []nodeinfo.NodeProtocol{
  49. nodeinfo.ProtocolActivityPub,
  50. },
  51. Services: nodeinfo.Services{
  52. Inbound: []nodeinfo.NodeService{},
  53. Outbound: []nodeinfo.NodeService{
  54. nodeinfo.ServiceRSS,
  55. },
  56. },
  57. Software: nodeinfo.SoftwareInfo{
  58. Name: strings.ToLower(serverSoftware),
  59. Version: softwareVer,
  60. },
  61. }
  62. }
  63. func (r nodeInfoResolver) IsOpenRegistration() (bool, error) {
  64. return r.cfg.App.OpenRegistration, nil
  65. }
  66. func (r nodeInfoResolver) Usage() (nodeinfo.Usage, error) {
  67. var collCount, postCount int64
  68. var activeHalfYear, activeMonth int
  69. var err error
  70. collCount, err = r.db.GetTotalCollections()
  71. if err != nil {
  72. collCount = 0
  73. }
  74. postCount, err = r.db.GetTotalPosts()
  75. if err != nil {
  76. log.Error("Unable to fetch post counts: %v", err)
  77. }
  78. if r.cfg.App.PublicStats {
  79. // Display bi-yearly / monthly stats
  80. err = r.db.QueryRow(`SELECT COUNT(*) FROM (
  81. SELECT DISTINCT collection_id
  82. FROM posts
  83. INNER JOIN collections c
  84. ON collection_id = c.id
  85. WHERE collection_id IS NOT NULL
  86. AND updated > DATE_SUB(NOW(), INTERVAL 6 MONTH)) co`).Scan(&activeHalfYear)
  87. err = r.db.QueryRow(`SELECT COUNT(*) FROM (
  88. SELECT DISTINCT collection_id
  89. FROM posts
  90. INNER JOIN FROM collections c
  91. ON collection_id = c.id
  92. WHERE collection_id IS NOT NULL
  93. AND updated > DATE_SUB(NOW(), INTERVAL 1 MONTH)) co`).Scan(&activeMonth)
  94. }
  95. return nodeinfo.Usage{
  96. Users: nodeinfo.UsageUsers{
  97. Total: int(collCount),
  98. ActiveHalfYear: activeHalfYear,
  99. ActiveMonth: activeMonth,
  100. },
  101. LocalPosts: int(postCount),
  102. }, nil
  103. }