Skip to content

Commit

Permalink
refactor: Move component interfaces to root types file
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinszuchet committed Jan 20, 2025
1 parent 04e27d1 commit 4d727f8
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 13 deletions.
8 changes: 1 addition & 7 deletions src/adapters/pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
import { IBaseComponent } from '@well-known-components/interfaces'
import { AppComponents, SubscriptionEventsEmitter } from '../types'
import { AppComponents, IPubSubComponent } from '../types'

const FRIENDSHIP_UPDATES_CHANNEL = 'FRIENDSHIP_UPDATES'

export type IPubSubComponent = IBaseComponent & {
subscribeToFriendshipUpdates(cb: (message: string) => void): Promise<void>
publishFriendshipUpdate(update: SubscriptionEventsEmitter['update']): Promise<void>
}

export default function createPubSubComponent(components: Pick<AppComponents, 'logs' | 'redis'>): IPubSubComponent {
const { logs, redis } = components
const logger = logs.getLogger('pubsub-component')
Expand Down
22 changes: 21 additions & 1 deletion src/logic/friendships.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import {
FriendshipUpdate,
UpsertFriendshipPayload,
FriendshipStatus as FriendshipRequestStatus
FriendshipStatus as FriendshipRequestStatus,
ConnectivityStatus
} from '@dcl/protocol/out-js/decentraland/social_service/v3/social_service_v3.gen'
import {
Action,
Friend,
FRIENDSHIP_ACTION_TRANSITIONS,
FriendshipAction,
FriendshipStatus,
Expand Down Expand Up @@ -195,3 +197,21 @@ export function getFriendshipRequestStatus(
const statusResolver = FRIENDSHIP_STATUS_BY_ACTION[action]
return statusResolver?.(acting_user, loggedUserAddress) ?? FriendshipRequestStatus.UNRECOGNIZED
}

const filtersByConnectivityStatus = {
[ConnectivityStatus.ONLINE]: (friend: Friend, connectedPeers: Record<string, boolean>) =>
connectedPeers[friend.address],
[ConnectivityStatus.OFFLINE]: (friend: Friend, connectedPeers: Record<string, boolean>) =>
!connectedPeers[friend.address],
[ConnectivityStatus.AWAY]: (friend: Friend, connectedPeers: Record<string, boolean>) =>
!connectedPeers[friend.address],
[ConnectivityStatus.UNRECOGNIZED]: () => true
}

export function byConnectivityStatus(status: ConnectivityStatus, connectedPeers: Record<string, boolean>) {
const filter = filtersByConnectivityStatus[status]

if (!filter) () => true

return (friend: Friend) => filter(friend, connectedPeers)
}
2 changes: 1 addition & 1 deletion test/mocks/components/db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IDatabaseComponent } from '../../../src/adapters/db'
import { IDatabaseComponent } from '../../../src/types'

export const mockDb: jest.Mocked<IDatabaseComponent> = {
createFriendship: jest.fn(),
Expand Down
2 changes: 2 additions & 0 deletions test/mocks/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export * from './pubsub'
export * from './pg'
export * from './config'
export * from './uws'
export * from './redis'
export * from './archipelago-stats'
2 changes: 0 additions & 2 deletions test/mocks/components/logs.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { IMetricsComponent } from '@well-known-components/interfaces'
import { ILoggerComponent } from '@well-known-components/interfaces/dist/components/logger'
import { createLogComponent } from '@well-known-components/logger'

export const mockLogs: jest.Mocked<ILoggerComponent> = {
getLogger: jest.fn().mockReturnValue({
Expand Down
1 change: 0 additions & 1 deletion test/mocks/components/pg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { IPgComponent } from '@well-known-components/pg-component'
import { release } from 'os'

export const mockPg: jest.Mocked<IPgComponent> = {
streamQuery: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion test/mocks/components/pubsub.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IPubSubComponent } from '../../../src/adapters/pubsub'
import { IPubSubComponent } from '../../../src/types'

export const mockPubSub: jest.Mocked<IPubSubComponent> = {
start: jest.fn(),
Expand Down

0 comments on commit 4d727f8

Please sign in to comment.