From 10b8a79389e7073f6bd65695c3d05c77b825bc33 Mon Sep 17 00:00:00 2001 From: citrons Date: Sun, 26 Jan 2025 01:56:53 -0600 Subject: initial commit --- server/user/command.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 server/user/command.go (limited to 'server/user/command.go') diff --git a/server/user/command.go b/server/user/command.go new file mode 100644 index 0000000..210dede --- /dev/null +++ b/server/user/command.go @@ -0,0 +1,76 @@ +package user + +import ( + "citrons.xyz/talk/server/session" + "citrons.xyz/talk/proto" +) + +func (u *User) SendRequest(r session.Request) { + switch r.Cmd.Kind { + + case "update": + if r.From.UserId != u.Id() { + r.Reply(proto.Fail{"forbidden", "", nil}.Cmd()) + return + } + if len(r.Cmd.Args) != 1 { + r.ReplyInvalid() + return + } + upd := r.Cmd.Args[0] + if upd.Kind != "u" { + r.ReplyInvalid() + return + } + name := u.name + for k, v := range upd.Fields { + switch k { + case "": + name = v + default: + r.ReplyInvalid() + return + } + } + if name != u.name { + err := u.Rename(name) + if err != nil { + r.Reply(err.Cmd()) + return + } + } + u.Stream.Event(r.Cmd) + r.Reply(proto.NewCmd("ok", "")) + + case "i": + r.Reply(proto.NewCmd("i", "", u.GetInfo())) + + case "s": + r.From.Subscribe(&u.Stream) + r.Reply(proto.NewCmd("i", "", u.GetInfo())) + + case "u": + r.From.Unsubscribe(&u.Stream) + r.Reply(proto.NewCmd("ok", "")) + + default: + r.ReplyInvalid() + } +} + +func (t Tombstone) SendRequest(r session.Request) { + switch r.Cmd.Kind { + + case "update": + r.Reply(proto.Fail{"bad-target", "", nil}.Cmd()) + + case "i", "s": + r.Reply(proto.NewCmd("i", "", t.GetInfo())) + + case "u": + r.Reply(proto.NewCmd("ok", "")) + + default: + r.ReplyInvalid() + } +} -- cgit v1.2.3