-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.go
More file actions
56 lines (48 loc) · 1.56 KB
/
thread.go
File metadata and controls
56 lines (48 loc) · 1.56 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
package chat
import (
"context"
"errors"
)
type Thread struct {
runtime *Chat
adapter Adapter
ref ThreadRef
}
func (c *Chat) newThread(adapter Adapter, ref ThreadRef) *Thread {
assert(adapter != nil, "newThread requires adapter")
assert(ref.ID != "", "newThread requires thread id")
return &Thread{runtime: c, adapter: adapter, ref: ref}
}
func (t *Thread) ID() ThreadID {
assert(t != nil, "ID called on nil thread")
return t.ref.ID
}
func (t *Thread) Subscribe(ctx context.Context) error {
assert(t != nil, "Subscribe called on nil thread")
return t.runtime.state.SubscribeThread(ctx, t.ref.ID)
}
func (t *Thread) Unsubscribe(ctx context.Context) error {
assert(t != nil, "Unsubscribe called on nil thread")
return t.runtime.state.UnsubscribeThread(ctx, t.ref.ID)
}
func (t *Thread) Post(ctx context.Context, msg PostableMessage) (*SentMessage, error) {
assert(t != nil, "Post called on nil thread")
if msg.Text == "" {
return nil, errors.New("chat: post message text is required")
}
return t.adapter.PostMessage(ctx, t.ref, msg)
}
func (t *Thread) PostEphemeral(ctx context.Context, actor Actor, msg PostableMessage, opts EphemeralOptions) (*SentMessage, error) {
assert(t != nil, "PostEphemeral called on nil thread")
if actor.ID == "" {
return nil, errors.New("chat: ephemeral actor id is required")
}
if msg.Text == "" {
return nil, errors.New("chat: ephemeral message text is required")
}
poster, ok := t.adapter.(EphemeralPoster)
if !ok {
return nil, ErrUnsupportedCapability
}
return poster.PostEphemeralMessage(ctx, t.ref, actor, msg, opts)
}