- commit
- bca32c0
- parent
- 8f87763
- author
- Antonio Mika
- date
- 2024-10-04 01:47:50 +0000 UTC
Fix write lock
2 files changed,
+42,
-13
+34,
-10
1@@ -148,6 +148,8 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
2 return
3 }
4
5+ isAdmin := handler.DBPool.HasFeatureForUser(user.ID, "admin")
6+
7 cmd := strings.TrimSpace(args[0])
8 if cmd == "help" {
9 wish.Println(sesh, helpStr(toSshCmd(handler.Cfg)))
10@@ -155,7 +157,7 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
11 return
12 } else if cmd == "ls" {
13 topicFilter := fmt.Sprintf("%s/", user.Name)
14- if handler.DBPool.HasFeatureForUser(user.ID, "admin") {
15+ if isAdmin {
16 topicFilter = ""
17 }
18
19@@ -252,10 +254,18 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
20 if topic == "" {
21 topic = uuid.NewString()
22 }
23- name := toTopic(user.Name, topic)
24- if *public {
25- name = toPublicTopic(topic)
26+
27+ var name string
28+
29+ if isAdmin && strings.HasPrefix(topic, "/") {
30+ name = strings.TrimPrefix(topic, "/")
31+ } else {
32+ name = toTopic(user.Name, topic)
33+ if *public {
34+ name = toPublicTopic(topic)
35+ }
36 }
37+
38 wish.Printf(
39 sesh,
40 "subscribe to this channel:\n\tssh %s sub %s\n",
41@@ -363,9 +373,15 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
42 "topic", topic,
43 )
44
45- name := toTopic(user.Name, topic)
46- if *public {
47- name = toPublicTopic(topic)
48+ var name string
49+
50+ if isAdmin && strings.HasPrefix(topic, "/") {
51+ name = strings.TrimPrefix(topic, "/")
52+ } else {
53+ name = toTopic(user.Name, topic)
54+ if *public {
55+ name = toPublicTopic(topic)
56+ }
57 }
58
59 err = pubsub.Sub(
60@@ -405,10 +421,18 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
61 if isCreator {
62 topic = uuid.NewString()
63 }
64- name := toTopic(user.Name, topic)
65- if *public {
66- name = toPublicTopic(topic)
67+
68+ var name string
69+
70+ if isAdmin && strings.HasPrefix(topic, "/") {
71+ name = strings.TrimPrefix(topic, "/")
72+ } else {
73+ name = toTopic(user.Name, topic)
74+ if *public {
75+ name = toPublicTopic(topic)
76+ }
77 }
78+
79 if isCreator {
80 wish.Printf(
81 sesh,
1@@ -204,14 +204,19 @@ func (c *SendLogWriter) Start() {
2 }
3
4 func (c *SendLogWriter) Write(data []byte) (int, error) {
5- c.connecMu.Lock()
6- defer c.connecMu.Unlock()
7-
8 var (
9 n int
10 err error
11 )
12
13+ ok := c.connecMu.TryLock()
14+
15+ if !ok {
16+ return n, fmt.Errorf("unable to acquire lock to write")
17+ }
18+
19+ defer c.connecMu.Unlock()
20+
21 if c.Messages == nil || c.Done == nil {
22 return n, fmt.Errorf("logger not viable")
23 }