Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/feeds-client/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './src/feeds-client';
export * from './src/feeds-client/feeds-client';
export * from './src/feed/feed';
export * from './src/gen/models';
export * from './src/types';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './user/handle-user-updated';
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { describe, it, beforeEach, expect } from 'vitest';

import { FeedsClient, handleUserUpdated } from '../..';
import { generateOwnUser, generateUserResponse } from '../../../test-utils';
import { EventPayload } from '../../../types-internal';

describe('handleUserUpdated', () => {
let feedsClient: FeedsClient;

beforeEach(() => {
feedsClient = new FeedsClient('mock-api-key');
const connectedUser = generateOwnUser();

feedsClient.state.partialNext({ connected_user: connectedUser });
});

it('should update the connected user in the state', () => {
const stateBefore = feedsClient.state.getLatestValue();

const event: EventPayload<'user.updated'> = {
type: 'user.updated',
created_at: new Date(),
custom: {},
user: {
...generateUserResponse(),
...stateBefore.connected_user!,
},
};

handleUserUpdated.call(feedsClient, event);

const stateAfter = feedsClient.state.getLatestValue();

expect(stateAfter.connected_user).toMatchObject({ name: event.user.name });
});

it('should not update the connected user if the incoming event contains other user', () => {
const event: EventPayload<'user.updated'> = {
type: 'user.updated',
created_at: new Date(),
custom: {},
user: generateUserResponse(),
};

const stateBefore = feedsClient.state.getLatestValue();

handleUserUpdated.call(feedsClient, event);

const stateAfter = feedsClient.state.getLatestValue();

expect(stateAfter.connected_user).toBe(stateBefore.connected_user);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import type { EventPayload } from '../../../types-internal';
import type { FeedsClient, FeedsClientState } from '../../feeds-client';

export function handleUserUpdated(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nitpick

Suggested change
export function handleUserUpdated(
export handleUserUpdated = (
this: FeedsClient,
event: EventPayload<'user.updated'>,
) =>

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this acts differently in arrow-type functions, not sure whether that would work.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn’t need to bind it then, that’s the only thing that should be different I think - but I might be wrong

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's not using this format then you would not able to access protected properties of the FeedsClient (if we ever need them). I'm just repeating the pattern established for the handlers in Feed.

this: FeedsClient,
event: EventPayload<'user.updated'>,
) {
this.state.next((currentState) => {
let newState: FeedsClientState | undefined;

const { connected_user } = currentState;

if (connected_user && connected_user.id === event.user.id) {
newState ??= {
...currentState,
};

newState.connected_user = {
...connected_user,
...event.user,
};
}

// TODO: update other users in user map (if/once applicable)

return newState ?? currentState;
});
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FeedsApi } from './gen/feeds/FeedsApi';
import { FeedsApi } from '../gen/feeds/FeedsApi';
import {
ActivityResponse,
FeedResponse,
Expand All @@ -14,35 +14,36 @@ import {
UpdateFollowRequest,
UserRequest,
WSEvent,
} from './gen/models';
import { FeedsEvent, StreamFile, TokenOrProvider } from './types';
import { StateStore } from './common/StateStore';
import { TokenManager } from './common/TokenManager';
import { ConnectionIdManager } from './common/ConnectionIdManager';
import { StableWSConnection } from './common/real-time/StableWSConnection';
import { EventDispatcher } from './common/EventDispatcher';
import { ApiClient } from './common/ApiClient';
} from '../gen/models';
import { FeedsEvent, StreamFile, TokenOrProvider } from '../types';
import { StateStore } from '../common/StateStore';
import { TokenManager } from '../common/TokenManager';
import { ConnectionIdManager } from '../common/ConnectionIdManager';
import { StableWSConnection } from '../common/real-time/StableWSConnection';
import { EventDispatcher } from '../common/EventDispatcher';
import { ApiClient } from '../common/ApiClient';
import {
addConnectionEventListeners,
removeConnectionEventListeners,
streamDevToken,
} from './common/utils';
import { decodeWSEvent } from './gen/model-decoders/event-decoder-mapping';
} from '../common/utils';
import { decodeWSEvent } from '../gen/model-decoders/event-decoder-mapping';
import {
FeedsClientOptions,
NetworkChangedEvent,
StreamResponse,
} from './common/types';
import { ModerationClient } from './moderation-client';
import { StreamPoll } from './common/Poll';
} from '../common/types';
import { ModerationClient } from '../moderation-client';
import { StreamPoll } from '../common/Poll';
import {
Feed,
handleFollowCreated,
handleFollowDeleted,
handleFollowUpdated,
handleWatchStarted,
handleWatchStopped,
} from './feed';
} from '../feed';
import { handleUserUpdated } from './event-handlers';

export type FeedsClientState = {
connected_user: OwnUser | undefined;
Expand Down Expand Up @@ -196,6 +197,10 @@ export class FeedsClient extends FeedsApi {

break;
}
case 'user.updated': {
handleUserUpdated.call(this, event);
break;
}
default: {
feed?.handleWSEvent(event as unknown as WSEvent);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/feeds-client/src/feeds-client/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './feeds-client';
export * from './event-handlers';
19 changes: 9 additions & 10 deletions packages/feeds-client/src/test-utils/response-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const generateUserResponse = (
overrides: Partial<UserResponse> = {},
): UserResponse => ({
id: `user-${getHumanId()}`,
name: humanId({ separator: ' ' }),
created_at: new Date(),
updated_at: new Date(),
banned: false,
Expand Down Expand Up @@ -58,28 +59,26 @@ export const generateFeedResponse = (
const id = overrides.id || `feed-${getHumanId()}`;
const groupId = overrides.group_id || 'user';
const fid = `${groupId}:${id}`;
const createdBy = generateUserResponse(overrides.created_by);
const description = humanId({
addAdverb: true,
adjectiveCount: 4,
});
const name = humanId();

return {
id,
group_id: groupId,
created_at: new Date(),
updated_at: new Date(),
description,
fid,
description: humanId({
addAdverb: true,
adjectiveCount: 4,
separator: ' ',
}),
follower_count: 0,
following_count: 0,
member_count: 0,
name,
name: humanId({ separator: ' ' }),
pin_count: 0,
custom: {},
...overrides,
created_by: createdBy,
fid,
created_by: generateUserResponse(overrides.created_by),
};
};

Expand Down