blob: 13c5aad519b242c45469dd47304558fcc619e7f7 [file] [log] [blame]
Serge Bazanski11b276d2021-07-11 23:49:55 +00001package main
2
3import (
4 "context"
5
6 "code.hackerspace.pl/hscloud/hswaw/site/calendar"
7 "github.com/golang/glog"
8)
9
10// SpaceAPIResponse, per https://spaceapi.io/ - kinda. Mostly rewritten from
11// old implementation, someone should update this to use the official schema.
12type SpaceAPIResponse struct {
13 API string `json:"api"`
14 Space string `json:"space"`
15 Logo string `json:"logo"`
16 URL string `json:"url"`
17 Location SpaceAPILocation `json:"location"`
18 State SpaceAPIState `json:"state"`
19 Contact map[string]string `json:"contact"`
20 IssueReportChannels []string `json:"issue_report_channels"`
21 Projects []string `json:"projects"`
22 Feeds map[string]SpaceAPIFeed `json:"feeds"`
23 Sensors map[string][]SpaceAPISensor `json:"sensors"`
24}
25
26type SpaceAPILocation struct {
27 Latitude float64 `json:"lat"`
28 Longitude float64 `json:"lon"`
29 Address string `json:"address"`
30}
31
32type SpaceAPIState struct {
33 Open bool `json:"open"`
34 Message string `json:"message"`
35 Icon struct {
36 Open string `json:"open"`
37 Closed string `json:"closed"`
38 } `json:"icon"`
39}
40
41type SpaceAPIFeed struct {
42 Type string `json:"type"`
43 URL string `json:"url"`
44}
45
46type SpaceAPISensor struct {
47 Value int `json:"value"`
48 Names []string `json:"names"`
49}
50
51func generateSpaceAPIResponse(ctx context.Context) SpaceAPIResponse {
52 state := SpaceAPIState{}
53 state.Icon.Open = "https://static.hackerspace.pl/img/status-open-small.png"
54 state.Icon.Closed = "https://static.hackerspace.pl/img/status-closed-small.png"
55 // TODO(q3k): post-coronavirus, make automatically open based on calendar
56 // events and Open Thursdays.
57 open := false
58 if open {
59 state.Open = true
60 state.Message = "open for public"
61 } else {
62 state.Open = false
63 state.Message = "members only"
64 }
65
66 peopleNowPresent := SpaceAPISensor{}
67 atState, err := getAt(ctx)
68 if err != nil {
69 glog.Errorf("Failed to get checkinator status: %v", err)
70 } else {
71 peopleNowPresent.Names = make([]string, len(atState.Users))
72 for i, u := range atState.Users {
73 peopleNowPresent.Names[i] = u.Login
74 }
75 peopleNowPresent.Value = len(peopleNowPresent.Names)
76 }
77
78 res := SpaceAPIResponse{
79 API: "0.13",
80 Space: "Warsaw Hackerspace",
81 Logo: "https://static.hackerspace.pl/img/syrenka-black.png",
82 URL: "https://hackerspace.pl",
83 Location: SpaceAPILocation{
84 Latitude: 52.24160,
85 Longitude: 20.98485,
86 Address: "ul. Wolność 2A, 01-018 Warszawa, Poland",
87 },
88 State: state,
89 Contact: map[string]string{
90 "irc": "irc://irc.libera.chat/#hswaw",
91 "twitter": "@hackerspacepl",
92 "facebook": "hackerspacepl",
93 "ml": "waw@lists.hackerspace.pl",
94 },
95 IssueReportChannels: []string{"irc"},
96 Projects: []string{
97 "https://wiki.hackerspace.pl/projects",
98 },
99 Feeds: map[string]SpaceAPIFeed{
100 "blog": SpaceAPIFeed{
101 Type: "atom",
102 URL: feedsURLs["blog"],
103 },
104 "calendar": SpaceAPIFeed{
105 Type: "ical",
106 URL: calendar.EventsURL,
107 },
108 "wiki": SpaceAPIFeed{
109 Type: "rss",
110 URL: "https://wiki.hackerspace.pl/feed.php",
111 },
112 },
113 Sensors: map[string][]SpaceAPISensor{
114 "people_now_present": []SpaceAPISensor{peopleNowPresent},
115 },
116 }
117
118 return res
119}