rewrite connections, messages system, add dm message history, fix multiple bugs, update machine-client
This commit is contained in:
@@ -3,12 +3,14 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var (
|
||||
mu sync.RWMutex
|
||||
CacheUsers = make(map[uint32]*User)
|
||||
Groups = make(map[uint32]*Group)
|
||||
mu sync.RWMutex
|
||||
CacheUsers = make(map[uint32]*User)
|
||||
CacheGroups = make(map[uint32]*Group)
|
||||
)
|
||||
|
||||
func CacheGetUserById(id uint32) (*User, error) {
|
||||
@@ -52,14 +54,53 @@ func CacheSaveGroup(group *Group) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
|
||||
Groups[group.Id] = group
|
||||
CacheGroups[group.Id] = group
|
||||
}
|
||||
|
||||
func CacheDeleteGroup(id uint32) {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
delete(CacheGroups, id)
|
||||
}
|
||||
|
||||
func CacheAddConnection(a, b *User, conn *Connection) {
|
||||
first, second := a, b
|
||||
if a.Id > b.Id {
|
||||
first, second = b, a
|
||||
}
|
||||
first.Mu.Lock()
|
||||
second.Mu.Lock()
|
||||
a.Connections[conn.Id] = conn
|
||||
b.Connections[conn.Id] = conn
|
||||
second.Mu.Unlock()
|
||||
first.Mu.Unlock()
|
||||
}
|
||||
|
||||
func CacheDeleteConnection(a, b *User, id uuid.UUID) {
|
||||
first, second := a, b
|
||||
if a.Id > b.Id {
|
||||
first, second = b, a
|
||||
}
|
||||
first.Mu.Lock()
|
||||
second.Mu.Lock()
|
||||
delete(a.Connections, id)
|
||||
delete(b.Connections, id)
|
||||
second.Mu.Unlock()
|
||||
first.Mu.Unlock()
|
||||
}
|
||||
|
||||
func CacheGetConnection(user *User, id uuid.UUID) (*Connection, bool) {
|
||||
user.Mu.RLock()
|
||||
defer user.Mu.RUnlock()
|
||||
conn, ok := user.Connections[id]
|
||||
return conn, ok
|
||||
}
|
||||
|
||||
func CacheGetGroup(id uint32) (*Group, error) {
|
||||
mu.RLock()
|
||||
defer mu.RUnlock()
|
||||
|
||||
group, ok := Groups[id]
|
||||
group, ok := CacheGroups[id]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("group %d not found", id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user