Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MI-3082]: Fixed the issue 198 'Added the logic to refresh and store the token' #7

Merged
merged 8 commits into from
May 30, 2023
7 changes: 3 additions & 4 deletions server/mscalendar/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"context"

"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/serializer"
)

type Client interface {
Expand All @@ -21,9 +20,9 @@ func (m *mscalendar) MakeClient() (remote.Client, error) {
return nil, err
}

tokenHelpers := &serializer.UserTokenHelpers{
CheckUserStatus: m.Store.MakeCheckUserStatus,
ChangeUserStatus: m.Store.MakeChangeUserStatus,
tokenHelpers := &remote.UserTokenHelpers{
CheckUserStatus: m.Store.CheckUserConnected,
ChangeUserStatus: m.Store.DisconnectUserFromStoreIfNecessary,
Kshitij-Katiyar marked this conversation as resolved.
Show resolved Hide resolved
RefreshAndStoreToken: m.Store.RefreshAndStoreToken,
}

Expand Down
6 changes: 3 additions & 3 deletions server/mscalendar/notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,9 +141,9 @@ func (processor *notificationProcessor) processNotification(n *remote.Notificati
n.Subscription = sub.Remote
n.SubscriptionCreator = creator.Remote

tokenHelpers := &serializer.UserTokenHelpers{
CheckUserStatus: processor.Store.MakeCheckUserStatus,
ChangeUserStatus: processor.Store.MakeChangeUserStatus,
tokenHelpers := &remote.UserTokenHelpers{
CheckUserStatus: processor.Store.CheckUserConnected,
ChangeUserStatus: processor.Store.DisconnectUserFromStoreIfNecessary,
RefreshAndStoreToken: processor.Store.RefreshAndStoreToken,
}
client := processor.Remote.MakeUserClient(context.Background(), creator.OAuth2Token, sub.MattermostCreatorID, processor.Poster, tokenHelpers)
Expand Down
8 changes: 4 additions & 4 deletions server/mscalendar/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/oauth2"

"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/serializer"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/oauth2connect"
)
Expand Down Expand Up @@ -72,9 +72,9 @@ func (app *oauth2App) CompleteOAuth2(authedUserID, code, state string) error {
return err
}

tokenHelpers := &serializer.UserTokenHelpers{
CheckUserStatus: app.Store.MakeCheckUserStatus,
ChangeUserStatus: app.Store.MakeChangeUserStatus,
tokenHelpers := &remote.UserTokenHelpers{
CheckUserStatus: app.Store.CheckUserConnected,
ChangeUserStatus: app.Store.DisconnectUserFromStoreIfNecessary,
RefreshAndStoreToken: app.Store.RefreshAndStoreToken,
}

Expand Down
2 changes: 1 addition & 1 deletion server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (p *Plugin) OnConfigurationChange() (err error) {

e.Dependencies.Poster = e.bot
e.Dependencies.Welcomer = mscalendarBot
e.Dependencies.Store = store.NewPluginStore(p.API, e.bot, e.Dependencies.Tracker)
e.Dependencies.Store = store.NewPluginStore(p.API, e.bot, e.bot, e.Dependencies.Tracker)
e.Dependencies.SettingsPanel = mscalendar.NewSettingsPanel(
e.bot,
e.Dependencies.Store,
Expand Down
1 change: 0 additions & 1 deletion server/remote/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ type Client interface {
GetMailboxSettings(remoteUserID string) (*MailboxSettings, error)
GetMe() (*serializer.User, error)
GetNotificationData(*Notification) (*Notification, error)
GetSchedule(requests []*ScheduleUserInfo, startTime, endTime *serializer.DateTime, availabilityViewInterval int) ([]*ScheduleInformation, error)
ListSubscriptions() ([]*serializer.Subscription, error)
RenewSubscription(subscriptionID string) (*serializer.Subscription, error)
TentativelyAcceptEvent(remoteUserID, eventID string) error
Expand Down
15 changes: 0 additions & 15 deletions server/remote/mock_remote/mock_client.go

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

5 changes: 2 additions & 3 deletions server/remote/mock_remote/mock_remote.go

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

4 changes: 2 additions & 2 deletions server/remote/msgraph/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
msgraph "github.com/yaegashi/msgraph.go/v1.0"

"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/serializer"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils/bot"
)

Expand All @@ -24,7 +24,7 @@ type client struct {

mattermostUserID string
conf *config.Config
tokenHelpers *serializer.UserTokenHelpers
tokenHelpers *remote.UserTokenHelpers

bot.Logger
bot.Poster
Expand Down
4 changes: 2 additions & 2 deletions server/remote/msgraph/create_calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ import (
// CreateCalendar creates a calendar
func (c *client) CreateCalendar(remoteUserID string, calIn *remote.Calendar) (*remote.Calendar, error) {
var calOut = remote.Calendar{}
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}

err := c.rbuilder.Users().ID(remoteUserID).Calendars().Request().JSONRequest(c.ctx, http.MethodPost, "", &calIn, &calOut)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return nil, errors.Wrap(err, "msgraph CreateCalendar")
}
c.Logger.With(bot.LogContext{
Expand Down
4 changes: 2 additions & 2 deletions server/remote/msgraph/create_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ import (
// CreateEvent creates a calendar event
func (c *client) CreateEvent(remoteUserID string, in *serializer.Event) (*serializer.Event, error) {
var out = serializer.Event{}
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}

err := c.rbuilder.Users().ID(remoteUserID).Events().Request().JSONRequest(c.ctx, http.MethodPost, "", &in, &out)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return nil, errors.Wrap(err, "msgraph CreateEvent")
}
return &out, nil
Expand Down
4 changes: 2 additions & 2 deletions server/remote/msgraph/delete_calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
)

func (c *client) DeleteCalendar(remoteUserID string, calID string) error {
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return errors.New(ErrorUserInactive)
}
err := c.rbuilder.Users().ID(remoteUserID).Calendars().ID(calID).Request().Delete(c.ctx)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return errors.Wrap(err, "msgraph DeleteCalendar")
}
c.Logger.With(bot.LogContext{}).Infof("msgraph: DeleteCalendar deleted calendar `%v`.", calID)
Expand Down
16 changes: 8 additions & 8 deletions server/remote/msgraph/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,30 @@ import (

func (c *client) GetEvent(remoteUserID, eventID string) (*serializer.Event, error) {
e := &serializer.Event{}
if !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}

err := c.rbuilder.Users().ID(remoteUserID).Events().ID(eventID).Request().JSONRequest(
c.ctx, http.MethodGet, "", nil, &e)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return nil, errors.Wrap(err, "msgraph GetEvent")
}
return e, nil
}

func (c *client) AcceptEvent(remoteUserID, eventID string) error {
dummy := &msgraph.EventAcceptRequestParameter{}
if !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return errors.New(ErrorUserInactive)
}

err := c.rbuilder.Users().ID(remoteUserID).Events().ID(eventID).Accept(dummy).Request().Post(c.ctx)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return errors.Wrap(err, "msgraph Accept Event")
}

Expand All @@ -46,29 +46,29 @@ func (c *client) AcceptEvent(remoteUserID, eventID string) error {

func (c *client) DeclineEvent(remoteUserID, eventID string) error {
dummy := &msgraph.EventDeclineRequestParameter{}
if !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return errors.New(ErrorUserInactive)
}

err := c.rbuilder.Users().ID(remoteUserID).Events().ID(eventID).Decline(dummy).Request().Post(c.ctx)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return errors.Wrap(err, "msgraph DeclineEvent")
}
return nil
}

func (c *client) TentativelyAcceptEvent(remoteUserID, eventID string) error {
dummy := &msgraph.EventTentativelyAcceptRequestParameter{}
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return errors.New(ErrorUserInactive)
}

err := c.rbuilder.Users().ID(remoteUserID).Events().ID(eventID).TentativelyAccept(dummy).Request().Post(c.ctx)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return errors.Wrap(err, "msgraph TentativelyAcceptEvent")
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions server/remote/msgraph/find_meeting_times.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ import (
// FindMeetingTimes finds meeting time suggestions for a calendar event
func (c *client) FindMeetingTimes(remoteUserID string, params *remote.FindMeetingTimesParameters) (*remote.MeetingTimeSuggestionResults, error) {
meetingsOut := &remote.MeetingTimeSuggestionResults{}
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}

req := c.rbuilder.Users().ID(remoteUserID).FindMeetingTimes(nil).Request()
err := req.JSONRequest(c.ctx, http.MethodPost, "", &params, &meetingsOut)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return nil, errors.Wrap(err, "msgraph FindMeetingTimes")
}

Expand Down
4 changes: 2 additions & 2 deletions server/remote/msgraph/get_calendars.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func (c *client) GetCalendars(remoteUserID string) ([]*remote.Calendar, error) {
var v struct {
Value []*remote.Calendar `json:"value"`
}
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}
Expand All @@ -25,7 +25,7 @@ func (c *client) GetCalendars(remoteUserID string) ([]*remote.Calendar, error) {
req.Expand("children")
err := req.JSONRequest(c.ctx, http.MethodGet, "", nil, &v)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return nil, errors.Wrap(err, "msgraph GetCalendars")
}
c.Logger.With(bot.LogContext{
Expand Down
10 changes: 2 additions & 8 deletions server/remote/msgraph/get_default_calendar_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type calendarViewBatchResponse struct {
func (c *client) GetDefaultCalendarView(remoteUserID string, start, end time.Time) ([]*serializer.Event, error) {
paramStr := getQueryParamStringForCalendarView(start, end)

if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}
Expand All @@ -42,7 +42,7 @@ func (c *client) GetDefaultCalendarView(remoteUserID string, start, end time.Tim
err := c.rbuilder.Users().ID(remoteUserID).CalendarView().Request().JSONRequest(
c.ctx, http.MethodGet, paramStr, nil, res)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
return nil, errors.Wrap(err, "msgraph GetDefaultCalendarView")
}

Expand All @@ -51,11 +51,6 @@ func (c *client) GetDefaultCalendarView(remoteUserID string, start, end time.Tim

func (c *client) DoBatchViewCalendarRequests(allParams []*remote.ViewCalendarParams) ([]*remote.ViewCalendarResponse, error) {
requests := []*singleRequest{}
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}

for _, params := range allParams {
u := getCalendarViewURL(params)
req := &singleRequest{
Expand All @@ -73,7 +68,6 @@ func (c *client) DoBatchViewCalendarRequests(allParams []*remote.ViewCalendarPar
batchRes := &calendarViewBatchResponse{}
err := c.batchRequest(req, batchRes)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
return nil, errors.Wrap(err, "msgraph ViewCalendar batch request")
}

Expand Down
4 changes: 2 additions & 2 deletions server/remote/msgraph/get_notification_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
func (c *client) GetNotificationData(orig *remote.Notification) (*remote.Notification, error) {
n := *orig
wh := n.Webhook.(*webhook)
if c.tokenHelpers != nil && !c.tokenHelpers.CheckUserStatus(c.Logger, c.mattermostUserID) {
if !c.tokenHelpers.CheckUserStatus(c.mattermostUserID) {
c.Logger.Warnf(LogUserInactive, c.mattermostUserID)
return nil, errors.New(ErrorUserInactive)
}
Expand All @@ -26,7 +26,7 @@ func (c *client) GetNotificationData(orig *remote.Notification) (*remote.Notific
event := serializer.Event{}
_, err := c.CallJSON(http.MethodGet, wh.Resource, nil, &event)
if err != nil {
c.tokenHelpers.ChangeUserStatus(err, c.Logger, c.mattermostUserID, c.Poster)
c.tokenHelpers.ChangeUserStatus(err, c.mattermostUserID)
c.Logger.With(bot.LogContext{
"Resource": wh.Resource,
"subscriptionID": wh.SubscriptionID,
Expand Down
Loading