From 5a3adea39f59f298bd7a14deee00a34ec5252124 Mon Sep 17 00:00:00 2001 From: Michael Lim Date: Thu, 21 Nov 2024 13:00:38 -0800 Subject: [PATCH 1/4] Updating Chat prop to include support for external users. --- .../microsoft_teams/microsoft_teams.app.mjs | 46 +++++++++++++++++-- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/components/microsoft_teams/microsoft_teams.app.mjs b/components/microsoft_teams/microsoft_teams.app.mjs index e95589729d10d..bcf3cee50a6cc 100644 --- a/components/microsoft_teams/microsoft_teams.app.mjs +++ b/components/microsoft_teams/microsoft_teams.app.mjs @@ -5,7 +5,7 @@ import constants from "./common/constants.mjs"; export default { type: "app", app: "microsoft_teams", - description: "**Personal accounts are not currently supported by Microsoft Teams.** Refer to Microsoft's documentation [here](https://learn.microsoft.com/en-us/graph/permissions-reference#remarks-7) to learn more.", + description: "Connect and interact with Microsoft Teams, supporting both internal and external communications.", propDefinitions: { team: { type: "string", @@ -58,16 +58,50 @@ export default { chat: { type: "string", label: "Chat", - description: "Team Chat within the organization (No external Contacts)", + description: "Team Chat (internal and external contacts)", async options({ prevContext }) { const response = prevContext.nextLink ? await this.makeRequest({ path: prevContext.nextLink, }) : await this.listChats(); + + const myTenantId = await this.getAuthenticatedUserTenant(); const options = []; + for (const chat of response.value) { - const members = chat.members.map((member) => member.displayName); + const messages = await this.makeRequest({ + path: `/chats/${chat.id}/messages?$top=50`, + }); + + const members = await Promise.all(chat.members.map(async (member) => { + let displayName = member.displayName; + + if (!displayName && messages.value.length > 0) { + const userMessage = messages.value.find((msg) => + msg.from?.user?.id === member.userId); + if (userMessage?.from?.user?.displayName) { + displayName = userMessage.from.user.displayName; + } + } + + if (!displayName) { + try { + const userDetails = await this.makeRequest({ + path: `/users/${member.userId}`, + }); + displayName = userDetails.displayName; + } catch (err) { + displayName = "Unknown User"; + } + } + + const isExternal = member.tenantId !== myTenantId || !member.tenantId; + return isExternal + ? `${displayName} (External)` + : displayName; + })); + options.push({ label: members.join(", "), value: chat.id, @@ -144,6 +178,12 @@ export default { : reduction; }, api); }, + async getAuthenticatedUserTenant() { + const { value } = await this.client() + .api("/organization") + .get(); + return value[0].id; + }, async authenticatedUserId() { const { id } = await this.client() .api("/me") From 2c5e1c345072aeda49ef981f35dd5cc32371571e Mon Sep 17 00:00:00 2001 From: Michael Lim Date: Thu, 21 Nov 2024 13:30:34 -0800 Subject: [PATCH 2/4] Bump --- .../microsoft_teams/actions/create-channel/create-channel.mjs | 2 +- .../microsoft_teams/actions/list-channels/list-channels.mjs | 2 +- components/microsoft_teams/actions/list-shifts/list-shifts.mjs | 2 +- .../actions/send-channel-message/send-channel-message.mjs | 2 +- .../actions/send-chat-message/send-chat-message.mjs | 2 +- components/microsoft_teams/package.json | 2 +- .../sources/new-channel-message/new-channel-message.mjs | 2 +- components/microsoft_teams/sources/new-channel/new-channel.mjs | 2 +- .../sources/new-chat-message/new-chat-message.mjs | 2 +- components/microsoft_teams/sources/new-chat/new-chat.mjs | 2 +- .../microsoft_teams/sources/new-team-member/new-team-member.mjs | 2 +- components/microsoft_teams/sources/new-team/new-team.mjs | 2 +- 12 files changed, 12 insertions(+), 12 deletions(-) diff --git a/components/microsoft_teams/actions/create-channel/create-channel.mjs b/components/microsoft_teams/actions/create-channel/create-channel.mjs index 55ecb53156e7f..76708df037b2f 100644 --- a/components/microsoft_teams/actions/create-channel/create-channel.mjs +++ b/components/microsoft_teams/actions/create-channel/create-channel.mjs @@ -5,7 +5,7 @@ export default { name: "Create Channel", description: "Create a new channel in Microsoft Teams. [See the docs here](https://docs.microsoft.com/en-us/graph/api/channel-post?view=graph-rest-1.0&tabs=http)", type: "action", - version: "0.0.6", + version: "0.0.7", props: { microsoftTeams, teamId: { diff --git a/components/microsoft_teams/actions/list-channels/list-channels.mjs b/components/microsoft_teams/actions/list-channels/list-channels.mjs index f4a7288a7a8d8..1752d87e0f694 100644 --- a/components/microsoft_teams/actions/list-channels/list-channels.mjs +++ b/components/microsoft_teams/actions/list-channels/list-channels.mjs @@ -5,7 +5,7 @@ export default { name: "List Channels", description: "Lists all channels in a Microsoft Team. [See the docs here](https://docs.microsoft.com/en-us/graph/api/channel-list?view=graph-rest-1.0&tabs=http)", type: "action", - version: "0.0.6", + version: "0.0.7", props: { microsoftTeams, teamId: { diff --git a/components/microsoft_teams/actions/list-shifts/list-shifts.mjs b/components/microsoft_teams/actions/list-shifts/list-shifts.mjs index f06125f04c523..cd679b51661d9 100644 --- a/components/microsoft_teams/actions/list-shifts/list-shifts.mjs +++ b/components/microsoft_teams/actions/list-shifts/list-shifts.mjs @@ -5,7 +5,7 @@ export default { name: "List Shifts", description: "Get the list of shift instances for a team. [See the documentation](https://learn.microsoft.com/en-us/graph/api/schedule-list-shifts?view=graph-rest-1.0&tabs=http)", type: "action", - version: "0.0.3", + version: "0.0.4", props: { microsoftTeams, teamId: { diff --git a/components/microsoft_teams/actions/send-channel-message/send-channel-message.mjs b/components/microsoft_teams/actions/send-channel-message/send-channel-message.mjs index 106170e461981..a5b4adc642d09 100644 --- a/components/microsoft_teams/actions/send-channel-message/send-channel-message.mjs +++ b/components/microsoft_teams/actions/send-channel-message/send-channel-message.mjs @@ -5,7 +5,7 @@ export default { name: "Send Channel Message", description: "Send a message to a team's channel. [See the docs here](https://docs.microsoft.com/en-us/graph/api/channel-post-messages?view=graph-rest-1.0&tabs=http)", type: "action", - version: "0.0.6", + version: "0.0.7", props: { microsoftTeams, teamId: { diff --git a/components/microsoft_teams/actions/send-chat-message/send-chat-message.mjs b/components/microsoft_teams/actions/send-chat-message/send-chat-message.mjs index 16da34eb98409..f02834e2b3b23 100644 --- a/components/microsoft_teams/actions/send-chat-message/send-chat-message.mjs +++ b/components/microsoft_teams/actions/send-chat-message/send-chat-message.mjs @@ -5,7 +5,7 @@ export default { name: "Send Chat Message", description: "Send a message to a team's chat. [See the docs here](https://docs.microsoft.com/en-us/graph/api/chat-post-messages?view=graph-rest-1.0&tabs=http)", type: "action", - version: "0.0.6", + version: "0.0.7", props: { microsoftTeams, chatId: { diff --git a/components/microsoft_teams/package.json b/components/microsoft_teams/package.json index 51e905d1a275e..0c13f4b2a7c0d 100644 --- a/components/microsoft_teams/package.json +++ b/components/microsoft_teams/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/microsoft_teams", - "version": "0.1.2", + "version": "0.1.3", "description": "Pipedream Microsoft Teams Components", "main": "microsoft_teams.app.mjs", "keywords": [ diff --git a/components/microsoft_teams/sources/new-channel-message/new-channel-message.mjs b/components/microsoft_teams/sources/new-channel-message/new-channel-message.mjs index 752a6423d5cd4..7d029a50f7f44 100644 --- a/components/microsoft_teams/sources/new-channel-message/new-channel-message.mjs +++ b/components/microsoft_teams/sources/new-channel-message/new-channel-message.mjs @@ -5,7 +5,7 @@ export default { key: "microsoft_teams-new-channel-message", name: "New Channel Message", description: "Emit new event when a new message is posted in a channel", - version: "0.0.7", + version: "0.0.8", type: "source", dedupe: "unique", props: { diff --git a/components/microsoft_teams/sources/new-channel/new-channel.mjs b/components/microsoft_teams/sources/new-channel/new-channel.mjs index 1096563781827..b3824a0246cc3 100644 --- a/components/microsoft_teams/sources/new-channel/new-channel.mjs +++ b/components/microsoft_teams/sources/new-channel/new-channel.mjs @@ -5,7 +5,7 @@ export default { key: "microsoft_teams-new-channel", name: "New Channel", description: "Emit new event when a new channel is created within a team", - version: "0.0.7", + version: "0.0.8", type: "source", dedupe: "unique", props: { diff --git a/components/microsoft_teams/sources/new-chat-message/new-chat-message.mjs b/components/microsoft_teams/sources/new-chat-message/new-chat-message.mjs index 71550d2308482..2beb9bb0bc6b2 100644 --- a/components/microsoft_teams/sources/new-chat-message/new-chat-message.mjs +++ b/components/microsoft_teams/sources/new-chat-message/new-chat-message.mjs @@ -5,7 +5,7 @@ export default { key: "microsoft_teams-new-chat-message", name: "New Chat Message", description: "Emit new event when a new message is received in a chat", - version: "0.0.7", + version: "0.0.8", type: "source", dedupe: "unique", props: { diff --git a/components/microsoft_teams/sources/new-chat/new-chat.mjs b/components/microsoft_teams/sources/new-chat/new-chat.mjs index 1fbe24853ad49..4ca96716efe61 100644 --- a/components/microsoft_teams/sources/new-chat/new-chat.mjs +++ b/components/microsoft_teams/sources/new-chat/new-chat.mjs @@ -5,7 +5,7 @@ export default { key: "microsoft_teams-new-chat", name: "New Chat", description: "Emit new event when a new chat is created", - version: "0.0.7", + version: "0.0.8", type: "source", dedupe: "unique", methods: { diff --git a/components/microsoft_teams/sources/new-team-member/new-team-member.mjs b/components/microsoft_teams/sources/new-team-member/new-team-member.mjs index 22b4ba4373165..bb673fbf496db 100644 --- a/components/microsoft_teams/sources/new-team-member/new-team-member.mjs +++ b/components/microsoft_teams/sources/new-team-member/new-team-member.mjs @@ -5,7 +5,7 @@ export default { key: "microsoft_teams-new-team-member", name: "New Team Member", description: "Emit new event when a new member is added to a team", - version: "0.0.7", + version: "0.0.8", type: "source", dedupe: "unique", props: { diff --git a/components/microsoft_teams/sources/new-team/new-team.mjs b/components/microsoft_teams/sources/new-team/new-team.mjs index 1a36128881d5a..92b48dcceda90 100644 --- a/components/microsoft_teams/sources/new-team/new-team.mjs +++ b/components/microsoft_teams/sources/new-team/new-team.mjs @@ -5,7 +5,7 @@ export default { key: "microsoft_teams-new-team", name: "New Team", description: "Emit new event when a new team is joined by the authenticated user", - version: "0.0.7", + version: "0.0.8", type: "source", dedupe: "unique", methods: { From a0dc9a4f689fbfcd18d88b115b9607e378b2e6a4 Mon Sep 17 00:00:00 2001 From: Michael Lim Date: Thu, 21 Nov 2024 13:50:42 -0800 Subject: [PATCH 3/4] Committed CodeRabbit suggestions for error handling and caching for performance. --- .../microsoft_teams/microsoft_teams.app.mjs | 60 +++++++++++++------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/components/microsoft_teams/microsoft_teams.app.mjs b/components/microsoft_teams/microsoft_teams.app.mjs index bcf3cee50a6cc..87c5550754591 100644 --- a/components/microsoft_teams/microsoft_teams.app.mjs +++ b/components/microsoft_teams/microsoft_teams.app.mjs @@ -69,30 +69,44 @@ export default { const myTenantId = await this.getAuthenticatedUserTenant(); const options = []; + this._userCache = this._userCache || new Map(); + for (const chat of response.value) { const messages = await this.makeRequest({ path: `/chats/${chat.id}/messages?$top=50`, }); const members = await Promise.all(chat.members.map(async (member) => { - let displayName = member.displayName; - - if (!displayName && messages.value.length > 0) { - const userMessage = messages.value.find((msg) => - msg.from?.user?.id === member.userId); - if (userMessage?.from?.user?.displayName) { - displayName = userMessage.from.user.displayName; - } - } + const cacheKey = `user_${member.userId}`; + let displayName = member.displayName || this._userCache.get(cacheKey); if (!displayName) { try { - const userDetails = await this.makeRequest({ - path: `/users/${member.userId}`, - }); - displayName = userDetails.displayName; + if (messages?.value?.length > 0) { + const userMessage = messages.value.find((msg) => + msg.from?.user?.id === member.userId); + if (userMessage?.from?.user?.displayName) { + displayName = userMessage.from.user.displayName; + } + } + + if (!displayName) { + const userDetails = await this.makeRequest({ + path: `/users/${member.userId}`, + }); + displayName = userDetails.displayName; + } + + this._userCache.set(cacheKey, displayName); } catch (err) { - displayName = "Unknown User"; + if (err.statusCode === 404) { + displayName = "User Not Found"; + } else if (err.statusCode === 403) { + displayName = "Access Denied"; + } else { + displayName = "Unknown User"; + } + console.error(`Failed to fetch user details for ${member.userId}:`, err); } } @@ -179,10 +193,20 @@ export default { }, api); }, async getAuthenticatedUserTenant() { - const { value } = await this.client() - .api("/organization") - .get(); - return value[0].id; + try { + const { value } = await this.client() + .api("/organization") + .get(); + + if (!value || value.length === 0) { + throw new Error("No organization found"); + } + + return value[0].id; + } catch (error) { + console.error("Failed to fetch tenant ID:", error); + throw new Error("Unable to determine tenant ID"); + } }, async authenticatedUserId() { const { id } = await this.client() From bf6fd9147fee72e212bf03e36b314b57ec2e113c Mon Sep 17 00:00:00 2001 From: Michael Lim Date: Fri, 22 Nov 2024 10:30:32 -0800 Subject: [PATCH 4/4] Adding docs link and original description. --- components/microsoft_teams/microsoft_teams.app.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/microsoft_teams/microsoft_teams.app.mjs b/components/microsoft_teams/microsoft_teams.app.mjs index 87c5550754591..d343128d39e5c 100644 --- a/components/microsoft_teams/microsoft_teams.app.mjs +++ b/components/microsoft_teams/microsoft_teams.app.mjs @@ -5,7 +5,7 @@ import constants from "./common/constants.mjs"; export default { type: "app", app: "microsoft_teams", - description: "Connect and interact with Microsoft Teams, supporting both internal and external communications.", + description: "**Personal accounts are not currently supported by Microsoft Teams.** Refer to Microsoft's documentation [here](https://learn.microsoft.com/en-us/graph/permissions-reference#remarks-7) to learn more.", propDefinitions: { team: { type: "string",