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 (
|
||||
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()
|
||||
|
||||
+10
-40
@@ -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
|
||||
|
||||
+4
-3
@@ -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
|
||||
)
|
||||
|
||||
+12
-5
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user