Skip to content
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

(feat) Listen for authorization expiry message #7454

Draft
wants to merge 6 commits into
base: next
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@ type RollCallEvent = {
session: string
}

type IncomingBifurEvent<T> = RollCallEvent | BifurStateMessage | BifurDisconnectMessage
type ExpireEvent = {
type: 'expires'
event: 'expires'
at: string
}

type IncomingBifurEvent<T> =
| RollCallEvent
| BifurStateMessage
| BifurDisconnectMessage
| ExpireEvent

const handleIncomingMessage = (event: IncomingBifurEvent<Location[]>): TransportEvent => {
// console.log('ws incoming event', event)
if (event.type === 'rollCall') {
return {
type: 'rollCall',
Expand Down Expand Up @@ -56,14 +67,34 @@ const handleIncomingMessage = (event: IncomingBifurEvent<Location[]>): Transport
}
}

if (event.event === 'expires') {
return {
type: 'authorizationExpire',
expiresAt: event.at,
timestamp: new Date().toISOString(),
}
}

throw new Error(`Got unknown presence event: ${JSON.stringify(event)}`)
}

export const createBifurTransport = (bifur: BifurClient, sessionId: string): Transport => {
const incomingEvents$: Observable<TransportEvent> = bifur
const incomingPresenceEvents$: Observable<TransportEvent> = bifur
.listen<IncomingBifurEvent<Location[]>>('presence')
.pipe(map(handleIncomingMessage))

const incomingAuthorizationEvents$: Observable<TransportEvent> = bifur
.listen<IncomingBifurEvent<Location[]>>('authorization')
.pipe(map(handleIncomingMessage))

// const testIncomingAuthorizationEvents$: Observable<TransportEvent> = bifur
// .listen<IncomingBifurEvent<Location[]>>('authorization')
// .pipe(map(handleIncomingMessage))
// .subscribe((event) => {
// // eslint-disable-next-line no-console
// console.log('[++++++]Authorization event', event)
// })

const dispatchMessage = (message: TransportMessage): Observable<undefined> => {
if (message.type === 'rollCall') {
return bifur.request('presence_rollcall', {session: sessionId})
Expand All @@ -82,5 +113,9 @@ export const createBifurTransport = (bifur: BifurClient, sessionId: string): Tra
return EMPTY
}

return [incomingEvents$.pipe(share()), dispatchMessage]
return [
incomingPresenceEvents$.pipe(share()),
incomingAuthorizationEvents$.pipe(share()),
dispatchMessage,
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,25 @@ export interface RollCallEvent extends RollCallMessage {
userId: string
}

export type TransportMessage = DisconnectMessage | StateMessage | RollCallMessage
export type TransportEvent = StateEvent | RollCallEvent | DisconnectEvent
export interface AuthorizationExpireMessage {
type: 'authorizationExpire'
}

export interface AuthorizationExpireEvent extends AuthorizationExpireMessage {
expiresAt: string
timestamp: string
}

export type TransportMessage =
| DisconnectMessage
| StateMessage
| RollCallMessage
| AuthorizationExpireMessage
export type TransportEvent = StateEvent | RollCallEvent | DisconnectEvent | AuthorizationExpireEvent

// This is the interface a transport must implement
export type Transport = [
Observable<TransportEvent>,
Observable<TransportEvent>,
(message: TransportMessage) => Observable<void>,
]
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,14 @@ export function __tmp_wrap_presenceStore(context: {
}): PresenceStore {
const {bifur, connectionStatusStore, userStore} = context

const [presenceEvents$, sendMessage] = createBifurTransport(bifur, SESSION_ID)
const [presenceEvents$, authorizationEvents$, sendMessage] = createBifurTransport(
bifur,
SESSION_ID,
)

authorizationEvents$.subscribe((event) => {
// console.log('[++++++]Authorization event', event)
})

const currentLocation$ = new BehaviorSubject<PresenceLocation[]>([])
const locationChange$ = currentLocation$.pipe(distinctUntilChanged())
Expand All @@ -143,6 +150,12 @@ export function __tmp_wrap_presenceStore(context: {
filter((event: RollCallEvent) => event.sessionId !== SESSION_ID),
)

// const authExpireNotifications$ = authorizationEvents$.pipe(
// filter((event: TransportEvent): event is RollCallEvent => event === 'rollCall'),
// // do not respond to my own rollcall requests
// filter((event: RollCallEvent) => event.sessionId !== SESSION_ID),
// )

const REPORT_MIN_INTERVAL = 30000

// Interval to report my own location at
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading