Skip to main content

Creating And Updating Posts

Create A Post#

substrateApi.tx.posts.createPost(spaceIdOpt, extension, content)
ParamsDescription
spaceIdOptThe space where a post will be published.
extensionA kind of post. It can be a regular post, a shared post or a comment.
contentIpfsContent is a function that returns a substrate like implementation for enum { IPFS: "CID of your content"}.

๐Ÿ†ƒ PostExtensionEnum: RegularPost | Comment | SharedPost

Examples:

A Regular Post#

import { IpfsContent } from "@subsocial/types/substrate/classes"
...
const cid = await ipfs.saveContent({
title: "What is Subsocial?",
image: "QmcWWpR176oFao49jrLHUoH3R9MCziE5d77fdD8qdoiinx",
tags: [ 'Hello world', 'FAQ' ],
body: 'Subsocial is an open protocol for decentralized social networks and marketplaces. It`s built with Substrate and IPFS.'
})
const tx = substrateApi.tx.posts.createPost('1', { RegularPost: null}, IpfsContent(cid))
...

A Shared Post#

import { IpfsContent } from "@subsocial/types/substrate/classes"
...
const cid = await ipfs.saveContent({
body: 'Keep up the good work!'
})
const tx = substrateApi.tx.posts.createPost('1', { SharedPost: '1'}, IpfsContent(cid))
...
}

Update A Post#

substrateApi.tx.posts.updatePost(postId: AnyPostId, update: PostUpdateType)
ParamsDescription
postIdThe ID of the current space.
updateThe fields available to be updated.

Update properties

PropertiesDescription
spaceId?If a new spaceId is provided, it will move this post to the new space.
content?IpfsContent is a function that returns a substrate like implementation for enum { IPFS: "CID of your content"}.
hidden?boolean, if post is hidden from other users.

Example:

import {
IpfsContent,
OptionBool,
PostUpdate
} from "@subsocial/types/substrate/classes"
...
const cid = await ipfs.saveContent({
title: "What is Subsocial?",
image: "QmcWWpR176oFao49jrLHUoH3R9MCziE5d77fdD8qdoiinx",
tags: ['Hello world', 'FAQ', 'Subsoical'], //updated field
body: 'Subsocial is an open protocol for decentralized social networks and marketplaces. It`s built with Substrate and IPFS.'
})
const update = new PostUpdate({
content: IpfsContent(cid),
hidden: OptionBool(true),
})
const tx = substrateApi.tx.spaces.posts.updatePost('1', update)
...