so much work to do :c
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
package ConnectionType
|
||||||
|
|
||||||
|
type ConnectionType uint8
|
||||||
|
|
||||||
|
const (
|
||||||
|
Stranger ConnectionType = iota
|
||||||
|
GroupFellow
|
||||||
|
Friend
|
||||||
|
GroupFriend
|
||||||
|
)
|
||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
CacheUsers = make(map[uint32]*User)
|
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
|
CacheUsers = make(map[uint32]*User)
|
||||||
Groups = make(map[uint32]*Group)
|
Groups = make(map[uint32]*Group)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -22,14 +22,6 @@ func CacheGetUserById(id uint32) (*User, error) {
|
|||||||
return user, nil
|
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) {
|
func CacheGetUserByName(name string) (*User, error) {
|
||||||
mu.RLock()
|
mu.RLock()
|
||||||
defer mu.RUnlock()
|
defer mu.RUnlock()
|
||||||
|
|||||||
+10
-40
@@ -37,7 +37,7 @@ func DbInit(ctx context.Context) {
|
|||||||
CREATE TABLE IF NOT EXISTS user_connections (
|
CREATE TABLE IF NOT EXISTS user_connections (
|
||||||
requestor_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
requestor_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
recipient_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(),
|
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||||
PRIMARY KEY (requestor_id, recipient_id)
|
PRIMARY KEY (requestor_id, recipient_id)
|
||||||
)
|
)
|
||||||
@@ -46,6 +46,15 @@ func DbInit(ctx context.Context) {
|
|||||||
panic(err)
|
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, `
|
_, err = dbConn.Exec(ctx, `
|
||||||
CREATE TABLE IF NOT EXISTS chat_groups (
|
CREATE TABLE IF NOT EXISTS chat_groups (
|
||||||
id SERIAL PRIMARY KEY,
|
id SERIAL PRIMARY KEY,
|
||||||
@@ -127,45 +136,6 @@ func DbUserGetGroups(ctx context.Context, user *User) error {
|
|||||||
return rows.Err()
|
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 {
|
func DbUserSetColor(ctx context.Context, user *User) error {
|
||||||
_, err := dbConn.Exec(ctx, `
|
_, err := dbConn.Exec(ctx, `
|
||||||
UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
UPDATE users SET color_red = $1, color_green = $2, color_blue = $3 WHERE id = $4
|
||||||
|
|||||||
+2
-1
@@ -3,5 +3,6 @@ package main
|
|||||||
const (
|
const (
|
||||||
MaxGroupsForUser uint32 = 8
|
MaxGroupsForUser uint32 = 8
|
||||||
MaxUsersInGroup uint32 = 12
|
MaxUsersInGroup uint32 = 12
|
||||||
DirectMsgMaxCache
|
MaxDirectMsgCache uint32 = 12
|
||||||
|
MessagesPartitions uint8 = 2
|
||||||
)
|
)
|
||||||
|
|||||||
+8
-1
@@ -20,10 +20,17 @@ type User struct {
|
|||||||
|
|
||||||
type Connection struct {
|
type Connection struct {
|
||||||
CreatedAt time.Time `json:"createdAt"`
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
MessagesBuf [MaxDirectMsgCache]*Message `json:"-"`
|
||||||
Id uint32 `json:"id"`
|
Id uint32 `json:"id"`
|
||||||
RequestorId uint32 `json:"requestorId"`
|
RequestorId uint32 `json:"requestorId"`
|
||||||
RecipientId uint32 `json:"recipientId"`
|
RecipientId uint32 `json:"recipientId"`
|
||||||
MessagesBuf
|
State uint8 `json:"state"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Message struct {
|
||||||
|
Content string `json:"content"`
|
||||||
|
CreatedAt time.Time `json:"createdAt"`
|
||||||
|
Sender uint32 `json:"sender"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Group struct {
|
type Group struct {
|
||||||
|
|||||||
Reference in New Issue
Block a user