-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathsegment.ts
More file actions
95 lines (79 loc) · 2.14 KB
/
segment.ts
File metadata and controls
95 lines (79 loc) · 2.14 KB
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
import type { StreamChat } from './client';
import type {
QuerySegmentTargetsFilter,
SegmentData,
SegmentResponse,
SortParam,
} from './types';
type SegmentType = 'user' | 'channel';
type SegmentUpdatableFields = {
description?: string;
filter?: {};
name?: string;
};
export class Segment {
type: SegmentType;
id: string | null;
client: StreamChat;
data?: SegmentData | SegmentResponse;
constructor(
client: StreamChat,
type: SegmentType,
id: string | null,
data?: SegmentData,
) {
this.client = client;
this.type = type;
this.id = id;
this.data = data;
}
create() {
const body = {
name: this.data?.name,
filter: this.data?.filter,
description: this.data?.description,
all_sender_channels: this.data?.all_sender_channels,
all_users: this.data?.all_users,
};
return this.client.createSegment(this.type, this.id, body);
}
verifySegmentId() {
if (!this.id) {
throw new Error(
'Segment id is missing. Either create the segment using segment.create() or set the id during instantiation - const segment = client.segment(id)',
);
}
}
get() {
this.verifySegmentId();
return this.client.getSegment(this.id as string);
}
update(data: Partial<SegmentUpdatableFields>) {
this.verifySegmentId();
return this.client.updateSegment(this.id as string, data);
}
addTargets(targets: string[]) {
this.verifySegmentId();
return this.client.addSegmentTargets(this.id as string, targets);
}
removeTargets(targets: string[]) {
this.verifySegmentId();
return this.client.removeSegmentTargets(this.id as string, targets);
}
delete() {
this.verifySegmentId();
return this.client.deleteSegment(this.id as string);
}
targetExists(targetId: string) {
this.verifySegmentId();
return this.client.segmentTargetExists(this.id as string, targetId);
}
queryTargets(
filter: QuerySegmentTargetsFilter | null = {},
sort: SortParam[] | null | [] = [],
options = {},
) {
this.verifySegmentId();
return this.client.querySegmentTargets(this.id as string, filter, sort, options);
}
}