-
Notifications
You must be signed in to change notification settings - Fork 115
/
Copy pathaddPost.ts
52 lines (46 loc) · 1.97 KB
/
addPost.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import { flags } from '@oclif/command'
import chalk from 'chalk'
import ForumCommandBase from '../../base/ForumCommandBase'
import { ForumPostMetadata, IForumPostMetadata } from '@joystream/metadata-protobuf'
import { metadataToBytes } from '../../helpers/serialization'
import { ForumPostId as PostId } from '@joystream/types/primitives'
export default class ForumAddPostCommand extends ForumCommandBase {
static description = 'Add forum post.'
static flags = {
categoryId: flags.integer({
required: true,
description: 'Id of the forum category of the parent thread',
}),
threadId: flags.integer({
required: true,
description: "Post's parent thread",
}),
text: flags.string({
required: true,
description: 'Post content (md-formatted text)',
}),
editable: flags.boolean({
required: false,
description: 'Whether the post should be editable',
}),
...ForumCommandBase.flags,
}
async run(): Promise<void> {
const api = await this.getOriginalApi()
const { categoryId, threadId, text, editable } = this.parse(ForumAddPostCommand).flags
await this.ensureThreadExists(categoryId, threadId)
await this.ensureCategoryMutable(categoryId)
const member = await this.getRequiredMemberContext()
// Replies not supported atm
const metadata: IForumPostMetadata = { text }
this.jsonPrettyPrint(JSON.stringify({ categoryId, threadId, text, editable }))
await this.requireConfirmation('Do you confirm the provided input?', true)
const result = await this.sendAndFollowTx(
await this.getDecodedPair(member.membership.controllerAccount),
api.tx.forum.addPost(member.id, categoryId, threadId, metadataToBytes(ForumPostMetadata, metadata), editable)
)
const postId: PostId = this.getEvent(result, 'forum', 'PostAdded').data[0]
this.log(chalk.green(`ForumPost with id ${chalk.magentaBright(postId.toString())} successfully created!`))
this.output(postId.toString())
}
}