2018-12-24 18:45:15 +01:00
|
|
|
/*
|
2021-04-06 23:24:07 +02:00
|
|
|
* Copyright © 2018-2019, 2021 A Bunch Tell LLC.
|
2018-12-24 18:45:15 +01:00
|
|
|
*
|
|
|
|
* This file is part of WriteFreely.
|
|
|
|
*
|
|
|
|
* WriteFreely is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, included
|
|
|
|
* in the LICENSE file in this source code package.
|
|
|
|
*/
|
2018-12-31 07:05:26 +01:00
|
|
|
|
2018-10-18 00:57:37 +02:00
|
|
|
package writefreely
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/writeas/web-core/log"
|
2018-12-17 07:27:04 +01:00
|
|
|
"github.com/writefreely/go-nodeinfo"
|
2021-04-06 23:24:07 +02:00
|
|
|
"github.com/writefreely/writefreely/config"
|
2018-11-08 19:37:42 +01:00
|
|
|
"strings"
|
2018-10-18 00:57:37 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type nodeInfoResolver struct {
|
|
|
|
cfg *config.Config
|
|
|
|
db *datastore
|
|
|
|
}
|
|
|
|
|
2018-11-10 04:10:46 +01:00
|
|
|
func nodeInfoConfig(db *datastore, cfg *config.Config) *nodeinfo.Config {
|
2018-10-18 00:57:37 +02:00
|
|
|
name := cfg.App.SiteName
|
2018-12-04 00:36:33 +01:00
|
|
|
desc := cfg.App.SiteDesc
|
|
|
|
if desc == "" {
|
|
|
|
desc = "Minimal, federated blogging platform."
|
|
|
|
}
|
2018-11-10 04:10:46 +01:00
|
|
|
if cfg.App.SingleUser {
|
|
|
|
// Fetch blog information, instead
|
|
|
|
coll, err := db.GetCollectionByID(1)
|
|
|
|
if err == nil {
|
|
|
|
desc = coll.Description
|
|
|
|
}
|
|
|
|
}
|
2018-10-18 00:57:37 +02:00
|
|
|
return &nodeinfo.Config{
|
2018-10-27 23:02:40 +02:00
|
|
|
BaseURL: cfg.App.Host,
|
2018-10-18 00:57:37 +02:00
|
|
|
InfoURL: "/api/nodeinfo",
|
|
|
|
|
|
|
|
Metadata: nodeinfo.Metadata{
|
|
|
|
NodeName: name,
|
2018-11-10 04:10:46 +01:00
|
|
|
NodeDescription: desc,
|
2018-10-18 00:57:37 +02:00
|
|
|
Private: cfg.App.Private,
|
|
|
|
Software: nodeinfo.SoftwareMeta{
|
|
|
|
HomePage: softwareURL,
|
2021-04-06 23:24:07 +02:00
|
|
|
GitHub: "https://github.com/writefreely/writefreely",
|
2018-10-18 00:57:37 +02:00
|
|
|
Follow: "https://writing.exchange/@write_as",
|
|
|
|
},
|
2019-01-30 01:07:12 +01:00
|
|
|
MaxBlogs: cfg.App.MaxBlogs,
|
|
|
|
PublicReader: cfg.App.LocalTimeline,
|
2019-04-12 02:37:01 +02:00
|
|
|
Invites: cfg.App.UserInvites != "",
|
2018-10-18 00:57:37 +02:00
|
|
|
},
|
|
|
|
Protocols: []nodeinfo.NodeProtocol{
|
|
|
|
nodeinfo.ProtocolActivityPub,
|
|
|
|
},
|
|
|
|
Services: nodeinfo.Services{
|
2018-11-08 19:38:08 +01:00
|
|
|
Inbound: []nodeinfo.NodeService{},
|
|
|
|
Outbound: []nodeinfo.NodeService{
|
|
|
|
nodeinfo.ServiceRSS,
|
|
|
|
},
|
2018-10-18 00:57:37 +02:00
|
|
|
},
|
|
|
|
Software: nodeinfo.SoftwareInfo{
|
2018-11-08 19:37:42 +01:00
|
|
|
Name: strings.ToLower(serverSoftware),
|
2018-10-18 00:57:37 +02:00
|
|
|
Version: softwareVer,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r nodeInfoResolver) IsOpenRegistration() (bool, error) {
|
2018-11-08 07:28:08 +01:00
|
|
|
return r.cfg.App.OpenRegistration, nil
|
2018-10-18 00:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r nodeInfoResolver) Usage() (nodeinfo.Usage, error) {
|
2018-11-21 20:05:44 +01:00
|
|
|
var collCount, postCount int64
|
|
|
|
var activeHalfYear, activeMonth int
|
|
|
|
var err error
|
|
|
|
collCount, err = r.db.GetTotalCollections()
|
2018-10-18 00:57:37 +02:00
|
|
|
if err != nil {
|
|
|
|
collCount = 0
|
|
|
|
}
|
2018-11-21 20:05:44 +01:00
|
|
|
postCount, err = r.db.GetTotalPosts()
|
2018-10-18 00:57:37 +02: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 18:16:52 +01:00
|
|
|
err = r.db.QueryRow(`SELECT COUNT(*) FROM (
|
2018-10-18 00:57:37 +02: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 18:16:52 +01:00
|
|
|
AND updated > DATE_SUB(NOW(), INTERVAL 6 MONTH)) co`).Scan(&activeHalfYear)
|
2018-10-18 00:57:37 +02:00
|
|
|
|
2018-11-08 18:16:52 +01:00
|
|
|
err = r.db.QueryRow(`SELECT COUNT(*) FROM (
|
2018-10-18 00:57:37 +02: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 18:16:52 +01:00
|
|
|
AND updated > DATE_SUB(NOW(), INTERVAL 1 MONTH)) co`).Scan(&activeMonth)
|
2018-10-18 00:57:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nodeinfo.Usage{
|
|
|
|
Users: nodeinfo.UsageUsers{
|
2018-11-21 20:05:44 +01:00
|
|
|
Total: int(collCount),
|
2018-10-18 00:57:37 +02:00
|
|
|
ActiveHalfYear: activeHalfYear,
|
|
|
|
ActiveMonth: activeMonth,
|
|
|
|
},
|
2018-11-21 20:05:44 +01:00
|
|
|
LocalPosts: int(postCount),
|
2018-10-18 00:57:37 +02:00
|
|
|
}, nil
|
|
|
|
}
|