Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions packages/chat-stateful-client/src/ChatContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,15 @@ export class ChatContext {
private _fullsizeImageQueue: ResourceDownloadQueue | undefined = undefined;
constructor(
maxListeners?: number,
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ credential?: CommunicationTokenCredential
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ credential?: CommunicationTokenCredential,
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ endpoint?: string
) {
this._logger = createClientLogger('communication-react:chat-context');
this._emitter = new EventEmitter();
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */
if (credential) {
this._inlineImageQueue = new ResourceDownloadQueue(this, credential);
this._fullsizeImageQueue = new ResourceDownloadQueue(this, credential);
this._inlineImageQueue = new ResourceDownloadQueue(this, { credential, endpoint: endpoint ?? '' });
this._fullsizeImageQueue = new ResourceDownloadQueue(this, { credential, endpoint: endpoint ?? '' });
Comment thread
jpeng-ms marked this conversation as resolved.
}
if (maxListeners) {
this._emitter.setMaxListeners(maxListeners);
Expand Down
23 changes: 17 additions & 6 deletions packages/chat-stateful-client/src/ResourceDownloadQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ export class ResourceDownloadQueue {
private _context: ChatContext;
private isActive = false;
private _credential: CommunicationTokenCredential;
private _endpoint: string;
private _requestsToCancel: Record<string, CancellationDetails> = {};

constructor(context: ChatContext, credential: CommunicationTokenCredential) {
constructor(context: ChatContext, authentication: { credential: CommunicationTokenCredential; endpoint: string }) {
this._context = context;
this._credential = credential;
this._credential = authentication.credential;
this._endpoint = authentication.endpoint;
}

public containsMessageWithSameAttachments(message: ChatMessageWithStatus): boolean {
Expand Down Expand Up @@ -138,7 +140,11 @@ export class ResourceDownloadQueue {
abortController: AbortController
): Promise<string> {
this._requestsToCancel[url] = { src: url, abortController };
const blobUrl = await operation(url, this._credential, { abortController });
const blobUrl = await operation(
url,
{ credential: this._credential, endpoint: this._endpoint },
{ abortController }
);
delete this._requestsToCancel[url];
return blobUrl;
}
Expand All @@ -150,7 +156,7 @@ export class ResourceDownloadQueue {
*/
export const fetchImageSource = async (
src: string,
credential: CommunicationTokenCredential,
authentication: { credential: CommunicationTokenCredential; endpoint: string },
options: { abortController: AbortController; timeout?: number }
): Promise<string> => {
async function fetchWithAuthentication(
Expand Down Expand Up @@ -188,7 +194,12 @@ export const fetchImageSource = async (
clearTimeout(id);
return response;
}
const accessToken = await credential.getToken();

const accessToken = await authentication.credential.getToken();
if (!src.startsWith(authentication.endpoint)) {
Comment thread
emlynmac marked this conversation as resolved.
Outdated
throw new ChatError('ChatThreadClient.getMessage', new Error('Invalid endpoint'));
}

const response = await fetchWithAuthentication(src, accessToken.token, options);
const blob = await response.blob();

Expand All @@ -198,7 +209,7 @@ export const fetchImageSource = async (
interface ImageRequest {
(
request: string,
credential: CommunicationTokenCredential,
authentication: { credential: CommunicationTokenCredential; endpoint: string },
options: { abortController: AbortController; timeout?: number }
): Promise<string>;
}
3 changes: 2 additions & 1 deletion packages/chat-stateful-client/src/StatefulChatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ export const _createStatefulChatClientWithDeps = (
): StatefulChatClient => {
const context = new ChatContext(
options?.maxStateChangeListeners,
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ args.credential
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ args.credential,
/* @conditional-compile-remove(teams-inline-images-and-file-sharing) */ args.endpoint
);

let eventSubscriber: EventSubscriber;
Expand Down