From 6547a2ce1f9d7039e77dcbfebb55fae1c5440f2b Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 10:59:52 +0100 Subject: [PATCH 01/16] changed sqlite to postgresql --- database/database.go | 5 +++-- database/message.go | 2 +- database/portal.go | 2 +- database/puppet.go | 2 +- database/user.go | 16 ++++++++-------- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/database/database.go b/database/database.go index 0e4e964..3a36939 100644 --- a/database/database.go +++ b/database/database.go @@ -19,7 +19,7 @@ package database import ( "database/sql" - _ "github.com/mattn/go-sqlite3" + _ "github.com/lib/pq" log "maunium.net/go/maulogger/v2" ) @@ -35,7 +35,8 @@ type Database struct { } func New(file string) (*Database, error) { - conn, err := sql.Open("sqlite3", file) + connStr := "postgres://synapse:changeme@db/whatsapp?sslmode=disable" + conn, err := sql.Open("postgres", connStr) if err != nil { return nil, err } diff --git a/database/message.go b/database/message.go index c6a2790..c8f6f9e 100644 --- a/database/message.go +++ b/database/message.go @@ -40,7 +40,7 @@ func (mq *MessageQuery) CreateTable() error { jid VARCHAR(255), mxid VARCHAR(255) NOT NULL UNIQUE, sender VARCHAR(25) NOT NULL, - content BLOB NOT NULL, + content bytea NOT NULL, PRIMARY KEY (chat_jid, chat_receiver, jid), FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) diff --git a/database/portal.go b/database/portal.go index 60eb486..ffb4318 100644 --- a/database/portal.go +++ b/database/portal.go @@ -70,7 +70,7 @@ func (pq *PortalQuery) CreateTable() error { avatar VARCHAR(255) NOT NULL, PRIMARY KEY (jid, receiver), - FOREIGN KEY (receiver) REFERENCES user(mxid) + FOREIGN KEY (receiver) REFERENCES whatsapp_user(mxid) )`) return err } diff --git a/database/puppet.go b/database/puppet.go index 3e1b169..bde6116 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -34,7 +34,7 @@ func (pq *PuppetQuery) CreateTable() error { jid VARCHAR(25) PRIMARY KEY, avatar VARCHAR(255), displayname VARCHAR(255), - name_quality TINYINT + name_quality SMALLINT )`) return err } diff --git a/database/user.go b/database/user.go index 090d70a..2f09555 100644 --- a/database/user.go +++ b/database/user.go @@ -34,7 +34,7 @@ type UserQuery struct { } func (uq *UserQuery) CreateTable() error { - _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS user ( + _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( mxid VARCHAR(255) PRIMARY KEY, jid VARCHAR(25) UNIQUE, @@ -43,8 +43,8 @@ func (uq *UserQuery) CreateTable() error { client_id VARCHAR(255), client_token VARCHAR(255), server_token VARCHAR(255), - enc_key BLOB, - mac_key BLOB + enc_key bytea, + mac_key bytea )`) return err } @@ -57,7 +57,7 @@ func (uq *UserQuery) New() *User { } func (uq *UserQuery) GetAll() (users []*User) { - rows, err := uq.db.Query("SELECT * FROM user") + rows, err := uq.db.Query("SELECT * FROM whatsapp_user") if err != nil || rows == nil { return nil } @@ -69,7 +69,7 @@ func (uq *UserQuery) GetAll() (users []*User) { } func (uq *UserQuery) GetByMXID(userID types.MatrixUserID) *User { - row := uq.db.QueryRow("SELECT * FROM user WHERE mxid=?", userID) + row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE mxid=?", 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 user WHERE jid=?", stripSuffix(userID)) + row := uq.db.QueryRow("SELECT * FROM whatsapp_user WHERE jid=?", 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 user VALUES (?, ?, ?, ?, ?, ?, ?, ?)", user.MXID, user.jidPtr(), + _, err := user.db.Exec("INSERT INTO whatsapp_user VALUES (?, ?, ?, ?, ?, ?, ?, ?)", 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 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=?, management_room=?, client_id=?, client_token=?, server_token=?, enc_key=?, mac_key=? WHERE mxid=?", user.jidPtr(), user.ManagementRoom, sess.ClientId, sess.ClientToken, sess.ServerToken, sess.EncKey, sess.MacKey, user.MXID) From daf0faab61d1abae828ef002c11cd4c2260967b2 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 16:33:42 +0100 Subject: [PATCH 02/16] 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) } From 25da34b8f2169f3297dec67c06c1f42da7415ddc Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 18:01:23 +0100 Subject: [PATCH 03/16] reverted to old version of whatsapp ext --- go.mod | 1 + 1 file changed, 1 insertion(+) diff --git a/go.mod b/go.mod index 764f745..7c3e154 100644 --- a/go.mod +++ b/go.mod @@ -2,6 +2,7 @@ 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 From 5fd9faec2a5c727c5acea4efd37fcc28be5532f0 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 18:18:08 +0100 Subject: [PATCH 04/16] Update example-config.yaml --- example-config.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/example-config.yaml b/example-config.yaml index 4839aee..b2a5e4d 100644 --- a/example-config.yaml +++ b/example-config.yaml @@ -20,6 +20,7 @@ appservice: # The database type. Only "sqlite3" is supported. type: sqlite3 # The database URI. Usually file name. https://github.com/mattn/go-sqlite3#connection-string + # postres example: postgres://synapse:changeme@db/whatsapp?sslmode=disable uri: mautrix-whatsapp.db # Path to the Matrix room state store. state_store_path: ./mx-state.json From aeb692d6162cdbd2e56a685351a855940f09d6b9 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 18:18:40 +0100 Subject: [PATCH 05/16] Update database.go --- database/database.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/database/database.go b/database/database.go index 3a36939..4e97260 100644 --- a/database/database.go +++ b/database/database.go @@ -35,8 +35,7 @@ type Database struct { } func New(file string) (*Database, error) { - connStr := "postgres://synapse:changeme@db/whatsapp?sslmode=disable" - conn, err := sql.Open("postgres", connStr) + conn, err := sql.Open("postgres", file) if err != nil { return nil, err } From 4ba331fbfdf24e83ca12c0b427d5098a554bafd0 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 18:28:26 +0100 Subject: [PATCH 06/16] Update main.go --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 4615983..2689db0 100644 --- a/main.go +++ b/main.go @@ -133,7 +133,7 @@ func (bridge *Bridge) Init() { bridge.AS.StateStore = bridge.StateStore bridge.Log.Debugln("Initializing database") - bridge.DB, err = database.New(bridge.Config.AppService.Database.URI) + bridge.DB, err = database.New(bridge.Config.AppService.Database.Type, bridge.Config.AppService.Database.URI) if err != nil { bridge.Log.Fatalln("Failed to initialize database:", err) os.Exit(14) From 96a509384d4ba036b9da604c51492fcd76da14cc Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 18:29:15 +0100 Subject: [PATCH 07/16] Update database.go --- database/database.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/database/database.go b/database/database.go index 4e97260..c065971 100644 --- a/database/database.go +++ b/database/database.go @@ -34,8 +34,8 @@ type Database struct { Message *MessageQuery } -func New(file string) (*Database, error) { - conn, err := sql.Open("postgres", file) +func New(dbType string, uri string) (*Database, error) { + conn, err := sql.Open(dbType, uri) if err != nil { return nil, err } From 5efd476c0a08bbff7d75cc2adec58b7207a54356 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 22:21:24 +0100 Subject: [PATCH 08/16] Update main.go --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 2689db0..57eda98 100644 --- a/main.go +++ b/main.go @@ -147,7 +147,7 @@ func (bridge *Bridge) Init() { } func (bridge *Bridge) Start() { - err := bridge.DB.CreateTables() + err := bridge.DB.CreateTables(bridge.Config.AppService.Database.Type) if err != nil { bridge.Log.Fatalln("Failed to create database tables:", err) os.Exit(15) From aaa57b353fc6ab210447fd23024c13783b0c7f9b Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 22:22:12 +0100 Subject: [PATCH 09/16] Update database.go --- database/database.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/database/database.go b/database/database.go index c065971..b23895c 100644 --- a/database/database.go +++ b/database/database.go @@ -63,20 +63,20 @@ func New(dbType string, uri string) (*Database, error) { return db, nil } -func (db *Database) CreateTables() error { - err := db.User.CreateTable() +func (db *Database) CreateTables(dbType string) error { + err := db.User.CreateTable(dbType) if err != nil { return err } - err = db.Portal.CreateTable() + err = db.Portal.CreateTable(dbType) if err != nil { return err } - err = db.Puppet.CreateTable() + err = db.Puppet.CreateTable(dbType) if err != nil { return err } - err = db.Message.CreateTable() + err = db.Message.CreateTable(dbType) if err != nil { return err } From 7c513c2f096317580fcd2e492e7092cb8670b490 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 22:22:54 +0100 Subject: [PATCH 10/16] Update message.go --- database/message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/message.go b/database/message.go index 5513df3..886bd56 100644 --- a/database/message.go +++ b/database/message.go @@ -33,7 +33,7 @@ type MessageQuery struct { log log.Logger } -func (mq *MessageQuery) CreateTable() error { +func (mq *MessageQuery) CreateTable(dbType string) error { _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( chat_jid VARCHAR(255), chat_receiver VARCHAR(255), From d91bc8c346cea9d85037a1883fe2783e87bbb1c0 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 22:23:05 +0100 Subject: [PATCH 11/16] Update portal.go --- database/portal.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/portal.go b/database/portal.go index 8bfffdb..008bf6f 100644 --- a/database/portal.go +++ b/database/portal.go @@ -59,7 +59,7 @@ type PortalQuery struct { log log.Logger } -func (pq *PortalQuery) CreateTable() error { +func (pq *PortalQuery) CreateTable(dbType string) error { _, err := pq.db.Exec(`CREATE TABLE IF NOT EXISTS portal ( jid VARCHAR(255), receiver VARCHAR(255), From c1e20f96d061b4660fdcbf42e1a9e6b1ffe53e12 Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 22:23:10 +0100 Subject: [PATCH 12/16] Update puppet.go --- database/puppet.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/puppet.go b/database/puppet.go index b6637f8..439be4a 100644 --- a/database/puppet.go +++ b/database/puppet.go @@ -29,7 +29,7 @@ type PuppetQuery struct { log log.Logger } -func (pq *PuppetQuery) CreateTable() error { +func (pq *PuppetQuery) CreateTable(dbType string) error { _, err := pq.db.Exec(`CREATE TABLE IF NOT EXISTS puppet ( jid VARCHAR(255) PRIMARY KEY, avatar VARCHAR(255), From 539699ba04669c6e84d1939c04d6a659e9d45ece Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 22:23:15 +0100 Subject: [PATCH 13/16] Update user.go --- database/user.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/database/user.go b/database/user.go index f871b67..919f32b 100644 --- a/database/user.go +++ b/database/user.go @@ -33,7 +33,7 @@ type UserQuery struct { log log.Logger } -func (uq *UserQuery) CreateTable() error { +func (uq *UserQuery) CreateTable(dbType string) error { _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( mxid VARCHAR(255) PRIMARY KEY, jid VARCHAR(255) UNIQUE, From 2d3de9740c0a41d056b381ef52959f2f3eb5fc3a Mon Sep 17 00:00:00 2001 From: Dominic Renner Date: Wed, 6 Mar 2019 22:26:55 +0100 Subject: [PATCH 14/16] Update user.go --- database/user.go | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/database/user.go b/database/user.go index 919f32b..988acc2 100644 --- a/database/user.go +++ b/database/user.go @@ -34,19 +34,35 @@ type UserQuery struct { } func (uq *UserQuery) CreateTable(dbType string) error { - _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( - mxid VARCHAR(255) PRIMARY KEY, - jid VARCHAR(255) UNIQUE, + if strings.ToLower(dbType) == "postgres" { + _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( + mxid VARCHAR(255) PRIMARY KEY, + jid VARCHAR(255) UNIQUE, - management_room VARCHAR(255), + management_room VARCHAR(255), - client_id VARCHAR(255), - client_token VARCHAR(255), - server_token VARCHAR(255), - enc_key bytea, - mac_key bytea - )`) - return err + client_id VARCHAR(255), + client_token VARCHAR(255), + server_token VARCHAR(255), + enc_key bytea, + mac_key bytea + )`) + return err + } else { + _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( + mxid VARCHAR(255) PRIMARY KEY, + jid VARCHAR(255) UNIQUE, + + management_room VARCHAR(255), + + client_id VARCHAR(255), + client_token VARCHAR(255), + server_token VARCHAR(255), + enc_key BLOB, + mac_key BLOB + )`) + return err + } } func (uq *UserQuery) New() *User { From d46578894e32a990e96fb43a4e0a0208306ac65e Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 6 Mar 2019 22:57:38 +0100 Subject: [PATCH 15/16] Rename user table back --- database/database.go | 1 + database/user.go | 16 ++++++++-------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/database/database.go b/database/database.go index b23895c..5e9b995 100644 --- a/database/database.go +++ b/database/database.go @@ -20,6 +20,7 @@ import ( "database/sql" _ "github.com/lib/pq" + _ "github.com/mattn/go-sqlite3" log "maunium.net/go/maulogger/v2" ) diff --git a/database/user.go b/database/user.go index 988acc2..a9c0dfd 100644 --- a/database/user.go +++ b/database/user.go @@ -35,7 +35,7 @@ type UserQuery struct { func (uq *UserQuery) CreateTable(dbType string) error { if strings.ToLower(dbType) == "postgres" { - _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( + _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS "user" ( mxid VARCHAR(255) PRIMARY KEY, jid VARCHAR(255) UNIQUE, @@ -49,7 +49,7 @@ func (uq *UserQuery) CreateTable(dbType string) error { )`) return err } else { - _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS whatsapp_user ( + _, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS "user" ( mxid VARCHAR(255) PRIMARY KEY, jid VARCHAR(255) UNIQUE, @@ -61,7 +61,7 @@ func (uq *UserQuery) CreateTable(dbType string) error { enc_key BLOB, mac_key BLOB )`) - return err + return err } } @@ -73,7 +73,7 @@ func (uq *UserQuery) New() *User { } func (uq *UserQuery) GetAll() (users []*User) { - rows, err := uq.db.Query("SELECT * FROM whatsapp_user") + rows, err := uq.db.Query(`SELECT * FROM "user"`) if err != nil || rows == nil { return nil } @@ -85,7 +85,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=$1", userID) + row := uq.db.QueryRow(`SELECT * FROM "user" WHERE mxid=$1`, userID) if row == nil { return nil } @@ -93,7 +93,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=$1", stripSuffix(userID)) + row := uq.db.QueryRow(`SELECT * FROM "user" WHERE jid=$1`, stripSuffix(userID)) if row == nil { return nil } @@ -166,7 +166,7 @@ func (user *User) sessionUnptr() (sess whatsapp.Session) { func (user *User) Insert() { sess := user.sessionUnptr() - _, err := user.db.Exec("INSERT INTO whatsapp_user VALUES ($1, $2, $3, $4, $5, $6, $7, $8)", user.MXID, user.jidPtr(), + _, err := user.db.Exec(`INSERT INTO "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 { @@ -176,7 +176,7 @@ func (user *User) Insert() { func (user *User) Update() { sess := user.sessionUnptr() - _, 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", + _, err := user.db.Exec(`UPDATE "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) From 9dde1894380a7ec36651cb195079ce3259f7a060 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 6 Mar 2019 23:03:19 +0100 Subject: [PATCH 16/16] Make message.CreateTable usable on postgres and sqlite --- database/message.go | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/database/message.go b/database/message.go index 886bd56..2e5f7e0 100644 --- a/database/message.go +++ b/database/message.go @@ -18,6 +18,7 @@ package database import ( "bytes" + "strings" "database/sql" "encoding/json" @@ -34,18 +35,33 @@ type MessageQuery struct { } func (mq *MessageQuery) CreateTable(dbType string) error { - _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( - chat_jid VARCHAR(255), - chat_receiver VARCHAR(255), - jid VARCHAR(255), - mxid VARCHAR(255) NOT NULL UNIQUE, - sender VARCHAR(255) NOT NULL, - content bytea NOT NULL, + if strings.ToLower(dbType) == "postgres" { + _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( + chat_jid VARCHAR(255), + chat_receiver VARCHAR(255), + jid VARCHAR(255), + mxid VARCHAR(255) NOT NULL UNIQUE, + sender VARCHAR(255) NOT NULL, + content bytea NOT NULL, - PRIMARY KEY (chat_jid, chat_receiver, jid), - FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) - )`) + PRIMARY KEY (chat_jid, chat_receiver, jid), + FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) + )`) + return err + } else { + _, err := mq.db.Exec(`CREATE TABLE IF NOT EXISTS message ( + chat_jid VARCHAR(255), + chat_receiver VARCHAR(255), + jid VARCHAR(255), + mxid VARCHAR(255) NOT NULL UNIQUE, + sender VARCHAR(255) NOT NULL, + content BLOB NOT NULL, + + PRIMARY KEY (chat_jid, chat_receiver, jid), + FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver) + )`) return err + } } func (mq *MessageQuery) New() *Message {