diff --git a/database/message.go b/database/message.go index 0dc66ba..c1e2a54 100644 --- a/database/message.go +++ b/database/message.go @@ -63,9 +63,24 @@ const ( SELECT conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status FROM message WHERE mxid=$1 ` - deleteAllInChat = ` + deleteAllMessagesInChatQuery = ` DELETE FROM message WHERE conv_id=$1 AND conv_receiver=$2 ` + insertMessageQuery = ` + INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) + ` + massInsertMessageQueryPrefix = ` + INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status) + VALUES + ` + updateMessageQuery = ` + UPDATE message + SET conv_id=$1, mxid=$4, mx_room=$5, sender=$6, timestamp=$7, status=$8 + WHERE conv_receiver=$2 AND id=$3 + ` + updateMessageStatusQuery = "UPDATE message SET status=$1, timestamp=$2 WHERE conv_receiver=$3 AND id=$4" + deleteMessageQuery = "DELETE FROM message WHERE conv_id=$1 AND conv_receiver=$2 AND id=$3" ) func (mq *MessageQuery) GetByID(ctx context.Context, receiver int, messageID string) (*Message, error) { @@ -85,7 +100,7 @@ func (mq *MessageQuery) GetLastInChatWithMXID(ctx context.Context, chat Key) (*M } func (mq *MessageQuery) DeleteAllInChat(ctx context.Context, chat Key) error { - _, err := mq.db.Conn(ctx).ExecContext(ctx, deleteAllInChat, chat.ID, chat.Receiver) + _, err := mq.db.Conn(ctx).ExecContext(ctx, deleteAllMessagesInChatQuery, chat.ID, chat.Receiver) return err } @@ -147,10 +162,7 @@ func (msg *Message) sqlVariables() []any { } func (msg *Message) Insert(ctx context.Context) error { - _, err := msg.db.Conn(ctx).ExecContext(ctx, ` - INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8) - `, msg.sqlVariables()...) + _, err := msg.db.Conn(ctx).ExecContext(ctx, insertMessageQuery, msg.sqlVariables()...) return err } @@ -173,30 +185,23 @@ func (mq *MessageQuery) MassInsert(ctx context.Context, messages []*Message) err params[baseIndex+4] = dbutil.JSON{Data: &msg.Status} placeholders[i] = fmt.Sprintf(valueStringFormat, baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4, baseIndex+5) } - query := ` - INSERT INTO message (conv_id, conv_receiver, id, mxid, mx_room, sender, timestamp, status) - VALUES - ` + strings.Join(placeholders, ",") + query := massInsertMessageQueryPrefix + strings.Join(placeholders, ",") _, err := mq.db.Conn(ctx).ExecContext(ctx, query, params...) return err } func (msg *Message) Update(ctx context.Context) error { - _, err := msg.db.Conn(ctx).ExecContext(ctx, ` - UPDATE message - SET conv_id=$1, mxid=$4, mx_room=$5, sender=$6, timestamp=$7, status=$8 - WHERE conv_receiver=$2 AND id=$3 - `, msg.sqlVariables()...) + _, err := msg.db.Conn(ctx).ExecContext(ctx, updateMessageQuery, msg.sqlVariables()...) return err } func (msg *Message) UpdateStatus(ctx context.Context) error { - _, err := msg.db.Conn(ctx).ExecContext(ctx, "UPDATE message SET status=$1, timestamp=$2 WHERE conv_receiver=$3 AND id=$4", dbutil.JSON{Data: &msg.Status}, msg.Timestamp.UnixMicro(), msg.Chat.Receiver, msg.ID) + _, err := msg.db.Conn(ctx).ExecContext(ctx, updateMessageStatusQuery, dbutil.JSON{Data: &msg.Status}, msg.Timestamp.UnixMicro(), msg.Chat.Receiver, msg.ID) return err } func (msg *Message) Delete(ctx context.Context) error { - _, err := msg.db.Conn(ctx).ExecContext(ctx, "DELETE FROM message WHERE conv_id=$1 AND conv_receiver=$2 AND id=$3", msg.Chat.ID, msg.Chat.Receiver, msg.ID) + _, err := msg.db.Conn(ctx).ExecContext(ctx, deleteMessageQuery, msg.Chat.ID, msg.Chat.Receiver, msg.ID) return err } diff --git a/database/portal.go b/database/portal.go index 94a0c8d..5201b5a 100644 --- a/database/portal.go +++ b/database/portal.go @@ -43,24 +43,42 @@ func (pq *PortalQuery) getDB() *Database { return pq.db } +const ( + getAllPortalsQuery = "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal" + getAllPortalsForUserQuery = getAllPortalsQuery + " WHERE receiver=$1" + getPortalByKeyQuery = getAllPortalsQuery + " WHERE id=$1 AND receiver=$2" + getPortalByOtherUserQuery = getAllPortalsQuery + " WHERE other_user=$1 AND receiver=$2" + getPortalByMXIDQuery = getAllPortalsQuery + " WHERE mxid=$1" + insertPortalQuery = ` + INSERT INTO portal (id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) + ` + updatePortalQuery = ` + UPDATE portal + SET self_user=$3, other_user=$4, type=$5, mxid=$6, name=$7, name_set=$8, encrypted=$9, in_space=$10 + WHERE id=$1 AND receiver=$2 + ` + deletePortalQuery = "DELETE FROM portal WHERE id=$1 AND receiver=$2" +) + func (pq *PortalQuery) GetAll(ctx context.Context) ([]*Portal, error) { - return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal") + return getAll[*Portal](pq, ctx, getAllPortalsQuery) } func (pq *PortalQuery) GetAllForUser(ctx context.Context, receiver int) ([]*Portal, error) { - return getAll[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE receiver=$1", receiver) + return getAll[*Portal](pq, ctx, getAllPortalsForUserQuery, receiver) } func (pq *PortalQuery) GetByKey(ctx context.Context, key Key) (*Portal, error) { - return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE id=$1 AND receiver=$2", key.ID, key.Receiver) + return get[*Portal](pq, ctx, getPortalByKeyQuery, key.ID, key.Receiver) } func (pq *PortalQuery) GetByOtherUser(ctx context.Context, key Key) (*Portal, error) { - return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE other_user=$1 AND receiver=$2", key.ID, key.Receiver) + return get[*Portal](pq, ctx, getPortalByOtherUserQuery, key.ID, key.Receiver) } func (pq *PortalQuery) GetByMXID(ctx context.Context, mxid id.RoomID) (*Portal, error) { - return get[*Portal](pq, ctx, "SELECT id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space FROM portal WHERE mxid=$1", mxid) + return get[*Portal](pq, ctx, getPortalByMXIDQuery, mxid) } type Key struct { @@ -125,23 +143,16 @@ func (portal *Portal) sqlVariables() []any { } func (portal *Portal) Insert(ctx context.Context) error { - _, err := portal.db.Conn(ctx).ExecContext(ctx, ` - INSERT INTO portal (id, receiver, self_user, other_user, type, mxid, name, name_set, encrypted, in_space) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) - `, portal.sqlVariables()...) + _, err := portal.db.Conn(ctx).ExecContext(ctx, insertPortalQuery, portal.sqlVariables()...) return err } func (portal *Portal) Update(ctx context.Context) error { - _, err := portal.db.Conn(ctx).ExecContext(ctx, ` - UPDATE portal - SET self_user=$3, other_user=$4, type=$5, mxid=$6, name=$7, name_set=$8, encrypted=$9, in_space=$10 - WHERE id=$1 AND receiver=$2 - `, portal.sqlVariables()...) + _, err := portal.db.Conn(ctx).ExecContext(ctx, updatePortalQuery, portal.sqlVariables()...) return err } func (portal *Portal) Delete(ctx context.Context) error { - _, err := portal.db.Conn(ctx).ExecContext(ctx, "DELETE FROM portal WHERE id=$1 AND receiver=$2", portal.ID, portal.Receiver) + _, err := portal.db.Conn(ctx).ExecContext(ctx, deletePortalQuery, portal.ID, portal.Receiver) return err } diff --git a/database/puppet.go b/database/puppet.go index 2d8f2b6..90a63dc 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -36,21 +36,31 @@ func (pq *PuppetQuery) New() *Puppet { } } +const ( + deleteAllPuppetsForUserQuery = "DELETE FROM puppet WHERE receiver=$1" + getPuppetQuery = "SELECT id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set FROM puppet WHERE id=$1 AND receiver=$2" + insertPuppetQuery = ` + INSERT INTO puppet (id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) + ` + updatePuppetQuery = ` + UPDATE puppet + SET phone=$3, contact_id=$4, name=$5, name_set=$6, avatar_hash=$7, avatar_mxc=$8, avatar_set=$9, avatar_update_ts=$10, contact_info_set=$11 + WHERE id=$1 AND receiver=$2 + ` +) + func (pq *PuppetQuery) getDB() *Database { return pq.db } -func (pq *PuppetQuery) GetAll(ctx context.Context) ([]*Puppet, error) { - return getAll[*Puppet](pq, ctx, "SELECT id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set FROM puppet") -} - func (pq *PuppetQuery) DeleteAllForUser(ctx context.Context, userID int) error { - _, err := pq.db.Conn(ctx).ExecContext(ctx, "DELETE FROM puppet WHERE receiver=$1", userID) + _, err := pq.db.Conn(ctx).ExecContext(ctx, deleteAllPuppetsForUserQuery, userID) return err } func (pq *PuppetQuery) Get(ctx context.Context, key Key) (*Puppet, error) { - return get[*Puppet](pq, ctx, "SELECT id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set FROM puppet WHERE id=$1 AND receiver=$2", key.ID, key.Receiver) + return get[*Puppet](pq, ctx, getPuppetQuery, key.ID, key.Receiver) } type Puppet struct { @@ -89,18 +99,11 @@ func (puppet *Puppet) sqlVariables() []any { } func (puppet *Puppet) Insert(ctx context.Context) error { - _, err := puppet.db.Conn(ctx).ExecContext(ctx, ` - INSERT INTO puppet (id, receiver, phone, contact_id, name, name_set, avatar_hash, avatar_mxc, avatar_set, avatar_update_ts, contact_info_set) - VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) - `, puppet.sqlVariables()...) + _, err := puppet.db.Conn(ctx).ExecContext(ctx, insertPuppetQuery, puppet.sqlVariables()...) return err } func (puppet *Puppet) Update(ctx context.Context) error { - _, err := puppet.db.Conn(ctx).ExecContext(ctx, ` - UPDATE puppet - SET phone=$3, contact_id=$4, name=$5, name_set=$6, avatar_hash=$7, avatar_mxc=$8, avatar_set=$9, avatar_update_ts=$10, contact_info_set=$11 - WHERE id=$1 AND receiver=$2 - `, puppet.sqlVariables()...) + _, err := puppet.db.Conn(ctx).ExecContext(ctx, updatePuppetQuery, puppet.sqlVariables()...) return err } diff --git a/database/reaction.go b/database/reaction.go index dbf0a81..dae327d 100644 --- a/database/reaction.go +++ b/database/reaction.go @@ -58,12 +58,13 @@ const ( SELECT conv_id, conv_receiver, msg_id, sender, reaction, mxid FROM reaction WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3 ` - insertReaction = ` + insertReactionQuery = ` INSERT INTO reaction (conv_id, conv_receiver, msg_id, sender, reaction, mxid) VALUES ($1, $2, $3, $4, $5, $6) ON CONFLICT (conv_receiver, msg_id, sender) DO UPDATE SET reaction=excluded.reaction, mxid=excluded.mxid ` + deleteReactionQuery = "DELETE FROM reaction WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3 AND sender=$4" ) func (rq *ReactionQuery) GetByID(ctx context.Context, receiver int, messageID, sender string) (*Reaction, error) { @@ -104,7 +105,7 @@ func (r *Reaction) Scan(row dbutil.Scannable) (*Reaction, error) { } func (r *Reaction) Insert(ctx context.Context) error { - _, err := r.db.Conn(ctx).ExecContext(ctx, insertReaction, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender, r.Reaction, r.MXID) + _, err := r.db.Conn(ctx).ExecContext(ctx, insertReactionQuery, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender, r.Reaction, r.MXID) return err } @@ -125,12 +126,12 @@ func (rq *ReactionQuery) MassInsert(ctx context.Context, reactions []*Reaction) params[baseIndex+3] = msg.MXID placeholders[i] = fmt.Sprintf(valueStringFormat, baseIndex+1, baseIndex+2, baseIndex+3, baseIndex+4) } - query := strings.Replace(insertReaction, "($1, $2, $3, $4, $5, $6)", strings.Join(placeholders, ","), 1) + query := strings.Replace(insertReactionQuery, "($1, $2, $3, $4, $5, $6)", strings.Join(placeholders, ","), 1) _, err := rq.db.Conn(ctx).ExecContext(ctx, query, params...) return err } func (r *Reaction) Delete(ctx context.Context) error { - _, err := r.db.Conn(ctx).ExecContext(ctx, "DELETE FROM reaction WHERE conv_id=$1 AND conv_receiver=$2 AND msg_id=$3 AND sender=$4", r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender) + _, err := r.db.Conn(ctx).ExecContext(ctx, deleteReactionQuery, r.Chat.ID, r.Chat.Receiver, r.MessageID, r.Sender) return err } diff --git a/database/user.go b/database/user.go index 913c095..14a2d1c 100644 --- a/database/user.go +++ b/database/user.go @@ -46,20 +46,39 @@ func (uq *UserQuery) getDB() *Database { return uq.db } +const ( + getUserBaseQuery = `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user"` + getAllUsersWithSessionQuery = getUserBaseQuery + " WHERE session IS NOT NULL" + getAllUsersWithDoublePuppetQuery = getUserBaseQuery + " WHERE access_token<>''" + getUserByRowIDQuery = getUserBaseQuery + " WHERE rowid=$1" + getUserByMXIDQuery = getUserBaseQuery + " WHERE mxid=$1" + + insertUserQuery = ` + INSERT INTO "user" (mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING rowid + ` + updateUserQuery = ` + UPDATE "user" + SET phone_id=$2, session=$3, self_participant_ids=$4, sim_metadata=$5, settings=$6, + management_room=$7, space_room=$8, access_token=$9 + WHERE mxid=$1 + ` +) + func (uq *UserQuery) GetAllWithSession(ctx context.Context) ([]*User, error) { - return getAll[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE session IS NOT NULL`) + return getAll[*User](uq, ctx, getAllUsersWithSessionQuery) } func (uq *UserQuery) GetAllWithDoublePuppet(ctx context.Context) ([]*User, error) { - return getAll[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE access_token<>''`) + return getAll[*User](uq, ctx, getAllUsersWithDoublePuppetQuery) } func (uq *UserQuery) GetByRowID(ctx context.Context, rowID int) (*User, error) { - return get[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE rowid=$1`, rowID) + return get[*User](uq, ctx, getUserByRowIDQuery, rowID) } func (uq *UserQuery) GetByMXID(ctx context.Context, userID id.UserID) (*User, error) { - return get[*User](uq, ctx, `SELECT rowid, mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token FROM "user" WHERE mxid=$1`, userID) + return get[*User](uq, ctx, getUserByMXIDQuery, userID) } type Settings struct { @@ -257,12 +276,12 @@ func (user *User) AddSelfParticipantID(ctx context.Context, id string) error { func (user *User) Insert(ctx context.Context) error { err := user.db.Conn(ctx). - QueryRowContext(ctx, `INSERT INTO "user" (mxid, phone_id, session, self_participant_ids, sim_metadata, settings, management_room, space_room, access_token) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING rowid`, user.sqlVariables()...). + QueryRowContext(ctx, insertUserQuery, user.sqlVariables()...). Scan(&user.RowID) return err } func (user *User) Update(ctx context.Context) error { - _, err := user.db.Conn(ctx).ExecContext(ctx, `UPDATE "user" SET phone_id=$2, session=$3, self_participant_ids=$4, sim_metadata=$5, settings=$6, management_room=$7, space_room=$8, access_token=$9 WHERE mxid=$1`, user.sqlVariables()...) + _, err := user.db.Conn(ctx).ExecContext(ctx, updateUserQuery, user.sqlVariables()...) return err } diff --git a/puppet.go b/puppet.go index 09b62d0..f0dd664 100644 --- a/puppet.go +++ b/puppet.go @@ -120,10 +120,6 @@ func (puppet *Puppet) GetMXID() id.UserID { return puppet.MXID } -func (br *GMBridge) GetAllPuppets() []*Puppet { - return br.loadManyPuppets(br.DB.Puppet.GetAll) -} - func (br *GMBridge) loadManyPuppets(query func(ctx context.Context) ([]*database.Puppet, error)) []*Puppet { br.puppetsLock.Lock() defer br.puppetsLock.Unlock()