Skip to content
2 changes: 2 additions & 0 deletions calendar/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("POST")
apiRoutes.HandleFunc(config.PathConnectedUser, api.connectedUserHandler)
Expand Down
32 changes: 31 additions & 1 deletion calendar/api/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
const (
createEventDateTimeFormat = "2006-01-02 15:04"
createEventDateFormat = "2006-01-02"
HeaderMattermostUserID = "Mattermost-User-Id"
)

type createEventPayload struct {
Expand All @@ -35,6 +36,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) {
Expand Down Expand Up @@ -226,7 +228,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)

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calendarID parameter is hardcoded as an empty string, but the payload includes a CalendarID field (line 41). Consider using payload.CalendarID instead of an empty string to utilize the calendar selection functionality.

Suggested change
event, err := client.CreateEvent("", user.Remote.ID, event)
event, err := client.CreateEvent(payload.CalendarID, user.Remote.ID, event)

Copilot uses AI. Check for mistakes.
if err != nil {
httputils.WriteInternalServerError(w, err)
return
Expand Down Expand Up @@ -272,3 +274,31 @@ 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"))
Comment thread
ayusht2810 marked this conversation as resolved.

@ghost ghost Nov 7, 2025

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider using a shared error like var ErrUnauthorized = errors.New("unauthorized")
to avoid repeating the literal "unauthorized" in multiple places.

return
}

user, errStore := api.Store.LoadUser(mattermostUserID)
if errStore != nil && !errors.Is(errStore, store.ErrNotFound) {
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 {
httputils.WriteInternalServerError(w, errMailbox)
return
}

httputils.WriteJSONResponse(w, calendars, http.StatusOK)
}
2 changes: 2 additions & 0 deletions calendar/config/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (

InternalAPIPath = "/api/v1"
PathEvents = "/events"
PathCalendar = "/calendar"
PathList = "/list"
Comment thread
ayusht2810 marked this conversation as resolved.
PathCreate = "/create"
PathProvider = "/provider"
PathConnectedUser = "/me"
Expand Down
2 changes: 1 addition & 1 deletion calendar/engine/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Copilot AI Nov 6, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The calendarID parameter is hardcoded as an empty string. If calendar selection is intended to be supported, this should accept a calendarID parameter and pass it through, or document why it's intentionally empty.

Copilot uses AI. Check for mistakes.
}

func (m *mscalendar) DeleteCalendar(user *User, calendarID string) error {
Expand Down
2 changes: 1 addition & 1 deletion calendar/remote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions calendar/remote/mock_remote/mock_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion msgraph/create_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
err := c.rbuilder.Users().ID(remoteUserID).Events().Request().JSONRequest(c.ctx, http.MethodPost, "", &in, &out)
if err != nil {
Expand Down