-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpost.ts
122 lines (103 loc) · 3.55 KB
/
post.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
* A decentralized storage of dao/channel/post meta data info.
*/
import { field, variant, option } from '@dao-xyz/borsh';
import { Program, CanOpenSubPrograms } from '@dao-xyz/peerbit-program';
import { Documents, Operation, DocumentIndex } from '@dao-xyz/peerbit-document';
import { AnySearch } from '@dao-xyz/peerbit-anysearch';
import { DQuery } from '@dao-xyz/peerbit-query';
import { DynamicAccessController } from '@dao-xyz/peerbit-dynamic-access-controller';
import { Entry } from '@dao-xyz/ipfs-log';
// @ts-ignore
import { v4 as uuid } from 'uuid';
import { Address } from '@dao-xyz/peerbit-store';
import { Content, ProgramContent, StaticContent } from './content.js';
import { CollaborativeText } from './post-types.js';
/**
* Content
*/
@variant([1, 2])
export class Post extends Program {
@field({ type: 'u64' })
timestamp: bigint;
@field({ type: Content })
content: Content; // Text, video, images etc
@field({ type: option(Address) })
parent?: Address;
@field({ type: option(DynamicAccessController) })
acl?: DynamicAccessController
constructor(opts?: {
parent?: Address,
content: Program | StaticContent,
timestamp?: bigint,
acl?: DynamicAccessController
}) {
super({ id: uuid() })
if (opts) {
this.parent = opts.parent;
this.timestamp = opts.timestamp || BigInt(+new Date);
this.content = opts.content instanceof Program ? new ProgramContent({ program: opts.content }) : opts.content;
this.acl = opts.acl
}
}
getContent<T>(): T {
return this.content as T
}
async setup(): Promise<void> {
await this.acl?.setup();
if (this.content instanceof ProgramContent) {
if (this.content.program instanceof CollaborativeText) {
await this.content.program.setup();
}
else {
throw new Error("Not supported")
}
}
else if (this.content instanceof StaticContent) {
/// Do nothing
}
else {
throw new Error("Not supported")
}
}
}
@variant([1, 1])
export class Posts extends Program implements CanOpenSubPrograms {
@field({ type: Documents })
posts: Documents<Post>;
@field({ type: option(DynamicAccessController) })
acl?: DynamicAccessController
constructor(properties?: {
id?: string,
acl?: DynamicAccessController
}) {
super(properties);
this.posts = new Documents({ id: properties?.id, index: new DocumentIndex({ id: properties?.id, indexBy: 'id', search: new AnySearch({ id: properties?.id, query: new DQuery({ id: properties?.id }) }) }) })
if (properties) {
this.acl = properties.acl;
}
}
async setup(): Promise<void> {
await this.acl?.setup();
await this.posts.setup({
type: Post,
canAppend: this.canCreatePost.bind(this),
canRead: (identity) => (this.acl?.canRead || (() => Promise.resolve(true)))(identity)
})
}
async canCreatePost(entry: Entry<Operation<Post>>): Promise<boolean> {
if (this.acl && !await this.acl?.canAppend(entry)) {
return false;
}
return true;
}
async canOpen(programToOpen: Program, fromEntry: Entry<any>): Promise<boolean> {
if (programToOpen.constructor !== Post) {
return false;
}
if (this.acl && !await this.acl.canAppend(fromEntry)) {
return false;
}
return true;
}
}