hscloud/hswaw/site/spaceapi.go
Robert Gerus f92437451c hswaw/site: spaceapi: make the open state depend on members presence
Change-Id: Ibe5b25a989b06f757a696fc2c325695b6ad9d158
Reviewed-on: https://gerrit.hackerspace.pl/c/hscloud/+/1248
Reviewed-by: q3k <q3k@hackerspace.pl>
2022-01-25 14:52:07 +00:00

129 lines
3.5 KiB
Go

package main
import (
"context"
"time"
"code.hackerspace.pl/hscloud/hswaw/site/calendar"
"github.com/golang/glog"
)
const (
openDayWeekday = time.Thursday
openDayHour = 18
)
// SpaceAPIResponse, per https://spaceapi.io/ - kinda. Mostly rewritten from
// old implementation, someone should update this to use the official schema.
type SpaceAPIResponse struct {
API string `json:"api"`
Space string `json:"space"`
Logo string `json:"logo"`
URL string `json:"url"`
Location SpaceAPILocation `json:"location"`
State SpaceAPIState `json:"state"`
Contact map[string]string `json:"contact"`
IssueReportChannels []string `json:"issue_report_channels"`
Projects []string `json:"projects"`
Feeds map[string]SpaceAPIFeed `json:"feeds"`
Sensors map[string][]SpaceAPISensor `json:"sensors"`
}
type SpaceAPILocation struct {
Latitude float64 `json:"lat"`
Longitude float64 `json:"lon"`
Address string `json:"address"`
}
type SpaceAPIState struct {
Open bool `json:"open"`
Message string `json:"message"`
Icon struct {
Open string `json:"open"`
Closed string `json:"closed"`
} `json:"icon"`
}
type SpaceAPIFeed struct {
Type string `json:"type"`
URL string `json:"url"`
}
type SpaceAPISensor struct {
Value int `json:"value"`
Names []string `json:"names"`
}
func generateSpaceAPIResponse(ctx context.Context) SpaceAPIResponse {
state := SpaceAPIState{}
state.Icon.Open = "https://static.hackerspace.pl/img/status-open-small.png"
state.Icon.Closed = "https://static.hackerspace.pl/img/status-closed-small.png"
location, _ := time.LoadLocation("Europe/Warsaw")
now := time.Now().In(location)
hour, _, _ := now.Clock()
peopleNowPresent := SpaceAPISensor{}
atState, err := getAt(ctx)
if err != nil {
glog.Errorf("Failed to get checkinator status: %v", err)
} else {
peopleNowPresent.Names = make([]string, len(atState.Users))
for i, u := range atState.Users {
peopleNowPresent.Names[i] = u.Login
}
peopleNowPresent.Value = len(peopleNowPresent.Names)
}
if peopleNowPresent.Value > 0 {
state.Open = true
}
if state.Open && now.Weekday() == openDayWeekday && hour >= openDayHour {
state.Message = "open for public"
} else {
state.Message = "members only"
}
res := SpaceAPIResponse{
API: "0.13",
Space: "Warsaw Hackerspace",
Logo: "https://static.hackerspace.pl/img/syrenka-black.png",
URL: "https://hackerspace.pl",
Location: SpaceAPILocation{
Latitude: 52.24160,
Longitude: 20.98485,
Address: "ul. Wolność 2A, 01-018 Warszawa, Poland",
},
State: state,
Contact: map[string]string{
"irc": "irc://irc.libera.chat/#hswaw",
"twitter": "@hackerspacepl",
"facebook": "hackerspacepl",
"ml": "waw@lists.hackerspace.pl",
},
IssueReportChannels: []string{"irc"},
Projects: []string{
"https://wiki.hackerspace.pl/projects",
},
Feeds: map[string]SpaceAPIFeed{
"blog": SpaceAPIFeed{
Type: "atom",
URL: feedsURLs["blog"],
},
"calendar": SpaceAPIFeed{
Type: "ical",
URL: calendar.EventsURL,
},
"wiki": SpaceAPIFeed{
Type: "rss",
URL: "https://wiki.hackerspace.pl/feed.php",
},
},
Sensors: map[string][]SpaceAPISensor{
"people_now_present": []SpaceAPISensor{peopleNowPresent},
},
}
return res
}