diff --git a/apps/d.d0rich.me/src/entities/post/model/PostOffChain.ts b/apps/d.d0rich.me/src/entities/post/model/PostOffChain.ts new file mode 100644 index 00000000..2ff19df7 --- /dev/null +++ b/apps/d.d0rich.me/src/entities/post/model/PostOffChain.ts @@ -0,0 +1,8 @@ +import type { Address } from 'ton-core' + +export type PostOffChain = { + url: string + date: Date + author: Address + contentMd: string +} diff --git a/apps/d.d0rich.me/src/entities/post/model/PostOnChain.ts b/apps/d.d0rich.me/src/entities/post/model/PostOnChain.ts new file mode 100644 index 00000000..726e3b04 --- /dev/null +++ b/apps/d.d0rich.me/src/entities/post/model/PostOnChain.ts @@ -0,0 +1,3 @@ +import type { NftMetadata } from '@d0rich/ton-contracts/wrappers/DSocialNetworkPost' + +export type PostOnChain = NftMetadata diff --git a/apps/d.d0rich.me/src/entities/post/model/index.ts b/apps/d.d0rich.me/src/entities/post/model/index.ts new file mode 100644 index 00000000..360ac540 --- /dev/null +++ b/apps/d.d0rich.me/src/entities/post/model/index.ts @@ -0,0 +1,2 @@ +export * from './PostOffChain' +export * from './PostOnChain' diff --git a/apps/d.d0rich.me/src/entities/post/utils/convertPostToOnChain.ts b/apps/d.d0rich.me/src/entities/post/utils/convertPostToOnChain.ts new file mode 100644 index 00000000..dd9575ef --- /dev/null +++ b/apps/d.d0rich.me/src/entities/post/utils/convertPostToOnChain.ts @@ -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') + } +} diff --git a/apps/d.d0rich.me/src/entities/post/utils/getPostFromOnChain.ts b/apps/d.d0rich.me/src/entities/post/utils/getPostFromOnChain.ts new file mode 100644 index 00000000..1a6cc0fc --- /dev/null +++ b/apps/d.d0rich.me/src/entities/post/utils/getPostFromOnChain.ts @@ -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 = JSON.parse(modelJSON) + return { + ...model, + date: new Date(model.date), + author: Address.parseRaw(model.author) + } +} diff --git a/apps/d.d0rich.me/src/entities/post/utils/symbols.ts b/apps/d.d0rich.me/src/entities/post/utils/symbols.ts new file mode 100644 index 00000000..4ffab777 --- /dev/null +++ b/apps/d.d0rich.me/src/entities/post/utils/symbols.ts @@ -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 } diff --git a/apps/d.d0rich.me/src/features/blog/model/DBlog.ts b/apps/d.d0rich.me/src/features/blog/model/DBlog.ts deleted file mode 100644 index 32d441aa..00000000 --- a/apps/d.d0rich.me/src/features/blog/model/DBlog.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { DSocialNetworkBlog } from '@d0rich/ton-contracts/wrappers/DSocialNetworkBlog' - -export class DBlog { - // eslint-disable-next-line no-useless-constructor - constructor(public readonly contract: DSocialNetworkBlog) {} -} diff --git a/apps/d.d0rich.me/src/features/post/model/DPost.ts b/apps/d.d0rich.me/src/features/post/model/DPost.ts deleted file mode 100644 index 13334b78..00000000 --- a/apps/d.d0rich.me/src/features/post/model/DPost.ts +++ /dev/null @@ -1,61 +0,0 @@ -import { Address } from 'ton-core' -import { - DSocialNetworkPost, - type NftMetadata -} from '@d0rich/ton-contracts/wrappers/DSocialNetworkPost' - -export type DPostModel = { - url: string - date: Date - author: Address - contentMd: string -} - -/** Start of text symbol */ -const stxChar = String.fromCharCode(2) -/** End of text symbol */ -const etxChar = String.fromCharCode(3) - -export class DPost { - model?: DPostModel - - // eslint-disable-next-line no-useless-constructor - constructor(public readonly contract: DSocialNetworkPost) {} - - static serializePostData(model: DPostModel): NftMetadata { - 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://d.d0rich.me/metadata/.jpg', - name, - description: stringBuilder.join('\n') - } - } - - static deserializePostData(metadata: NftMetadata): DPostModel { - const modelJSON = metadata.description.split(etxChar)[0].split(stxChar)[1] - const model: DPostModel = JSON.parse(modelJSON) - model.date = new Date(model.date) - model.author = Address.parseRaw(model.author as unknown as string) - return model - } -}