diff options
Diffstat (limited to 'server/user/command.go')
| -rw-r--r-- | server/user/command.go | 76 |
1 files changed, 76 insertions, 0 deletions
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() + } +} |
