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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
package user
import (
"citrons.xyz/talk/server/object"
"citrons.xyz/talk/server/session"
"citrons.xyz/talk/server/validate"
"citrons.xyz/talk/proto"
)
type UserStore struct {
world *object.World
byName map[string]*User
gone map[string]Tombstone
}
type User struct {
store *UserStore
name string
id string
status string
description string
Stream session.Stream
Channels map[string]bool
Anonymous bool
}
type Tombstone struct {
name string
}
func NewStore(world *object.World) *UserStore {
return &UserStore {
world, make(map[string]*User), make(map[string]Tombstone),
}
}
func (us *UserStore) CreateUser(name string) (*User, *proto.Fail) {
if us.ByName(name) != nil {
return nil, &proto.Fail {
"name-taken", "", map[string]string {"": name},
}
}
if !validate.Name(name) {
return nil, &proto.Fail {
"invalid-name", "", map[string]string {"": name},
}
}
var u User
u.store = us
u.name = name
us.byName[validate.Fold(name)] = &u
u.id = us.world.NewObject(&u)
u.Channels = make(map[string]bool)
return &u, nil
}
func (us *UserStore) ByName(name string) *User {
return us.byName[validate.Fold(name)]
}
func (u *User) Name() string {
return u.name
}
func (u *User) Id() string {
return u.id
}
func (u *User) Rename(name string) *proto.Fail {
if !validate.Name(name) {
return &proto.Fail {
"invalid-name", "", map[string]string {"": name},
}
}
if validate.Fold(name) == validate.Fold(u.name) {
u.name = name
return nil
}
if u.store.ByName(name) != nil {
return &proto.Fail {
"name-taken", "", map[string]string {"": name},
}
}
u.store.byName[validate.Fold(u.name)] = nil
u.store.byName[validate.Fold(name)] = u
u.name = name
return nil
}
func (u *User) Delete() {
u.Stream.Event(proto.NewCmd("delete", u.id))
u.Stream.UnsubscribeAll()
delete(u.store.byName, validate.Fold(u.name))
u.store.world.RemoveObject(u.id)
gone := Tombstone {u.name}
u.store.gone[u.id] = gone
u.store.world.PutObject(u.id, gone)
}
func (u *User) InfoFor(uid string) proto.Object {
i := map[string]string {"": u.name}
if u.status != "" {
i["status"] = u.status
}
if u.Anonymous {
i["anonymous"] = "yes"
} else {
i["anonymous"] = "no"
}
return proto.Object {"u", u.id, i}
}
func (t Tombstone) GetInfo() proto.Object {
return proto.Object {
"gone", "", map[string]string {"": t.name, "kind": "u"},
}
}
|