2018-10-17 22:57:37 +00:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/writeas/web-core/log"
|
|
|
|
"github.com/writeas/writefreely/config"
|
2018-12-17 06:27:04 +00:00
|
|
|
"github.com/writefreely/go-nodeinfo"
|
2018-11-08 18:37:42 +00:00
|
|
|
"strings"
|
2018-10-17 22:57:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type nodeInfoResolver struct {
|
|
|
|
cfg *config.Config
|
|
|
|
db *datastore
|
|
|
|
}
|
|
|
|
|
2018-11-10 03:10:46 +00:00
|
|
|
func nodeInfoConfig(db *datastore, cfg *config.Config) *nodeinfo.Config {
|
2018-10-17 22:57:37 +00:00
|
|
|
name := cfg.App.SiteName
|
2018-12-03 23:36:33 +00:00
|
|
|
desc := cfg.App.SiteDesc
|
|
|
|
if desc == "" {
|
|
|
|
desc = "Minimal, federated blogging platform."
|
|
|
|
}
|
2018-11-10 03:10:46 +00:00
|
|
|
if cfg.App.SingleUser {
|
|
|
|
// Fetch blog information, instead
|
|
|
|
coll, err := db.GetCollectionByID(1)
|
|
|
|
if err == nil {
|
|
|
|
desc = coll.Description
|
|
|
|
}
|
|
|
|
}
|
2018-10-17 22:57:37 +00:00
|
|
|
return &nodeinfo.Config{
|
2018-10-27 21:02:40 +00:00
|
|
|
BaseURL: cfg.App.Host,
|
2018-10-17 22:57:37 +00:00
|
|
|
InfoURL: "/api/nodeinfo",
|
|
|
|
|
|
|
|
Metadata: nodeinfo.Metadata{
|
|
|
|
NodeName: name,
|
2018-11-10 03:10:46 +00:00
|
|
|
NodeDescription: desc,
|
2018-10-17 22:57:37 +00:00
|
|
|
Private: cfg.App.Private,
|
|
|
|
Software: nodeinfo.SoftwareMeta{
|
|
|
|
HomePage: softwareURL,
|
|
|
|
GitHub: "https://github.com/writeas/writefreely",
|
|
|
|
Follow: "https://writing.exchange/@write_as",
|
|
|
|
},
|
2018-12-17 06:27:04 +00:00
|
|
|
MaxBlogs: cfg.App.MaxBlogs,
|
2018-10-17 22:57:37 +00:00
|
|
|
},
|
|
|
|
Protocols: []nodeinfo.NodeProtocol{
|
|
|
|
nodeinfo.ProtocolActivityPub,
|
|
|
|
},
|
|
|
|
Services: nodeinfo.Services{
|
2018-11-08 18:38:08 +00:00
|
|
|
Inbound: []nodeinfo.NodeService{},
|
|
|
|
Outbound: []nodeinfo.NodeService{
|
|
|
|
nodeinfo.ServiceRSS,
|
|
|
|
},
|
2018-10-17 22:57:37 +00:00
|
|
|
},
|
|
|
|
Software: nodeinfo.SoftwareInfo{
|
2018-11-08 18:37:42 +00:00
|
|
|
Name: strings.ToLower(serverSoftware),
|
2018-10-17 22:57:37 +00:00
|
|
|
Version: softwareVer,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r nodeInfoResolver) IsOpenRegistration() (bool, error) {
|
2018-11-08 06:28:08 +00:00
|
|
|
return r.cfg.App.OpenRegistration, nil
|
2018-10-17 22:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r nodeInfoResolver) Usage() (nodeinfo.Usage, error) {
|
2018-11-21 19:05:44 +00:00
|
|
|
var collCount, postCount int64
|
|
|
|
var activeHalfYear, activeMonth int
|
|
|
|
var err error
|
|
|
|
collCount, err = r.db.GetTotalCollections()
|
2018-10-17 22:57:37 +00:00
|
|
|
if err != nil {
|
|
|
|
collCount = 0
|
|
|
|
}
|
2018-11-21 19:05:44 +00:00
|
|
|
postCount, err = r.db.GetTotalPosts()
|
2018-10-17 22:57:37 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error("Unable to fetch post counts: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.cfg.App.PublicStats {
|
|
|
|
// Display bi-yearly / monthly stats
|
2018-11-08 17:16:52 +00:00
|
|
|
err = r.db.QueryRow(`SELECT COUNT(*) FROM (
|
2018-10-17 22:57:37 +00:00
|
|
|
SELECT DISTINCT collection_id
|
|
|
|
FROM posts
|
|
|
|
INNER JOIN collections c
|
|
|
|
ON collection_id = c.id
|
|
|
|
WHERE collection_id IS NOT NULL
|
2018-11-08 17:16:52 +00:00
|
|
|
AND updated > DATE_SUB(NOW(), INTERVAL 6 MONTH)) co`).Scan(&activeHalfYear)
|
2018-10-17 22:57:37 +00:00
|
|
|
|
2018-11-08 17:16:52 +00:00
|
|
|
err = r.db.QueryRow(`SELECT COUNT(*) FROM (
|
2018-10-17 22:57:37 +00:00
|
|
|
SELECT DISTINCT collection_id
|
|
|
|
FROM posts
|
|
|
|
INNER JOIN FROM collections c
|
|
|
|
ON collection_id = c.id
|
|
|
|
WHERE collection_id IS NOT NULL
|
2018-11-08 17:16:52 +00:00
|
|
|
AND updated > DATE_SUB(NOW(), INTERVAL 1 MONTH)) co`).Scan(&activeMonth)
|
2018-10-17 22:57:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodeinfo.Usage{
|
|
|
|
Users: nodeinfo.UsageUsers{
|
2018-11-21 19:05:44 +00:00
|
|
|
Total: int(collCount),
|
2018-10-17 22:57:37 +00:00
|
|
|
ActiveHalfYear: activeHalfYear,
|
|
|
|
ActiveMonth: activeMonth,
|
|
|
|
},
|
2018-11-21 19:05:44 +00:00
|
|
|
LocalPosts: int(postCount),
|
2018-10-17 22:57:37 +00:00
|
|
|
}, nil
|
|
|
|
}
|