- 
                Notifications
    You must be signed in to change notification settings 
- Fork 5.5k
Updating Chat prop to include support for external users. #14711
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
          
     Merged
      
      
    
  
     Merged
                    Changes from 3 commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      5a3adea
              
                Updating Chat prop to include support for external users.
              
              
                malexanderlim 2c5e1c3
              
                Bump
              
              
                malexanderlim a0dc9a4
              
                Committed CodeRabbit suggestions for error handling and caching for p…
              
              
                malexanderlim bf6fd91
              
                Adding docs link and original description.
              
              
                malexanderlim File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -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,64 @@ 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 = []; | ||
|  | ||
| this._userCache = this._userCache || new Map(); | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance cache implementation. The current cache implementation could be improved: 
 Consider implementing a more robust caching solution: +    _initCache() {
+      if (!this._userCache) {
+        this._userCache = new Map();
+        this._cacheExpiry = new Map();
+        this._maxCacheSize = 1000;
+        this._cacheTimeout = 3600000; // 1 hour
+      }
+    },
+    _getCachedUser(userId) {
+      this._initCache();
+      const now = Date.now();
+      if (this._cacheExpiry.get(userId) < now) {
+        this._userCache.delete(userId);
+        this._cacheExpiry.delete(userId);
+        return null;
+      }
+      return this._userCache.get(userId);
+    },
+    _setCachedUser(userId, displayName) {
+      this._initCache();
+      if (this._userCache.size >= this._maxCacheSize) {
+        const oldestKey = this._cacheExpiry.keys().next().value;
+        this._userCache.delete(oldestKey);
+        this._cacheExpiry.delete(oldestKey);
+      }
+      this._userCache.set(userId, displayName);
+      this._cacheExpiry.set(userId, Date.now() + this._cacheTimeout);
+    },Also applies to: 80-81 | ||
|  | ||
| 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) => { | ||
| const cacheKey = `user_${member.userId}`; | ||
| let displayName = member.displayName || this._userCache.get(cacheKey); | ||
|  | ||
| if (!displayName) { | ||
| try { | ||
| 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) { | ||
| 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); | ||
| } | ||
| } | ||
|  | ||
| const isExternal = member.tenantId !== myTenantId || !member.tenantId; | ||
| return isExternal | ||
| ? `${displayName} (External)` | ||
| : displayName; | ||
| })); | ||
|  | ||
| options.push({ | ||
| label: members.join(", "), | ||
| value: chat.id, | ||
|  | @@ -144,6 +192,22 @@ export default { | |
| : reduction; | ||
| }, api); | ||
| }, | ||
| async getAuthenticatedUserTenant() { | ||
| 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() | ||
| .api("/me") | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume removing the dcumentation link is intentional here