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
|
package user
import (
"citrons.xyz/talk/server/object"
"citrons.xyz/talk/server/session"
"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
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},
}
}
var u User
u.store = us
u.name = name
us.byName[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[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 u.store.byName[name] != nil {
return &proto.Fail {
"name-taken", "", map[string]string {"": name},
}
}
u.store.byName[u.name] = nil
u.store.byName[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, 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) GetInfo() proto.Object {
return proto.Object {
"u", u.id, map[string]string {"": u.name},
}
}
func (t Tombstone) GetInfo() proto.Object {
return proto.Object {
"gone", "", map[string]string {"": t.name},
}
}
|