From daf0faab61d1abae828ef002c11cd4c2260967b2 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 16:33:42 +0100 Subject: [PATCH] Fixed many database issues --- database/message.go | 14 +++++++------- database/portal.go | 15 +++++++-------- database/puppet.go | 8 ++++---- database/user.go | 10 +++++----- go.mod | 1 - main.go | 1 + 6 files changed, 24 insertions(+), 25 deletions(-) diff --git a/database/message.go b/database/message.go index c8f6f9e..5513df3 100644 --- a/database/message.go +++ b/database/message.go @@ -35,11 +35,11 @@ type MessageQuery struct { func (mq *MessageQuery) CreateTable() error { _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( - chat_jid VARCHAR(25), - chat_receiver VARCHAR(25), + chat_jid VARCHAR(255), + chat_receiver VARCHAR(255), jid VARCHAR(255), mxid VARCHAR(255) NOT NULL UNIQUE, - sender VARCHAR(25) NOT NULL, + sender VARCHAR(255) NOT NULL, content bytea NOT NULL, PRIMARY KEY (chat_jid, chat_receiver, jid), @@ -56,7 +56,7 @@ func (mq *MessageQuery) New() *Message { } func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) { - rows, err := mq.db.Query("SELECT * FROM message WHERE chat_jid=? AND chat_receiver=?", chat.JID, chat.Receiver) + rows, err := mq.db.Query("SELECT * FROM message WHERE chat_jid=$1 AND chat_receiver=$2", chat.JID, chat.Receiver) if err != nil || rows == nil { return nil } @@ -68,11 +68,11 @@ func (mq *MessageQuery) GetAll(chat PortalKey) (messages []*Message) { } func (mq *MessageQuery) GetByJID(chat PortalKey, jid types.WhatsAppMessageID) *Message { - return mq.get("SELECT * FROM message WHERE chat_jid=? AND chat_receiver=? AND jid=?", chat.JID, chat.Receiver, jid) + return mq.get("SELECT * FROM message WHERE chat_jid=$1 AND chat_receiver=$2 AND jid=$3", chat.JID, chat.Receiver, jid) } func (mq *MessageQuery) GetByMXID(mxid types.MatrixEventID) *Message { - return mq.get("SELECT * FROM message WHERE mxid=?", mxid) + return mq.get("SELECT * FROM message WHERE mxid=$1", mxid) } func (mq *MessageQuery) get(query string, args ...interface{}) *Message { @@ -130,7 +130,7 @@ func (msg *Message) encodeBinaryContent() []byte { } func (msg *Message) Insert() { - _, err := msg.db.Exec("INSERT INTO message VALUES (?, ?, ?, ?, ?, ?)", msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, msg.Sender, msg.encodeBinaryContent()) + _, err := msg.db.Exec("INSERT INTO message VALUES ($1, $2, $3, $4, $5, $6)", msg.Chat.JID, msg.Chat.Receiver, msg.JID, msg.MXID, msg.Sender, msg.encodeBinaryContent()) if err != nil { msg.log.Warnfln("Failed to insert %s@%s: %v", msg.Chat, msg.JID, err) } diff --git a/database/portal.go b/database/portal.go index ffb4318..8bfffdb 100644 --- a/database/portal.go +++ b/database/portal.go @@ -61,16 +61,15 @@ type PortalQuery struct { func (pq *PortalQuery) CreateTable() error { _, err := pq.db.Exec(`CREATE TABLE IF NOT EXISTS portal ( - jid VARCHAR(25), - receiver VARCHAR(25), + jid VARCHAR(255), + receiver VARCHAR(255), mxid VARCHAR(255) UNIQUE, name VARCHAR(255) NOT NULL, topic VARCHAR(255) NOT NULL, avatar VARCHAR(255) NOT NULL, - PRIMARY KEY (jid, receiver), - FOREIGN KEY (receiver) REFERENCES whatsapp_user(mxid) + PRIMARY KEY (jid, receiver) )`) return err } @@ -95,11 +94,11 @@ func (pq *PortalQuery) GetAll() (portals []*Portal) { } func (pq *PortalQuery) GetByJID(key PortalKey) *Portal { - return pq.get("SELECT * FROM portal WHERE jid=? AND receiver=?", key.JID, key.Receiver) + return pq.get("SELECT * FROM portal WHERE jid=$1 AND receiver=$2", key.JID, key.Receiver) } func (pq *PortalQuery) GetByMXID(mxid types.MatrixRoomID) *Portal { - return pq.get("SELECT * FROM portal WHERE mxid=?", mxid) + return pq.get("SELECT * FROM portal WHERE mxid=$1", mxid) } func (pq *PortalQuery) get(query string, args ...interface{}) *Portal { @@ -143,7 +142,7 @@ func (portal *Portal) mxidPtr() *string { } func (portal *Portal) Insert() { - _, err := portal.db.Exec("INSERT INTO portal VALUES (?, ?, ?, ?, ?, ?)", + _, err := portal.db.Exec("INSERT INTO portal VALUES ($1, $2, $3, $4, $5, $6)", portal.Key.JID, portal.Key.Receiver, portal.mxidPtr(), portal.Name, portal.Topic, portal.Avatar) if err != nil { portal.log.Warnfln("Failed to insert %s: %v", portal.Key, err) @@ -155,7 +154,7 @@ func (portal *Portal) Update() { if len(portal.MXID) > 0 { mxid = &portal.MXID } - _, err := portal.db.Exec("UPDATE portal SET mxid=?, name=?, topic=?, avatar=? WHERE jid=? AND receiver=?", + _, err := portal.db.Exec("UPDATE portal SET mxid=$1, name=$2, topic=$3, avatar=$4 WHERE jid=$5 AND receiver=$6", mxid, portal.Name, portal.Topic, portal.Avatar, portal.Key.JID, portal.Key.Receiver) if err != nil { portal.log.Warnfln("Failed to update %s: %v", portal.Key, err) diff --git a/database/puppet.go b/database/puppet.go index bde6116..b6637f8 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -31,7 +31,7 @@ type PuppetQuery struct { func (pq *PuppetQuery) CreateTable() error { _, err := pq.db.Exec(`CREATE TABLE IF NOT EXISTS puppet ( - jid VARCHAR(25) PRIMARY KEY, + jid VARCHAR(255) PRIMARY KEY, avatar VARCHAR(255), displayname VARCHAR(255), name_quality SMALLINT @@ -59,7 +59,7 @@ func (pq *PuppetQuery) GetAll() (puppets []*Puppet) { } func (pq *PuppetQuery) Get(jid types.WhatsAppID) *Puppet { - row := pq.db.QueryRow("SELECT * FROM puppet WHERE jid=?", jid) + row := pq.db.QueryRow("SELECT * FROM puppet WHERE jid=$1", jid) if row == nil { return nil } @@ -93,7 +93,7 @@ func (puppet *Puppet) Scan(row Scannable) *Puppet { } func (puppet *Puppet) Insert() { - _, err := puppet.db.Exec("INSERT INTO puppet VALUES (?, ?, ?, ?)", + _, err := puppet.db.Exec("INSERT INTO puppet VALUES ($1, $2, $3, $4)", puppet.JID, puppet.Avatar, puppet.Displayname, puppet.NameQuality) if err != nil { puppet.log.Warnfln("Failed to insert %s: %v", puppet.JID, err) @@ -101,7 +101,7 @@ func (puppet *Puppet) Insert() { } func (puppet *Puppet) Update() { - _, err := puppet.db.Exec("UPDATE puppet SET displayname=?, name_quality=?, avatar=? WHERE jid=?", + _, err := puppet.db.Exec("UPDATE puppet SET displayname=$1, name_quality=$2, avatar=$3 WHERE jid=$4", puppet.Displayname, puppet.NameQuality, puppet.Avatar, puppet.JID) if err != nil { puppet.log.Warnfln("Failed to update %s->%s: %v", puppet.JID, err) diff --git a/database/user.go b/database/user.go index 2f09555..f871b67 100644 --- a/database/user.go +++ b/database/user.go @@ -36,7 +36,7 @@ type UserQuery struct { func (uq *UserQuery) CreateTable() error { _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( mxid VARCHAR(255) PRIMARY KEY, - jid VARCHAR(25) UNIQUE, + jid VARCHAR(255) UNIQUE, management_room VARCHAR(255), @@ -69,7 +69,7 @@ func (uq *UserQuery) GetAll() (users []*User) { } func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User { - row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE mxid=?", userID) + row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE mxid=$1", userID) if row == nil { return nil } @@ -77,7 +77,7 @@ func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User { } func (uq *UserQuery) GetByJID(userID types.WhatsAppID) *User { - row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE jid=?", stripSuffix(userID)) + row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE jid=$1", stripSuffix(userID)) if row == nil { return nil } @@ -150,7 +150,7 @@ func (user *User) sessionUnptr() (sess whatsapp.Session) { func (user *User) Insert() { sess := user.sessionUnptr() - _, err := user.db.Exec("INSERT INTO whatsapp_user VALUES (?, ?, ?, ?, ?, ?, ?, ?)", user.MXID, user.jidPtr(), + _, err := user.db.Exec("INSERT INTO whatsapp_user VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", user.MXID, user.jidPtr(), user.ManagementRoom, sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey) if err != nil { @@ -160,7 +160,7 @@ func (user *User) Insert() { func (user *User) Update() { sess := user.sessionUnptr() - _, err := user.db.Exec("UPDATE whatsapp_user SET jid=?, management_room=?, client_id=?, client_token=?, server_token=?, enc_key=?, mac_key=? WHERE mxid=?", + _, err := user.db.Exec("UPDATE whatsapp_user SET jid=$1, management_room=$2, client_id=$3, client_token=$4, server_token=$5, enc_key=$6, mac_key=$7 WHERE mxid=$8", user.jidPtr(), user.ManagementRoom, sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey, user.MXID) diff --git a/go.mod b/go.mod index 7c3e154..764f745 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,6 @@ module maunium.net/go/mautrix-whatsapp require ( github.com/Baozisoftware/qrcode-terminal-go v0.0.0-20170407111555-c0650d8dff0f // indirect - github.com/Rhymen/go-whatsapp v0.0.0-20190208090600-c1173899de99 github.com/gorilla/mux v1.7.0 // indirect github.com/mattn/go-colorable v0.1.1 // indirect github.com/mattn/go-sqlite3 v1.10.0 diff --git a/main.go b/main.go index 22ee7a9..4615983 100644 --- a/main.go +++ b/main.go @@ -185,6 +185,7 @@ func (bridge *Bridge) UpdateBotProfile() { } func (bridge *Bridge) StartUsers() { + bridge.Log.Debugln("Starting users") for _, user := range bridge.GetAllUsers() { go user.Connect(false) }