This repository has been archived by the owner on Feb 2, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
nodeinfo.go
157 lines (140 loc) · 3.96 KB
/
nodeinfo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package controllers
//
// GangGo Application Server
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import (
"github.com/revel/revel"
"git.feneas.org/ganggo/ganggo/app"
"git.feneas.org/ganggo/ganggo/app/models"
)
type NodeInfo struct {
*revel.Controller
}
type IndexJson struct {
Links []IndexLinkJson `json:"links"`
}
type IndexLinkJson struct {
Rel string `json:"rel"`
Href string `json:"href"`
}
type SchemaJson struct {
Version string `json:"version"`
Software SchemaSoftwareJson `json:"software"`
Protocols interface{} `json:"protocols"`
Services SchemaInOutJson `json:"services"`
OpenRegistrations bool `json:"openRegistrations"`
Usage SchemaUsageJson `json:"usage"`
MetaData SchemaMetaDataJson `json:"metadata"`
}
type SchemaInOutJson struct {
Inbound []string `json:"inbound"`
Outbound []string `json:"outbound"`
}
type SchemaSoftwareJson struct {
Name string `json:"name"`
Version string `json:"version"`
}
type SchemaUsageJson struct {
Users SchemaUsersJson `json:"users"`
LocalPosts int `json:"localPosts"`
LocalComments int `json:"localComments"`
}
type SchemaUsersJson struct {
Total int `json:"total"`
ActiveHalfyear int `json:"activeHalfyear"`
ActiveMonth int `json:"activeMonth"`
}
type SchemaMetaDataJson struct {
NodeName string `json:"nodeName"`
XmppChat bool `json:"xmppChat"`
AdminAccount string `json:"adminAccount"`
}
func (n NodeInfo) Index() revel.Result {
revel.Config.SetSection("ganggo")
address, found := revel.Config.String("address")
if !found {
return n.RenderJSON(struct{}{})
}
proto, found := revel.Config.String("proto")
if !found {
return n.RenderJSON(struct{}{})
}
return n.RenderJSON(IndexJson{
Links: []IndexLinkJson{
IndexLinkJson{
Rel: "http://nodeinfo.diaspora.software/ns/schema/1.0",
Href: proto + address + "/nodeinfo/1.0",
},
IndexLinkJson{
Rel: "http://nodeinfo.diaspora.software/ns/schema/2.0",
Href: proto + address + "/nodeinfo/2.0",
},
},
})
}
func generateSchema(version string) SchemaJson {
var protocols interface{}
revel.Config.SetSection("DEFAULT")
appName, found := revel.Config.String("app.name")
if !found {
revel.AppLog.Error("app.name configuration value not found!")
return SchemaJson{}
}
if version == "1.0" {
protocols = SchemaInOutJson{
Inbound: []string{"diaspora"},
Outbound: []string{"diaspora"},
}
} else if version == "2.0" {
protocols = []string{"diaspora"}
}
var (
users models.Users
comments models.Comments
posts models.Posts
)
return SchemaJson{
Version: version,
Software: SchemaSoftwareJson{
Name: "ganggo",
Version: app.AppVersion,
},
Protocols: protocols,
Services: SchemaInOutJson{
Inbound: []string{},
Outbound: []string{},
},
OpenRegistrations: true,
Usage: SchemaUsageJson{
Users: SchemaUsersJson{
Total: users.Count(),
ActiveHalfyear: users.ActiveHalfyear(),
ActiveMonth: users.ActiveMonth(),
},
LocalPosts: posts.CountLocal(),
LocalComments: comments.CountLocal(),
},
MetaData: SchemaMetaDataJson{
NodeName: appName,
XmppChat: false,
AdminAccount: "hq",
},
}
}
func (n NodeInfo) Schema(version string) revel.Result {
return n.RenderJSON(generateSchema(version))
}