Skip to content

Commit

Permalink
added PeerDisplayName
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexErrant committed Aug 12, 2023
1 parent b4fbcb2 commit 0a46e28
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 14 deletions.
23 changes: 19 additions & 4 deletions app/src/pages/peers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import {
import { getCrRtc } from "../sqlite/crsqlite"
import { stringify as uuidStringify } from "uuid"
import { cwaClient, isTrpcClientError } from "../trpcClient"
import { peerValidator } from "shared"
import {
type PeerJsId,
peerValidator,
peerDisplayNameValidator,
peerIdValidator,
} from "shared"

export default function Peers() {
const [pending, setPending] = createSignal<string[]>([])
Expand All @@ -22,7 +27,7 @@ export default function Peers() {
setPending(pending)
setEstablished(established)
})
return uuidStringify(rtc.siteId)
return peerIdValidator.parse(uuidStringify(rtc.siteId))
} catch (error) {
if (isTrpcClientError(error) && error.data?.httpStatus === 401) {
return null
Expand All @@ -42,18 +47,28 @@ export default function Peers() {
}

const RenderPeerControls: VoidComponent<{
siteId: string
siteId: PeerJsId
pending: string[]
established: string[]
}> = (props) => {
const [name, setName] = createSignal("")
return (
<>
{peers()}
<input
class="w-75px p-1 bg-white text-sm rounded-lg"
type="text"
onInput={(e) => setName(e.currentTarget.value)}
/>
<button
type="button"
class="border rounded-lg px-2 border-gray-900"
onClick={async () => {
await cwaClient.setPeer.mutate(props.siteId)
const displayName = peerDisplayNameValidator.parse(name())
await cwaClient.setPeer.mutate({
peerId: props.siteId,
displayName,
})
}}
>
Add {props.siteId} as peer
Expand Down
11 changes: 8 additions & 3 deletions cwa/src/userRouter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type PeerJsId } from "shared"
import { peerDisplayNameValidator, peerIdValidator } from "shared"
import { z } from "zod"
import { authedProcedure, publicProcedure } from "./trpc"
import { getUserPeer, setUserPeer } from "shared-edge"
Expand All @@ -9,9 +9,14 @@ export const userRouter = {
async ({ ctx }) => await getUserPeer(ctx.user)
),
setPeer: authedProcedure
.input(z.string().uuid())
.input(
z.object({
peerId: peerIdValidator,
displayName: peerDisplayNameValidator,
})
)
.mutation(async ({ input, ctx }) => {
await setUserPeer(ctx.user, input as PeerJsId)
await setUserPeer(ctx.user, input.peerId, input.displayName)
}),
getPeerSyncToken: authedProcedure.query(
async ({ ctx }) => await getPeerToken(ctx.user, ctx.env.peerSyncPrivateKey)
Expand Down
7 changes: 4 additions & 3 deletions shared-edge/src/kysely.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,9 @@ export function dbIdToBase64Url(dbId: DbId): Base64Url {
}

// use with .$call(log)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
function log<T extends Compilable>(qb: T): T {
console.log("Compiled SQL: ", qb.compile())
export function log<T extends Compilable>(qb: T): T {
console.log("Query : ", qb.compile().query)
console.log("SQL : ", qb.compile().sql)
console.log("Params: ", qb.compile().parameters)
return qb
}
10 changes: 7 additions & 3 deletions shared-edge/src/user.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
import { sql } from "kysely"
import { type UserId, type PeerJsId } from "shared"
import { type UserId, type PeerJsId, type PeerDisplayName } from "shared"
import { db } from "./kysely"

export async function setUserPeer(userId: UserId, peerId: PeerJsId) {
export async function setUserPeer(
userId: UserId,
peerId: PeerJsId,
peerDisplayName: PeerDisplayName
) {
await db
.updateTable("user")
.set({
peer: (x) =>
sql`JSON_SET(${sql`IFNULL(${x.ref(
"peer"
)},'{}')`}, ${`$."${peerId}"`}, "")`,
)},'{}')`}, ${`$."${peerId}"`}, ${`${peerDisplayName}`})`,
})
.where("id", "=", userId)
.execute()
Expand Down
1 change: 1 addition & 0 deletions shared/src/brand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,4 @@ export type DeckId = Brand<string, "deckId">
export type RemoteMediaNum = Brand<number, "remoteMediaNum">

export type PeerJsId = Brand<string, "peerJsId"> // in uuid format
export type PeerDisplayName = Brand<string, "peerDisplayName">
11 changes: 10 additions & 1 deletion shared/src/domain/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { z } from "zod"
import { type PeerDisplayName, type PeerJsId } from "../brand"

export const peerValidator = z.record(z.string().uuid(), z.string().max(0))
export const peerDisplayNameValidator = z
.string()
.max(20) as unknown as z.Schema<PeerDisplayName>

export const peerIdValidator = z
.string()
.uuid() as unknown as z.Schema<PeerJsId>

export const peerValidator = z.record(peerIdValidator, peerDisplayNameValidator)

export type PeerValidator = z.infer<typeof peerValidator>

0 comments on commit 0a46e28

Please sign in to comment.