rewrite connections, messages system, add dm message history, fix multiple bugs, update machine-client
This commit is contained in:
+20
-2
@@ -20,6 +20,9 @@ func DbInit(ctx context.Context) {
|
||||
}
|
||||
|
||||
_, err = dbConn.Exec(ctx, `CREATE EXTENSION IF NOT EXISTS "gen_random_uuid";`)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
_, err = dbConn.Exec(ctx, `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
@@ -55,6 +58,7 @@ func DbInit(ctx context.Context) {
|
||||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||||
sender_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
receiver_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
|
||||
content TEXT NOT NULL,
|
||||
is_group_message BOOLEAN DEFAULT FALSE
|
||||
)
|
||||
@@ -110,7 +114,7 @@ func DbUserDelete(ctx context.Context, id uint32) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func DbUserGetByName(ctx context.Context, user *User) error {
|
||||
func DbUserGetStandardInfoByName(ctx context.Context, user *User) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
SELECT id, name, pass_hash, pronouns, color_red, color_green, color_blue, created_at FROM users WHERE name = $1
|
||||
`, user.Name).Scan(&user.Id, &user.Name, &user.PasswordHash, &user.Pronouns, &user.Color[0], &user.Color[1], &user.Color[2], &user.CreatedAt)
|
||||
@@ -173,6 +177,13 @@ func DbConnectionSave(ctx context.Context, conn *Connection) error {
|
||||
`, conn.RequestorId, conn.RecipientId, conn.State, conn.CreatedAt).Scan(&conn.Id)
|
||||
}
|
||||
|
||||
func DbConnectionDelete(ctx context.Context, conn *Connection) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
DELETE FROM user_connections WHERE id = $1
|
||||
`, conn.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbConnectionsGetBelongingToUser(ctx context.Context, user *User) error {
|
||||
rows, err := dbConn.Query(ctx, `
|
||||
SELECT id, requestor_id, recipient_id, state, created_at
|
||||
@@ -198,13 +209,20 @@ func DbConnectionsGetBelongingToUser(ctx context.Context, user *User) error {
|
||||
return rows.Err()
|
||||
}
|
||||
|
||||
func DbConnectionSet(ctx context.Context, conn *Connection) error {
|
||||
func DbConnectionUpdateState(ctx context.Context, conn *Connection) error {
|
||||
_, err := dbConn.Exec(ctx, `
|
||||
UPDATE user_connections SET state = $1 WHERE id = $2
|
||||
`, conn.State, conn.Id)
|
||||
return err
|
||||
}
|
||||
|
||||
func DbMessageSave(ctx context.Context, message *Message) error {
|
||||
return dbConn.QueryRow(ctx, `
|
||||
INSERT INTO messages (sender_id, receiver_id, created_at, content, is_group_message) VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id
|
||||
`, message.Sender, message.Receiver, message.CreatedAt, message.Content, message.IsGroupMessage).Scan(&message.Id)
|
||||
}
|
||||
|
||||
func DbGroupSave(ctx context.Context, group *Group) error {
|
||||
err := dbConn.QueryRow(ctx, `
|
||||
INSERT INTO chat_groups (name, creator_id, owner_id, enable_client_colors, color_red, color_green, color_blue, created_at)
|
||||
|
||||
Reference in New Issue
Block a user