Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
totegamma committed Feb 2, 2025
1 parent 7485330 commit 1b60905
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 79 deletions.
16 changes: 16 additions & 0 deletions client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ import {
SubKeyAuthProvider,
Socket,
SocketListener,
InMemoryKVS,
GuestAuthProvider,
} from '@concrnt/client'

import { Schemas, Schema } from "./schemas";
Expand Down Expand Up @@ -186,6 +188,20 @@ export class Client {
opts?.progressCallback?.("done")
return c
}

static async createAsGuest(host: FQDN, _opts?: ClientOptions): Promise<Client> {
const cacheEngine = new InMemoryKVS()
const authProvider = new GuestAuthProvider(host)
const api = new Api(authProvider, cacheEngine)
const c = new Client(api)

c.server = await c.api.getDomain(host).catch((e) => {
console.error('CLIENT::create::getDomain::error', e)
return null
}) ?? undefined

return c
}

async reloadUser(): Promise<void> {
if (!this.ccid) return
Expand Down
38 changes: 19 additions & 19 deletions src/components/Importer/ImportMasterkey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ export function ImportMasterKey(): JSX.Element {

const timer = setTimeout(() => {
try {
const client = new Client(searchTarget, keypair, ccid)
client.api
.getEntity(ccid, searchTarget)
.then((entity) => {
if (entity?.domain) {
setDomainInput(entity.domain)
setErrorMessage('')
} else {
setDomainAutoDetectionFailed(true)
Client.createAsGuest(searchTarget).then((client) => {
client.api
.getEntity(ccid, searchTarget)
.then((entity) => {
if (entity?.domain) {
setDomainInput(entity.domain)
setErrorMessage('')
} else {
setDomainAutoDetectionFailed(true)
setErrorMessage(t('notFound'))
}
})
.catch((_) => {
setErrorMessage(t('notFound'))
}
})
.catch((_) => {
setErrorMessage(t('notFound'))
})
})
})
} catch (e) {
console.error(e)
}
Expand All @@ -82,14 +83,13 @@ export function ImportMasterKey(): JSX.Element {
if (!domainInput || !keypair || !ccid) return
const timer = setTimeout(() => {
try {
const client = new Client(domainInput, keypair, ccid)
client.api.fetchWithCredential(domainInput, '/api/v1/entity', {}).then((res) => {
if (res.ok) {
Client.create(keypair.privatekey, domainInput).then((client) => {
client.api.fetchWithCredential(domainInput, '/api/v1/entity', {}).then((_) => {
setErrorMessage('')
setRegistrationOK(true)
} else {
}).catch((_) => {
setRegistrationOK(false)
}
})
})
} catch (e) {
console.error(e)
Expand Down
2 changes: 1 addition & 1 deletion src/components/Importer/ImportSubkey.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useTranslation } from 'react-i18next'
import Visibility from '@mui/icons-material/Visibility'
import VisibilityOff from '@mui/icons-material/VisibilityOff'
import CheckCircleIcon from '@mui/icons-material/CheckCircle'
import { Client } from '@concurrent-world/client'
import { Client } from 'client'

export const ImportSubkey = (): JSX.Element => {
const { t } = useTranslation('', { keyPrefix: 'import' })
Expand Down
2 changes: 1 addition & 1 deletion src/components/SubprofileSelector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const SubprofileSelector = (props: SubprofileSelectorProps): JSX.Element
}}
>
<ListItemIcon>
<Avatar alt={p.document.body.username} src={p.document.body.avatar} variant="square" />
<Avatar alt={p.parsedDoc.body.username} src={p.parsedDoc.body.avatar} variant="square" />
</ListItemIcon>
</MenuItem>
)
Expand Down
2 changes: 1 addition & 1 deletion src/context/StorageContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export const StorageProvider = ({ children }: { children: JSX.Element | JSX.Elem
const xhr = new XMLHttpRequest()
xhr.open('POST', `https://${client.host}/storage/files`, true)
xhr.setRequestHeader('Content-Type', file.type)
xhr.setRequestHeader('Authorization', `Bearer ${client.api.generateApiToken(client.host)}`)
xhr.setRequestHeader('Authorization', `Bearer ${client.api.authProvider.getAuthToken(client.host)}`)

xhr.upload.onprogress = (e) => {
if (e.lengthComputable) {
Expand Down
43 changes: 22 additions & 21 deletions src/pages/GuestMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,32 +44,33 @@ export default function GuestMessagePage(): JSX.Element {
const [client, initializeClient] = useState<Client>()
useEffect(() => {
if (!authorID || !messageID) return
const client = new Client('ariake.concrnt.net')
initializeClient(client)

let isMounted = true
client
.getMessage<any>(messageID, authorID)
.then((msg) => {
if (!isMounted || !msg) return
setMessage(msg)
Client.createAsGuest('ariake.concrnt.net').then((client) => {
initializeClient(client)
client
.getMessage<any>(messageID, authorID)
.then((msg) => {
if (!isMounted || !msg) return
setMessage(msg)

msg.getReplyMessages().then((replies) => {
if (!isMounted) return
setReplies(replies)
})

if (msg.schema === Schemas.replyMessage) {
msg.getReplyTo().then((replyTo) => {
msg.getReplyMessages().then((replies) => {
if (!isMounted) return
setReplyTo(replyTo)
setReplies(replies)
})
}
})
.finally(() => {
if (!isMounted) return
setIsFetching(false)
})

if (msg.schema === Schemas.replyMessage) {
msg.getReplyTo().then((replyTo) => {
if (!isMounted) return
setReplyTo(replyTo)
})
}
})
.finally(() => {
if (!isMounted) return
setIsFetching(false)
})
})

return () => {
isMounted = false
Expand Down
40 changes: 21 additions & 19 deletions src/pages/GuestProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,29 @@ export default function GuestProfilePage(): JSX.Element {

useEffect(() => {
if (!id) return
const client = new Client('ariake.concrnt.net')
initializeClient(client)

client.getUser(id).then((u) => {
if (!u) return
setUser(u)
const timelineID = subProfileID ? 'world.concrnt.t-subhome.' + subProfileID + '@' + u.ccid : u.homeTimeline
setTargetStream([timelineID])
Client.createAsGuest('ariake.concrnt.net').then((client) => {
initializeClient(client)

client.api.getTimeline(timelineID).then((t) => {
if (!t) return
if (t.policy !== 'https://policy.concrnt.world/t/inline-read-write.json' || !t.policyParams) {
setIsPrivateTimeline(false)
return
}
try {
const params = JSON.parse(t.policyParams)
setIsPrivateTimeline(params.isReadPublic === false)
} catch (e) {
console.error(e)
}
client.getUser(id).then((u) => {
if (!u) return
setUser(u)
const timelineID = subProfileID ? 'world.concrnt.t-subhome.' + subProfileID + '@' + u.ccid : u.homeTimeline
setTargetStream([timelineID])

client.api.getTimeline(timelineID).then((t) => {
if (!t) return
if (t.policy !== 'https://policy.concrnt.world/t/inline-read-write.json' || !t.policyParams) {
setIsPrivateTimeline(false)
return
}
try {
const params = JSON.parse(t.policyParams)
setIsPrivateTimeline(params.isReadPublic === false)
} catch (e) {
console.error(e)
}
})
})
})
}, [id, path.hash])
Expand Down
37 changes: 20 additions & 17 deletions src/pages/GuestTimeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { useEffect, useState } from 'react'
import { Box, Button, Paper, Typography } from '@mui/material'
import { useParams, Link as NavLink } from 'react-router-dom'
import { Timeline } from '../components/Timeline/main'
import { Client, type CoreTimeline } from '@concurrent-world/client'
import { Client } from 'client'
import { Timeline as CoreTimeline } from '@concrnt/client'
import { FullScreenLoading } from '../components/ui/FullScreenLoading'
import { ClientProvider } from '../context/ClientContext'
import { TimelineHeader } from '../components/TimelineHeader'
Expand All @@ -27,21 +28,23 @@ export default function GuestTimelinePage(): JSX.Element {
if (!id) return
setTargetStream([id])
const resolver = id.split('@')[1]
const client = new Client(resolver)
initializeClient(client)

client.api.getTimeline(id).then((e) => {
if (!e) return
setTimeline(e)
Client.createAsGuest(resolver).then((client) => {
initializeClient(client)

if (e.policy === 'https://policy.concrnt.world/t/inline-read-write.json' && e?.policyParams) {
try {
const params = JSON.parse(e.policyParams)
setIsPrivateTimeline(!params.isReadPublic)
} catch (e) {
setIsPrivateTimeline(true)
client.api.getTimeline(id).then((e) => {
if (!e) return
setTimeline(e)

if (e.policy === 'https://policy.concrnt.world/t/inline-read-write.json' && e?.policyParams) {
try {
const params = JSON.parse(e.policyParams)
setIsPrivateTimeline(!params.isReadPublic)
} catch (e) {
setIsPrivateTimeline(true)
}
}
}
})
})
}, [id])

Expand All @@ -51,12 +54,12 @@ export default function GuestTimelinePage(): JSX.Element {
<MediaViewerProvider>
<>
<Helmet>
<title>{`#${timeline.document.body.name || 'No Title'} - Concrt`}</title>
<title>{`#${timeline.parsedDoc.body.name || 'No Title'} - Concrt`}</title>
<meta
name="description"
content={
timeline.document.body.description ||
`Concrnt timeline ${timeline.document.body.name || 'No Title'}`
timeline.parsedDoc.body.description ||
`Concrnt timeline ${timeline.parsedDoc.body.name || 'No Title'}`
}
/>
<link rel="canonical" href={`https://concrnt.com/timeline/${id}`} />
Expand Down Expand Up @@ -105,7 +108,7 @@ export default function GuestTimelinePage(): JSX.Element {
}}
>
<TimelineHeader
title={timeline.document.body.name || 'No Title'}
title={timeline.parsedDoc.body.name || 'No Title'}
titleIcon={isPrivateTimeline ? <LockIcon /> : <TagIcon />}
/>

Expand Down

0 comments on commit 1b60905

Please sign in to comment.