summaryrefslogtreecommitdiff
path: root/server/user/command.go
blob: 210dede9ce60efecfc918a4f6674d19cc818c174 (plain)
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
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()
	}
}