Skip to content

Commit

Permalink
[MI-3485] Fix content-type of APIs and remove additional information …
Browse files Browse the repository at this point in the history
…from logs (#292)

* [MI-3485] Fix content-type of APIs and remove additional information from logs

* [MI-3485] Update logs

* [MI-3485] Update test and logs
  • Loading branch information
ayusht2810 authored Sep 7, 2023
1 parent a4b030c commit 7f70e9f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 41 deletions.
6 changes: 3 additions & 3 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (a *API) getAvatar(w http.ResponseWriter, r *http.Request) {
func (a *API) processActivity(w http.ResponseWriter, req *http.Request) {
validationToken := req.URL.Query().Get("validationToken")
if validationToken != "" {
w.Header().Add("Content-Type", "plain/text")
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(validationToken))
return
Expand Down Expand Up @@ -114,7 +114,7 @@ func (a *API) processActivity(w http.ResponseWriter, req *http.Request) {
func (a *API) processLifecycle(w http.ResponseWriter, req *http.Request) {
validationToken := req.URL.Query().Get("validationToken")
if validationToken != "" {
w.Header().Add("Content-Type", "plain/text")
w.Header().Add("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(validationToken))
return
Expand All @@ -135,7 +135,7 @@ func (a *API) processLifecycle(w http.ResponseWriter, req *http.Request) {
a.p.API.LogError("Invalid webhook secret received in lifecycle event")
continue
}
a.p.activityHandler.HandleLifecycleEvent(event, a.p.getConfiguration().WebhookSecret, a.p.getConfiguration().EvaluationAPI)
a.p.activityHandler.HandleLifecycleEvent(event)
}

w.WriteHeader(http.StatusOK)
Expand Down
22 changes: 6 additions & 16 deletions server/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (ah *ActivityHandler) Handle(activity msteams.Activity) error {
return nil
}

func (ah *ActivityHandler) HandleLifecycleEvent(event msteams.Activity, webhookSecret string, evaluationAPI bool) {
func (ah *ActivityHandler) HandleLifecycleEvent(event msteams.Activity) {
if !ah.checkSubscription(event.SubscriptionID) {
return
}
Expand All @@ -110,16 +110,6 @@ func (ah *ActivityHandler) HandleLifecycleEvent(event msteams.Activity, webhookS
ah.plugin.GetAPI().LogError("Unable to store the subscription new expiry date", "subscriptionID", event.SubscriptionID, "error", err.Error())
}
}
} else if event.LifecycleEvent == "subscriptionRemoved" {
_, err := ah.plugin.GetClientForApp().SubscribeToChannels(ah.plugin.GetURL()+"/", webhookSecret, !evaluationAPI)
if err != nil {
ah.plugin.GetAPI().LogError("Unable to subscribe to channels", "error", err)
}

_, err = ah.plugin.GetClientForApp().SubscribeToChats(ah.plugin.GetURL()+"/", webhookSecret, !evaluationAPI)
if err != nil {
ah.plugin.GetAPI().LogError("Unable to subscribe to chats", "error", err)
}
}
}

Expand Down Expand Up @@ -242,7 +232,7 @@ func (ah *ActivityHandler) handleCreatedActivity(activityIds msteams.ActivityIds
}

post, errorFound := ah.msgToPost(channelID, senderID, msg, chat)
ah.plugin.GetAPI().LogDebug("Post generated", "post", post)
ah.plugin.GetAPI().LogDebug("Post generated")

// Avoid possible duplication
postInfo, _ := ah.plugin.GetStore().GetPostInfoByMSTeamsID(msg.ChatID+msg.ChannelID, msg.ID)
Expand All @@ -255,11 +245,11 @@ func (ah *ActivityHandler) handleCreatedActivity(activityIds msteams.ActivityIds
newPost, appErr := ah.plugin.GetAPI().CreatePost(post)

if appErr != nil {
ah.plugin.GetAPI().LogError("Unable to create post", "post", post, "error", appErr)
ah.plugin.GetAPI().LogError("Unable to create post", "Error", appErr)
return
}

ah.plugin.GetAPI().LogDebug("Post created", "post", newPost)
ah.plugin.GetAPI().LogDebug("Post created")
if errorFound {
_ = ah.plugin.GetAPI().SendEphemeralPost(senderID, &model.Post{
ChannelId: channelID,
Expand Down Expand Up @@ -358,11 +348,11 @@ func (ah *ActivityHandler) handleUpdatedActivity(activityIds msteams.ActivityIds
if _, appErr := ah.plugin.GetAPI().UpdatePost(post); appErr != nil {
if strings.EqualFold(appErr.Id, "app.post.get.app_error") {
if err = ah.plugin.GetStore().RecoverPost(post.Id); err != nil {
ah.plugin.GetAPI().LogError("Unable to recover the post", "post", post, "error", err)
ah.plugin.GetAPI().LogError("Unable to recover the post", "PostID", post.Id, "error", err)
return
}
} else {
ah.plugin.GetAPI().LogError("Unable to update post", "post", post, "error", appErr)
ah.plugin.GetAPI().LogError("Unable to update post", "PostID", post.Id, "Error", appErr)
return
}
}
Expand Down
18 changes: 9 additions & 9 deletions server/handlers/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestHandleCreatedActivity(t *testing.T) {
setupAPI: func(mockAPI *plugintest.API) {
mockAPI.On("GetDirectChannel", "mockUserID-1", "mockUserID-2").Return(&model.Channel{Id: testutils.GetChannelID()}, nil).Times(1)
mockAPI.On("GetUser", testutils.GetUserID()).Return(testutils.GetUser(model.ChannelAdminRoleId, "test@test.com"), nil).Once()
mockAPI.On("LogDebug", "Post generated", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post generated").Times(1)
mockAPI.On("LogDebug", "duplicate post").Times(1)
mockAPI.On("KVSet", lastReceivedChangeKey, mock.Anything).Return(nil).Once()
},
Expand Down Expand Up @@ -309,9 +309,9 @@ func TestHandleCreatedActivity(t *testing.T) {
setupAPI: func(mockAPI *plugintest.API) {
mockAPI.On("GetDirectChannel", "mockUserID-1", "mockUserID-2").Return(&model.Channel{Id: testutils.GetChannelID()}, nil).Times(1)
mockAPI.On("GetUser", testutils.GetUserID()).Return(testutils.GetUser(model.ChannelAdminRoleId, "test@test.com"), nil).Once()
mockAPI.On("LogDebug", "Post generated", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post generated").Times(1)
mockAPI.On("CreatePost", testutils.GetPostFromTeamsMessage()).Return(nil, testutils.GetInternalServerAppError("unable to create the post")).Times(1)
mockAPI.On("LogError", "Unable to create post", "post", mock.Anything, "error", mock.Anything).Times(1)
mockAPI.On("LogError", "Unable to create post", "Error", mock.Anything).Times(1)
},
setupStore: func(store *mocksStore.Store) {
store.On("MattermostToTeamsUserID", "mock-BotUserID").Return(testutils.GetTeamsUserID(), nil).Times(1)
Expand Down Expand Up @@ -358,9 +358,9 @@ func TestHandleCreatedActivity(t *testing.T) {
setupAPI: func(mockAPI *plugintest.API) {
mockAPI.On("GetDirectChannel", "mockUserID-1", "mockUserID-2").Return(&model.Channel{Id: testutils.GetChannelID()}, nil).Times(1)
mockAPI.On("GetUser", testutils.GetUserID()).Return(testutils.GetUser(model.ChannelAdminRoleId, "test@test.com"), nil).Once()
mockAPI.On("LogDebug", "Post generated", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post generated").Times(1)
mockAPI.On("CreatePost", testutils.GetPostFromTeamsMessage()).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID()), nil).Times(1)
mockAPI.On("LogDebug", "Post created", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post created").Times(1)
mockAPI.On("KVSet", lastReceivedChangeKey, mock.Anything).Return(nil).Times(1)
mockAPI.On("LogWarn", "Error updating the MSTeams/Mattermost post link metadata", "error", mock.Anything).Times(1)
},
Expand Down Expand Up @@ -414,9 +414,9 @@ func TestHandleCreatedActivity(t *testing.T) {
setupAPI: func(mockAPI *plugintest.API) {
mockAPI.On("GetDirectChannel", "mockUserID-1", "mockUserID-2").Return(&model.Channel{Id: testutils.GetChannelID()}, nil).Times(1)
mockAPI.On("GetUser", testutils.GetUserID()).Return(testutils.GetUser(model.ChannelAdminRoleId, "test@test.com"), nil).Once()
mockAPI.On("LogDebug", "Post generated", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post generated").Times(1)
mockAPI.On("CreatePost", testutils.GetPostFromTeamsMessage()).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID()), nil).Times(1)
mockAPI.On("LogDebug", "Post created", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post created").Times(1)
mockAPI.On("KVSet", lastReceivedChangeKey, mock.Anything).Return(nil).Times(1)
},
setupStore: func(store *mocksStore.Store) {
Expand Down Expand Up @@ -458,9 +458,9 @@ func TestHandleCreatedActivity(t *testing.T) {
},
setupAPI: func(mockAPI *plugintest.API) {
mockAPI.On("GetUser", testutils.GetUserID()).Return(testutils.GetUser(model.ChannelAdminRoleId, "test@test.com"), nil).Once()
mockAPI.On("LogDebug", "Post generated", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post generated").Times(1)
mockAPI.On("CreatePost", testutils.GetPostFromTeamsMessage()).Return(testutils.GetPost(testutils.GetChannelID(), testutils.GetUserID()), nil).Times(1)
mockAPI.On("LogDebug", "Post created", "post", mock.Anything).Times(1)
mockAPI.On("LogDebug", "Post created").Times(1)
mockAPI.On("KVSet", lastReceivedChangeKey, mock.Anything).Return(nil).Times(1)
},
setupStore: func(store *mocksStore.Store) {
Expand Down
20 changes: 10 additions & 10 deletions server/message_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (p *Plugin) MessageHasBeenPosted(_ *plugin.Context, post *model.Post) {
}

if post.IsSystemMessage() {
p.API.LogDebug("Skipping system message post", "post", post)
p.API.LogDebug("Skipping system message post", "PostID", post.Id)
return
}

Expand Down Expand Up @@ -226,7 +226,7 @@ func (p *Plugin) SetChatReaction(teamsMessageID, srcUser, channelID string, emoj
}

func (p *Plugin) SetReaction(teamID, channelID, userID string, post *model.Post, emojiName string) error {
p.API.LogDebug("Setting reaction", "teamID", teamID, "channelID", channelID, "post", post, "emojiName", emojiName)
p.API.LogDebug("Setting reaction", "teamID", teamID, "channelID", channelID, "PostID", post.Id, "emojiName", emojiName)

postInfo, err := p.store.GetPostInfoByMattermostID(post.Id)
if err != nil {
Expand Down Expand Up @@ -313,7 +313,7 @@ func (p *Plugin) UnsetChatReaction(teamsMessageID, srcUser, channelID string, em
}

func (p *Plugin) UnsetReaction(teamID, channelID, userID string, post *model.Post, emojiName string) error {
p.API.LogDebug("Unsetting reaction", "teamID", teamID, "channelID", channelID, "post", post, "emojiName", emojiName)
p.API.LogDebug("Unsetting reaction", "teamID", teamID, "channelID", channelID, "PostID", post.Id, "emojiName", emojiName)

postInfo, err := p.store.GetPostInfoByMattermostID(post.Id)
if err != nil {
Expand Down Expand Up @@ -362,7 +362,7 @@ func (p *Plugin) UnsetReaction(teamID, channelID, userID string, post *model.Pos
}

func (p *Plugin) SendChat(srcUser string, usersIDs []string, post *model.Post) (string, error) {
p.API.LogDebug("Sending direct message to MS Teams", "srcUser", srcUser, "usersIDs", usersIDs, "post", post)
p.API.LogDebug("Sending direct message to MS Teams", "SrcUser", srcUser, "UsersIDs", usersIDs, "PostID", post.Id)

parentID := ""
if post.RootId != "" {
Expand Down Expand Up @@ -395,7 +395,7 @@ func (p *Plugin) SendChat(srcUser string, usersIDs []string, post *model.Post) (
teamsUsersIDs[idx] = teamsUserID
}

p.API.LogDebug("Sending direct message to MS Teams", "srcUserID", srcUserID, "teamsUsersIDs", teamsUsersIDs, "post", post)
p.API.LogDebug("Sending direct message to MS Teams", "SrcUserID", srcUserID, "TeamsUsersIDs", teamsUsersIDs, "PostID", post.Id)
text := post.Message

chat, err := client.CreateOrGetChatForUsers(teamsUsersIDs)
Expand Down Expand Up @@ -476,7 +476,7 @@ func (p *Plugin) handlePromptForConnection(userID, channelID string) {
}

func (p *Plugin) Send(teamID, channelID string, user *model.User, post *model.Post) (string, error) {
p.API.LogDebug("Sending message to MS Teams", "teamID", teamID, "channelID", channelID, "post", post)
p.API.LogDebug("Sending message to MS Teams", "TeamID", teamID, "ChannelID", channelID, "PostID", post.Id)

parentID := ""
if post.RootId != "" {
Expand Down Expand Up @@ -540,7 +540,7 @@ func (p *Plugin) Send(teamID, channelID string, user *model.User, post *model.Po
}

func (p *Plugin) Delete(teamID, channelID string, user *model.User, post *model.Post) error {
p.API.LogDebug("Deleting message from MS Teams", "teamID", teamID, "channelID", channelID, "post", post)
p.API.LogDebug("Deleting message from MS Teams", "teamID", teamID, "channelID", channelID)

parentID := ""
if post.RootId != "" {
Expand Down Expand Up @@ -578,7 +578,7 @@ func (p *Plugin) Delete(teamID, channelID string, user *model.User, post *model.
}

func (p *Plugin) DeleteChat(chatID string, user *model.User, post *model.Post) error {
p.API.LogDebug("Deleting direct message from MS Teams", "chatID", chatID, "post", post)
p.API.LogDebug("Deleting direct message from MS Teams", "ChatID", chatID, "PostID", post.Id)

client, err := p.GetClientForUser(user.Id)
if err != nil {
Expand All @@ -605,7 +605,7 @@ func (p *Plugin) DeleteChat(chatID string, user *model.User, post *model.Post) e
}

func (p *Plugin) Update(teamID, channelID string, user *model.User, newPost, oldPost *model.Post) error {
p.API.LogDebug("Updating message to MS Teams", "teamID", teamID, "channelID", channelID, "oldPost", oldPost, "newPost", newPost)
p.API.LogDebug("Updating message to MS Teams", "TeamID", teamID, "ChannelID", channelID, "OldPostID", oldPost.Id, "NewPostID", newPost.Id)

parentID := ""
if oldPost.RootId != "" {
Expand Down Expand Up @@ -671,7 +671,7 @@ func (p *Plugin) Update(teamID, channelID string, user *model.User, newPost, old
}

func (p *Plugin) UpdateChat(chatID string, user *model.User, newPost, oldPost *model.Post) error {
p.API.LogDebug("Updating direct message to MS Teams", "chatID", chatID, "oldPost", oldPost, "newPost", newPost)
p.API.LogDebug("Updating direct message to MS Teams", "ChatID", chatID, "OldPostID", oldPost.Id, "NewPostID", newPost.Id)

postInfo, err := p.store.GetPostInfoByMattermostID(newPost.Id)
if err != nil {
Expand Down
3 changes: 0 additions & 3 deletions server/msteams/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -977,9 +977,6 @@ func (tc *ClientImpl) GetChat(chatID string) (*Chat, error) {
}

func convertToMessage(msg models.ChatMessageable, teamID, channelID, chatID string) *Message {
data, _ := json.Marshal(msg)
fmt.Println("==================", string(data), "===================")

userID := ""
if msg.GetFrom() != nil && msg.GetFrom().GetUser() != nil && msg.GetFrom().GetUser().GetId() != nil {
userID = *msg.GetFrom().GetUser().GetId()
Expand Down

0 comments on commit 7f70e9f

Please sign in to comment.