Skip to content

Commit

Permalink
Merge pull request #236 from xmtp/break-up-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
neekolas authored Dec 9, 2022
2 parents af0b801 + 9e632c7 commit 2645587
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/Message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
ContentTypeId,
EncodedContent,
} from './MessageContent'
import { nsToDate } from './utils'
import { nsToDate } from './utils/date'

const headerBytesAndCiphertext = (
msg: proto.Message
Expand Down
2 changes: 1 addition & 1 deletion src/authn/AuthData.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { authn as authnProto } from '@xmtp/proto'
import Long from 'long'
import { dateToNs } from '../utils'
import { dateToNs } from '../utils/date'

export default class AuthData implements authnProto.AuthData {
walletAddr: string
Expand Down
8 changes: 6 additions & 2 deletions src/conversations/Conversation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { buildUserIntroTopic } from './../utils'
import {
buildUserIntroTopic,
buildDirectMessageTopic,
dateToNs,
nsToDate,
} from '../utils'
import { DecodedMessage } from './../Message'
import Stream from '../Stream'
import Client, {
Expand All @@ -22,7 +27,6 @@ import {
} from '../crypto'
import Ciphertext from '../crypto/Ciphertext'
import { sha256 } from '../crypto/encryption'
import { buildDirectMessageTopic, dateToNs, nsToDate } from '../utils'
import { ContentTypeText } from '../codecs/Text'
const { b64Decode } = fetcher

Expand Down
2 changes: 1 addition & 1 deletion src/store/PrivateTopicStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { messageApi, fetcher } from '@xmtp/proto'
import { Store } from './Store'
import { buildUserPrivateStoreTopic } from '../utils'
import { buildUserPrivateStoreTopic } from '../utils/topic'
import ApiClient from '../ApiClient'
import { Authenticator } from '../authn'
const b64Decode = fetcher.b64Decode
Expand Down
51 changes: 0 additions & 51 deletions src/utils.ts → src/utils/async.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,7 @@
import { messageApi } from '@xmtp/proto'
import Long from 'long'

export type IsRetryable = (err?: Error) => boolean

export const buildContentTopic = (name: string): string =>
`/xmtp/0/${name}/proto`

export const buildDirectMessageTopic = (
sender: string,
recipient: string
): string => {
const members = [sender, recipient]
members.sort()
return buildContentTopic(`dm-${members.join('-')}`)
}

export const buildDirectMessageTopicV2 = (randomString: string): string => {
return buildContentTopic(`m-${randomString}`)
}

export const buildUserContactTopic = (walletAddr: string): string => {
return buildContentTopic(`contact-${walletAddr}`)
}

export const buildUserIntroTopic = (walletAddr: string): string => {
return buildContentTopic(`intro-${walletAddr}`)
}

export const buildUserInviteTopic = (walletAddr: string): string => {
return buildContentTopic(`invite-${walletAddr}`)
}
export const buildUserPrivateStoreTopic = (walletAddr: string): string => {
return buildContentTopic(`privatestore-${walletAddr}`)
}

export const sleep = (ms: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, ms))

Expand Down Expand Up @@ -106,22 +74,3 @@ export async function* mapPaginatedStream<Out>(
yield out
}
}

export function dateToNs(date: Date): Long {
return Long.fromNumber(date.valueOf()).multiply(1_000_000)
}

export function nsToDate(ns: Long): Date {
return new Date(ns.divide(1_000_000).toNumber())
}

export const toNanoString = (d: Date | undefined): undefined | string => {
return d && dateToNs(d).toString()
}

export const fromNanoString = (s: string | undefined): undefined | Date => {
if (!s) {
return undefined
}
return nsToDate(Long.fromString(s))
}
20 changes: 20 additions & 0 deletions src/utils/date.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Long from 'long'

export function dateToNs(date: Date): Long {
return Long.fromNumber(date.valueOf()).multiply(1_000_000)
}

export function nsToDate(ns: Long): Date {
return new Date(ns.divide(1_000_000).toNumber())
}

export const toNanoString = (d: Date | undefined): undefined | string => {
return d && dateToNs(d).toString()
}

export const fromNanoString = (s: string | undefined): undefined | Date => {
if (!s) {
return undefined
}
return nsToDate(Long.fromString(s))
}
3 changes: 3 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './topic'
export * from './async'
export * from './date'
30 changes: 30 additions & 0 deletions src/utils/topic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export const buildContentTopic = (name: string): string =>
`/xmtp/0/${name}/proto`

export const buildDirectMessageTopic = (
sender: string,
recipient: string
): string => {
const members = [sender, recipient]
members.sort()
return buildContentTopic(`dm-${members.join('-')}`)
}

export const buildDirectMessageTopicV2 = (randomString: string): string => {
return buildContentTopic(`m-${randomString}`)
}

export const buildUserContactTopic = (walletAddr: string): string => {
return buildContentTopic(`contact-${walletAddr}`)
}

export const buildUserIntroTopic = (walletAddr: string): string => {
return buildContentTopic(`intro-${walletAddr}`)
}

export const buildUserInviteTopic = (walletAddr: string): string => {
return buildContentTopic(`invite-${walletAddr}`)
}
export const buildUserPrivateStoreTopic = (walletAddr: string): string => {
return buildContentTopic(`privatestore-${walletAddr}`)
}

0 comments on commit 2645587

Please sign in to comment.