diff --git a/calendar/api/api.go b/calendar/api/api.go index 5edc6585..01ca9119 100644 --- a/calendar/api/api.go +++ b/calendar/api/api.go @@ -40,6 +40,8 @@ func Init(h *httputils.Handler, env engine.Env, notificationProcessor engine.Not dialogRouter.HandleFunc(config.PathUsers, api.autocompleteConnectedUsers) apiRoutes := h.Router.PathPrefix(config.InternalAPIPath).Subrouter() + calendarsRouter := apiRoutes.PathPrefix(config.PathCalendar).Subrouter() + calendarsRouter.HandleFunc(config.PathList, api.listCalendars).Methods(http.MethodGet) eventsRouter := apiRoutes.PathPrefix(config.PathEvents).Subrouter() eventsRouter.HandleFunc(config.PathCreate, api.createEvent).Methods(http.MethodPost) apiRoutes.HandleFunc(config.PathConnectedUser, api.connectedUserHandler) diff --git a/calendar/api/events.go b/calendar/api/events.go index 63e2ac93..4d41c09d 100644 --- a/calendar/api/events.go +++ b/calendar/api/events.go @@ -24,6 +24,7 @@ import ( const ( createEventDateTimeFormat = "2006-01-02 15:04" createEventDateFormat = "2006-01-02" + HeaderMattermostUserID = "Mattermost-User-Id" ) type createEventPayload struct { @@ -37,6 +38,7 @@ type createEventPayload struct { Subject string `json:"subject"` Location string `json:"location,omitempty"` ChannelID string `json:"channel_id"` + CalendarID string `json:"calendar_id"` } func (cep createEventPayload) ToRemoteEvent(loc *time.Location) (*remote.Event, error) { @@ -237,7 +239,7 @@ func (api *api) createEvent(w http.ResponseWriter, r *http.Request) { }) } - event, err := client.CreateEvent(user.Remote.ID, event) + event, err := client.CreateEvent("", user.Remote.ID, event) if err != nil { api.Logger.With(bot.LogContext{"err": err.Error()}).Errorf("createEvent, error occurred while creating event") httputils.WriteInternalServerError(w, err) @@ -285,3 +287,33 @@ func (api *api) createEvent(w http.ResponseWriter, r *http.Request) { httputils.WriteJSONResponse(w, `{"ok": true}`, http.StatusCreated) } + +func (api *api) listCalendars(w http.ResponseWriter, r *http.Request) { + mattermostUserID := r.Header.Get(HeaderMattermostUserID) + if mattermostUserID == "" { + httputils.WriteUnauthorizedError(w, fmt.Errorf("unauthorized")) + return + } + + user, errStore := api.Store.LoadUser(mattermostUserID) + if errStore != nil && !errors.Is(errStore, store.ErrNotFound) { + api.Logger.With(bot.LogContext{"err": errStore}).Errorf("error loading user from store") + httputils.WriteInternalServerError(w, errStore) + return + } + if errors.Is(errStore, store.ErrNotFound) { + httputils.WriteUnauthorizedError(w, fmt.Errorf("unauthorized")) + return + } + + client := api.Remote.MakeClient(context.Background(), user.OAuth2Token) + + calendars, errMailbox := client.GetCalendars(user.Remote.ID) + if errMailbox != nil { + api.Logger.With(bot.LogContext{"err": errMailbox}).Errorf("error fetching calendar list") + httputils.WriteInternalServerError(w, errMailbox) + return + } + + httputils.WriteJSONResponse(w, calendars, http.StatusOK) +} diff --git a/calendar/config/const.go b/calendar/config/const.go index 04654e6a..89e4ceeb 100644 --- a/calendar/config/const.go +++ b/calendar/config/const.go @@ -27,6 +27,8 @@ const ( InternalAPIPath = "/api/v1" PathEvents = "/events" + PathCalendar = "/calendar" + PathList = "/list" PathCreate = "/create" PathProvider = "/provider" PathConnectedUser = "/me" diff --git a/calendar/engine/calendar.go b/calendar/engine/calendar.go index fd5afd90..8ab30a41 100644 --- a/calendar/engine/calendar.go +++ b/calendar/engine/calendar.go @@ -91,7 +91,7 @@ func (m *mscalendar) CreateEvent(user *User, event *remote.Event, mattermostUser } } - return m.client.CreateEvent(user.Remote.ID, event) + return m.client.CreateEvent("", user.Remote.ID, event) } func (m *mscalendar) DeleteCalendar(user *User, calendarID string) error { diff --git a/calendar/remote/client.go b/calendar/remote/client.go index 17fd0eeb..71b0b8ca 100644 --- a/calendar/remote/client.go +++ b/calendar/remote/client.go @@ -30,7 +30,7 @@ type Calendars interface { } type Events interface { - CreateEvent(remoteUserID string, calendarEvent *Event) (*Event, error) + CreateEvent(calendarID, remoteUserID string, calendarEvent *Event) (*Event, error) AcceptEvent(remoteUserID, eventID string) error DeclineEvent(remoteUserID, eventID string) error TentativelyAcceptEvent(remoteUserID, eventID string) error diff --git a/calendar/remote/mock_remote/mock_client.go b/calendar/remote/mock_remote/mock_client.go index 892d979d..06fed12d 100644 --- a/calendar/remote/mock_remote/mock_client.go +++ b/calendar/remote/mock_remote/mock_client.go @@ -96,9 +96,9 @@ func (mr *MockClientMockRecorder) CreateCalendar(arg0, arg1 interface{}) *gomock } // CreateEvent mocks base method. -func (m *MockClient) CreateEvent(arg0 string, arg1 *remote.Event) (*remote.Event, error) { +func (m *MockClient) CreateEvent(arg0, arg1 string, arg2 *remote.Event) (*remote.Event, error) { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "CreateEvent", arg0, arg1) + ret := m.ctrl.Call(m, "CreateEvent", arg0, arg1, arg2) ret0, _ := ret[0].(*remote.Event) ret1, _ := ret[1].(error) return ret0, ret1 diff --git a/msgraph/create_event.go b/msgraph/create_event.go index fb0a78f8..cdc3b03c 100644 --- a/msgraph/create_event.go +++ b/msgraph/create_event.go @@ -12,7 +12,7 @@ import ( ) // CreateEvent creates a calendar event -func (c *client) CreateEvent(remoteUserID string, in *remote.Event) (*remote.Event, error) { +func (c *client) CreateEvent(calendarID, remoteUserID string, in *remote.Event) (*remote.Event, error) { var out = remote.Event{} if !c.tokenHelpers.CheckUserConnected(c.mattermostUserID) { c.Logger.Warnf(LogUserInactive, c.mattermostUserID)