Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 59 additions & 16 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,16 +18,26 @@ export interface Post {
subtitle: string | null
content: string
category: string
featuredPhotoCID?: string | null
featuredPhotoCaption?: string | null
featuredPhotoCID: string | null
featuredPhotoCaption: string | null
timestamp: number
tags: Tag[]
encrypted?: boolean
postImages?: Array<string>
postImages: Array<string>
version: string
lastUpdated: number
}

export interface IRegularPost extends Post {
encrypted?: false
encrypted: false
}

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

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

Expand All @@ -91,8 +106,11 @@ export function createEncryptedPost(
category: string,
tags: Tag[],
authorID: string,
featuredPhotoCID?: string | null,
featuredPhotoCaption?: string | null,
postImages: Array<string>,
featuredPhotoCID: string | null,
featuredPhotoCaption: string | null,
version: string,
lastUpdated: number,
): IEncryptedPost {
if (subtitle !== null) {
subtitle = subtitle.trim()
Expand All @@ -105,21 +123,46 @@ export function createEncryptedPost(
timestamp: Date.now(),
tags,
authorID,
...(featuredPhotoCID ? { featuredPhotoCID } : {}),
...(featuredPhotoCaption ? { featuredPhotoCaption } : {}),
postImages,
featuredPhotoCID,
featuredPhotoCaption,
encrypted: true,
version,
lastUpdated,
}
}

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,
featuredPhotoCaption: data.featuredPhotoCaption,
timestamp: data.timestamp,
tags: data.tags,
encrypted: data.encrypted,
version: data.version,
lastUpdated: data.lastUpdated,
},
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
10 changes: 5 additions & 5 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,
featuredPhotoCaption?: string | null,
postImages?: Array<string>,
postImages: Array<string>,
featuredPhotoCID: string | null,
featuredPhotoCaption: string | null,
): 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
14 changes: 14 additions & 0 deletions src/store/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const state = (): DraftState => ({
category: ``,
timestamp: 0,
postImages: [],
version: `v1`,
lastUpdated: 0,
},
],
activeIndex: 0,
Expand Down Expand Up @@ -95,9 +97,12 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
version: `v1`,
lastUpdated: 0,
})
state.activeIndex = state.drafts.length - 1
},
Expand All @@ -111,9 +116,12 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
version: `v1`,
lastUpdated: 0,
})
state.activeIndex = 0
}
Expand All @@ -131,9 +139,12 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
version: `v1`,
lastUpdated: 0,
})
}
state.activeIndex = state.drafts.length - 1
Expand All @@ -149,9 +160,12 @@ export const mutations: MutationTree<DraftState> = {
content: ``,
featuredPhotoCID: null,
featuredPhotoCaption: null,
postImages: [],
tags: [],
category: ``,
timestamp: 0,
version: `v1`,
lastUpdated: 0,
},
]
state.activeIndex = 0
Expand Down