Skip to content

Commit

Permalink
feat: add posts to IPFS as DAG with CID objects for images
Browse files Browse the repository at this point in the history
  • Loading branch information
David Grisham committed Jun 1, 2022
1 parent e871f77 commit cc10738
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 15 deletions.
49 changes: 39 additions & 10 deletions src/backend/post.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<string>
postImages: Array<string>
}

export interface IRegularPost extends Post {
encrypted?: false
}

export interface IRegularPostDAG extends Omit<Post, `featuredPhotoCID` | `postImages`> {
encrypted?: false
featuredPhotoCID: CID | null
postImages: Array<CID>
}

export interface IEncryptedPost extends Post {
encrypted: true
}
Expand Down Expand Up @@ -62,9 +69,9 @@ export function createRegularPost(
category: string,
tags: Tag[],
authorID: string,
featuredPhotoCID?: string | null,
postImages: Array<string>,
featuredPhotoCID: string | null,
featuredPhotoCaption?: string | null,
postImages?: Array<string>,
): IRegularPost {
if (subtitle !== null) {
subtitle = subtitle.trim()
Expand All @@ -77,10 +84,10 @@ export function createRegularPost(
timestamp: Date.now(),
tags,
authorID,
...(featuredPhotoCID ? { featuredPhotoCID } : {}),
postImages,
featuredPhotoCID,
...(featuredPhotoCaption ? { featuredPhotoCaption } : {}),
encrypted: false,
postImages,
}
}

Expand All @@ -91,7 +98,8 @@ export function createEncryptedPost(
category: string,
tags: Tag[],
authorID: string,
featuredPhotoCID?: string | null,
postImages: Array<string>,
featuredPhotoCID: string | null,
featuredPhotoCaption?: string | null,
): IEncryptedPost {
if (subtitle !== null) {
Expand All @@ -105,21 +113,42 @@ export function createEncryptedPost(
timestamp: Date.now(),
tags,
authorID,
...(featuredPhotoCID ? { featuredPhotoCID } : {}),
postImages,
featuredPhotoCID,
...(featuredPhotoCaption ? { featuredPhotoCaption } : {}),
encrypted: true,
}
}

export async function sendRegularPost(data: IRegularPost): Promise<string> {
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<IRegularPost> = { data, sig: uint8ArrayToHexString(sig), public_key: publicKey }
const ipfsData: ISignedIPFSObject<IRegularPostDAG> = {
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<IRegularPost> = { data, sig: uint8ArrayToHexString(sig), public_key: publicKey }
await axios.post(`${nodeUrl()}/content`, {
cid,
data: ipfsData,
data: postData,
type: `post`,
})

Expand Down
6 changes: 5 additions & 1 deletion src/backend/utilities/ipfs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,11 @@ async function createIPFSInterface(): Promise<IPFSInterface> {
}

const sendJSONData = async <T>(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()
}

Expand Down
8 changes: 4 additions & 4 deletions src/components/post/Editor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>,
featuredPhotoCID: string | null,
featuredPhotoCaption?: string | null,
postImages?: Array<string>,
): Promise<void> {
const p = createRegularPost(
this.title,
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions src/store/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
Expand All @@ -111,6 +112,7 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
Expand All @@ -131,6 +133,7 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
Expand All @@ -149,6 +152,7 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
Expand Down

0 comments on commit cc10738

Please sign in to comment.