Skip to content

Commit

Permalink
Refactor DPost
Browse files Browse the repository at this point in the history
  • Loading branch information
d0rich committed Nov 25, 2023
1 parent 9f8e404 commit 99bebdd
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 67 deletions.
8 changes: 8 additions & 0 deletions apps/d.d0rich.me/src/entities/post/model/PostOffChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import type { Address } from 'ton-core'

export type PostOffChain = {
url: string
date: Date
author: Address
contentMd: string
}
3 changes: 3 additions & 0 deletions apps/d.d0rich.me/src/entities/post/model/PostOnChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { NftMetadata } from '@d0rich/ton-contracts/wrappers/DSocialNetworkPost'

export type PostOnChain = NftMetadata
2 changes: 2 additions & 0 deletions apps/d.d0rich.me/src/entities/post/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './PostOffChain'
export * from './PostOnChain'
36 changes: 36 additions & 0 deletions apps/d.d0rich.me/src/entities/post/utils/convertPostToOnChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { PostOnChain, PostOffChain } from '../model'
import { useAppConfig } from '../../../shared/composables/useAppConfig'
import { etxChar, stxChar } from './symbols'

const config = useAppConfig()

export function convertPostToOnChain(model: PostOffChain): PostOnChain {
const name = `Post on D from ${model.date.toLocaleDateString(
'de-DE'
)} by ${model.author.toRawString()}`
const stringBuilder: string[] = []
stringBuilder.push(`Posted: ${model.date.toLocaleDateString('de-DE')}`)
stringBuilder.push(`Author: ${model.author.toString()}`)
stringBuilder.push(`See on D: ${model.url}`)
stringBuilder.push('')
// TODO: Convert markdown to plain text
stringBuilder.push(model.contentMd)
stringBuilder.push('', '', '')
stringBuilder.push('===== Technical information =====')
stringBuilder.push(
`${stxChar}${JSON.stringify({
url: model.url,
date: model.date.toISOString(),
author: model.author.toRawString(),
contentMd: model.contentMd
})}${etxChar}`
)
return {
$$type: 'NftMetadata',
image: `https://${
config.network === 'TESTNET' ? 'testnet.' : ''
}d.d0rich.me/metadata/covers/post.jpg`,
name,
description: stringBuilder.join('\n')
}
}
13 changes: 13 additions & 0 deletions apps/d.d0rich.me/src/entities/post/utils/getPostFromOnChain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Address } from 'ton-core/dist/address/Address'
import type { PostOnChain, PostOffChain } from '../model'
import { etxChar, stxChar } from './symbols'

export function getPostFromOnChain(metadata: PostOnChain): PostOffChain {
const modelJSON = metadata.description.split(etxChar)[0].split(stxChar)[1]
const model: Record<keyof PostOffChain, string> = JSON.parse(modelJSON)
return {
...model,
date: new Date(model.date),
author: Address.parseRaw(model.author)
}
}
6 changes: 6 additions & 0 deletions apps/d.d0rich.me/src/entities/post/utils/symbols.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/** Start of text symbol */
const stxChar = String.fromCharCode(2)
/** End of text symbol */
const etxChar = String.fromCharCode(3)

export { stxChar, etxChar }
6 changes: 0 additions & 6 deletions apps/d.d0rich.me/src/features/blog/model/DBlog.ts

This file was deleted.

61 changes: 0 additions & 61 deletions apps/d.d0rich.me/src/features/post/model/DPost.ts

This file was deleted.

0 comments on commit 99bebdd

Please sign in to comment.