Skip to content

Commit

Permalink
Merge pull request #168 from GetStream/connectUser-call-signature
Browse files Browse the repository at this point in the history
feat: Extend connectUser call signature #167
  • Loading branch information
szuperaz authored Jan 7, 2022
2 parents 8dbb678 + 991f21c commit f2e22bc
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docusaurus/docs/Angular/services/chat-client.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The [StreamChat client](https://github.com/GetStream/stream-chat-js/blob/master/

## init

Creates a [`StreamChat`](https://github.com/GetStream/stream-chat-js/blob/668b3e5521339f4e14fc657834531b4c8bf8176b/src/client.ts#L124) instance using the provided `apiKey`, and connects a user with the given `userId` and `userToken`. More info about [connecting users](https://getstream.io/chat/docs/javascript/init_and_users/?language=javascript) can be found in the platform documentation.
Creates a [`StreamChat`](https://github.com/GetStream/stream-chat-js/blob/668b3e5521339f4e14fc657834531b4c8bf8176b/src/client.ts#L124) instance using the provided `apiKey`, and connects a user with the given meta data and token. More info about [connecting users](https://getstream.io/chat/docs/javascript/init_and_users/?language=javascript) can be found in the platform documentation.

## notification$

Expand Down
21 changes: 20 additions & 1 deletion projects/stream-chat-angular/src/lib/chat-client.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TestBed } from '@angular/core/testing';
import { Event, StreamChat } from 'stream-chat';
import { Event, OwnUserResponse, StreamChat } from 'stream-chat';
import { version } from '../assets/version';
import { ChatClientService } from './chat-client.service';
import { mockStreamChatClient, MockStreamChatClient } from './mocks';
Expand Down Expand Up @@ -32,6 +32,25 @@ describe('ChatClientService', () => {
expect(spy).toHaveBeenCalledWith(undefined);
});

it('should init with user meta data', async () => {
const user = { id: userId, name: 'Test user' } as OwnUserResponse;
mockChatClient.connectUser.calls.reset();
await service.init(apiKey, user, userToken);

expect(mockChatClient.connectUser).toHaveBeenCalledWith(user, userToken);
});

it('should init with token provider', async () => {
const tokenProvider = () => Promise.resolve('test');
mockChatClient.connectUser.calls.reset();
await service.init(apiKey, userId, tokenProvider);

expect(mockChatClient.connectUser).toHaveBeenCalledWith(
{ id: userId },
tokenProvider
);
});

it('should emit app settings, if app settings not yet loaded', async () => {
const spy = jasmine.createSpy();
service.appSettings$.subscribe(spy);
Expand Down
12 changes: 9 additions & 3 deletions projects/stream-chat-angular/src/lib/chat-client.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, NgZone } from '@angular/core';
import { BehaviorSubject, Observable, ReplaySubject } from 'rxjs';
import { AppSettings, Event, StreamChat } from 'stream-chat';
import { OwnUserResponse } from 'stream-chat';
import { AppSettings, Event, StreamChat, TokenOrProvider } from 'stream-chat';
import { version } from '../assets/version';
import { NotificationService } from './notification.service';

Expand Down Expand Up @@ -32,10 +33,15 @@ export class ChatClientService {
this.appSettings$ = this.appSettingsSubject.asObservable();
}

async init(apiKey: string, userId: string, userToken: string) {
async init(
apiKey: string,
userOrId: string | OwnUserResponse,
userTokenOrProvider: TokenOrProvider
) {
this.chatClient = StreamChat.getInstance(apiKey);
await this.ngZone.runOutsideAngular(async () => {
await this.chatClient.connectUser({ id: userId }, userToken);
const user = typeof userOrId === 'string' ? { id: userOrId } : userOrId;
await this.chatClient.connectUser(user, userTokenOrProvider);
this.chatClient.setUserAgent(
`stream-chat-angular-${version}-${this.chatClient.getUserAgent()}`
);
Expand Down

0 comments on commit f2e22bc

Please sign in to comment.