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/user.go | 95 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 server/user/user.go (limited to 'server/user/user.go') diff --git a/server/user/user.go b/server/user/user.go new file mode 100644 index 0000000..41a5c4f --- /dev/null +++ b/server/user/user.go @@ -0,0 +1,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}, + } +} -- cgit v1.2.3