Skip to content

Commit

Permalink
inject rooms into ical
Browse files Browse the repository at this point in the history
  • Loading branch information
CubicrootXYZ committed Dec 22, 2024
1 parent 97762b4 commit 119cca6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
1 change: 1 addition & 0 deletions internal/api/my_schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func (service *service) MySchedule(w http.ResponseWriter, r *http.Request) {
calEv.SetStartAt(ev.StartTime)
calEv.SetEndAt(ev.EndTime)
calEv.SetSummary(ev.Name)
calEv.SetLocation(ev.Room)
}

respBody := cal.Serialize()
Expand Down
7 changes: 7 additions & 0 deletions internal/c3hub/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@ type Event struct {
StartTime time.Time
EndTime time.Time
}

type Room struct {
ID string
Name string
Type string
Assembly string
}
68 changes: 66 additions & 2 deletions internal/c3hub/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,15 @@ func (service *service) getEvents(
return nil, err
}

// TODO: inject rooms.
rooms, err := service.getRooms(ctx)
if err != nil {
return nil, err
}
roomIDToRoom := make(map[string]Room, len(rooms))
for _, room := range rooms {
roomIDToRoom[room.ID] = room
}

events := make([]Event, len(intEvents))
for i := range intEvents {
start, err := time.Parse(time.RFC3339, intEvents[i].ScheduleStart)
Expand All @@ -101,11 +109,67 @@ func (service *service) getEvents(
events[i] = Event{
ID: intEvents[i].ID,
Name: intEvents[i].Name,
Room: "",
StartTime: start,
EndTime: end,
}

if intEvents[i].Room != nil {
if room, ok := roomIDToRoom[*intEvents[i].Room]; ok {
events[i].Room = room.Name
if room.Assembly != "" {
events[i].Room += " - " + room.Assembly
}
events[i].Room += " [" + room.Type + "]"
}
} else if intEvents[i].Location != nil {
events[i].Room = *intEvents[i].Location
}

}

return events, nil
}

func (service *service) getRooms(
ctx context.Context,
) ([]Room, error) {
type HubRoom struct {
ID string
Name string
Assembly string
RoomType string `json:"room_type"`
}

r, err := http.NewRequest(http.MethodGet, service.config.BaseURL+"/rooms", nil)
if err != nil {
return nil, err
}
r.Header.Add("Cookie", "38C3_SESSION="+service.config.SessionCookie)
r = r.WithContext(ctx)

resp, err := http.DefaultClient.Do(r)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}

var intRooms []HubRoom
err = json.NewDecoder(resp.Body).Decode(&intRooms)
if err != nil {
return nil, err
}

rooms := make([]Room, len(intRooms))
for i := range intRooms {
rooms[i] = Room{
ID: intRooms[i].ID,
Name: intRooms[i].Name,
Assembly: intRooms[i].Assembly,
Type: intRooms[i].RoomType,
}
}

return rooms, nil
}

0 comments on commit 119cca6

Please sign in to comment.