use uuid in connection, message. Add fetch connections function

This commit is contained in:
2026-04-07 20:06:06 +02:00
parent 9b57157769
commit 26fef0a777
4 changed files with 47 additions and 22 deletions
+35 -15
View File
@@ -2,8 +2,10 @@ package main
import ( import (
"context" "context"
"fmt"
"time" "time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool" "github.com/jackc/pgx/v5/pgxpool"
) )
@@ -17,6 +19,8 @@ func DbInit(ctx context.Context) {
panic(err) panic(err)
} }
_, err = dbConn.Exec(ctx, `CREATE EXTENSION IF NOT EXISTS "gen_random_uuid";`)
_, err = dbConn.Exec(ctx, ` _, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
@@ -35,11 +39,11 @@ func DbInit(ctx context.Context) {
_, err = dbConn.Exec(ctx, ` _, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS user_connections ( CREATE TABLE IF NOT EXISTS user_connections (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
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,
state TINYINT NOT NULL DEFAULT 0 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)
) )
`) `)
if err != nil { if err != nil {
@@ -48,12 +52,16 @@ func DbInit(ctx context.Context) {
_, err = dbConn.Exec(ctx, ` _, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS messages ( CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
sender_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE, sender_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
receiver_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, content TEXT NOT NULL,
is_group_message BOOLEAN DEFAULT FALSE is_group_message BOOLEAN DEFAULT FALSE
) )
`) `)
if err != nil {
panic(err)
}
_, err = dbConn.Exec(ctx, ` _, err = dbConn.Exec(ctx, `
CREATE TABLE IF NOT EXISTS chat_groups ( CREATE TABLE IF NOT EXISTS chat_groups (
@@ -158,25 +166,37 @@ func DbGroupSetColor(ctx context.Context, group *Group) error {
return err return err
} }
func DbConnectionSave(ctx context.Context, creationDate time.Time, requestorId uint32, recipientId uint32, isAccepted bool) error { func DbConnectionSave(ctx context.Context, conn *Connection) error {
_, err := dbConn.Exec(ctx, ` return dbConn.QueryRow(ctx, `
INSERT INTO user_connections (created_at, requestor_id, recipient_id, is_accepted) VALUES ($1, $2, $3, $4) INSERT INTO user_connections (requestor_id, recipient_id, state, created_at) VALUES ($1, $2, $3, $4)
`, creationDate, requestorId, recipientId, isAccepted) RETURNING id
return err `, conn.RequestorId, conn.RecipientId, conn.State, conn.CreatedAt).Scan(&conn.Id)
} }
func DbConnectionAccept(ctx context.Context, requestorId uint32, recipientId uint32) error { func DbConnectionGetBelongingToUser(ctx context.Context, user *User) error {
_, err := dbConn.Exec(ctx, ` rows, err := dbConn.Query(ctx, `
UPDATE user_connections SET is_accepted = true WHERE requestor_id = $1 AND recipient_id = $2 SELECT id, requestor_id, recipient_id, state, created_at
`, requestorId, recipientId) FROM connections
WHERE requestor_id = $1 OR recipient_id = $1
`, user.Id)
if err != nil {
return err return err
} }
defer rows.Close()
func DbConnectionDelete(ctx context.Context, requestorId uint32, recipientId uint32) error { if user.Connections == nil {
_, err := dbConn.Exec(ctx, ` user.Connections = make(map[uuid.UUID]*Connection)
DELETE FROM user_connections WHERE requestor_id = $1 AND recipient_id = $2 }
`, requestorId, recipientId)
return err for rows.Next() {
conn := &Connection{}
if err = rows.Scan(&conn.Id, &conn.RequestorId, &conn.RecipientId, &conn.State, &conn.CreatedAt); err != nil {
return fmt.Errorf("scanning connection row: %w", err)
}
user.Connections[conn.Id] = conn
}
return rows.Err()
} }
func DbGroupSave(ctx context.Context, group *Group) error { func DbGroupSave(ctx context.Context, group *Group) error {
+1
View File
@@ -9,6 +9,7 @@ require (
) )
require ( require (
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect
golang.org/x/sync v0.20.0 // indirect golang.org/x/sync v0.20.0 // indirect
) )
+5 -2
View File
@@ -4,6 +4,7 @@ import (
"time" "time"
"github.com/coder/websocket" "github.com/coder/websocket"
"github.com/google/uuid"
) )
type User struct { type User struct {
@@ -14,23 +15,25 @@ type User struct {
WsConn *websocket.Conn WsConn *websocket.Conn
Id uint32 Id uint32
Groups map[uint32]struct{} Groups map[uint32]struct{}
Connections map[uint32]*Connection Connections map[uuid.UUID]*Connection
Color [3]uint8 Color [3]uint8
} }
type Connection struct { type Connection struct {
Id uuid.UUID `json:"id"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
MessagesBuf [MaxDirectMsgCache]*Message `json:"-"` MessagesBuf [MaxDirectMsgCache]*Message `json:"-"`
Id uint32 `json:"id"`
RequestorId uint32 `json:"requestorId"` RequestorId uint32 `json:"requestorId"`
RecipientId uint32 `json:"recipientId"` RecipientId uint32 `json:"recipientId"`
State uint8 `json:"state"` State uint8 `json:"state"`
} }
type Message struct { type Message struct {
Id uuid.UUID `json:"id"`
Content string `json:"content"` Content string `json:"content"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
Sender uint32 `json:"sender"` Sender uint32 `json:"sender"`
IsGroupMessage bool `json:"isGroupMessage"`
} }
type Group struct { type Group struct {
+1
View File
@@ -1,5 +1,6 @@
chat history chat history
rewrite to use uuid
media support media support
fix color saving to use INT (002255255035) rgba fix color saving to use INT (002255255035) rgba