Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
Robert Gerus | f924374 | 2022-01-25 13:35:33 +0000 | [diff] [blame] | 5 | "time" |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 6 | |
| 7 | "code.hackerspace.pl/hscloud/hswaw/site/calendar" |
| 8 | "github.com/golang/glog" |
| 9 | ) |
| 10 | |
Robert Gerus | f924374 | 2022-01-25 13:35:33 +0000 | [diff] [blame] | 11 | const ( |
| 12 | openDayWeekday = time.Thursday |
| 13 | openDayHour = 18 |
| 14 | ) |
| 15 | |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 16 | // SpaceAPIResponse, per https://spaceapi.io/ - kinda. Mostly rewritten from |
| 17 | // old implementation, someone should update this to use the official schema. |
| 18 | type SpaceAPIResponse struct { |
| 19 | API string `json:"api"` |
| 20 | Space string `json:"space"` |
| 21 | Logo string `json:"logo"` |
| 22 | URL string `json:"url"` |
| 23 | Location SpaceAPILocation `json:"location"` |
| 24 | State SpaceAPIState `json:"state"` |
| 25 | Contact map[string]string `json:"contact"` |
| 26 | IssueReportChannels []string `json:"issue_report_channels"` |
| 27 | Projects []string `json:"projects"` |
| 28 | Feeds map[string]SpaceAPIFeed `json:"feeds"` |
| 29 | Sensors map[string][]SpaceAPISensor `json:"sensors"` |
| 30 | } |
| 31 | |
| 32 | type SpaceAPILocation struct { |
| 33 | Latitude float64 `json:"lat"` |
| 34 | Longitude float64 `json:"lon"` |
| 35 | Address string `json:"address"` |
| 36 | } |
| 37 | |
| 38 | type SpaceAPIState struct { |
| 39 | Open bool `json:"open"` |
| 40 | Message string `json:"message"` |
| 41 | Icon struct { |
| 42 | Open string `json:"open"` |
| 43 | Closed string `json:"closed"` |
| 44 | } `json:"icon"` |
| 45 | } |
| 46 | |
| 47 | type SpaceAPIFeed struct { |
| 48 | Type string `json:"type"` |
| 49 | URL string `json:"url"` |
| 50 | } |
| 51 | |
| 52 | type SpaceAPISensor struct { |
| 53 | Value int `json:"value"` |
| 54 | Names []string `json:"names"` |
| 55 | } |
| 56 | |
| 57 | func generateSpaceAPIResponse(ctx context.Context) SpaceAPIResponse { |
| 58 | state := SpaceAPIState{} |
| 59 | state.Icon.Open = "https://static.hackerspace.pl/img/status-open-small.png" |
| 60 | state.Icon.Closed = "https://static.hackerspace.pl/img/status-closed-small.png" |
Robert Gerus | f924374 | 2022-01-25 13:35:33 +0000 | [diff] [blame] | 61 | |
| 62 | location, _ := time.LoadLocation("Europe/Warsaw") |
| 63 | now := time.Now().In(location) |
| 64 | hour, _, _ := now.Clock() |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 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 | |
Robert Gerus | f924374 | 2022-01-25 13:35:33 +0000 | [diff] [blame] | 78 | if peopleNowPresent.Value > 0 { |
| 79 | state.Open = true |
| 80 | } |
| 81 | |
| 82 | if state.Open && now.Weekday() == openDayWeekday && hour >= openDayHour { |
| 83 | state.Message = "open for public" |
| 84 | } else { |
| 85 | state.Message = "members only" |
| 86 | } |
| 87 | |
Serge Bazanski | 11b276d | 2021-07-11 23:49:55 +0000 | [diff] [blame] | 88 | res := SpaceAPIResponse{ |
| 89 | API: "0.13", |
| 90 | Space: "Warsaw Hackerspace", |
| 91 | Logo: "https://static.hackerspace.pl/img/syrenka-black.png", |
| 92 | URL: "https://hackerspace.pl", |
| 93 | Location: SpaceAPILocation{ |
| 94 | Latitude: 52.24160, |
| 95 | Longitude: 20.98485, |
| 96 | Address: "ul. Wolność 2A, 01-018 Warszawa, Poland", |
| 97 | }, |
| 98 | State: state, |
| 99 | Contact: map[string]string{ |
| 100 | "irc": "irc://irc.libera.chat/#hswaw", |
| 101 | "twitter": "@hackerspacepl", |
| 102 | "facebook": "hackerspacepl", |
| 103 | "ml": "waw@lists.hackerspace.pl", |
| 104 | }, |
| 105 | IssueReportChannels: []string{"irc"}, |
| 106 | Projects: []string{ |
| 107 | "https://wiki.hackerspace.pl/projects", |
| 108 | }, |
| 109 | Feeds: map[string]SpaceAPIFeed{ |
| 110 | "blog": SpaceAPIFeed{ |
| 111 | Type: "atom", |
| 112 | URL: feedsURLs["blog"], |
| 113 | }, |
| 114 | "calendar": SpaceAPIFeed{ |
| 115 | Type: "ical", |
| 116 | URL: calendar.EventsURL, |
| 117 | }, |
| 118 | "wiki": SpaceAPIFeed{ |
| 119 | Type: "rss", |
| 120 | URL: "https://wiki.hackerspace.pl/feed.php", |
| 121 | }, |
| 122 | }, |
| 123 | Sensors: map[string][]SpaceAPISensor{ |
| 124 | "people_now_present": []SpaceAPISensor{peopleNowPresent}, |
| 125 | }, |
| 126 | } |
| 127 | |
| 128 | return res |
| 129 | } |