repos / pico

pico services - prose.sh, pastes.sh, imgs.sh, feeds.sh, pgs.sh
git clone https://github.com/picosh/pico.git

commit
cafbdae
parent
aeccda9
author
Eric Bower
date
2024-09-08 15:39:53 +0000 UTC
feat(pubsub): send messages over public channels
3 files changed,  +27, -9
M go.mod
M go.sum
M go.mod
+1, -1
1@@ -36,7 +36,7 @@ require (
2 	github.com/muesli/termenv v0.15.3-0.20240509142007-81b8f94111d5
3 	github.com/neurosnap/go-exif-remove v0.0.0-20221010134343-50d1e3c35577
4 	github.com/picosh/pobj v0.0.0-20240709135546-27097077b26a
5-	github.com/picosh/pubsub v0.0.0-20240905034711-96ac1473168b
6+	github.com/picosh/pubsub v0.0.0-20240908153701-d8e78982a3da
7 	github.com/picosh/send v0.0.0-20240820031602-5d3b1a4494cc
8 	github.com/picosh/tunkit v0.0.0-20240709033345-8315d4f3cd0e
9 	github.com/sabhiram/go-gitignore v0.0.0-20210923224102-525f6e181f06
M go.sum
+2, -6
 1@@ -224,12 +224,8 @@ github.com/picosh/go-rsync-receiver v0.0.0-20240709135253-1daf4b12a9fc h1:bvcsoO
 2 github.com/picosh/go-rsync-receiver v0.0.0-20240709135253-1daf4b12a9fc/go.mod h1:i0iR3W4GSm1PuvVxB9OH32E5jP+CYkVb2NQSe0JCtlo=
 3 github.com/picosh/pobj v0.0.0-20240709135546-27097077b26a h1:Cr1xODiyd/SjjBRtYA9VX6Do3D+w+DansQzkb4NGeyA=
 4 github.com/picosh/pobj v0.0.0-20240709135546-27097077b26a/go.mod h1:VIkR1MZBvxSK2OO47jikxikAO/sKb/vTmXX5ZuYTIvo=
 5-github.com/picosh/pubsub v0.0.0-20240905021216-076708fdc9c2 h1:evyOvl/lZeW06naB1KOuZr6DRxCvbK98IqI5AfYF5pI=
 6-github.com/picosh/pubsub v0.0.0-20240905021216-076708fdc9c2/go.mod h1:FKC8uot+40iXmuDzTfbxYDG5PIc3ghwkmP2iItBKH0I=
 7-github.com/picosh/pubsub v0.0.0-20240905023503-81b6f9d0c296 h1:kNcOi4dfl+EUy56bOWZZ/9FtKnjmTGdxM/s01TUnPJg=
 8-github.com/picosh/pubsub v0.0.0-20240905023503-81b6f9d0c296/go.mod h1:FKC8uot+40iXmuDzTfbxYDG5PIc3ghwkmP2iItBKH0I=
 9-github.com/picosh/pubsub v0.0.0-20240905034711-96ac1473168b h1:GdwZbhoda9K8RpQH8mFovszBvIBH3l8bvTKzelL7FUc=
10-github.com/picosh/pubsub v0.0.0-20240905034711-96ac1473168b/go.mod h1:FKC8uot+40iXmuDzTfbxYDG5PIc3ghwkmP2iItBKH0I=
11+github.com/picosh/pubsub v0.0.0-20240908153701-d8e78982a3da h1:HCu4NyCoexe4Xjm05tWnx8QFxzRXxMoeixDqN6kiZG4=
12+github.com/picosh/pubsub v0.0.0-20240908153701-d8e78982a3da/go.mod h1:FKC8uot+40iXmuDzTfbxYDG5PIc3ghwkmP2iItBKH0I=
13 github.com/picosh/send v0.0.0-20240820031602-5d3b1a4494cc h1:IIsJuAFG2ju3cygKVKTIsYYZf21q5S3Dr1H4fGbfgJg=
14 github.com/picosh/send v0.0.0-20240820031602-5d3b1a4494cc/go.mod h1:RAgLDK3LrDK6pNeXtU9tjo28obl5DxShcTUk2nm/KCM=
15 github.com/picosh/senpai v0.0.0-20240503200611-af89e73973b0 h1:pBRIbiCj7K6rGELijb//dYhyCo8A3fvxW5dijrJVtjs=
M pubsub/cli.go
+24, -2
 1@@ -62,6 +62,10 @@ func toChannel(userName, name string) string {
 2 	return fmt.Sprintf("%s@%s", userName, name)
 3 }
 4 
 5+func toPublicChannel(name string) string {
 6+	return fmt.Sprintf("public@%s", name)
 7+}
 8+
 9 // extract user and scoped channel from channel.
10 func fromChannel(channel string) (string, string) {
11 	sp := strings.SplitN(channel, "@", 2)
12@@ -157,6 +161,7 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
13 			if cmd == "pub" {
14 				pubCmd := flagSet("pub", sesh)
15 				empty := pubCmd.Bool("e", false, "Send an empty message to subs")
16+				public := pubCmd.Bool("p", false, "Anyone can sub to this channel")
17 				if !flagCheck(pubCmd, repoName, cmdArgs) {
18 					return
19 				}
20@@ -169,9 +174,14 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
21 					reader = sesh
22 				}
23 
24+				name := toChannel(user.Name, channelName)
25+				if *public {
26+					name = toPublicChannel(channelName)
27+				}
28+
29 				wish.Println(sesh, "sending msg ...")
30 				msg := &psub.Msg{
31-					Name:   toChannel(user.Name, channelName),
32+					Name:   name,
33 					Reader: reader,
34 				}
35 
36@@ -195,9 +205,21 @@ func WishMiddleware(handler *CliHandler) wish.Middleware {
37 					wish.Errorln(sesh, err)
38 				}
39 			} else if cmd == "sub" {
40+				pubCmd := flagSet("pub", sesh)
41+				public := pubCmd.Bool("p", false, "Subscribe to a public channel")
42+				if !flagCheck(pubCmd, repoName, cmdArgs) {
43+					return
44+				}
45+				channelName := repoName
46+
47+				name := toChannel(user.Name, channelName)
48+				if *public {
49+					name = toPublicChannel(channelName)
50+				}
51+
52 				err = pubsub.PubSub.Sub(&psub.Subscriber{
53 					ID:     uuid.NewString(),
54-					Name:   toChannel(user.Name, repoName),
55+					Name:   name,
56 					Writer: sesh,
57 					Chan:   make(chan error),
58 				})