diff --git a/src/backend/post.ts b/src/backend/post.ts index 778ca550a..9cbcd55eb 100644 --- a/src/backend/post.ts +++ b/src/backend/post.ts @@ -1,4 +1,5 @@ import axios, { AxiosError } from 'axios' +import { CID } from 'ipfs-core' import { signContent, verifyContent } from './utilities/keys' import ipfs from './utilities/ipfs' @@ -17,18 +18,24 @@ export interface Post { subtitle: string | null content: string category: string - featuredPhotoCID?: string | null + featuredPhotoCID: string | null featuredPhotoCaption?: string | null timestamp: number tags: Tag[] encrypted?: boolean - postImages?: Array + postImages: Array } export interface IRegularPost extends Post { encrypted?: false } +export interface IRegularPostDAG extends Omit { + encrypted?: false + featuredPhotoCID: CID | null + postImages: Array +} + export interface IEncryptedPost extends Post { encrypted: true } @@ -62,9 +69,9 @@ export function createRegularPost( category: string, tags: Tag[], authorID: string, - featuredPhotoCID?: string | null, + postImages: Array, + featuredPhotoCID: string | null, featuredPhotoCaption?: string | null, - postImages?: Array, ): IRegularPost { if (subtitle !== null) { subtitle = subtitle.trim() @@ -77,10 +84,10 @@ export function createRegularPost( timestamp: Date.now(), tags, authorID, - ...(featuredPhotoCID ? { featuredPhotoCID } : {}), + postImages, + featuredPhotoCID, ...(featuredPhotoCaption ? { featuredPhotoCaption } : {}), encrypted: false, - postImages, } } @@ -91,7 +98,8 @@ export function createEncryptedPost( category: string, tags: Tag[], authorID: string, - featuredPhotoCID?: string | null, + postImages: Array, + featuredPhotoCID: string | null, featuredPhotoCaption?: string | null, ): IEncryptedPost { if (subtitle !== null) { @@ -105,7 +113,8 @@ export function createEncryptedPost( timestamp: Date.now(), tags, authorID, - ...(featuredPhotoCID ? { featuredPhotoCID } : {}), + postImages, + featuredPhotoCID, ...(featuredPhotoCaption ? { featuredPhotoCaption } : {}), encrypted: true, } @@ -113,13 +122,33 @@ export function createEncryptedPost( export async function sendRegularPost(data: IRegularPost): Promise { const { sig, publicKey } = await signContent(data) + const featuredPhotoCID = data.featuredPhotoCID ? CID.parse(data.featuredPhotoCID) : null + const postImages = data.postImages.map((imageCID) => CID.parse(imageCID)) - const ipfsData: ISignedIPFSObject = { data, sig: uint8ArrayToHexString(sig), public_key: publicKey } + const ipfsData: ISignedIPFSObject = { + data: { + authorID: data.authorID, + title: data.title, + subtitle: data.subtitle, + content: data.content, + category: data.category, + postImages, + featuredPhotoCID, + ...(data.featuredPhotoCaption ? { featuredPhotoCaption: data.featuredPhotoCaption } : {}), + timestamp: data.timestamp, + tags: data.tags, + encrypted: data.encrypted, + }, + sig: uint8ArrayToHexString(sig), + public_key: publicKey, + } const cid = await ipfs().sendJSONData(ipfsData) + + const postData: ISignedIPFSObject = { data, sig: uint8ArrayToHexString(sig), public_key: publicKey } await axios.post(`${nodeUrl()}/content`, { cid, - data: ipfsData, + data: postData, type: `post`, }) diff --git a/src/backend/utilities/ipfs.ts b/src/backend/utilities/ipfs.ts index 8beafd703..73b3c499a 100644 --- a/src/backend/utilities/ipfs.ts +++ b/src/backend/utilities/ipfs.ts @@ -116,7 +116,11 @@ async function createIPFSInterface(): Promise { } const sendJSONData = async (content: T) => { - const cid = await node.dag.put(content) + const cid = await node.dag.put(content, { + storeCodec: `dag-cbor`, + hashAlg: `sha2-256`, + }) + return cid.toString() } diff --git a/src/components/post/Editor.vue b/src/components/post/Editor.vue index cf13efb71..c4e667795 100644 --- a/src/components/post/Editor.vue +++ b/src/components/post/Editor.vue @@ -656,16 +656,16 @@ export default Vue.extend({ if (checksOnly) { return true } - this.sendPost(clean, category, tags, featuredPhotoCID, featuredPhotoCaption, postImages) + this.sendPost(clean, category, tags, postImages, featuredPhotoCID, featuredPhotoCaption) return true }, async sendPost( clean: string, category: string, tags: Tag[], - featuredPhotoCID?: string | null, + postImages: Array, + featuredPhotoCID: string | null, featuredPhotoCaption?: string | null, - postImages?: Array, ): Promise { const p = createRegularPost( this.title, @@ -674,9 +674,9 @@ export default Vue.extend({ category, tags, this.$store.state.session.id, + postImages, featuredPhotoCID, featuredPhotoCaption, - postImages, ) try { const cid = await sendRegularPost(p) diff --git a/src/store/draft.ts b/src/store/draft.ts index 926984247..534209957 100644 --- a/src/store/draft.ts +++ b/src/store/draft.ts @@ -95,6 +95,7 @@ export const mutations: MutationTree = { content: ``, featuredPhotoCID: null, featuredPhotoCaption: null, + postImages: [], tags: [], category: ``, timestamp: 0, @@ -111,6 +112,7 @@ export const mutations: MutationTree = { content: ``, featuredPhotoCID: null, featuredPhotoCaption: null, + postImages: [], tags: [], category: ``, timestamp: 0, @@ -131,6 +133,7 @@ export const mutations: MutationTree = { content: ``, featuredPhotoCID: null, featuredPhotoCaption: null, + postImages: [], tags: [], category: ``, timestamp: 0, @@ -149,6 +152,7 @@ export const mutations: MutationTree = { content: ``, featuredPhotoCID: null, featuredPhotoCaption: null, + postImages: [], tags: [], category: ``, timestamp: 0,