diff --git a/Enums/ConnectionType/ConnectionType.go b/Enums/ConnectionType/ConnectionType.go new file mode 100644 index 0000000..ba5fceb --- /dev/null +++ b/Enums/ConnectionType/ConnectionType.go @@ -0,0 +1,10 @@ +package ConnectionType + +type ConnectionType uint8 + +const ( + Stranger ConnectionType = iota + GroupFellow + Friend + GroupFriend +) diff --git a/cache.go b/cache.go index 8462f47..ff3ca7e 100644 --- a/cache.go +++ b/cache.go @@ -6,8 +6,8 @@ import ( ) var ( - CacheUsers = make(map[uint32]*User) mu sync.RWMutex + CacheUsers = make(map[uint32]*User) Groups = make(map[uint32]*Group) ) @@ -22,14 +22,6 @@ func CacheGetUserById(id uint32) (*User, error) { return user, nil } -func CacheGetIdByName(name string) (uint32, error) { - user, err := CacheGetUserByName(name) - if err != nil { - return 0, err - } - return user.Id, nil -} - func CacheGetUserByName(name string) (*User, error) { mu.RLock() defer mu.RUnlock() diff --git a/database.go b/database.go index d9008b8..69df436 100644 --- a/database.go +++ b/database.go @@ -37,7 +37,7 @@ func DbInit(ctx context.Context) { CREATE TABLE IF NOT EXISTS user_connections ( requestor_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, recipient_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, - is_accepted BOOLEAN NOT NULL DEFAULT FALSE, + state TINYINT NOT NULL DEFAULT 0 created_at TIMESTAMP NOT NULL DEFAULT NOW(), PRIMARY KEY (requestor_id, recipient_id) ) @@ -46,6 +46,15 @@ func DbInit(ctx context.Context) { panic(err) } + _, err = dbConn.Exec(ctx, ` + CREATE TABLE IF NOT EXISTS messages ( + sender_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + receiver_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, + content TEXT NOT NULL, + is_group_message BOOLEAN DEFAULT FALSE + ) + `) + _, err = dbConn.Exec(ctx, ` CREATE TABLE IF NOT EXISTS chat_groups ( id SERIAL PRIMARY KEY, @@ -127,45 +136,6 @@ func DbUserGetGroups(ctx context.Context, user *User) error { return rows.Err() } -func DbUserGetConnections(ctx context.Context, user *User) error { - rows, err := dbConn.Query(ctx, ` - SELECT - CASE WHEN requestor_id = $1 THEN recipient_id ELSE requestor_id END AS other_id, - requestor_id = $1 AS is_from_user, - is_accepted, - created_at - FROM user_connections - WHERE requestor_id = $1 OR recipient_id = $1 - `, user.Id) - if err != nil { - return err - } - - user.Connections = make(map[uint32]*Connection) - defer rows.Close() - for rows.Next() { - var ( - otherId uint32 - isFromUser bool - isAccepted bool - createdAt time.Time - ) - err = rows.Scan(&otherId, &isFromUser, &isAccepted, &createdAt) - if err != nil { - return err - } - - user.Connections[otherId] = &Connection{ - CreatedAt: createdAt, - With: otherId, - IsFromUser: isFromUser, - IsAccepted: isAccepted, - } - } - - return rows.Err() -} - func DbUserSetColor(ctx context.Context, user *User) error { _, err := dbConn.Exec(ctx, ` UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4 diff --git a/globals.go b/globals.go index efe7c64..835b257 100644 --- a/globals.go +++ b/globals.go @@ -1,7 +1,8 @@ package main const ( - MaxGroupsForUser uint32 = 8 - MaxUsersInGroup uint32 = 12 - DirectMsgMaxCache + MaxGroupsForUser uint32 = 8 + MaxUsersInGroup uint32 = 12 + MaxDirectMsgCache uint32 = 12 + MessagesPartitions uint8 = 2 ) diff --git a/structs.go b/structs.go index 8be5b0d..e8fac2f 100644 --- a/structs.go +++ b/structs.go @@ -19,11 +19,18 @@ type User struct { } type Connection struct { - CreatedAt time.Time `json:"createdAt"` - Id uint32 `json:"id"` - RequestorId uint32 `json:"requestorId"` - RecipientId uint32 `json:"recipientId"` - MessagesBuf + CreatedAt time.Time `json:"createdAt"` + MessagesBuf [MaxDirectMsgCache]*Message `json:"-"` + Id uint32 `json:"id"` + RequestorId uint32 `json:"requestorId"` + RecipientId uint32 `json:"recipientId"` + State uint8 `json:"state"` +} + +type Message struct { + Content string `json:"content"` + CreatedAt time.Time `json:"createdAt"` + Sender uint32 `json:"sender"` } type Group struct { diff --git a/todo.txt b/todo.txt index b25583e..106cc19 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,8 @@ -media support + chat history +media support + +fix color saving to use INT (002255255035) rgba more user customization (avatar, banner, desc)