-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtitle.go
More file actions
244 lines (212 loc) Β· 6.34 KB
/
title.go
File metadata and controls
244 lines (212 loc) Β· 6.34 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package slagent
import (
"fmt"
"strings"
slackapi "github.com/slack-go/slack"
"github.com/sttts/slagent/access"
)
// formatTitle builds the thread parent label reflecting access state.
//
// Open for all: ":instanceID:π§΅ Topic"
// Selective (allowed users): ":instanceID:π§΅ <@U1> <@U2> Topic"
// Selective + observe: ":instanceID:ππ§΅ <@U1> <@U2> Topic"
// Locked (owner only): ":instanceID:ππ§΅ Topic"
// Locked + observe (= observe): ":instanceID:ππ§΅ Topic"
// With bans (appended): "... (π <@U3>)"
func (t *Thread) formatTitle() string {
t.mu.Lock()
defer t.mu.Unlock()
title := t.topic
if title == "" {
title = "Agent session"
}
st := t.Controller.State()
// Build access marker: π replaces π when observe is on
var label string
if st.OpenAccess {
label = fmt.Sprintf(":%s:π§΅ %s", t.instanceID, title)
} else if st.Observe {
// Observe implies closed; π replaces π
if len(st.AllowedUsers) > 0 {
var mentions []string
for _, u := range st.AllowedUsers {
mentions = append(mentions, fmt.Sprintf("<@%s>", u))
}
label = fmt.Sprintf(":%s:ππ§΅ %s %s", t.instanceID, strings.Join(mentions, " "), title)
} else {
label = fmt.Sprintf(":%s:ππ§΅ %s", t.instanceID, title)
}
} else if len(st.AllowedUsers) > 0 {
// Selective without observe
var mentions []string
for _, u := range st.AllowedUsers {
mentions = append(mentions, fmt.Sprintf("<@%s>", u))
}
label = fmt.Sprintf(":%s:π§΅ %s %s", t.instanceID, strings.Join(mentions, " "), title)
} else {
// Locked (no allowed users, no observe)
label = fmt.Sprintf(":%s:ππ§΅ %s", t.instanceID, title)
}
// Append mode suffix (e.g. " β π planning")
if t.modeSuffix != "" {
label += t.modeSuffix
}
// Append ban list
if len(st.BannedUsers) > 0 {
var mentions []string
for _, u := range st.BannedUsers {
mentions = append(mentions, fmt.Sprintf("<@%s>", u))
}
label += fmt.Sprintf(" (π %s)", strings.Join(mentions, " "))
}
// t.title is for terminal display β convert shortcode to Unicode
t.title = ShortcodesToUnicode(label)
return label
}
// Title returns the full thread title with shortcodes converted to Unicode.
func (t *Thread) Title() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.title
}
// Topic returns the parsed topic text (without emojis, mentions, access markers).
func (t *Thread) Topic() string {
t.mu.Lock()
defer t.mu.Unlock()
return t.topic
}
// refreshTitle re-fetches the thread parent message and re-parses the title.
// Used by joined instances to pick up access changes made by the original instance.
func (t *Thread) refreshTitle() {
t.mu.Lock()
threadTS := t.threadTS
t.mu.Unlock()
if threadTS == "" {
return
}
params := &slackapi.GetConversationRepliesParameters{
ChannelID: t.channel,
Timestamp: threadTS,
Limit: 1,
}
msgs, _, _, err := t.client.GetConversationReplies(params)
if err == nil && len(msgs) > 0 {
t.parseTitle(msgs[0].Text)
}
}
// parseTitle recovers access state from a thread parent message.
// Handles both Unicode (ππ§΅) and Slack shortcode (:lock::thread:) formats.
func (t *Thread) parseTitle(text string) {
t.mu.Lock()
defer t.mu.Unlock()
// Normalize shortcodes to Unicode for consistent parsing
text = ShortcodesToUnicode(text)
t.title = text
// Detect π observe marker (ππ§΅ = observe mode, replaces π)
observe := strings.Contains(text, "ππ§΅")
locked := strings.Contains(text, "ππ§΅")
// Extract content after π§΅ (with optional space)
if idx := strings.Index(text, "π§΅ "); idx >= 0 {
t.topic = text[idx+len("π§΅ "):]
} else if idx := strings.Index(text, "π§΅"); idx >= 0 {
t.topic = text[idx+len("π§΅"):]
}
// Parse "(π <@U3>)" β banned users (strip from title)
bannedUsers := make(map[string]bool)
if idx := strings.Index(t.topic, " (π "); idx >= 0 {
end := strings.Index(t.topic[idx:], ")")
if end >= 0 {
extractMentions(t.topic[idx:idx+end+1], bannedUsers)
t.topic = strings.TrimSpace(t.topic[:idx] + t.topic[idx+end+1:])
}
}
// Strip mode suffix (e.g. " β π planning") β not part of the topic.
if idx := strings.LastIndex(t.topic, " β π"); idx >= 0 {
t.topic = t.topic[:idx]
}
// Build access state
var st access.State
st.Observe = observe
for u := range bannedUsers {
st.BannedUsers = append(st.BannedUsers, u)
}
if locked {
st.OpenAccess = false
t.Controller.Apply(st)
return
}
// Not locked: parse leading <@...> mentions as allowed users
allowedUsers := make(map[string]bool)
for strings.HasPrefix(t.topic, "<@") {
end := strings.Index(t.topic, ">")
if end < 0 {
break
}
uid := t.topic[2:end]
// Strip display name suffix: <@U12345|sttts> β U12345
if idx := strings.Index(uid, "|"); idx >= 0 {
uid = uid[:idx]
}
allowedUsers[uid] = true
t.topic = strings.TrimLeft(t.topic[end+1:], " ")
}
for u := range allowedUsers {
st.AllowedUsers = append(st.AllowedUsers, u)
}
// π means not open (observe replaces π); otherwise open if no allowed users
if observe {
st.OpenAccess = false
} else {
st.OpenAccess = len(allowedUsers) == 0
}
t.Controller.Apply(st)
}
// extractMentions parses <@U...> mentions from a string into the target map.
func extractMentions(s string, target map[string]bool) {
rest := s
for {
start := strings.Index(rest, "<@")
if start < 0 {
break
}
end := strings.Index(rest[start:], ">")
if end < 0 {
break
}
uid := rest[start+2 : start+end]
// Strip display name suffix: <@U12345|sttts> β U12345
if idx := strings.Index(uid, "|"); idx >= 0 {
uid = uid[:idx]
}
target[uid] = true
rest = rest[start+end+1:]
}
}
// updateTitle updates the thread parent message to reflect current access state.
func (t *Thread) updateTitle() {
t.mu.Lock()
threadTS := t.threadTS
t.mu.Unlock()
if threadTS == "" {
return
}
label := t.formatTitle()
t.logSlack("updateMessage(title)", label)
t.client.UpdateMessage(
t.channel,
threadTS,
slackapi.MsgOptionBlocks(t.slagentSection(label)),
slackapi.MsgOptionText(label, false),
)
}
// SetModeSuffix sets a suffix appended to the thread title (e.g. " β π planning")
// and updates the thread parent message. Pass "" to clear.
func (t *Thread) SetModeSuffix(suffix string) {
t.mu.Lock()
old := t.modeSuffix
t.modeSuffix = suffix
t.mu.Unlock()
if suffix != old {
t.updateTitle()
}
}