Add database models and improve config/main

This commit is contained in:
Tulir Asokan
2018-08-13 23:24:44 +03:00
parent ace08205d9
commit fd3c1fb77c
11 changed files with 440 additions and 16 deletions

51
database/database.go Normal file
View File

@ -0,0 +1,51 @@
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package database
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
log "maunium.net/go/maulogger"
)
type Database struct {
*sql.DB
log *log.Sublogger
User *UserQuery
}
func New(file string) (*Database, error) {
conn, err := sql.Open("sqlite3", file)
if err != nil {
return nil, err
}
db := &Database{
DB: conn,
log: log.CreateSublogger("Database", log.LevelDebug),
}
db.User = &UserQuery{
db: db,
log: log.CreateSublogger("Database/User", log.LevelDebug),
}
return db, nil
}
type Scannable interface {
Scan(...interface{}) error
}

100
database/portal.go Normal file
View File

@ -0,0 +1,100 @@
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package database
import (
log "maunium.net/go/maulogger"
)
type PortalQuery struct {
db *Database
log *log.Sublogger
}
func (pq *PortalQuery) CreateTable() error {
_, err := pq.db.Exec(`CREATE TABLE IF NOT EXISTS portal (
jid VARCHAR(255),
owner VARCHAR(255),
mxid VARCHAR(255) NOT NULL UNIQUE,
PRIMARY KEY (jid, owner),
FOREIGN KEY owner REFERENCES user(mxid)
)`)
return err
}
func (pq *PortalQuery) New() *Portal {
return &Portal{
db: pq.db,
log: pq.log,
}
}
func (pq *PortalQuery) GetAll() (portals []*Portal) {
rows, err := pq.db.Query("SELECT * FROM portal")
if err != nil || rows == nil {
return nil
}
defer rows.Close()
for rows.Next() {
portals = append(portals, pq.New().Scan(rows))
}
return
}
func (pq *PortalQuery) GetByJID(owner, jid string) *Portal {
return pq.get("SELECT * FROM portal WHERE jid=? AND owner=?", jid, owner)
}
func (pq *PortalQuery) GetByMXID(mxid string) *Portal {
return pq.get("SELECT * FROM portal WHERE mxid=?", mxid)
}
func (pq *PortalQuery) get(query string, args ...interface{}) *Portal {
row := pq.db.QueryRow(query, args...)
if row == nil {
return nil
}
return pq.New().Scan(row)
}
type Portal struct {
db *Database
log *log.Sublogger
JID string
MXID string
Owner string
}
func (portal *Portal) Scan(row Scannable) *Portal {
err := row.Scan(&portal.JID, &portal.MXID, &portal.Owner)
if err != nil {
portal.log.Fatalln("Database scan failed:", err)
}
return portal
}
func (portal *Portal) Insert() error {
_, err := portal.db.Exec("INSERT INTO portal VALUES (?, ?, ?)", portal.JID, portal.Owner, portal.MXID)
return err
}
func (portal *Portal) Update() error {
_, err := portal.db.Exec("UPDATE portal SET mxid=? WHERE jid=? AND owner=?", portal.MXID, portal.JID, portal.Owner)
return err
}

99
database/user.go Normal file
View File

@ -0,0 +1,99 @@
// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
// Copyright (C) 2018 Tulir Asokan
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package database
import (
log "maunium.net/go/maulogger"
"github.com/Rhymen/go-whatsapp"
)
type UserQuery struct {
db *Database
log *log.Sublogger
}
func (uq *UserQuery) CreateTable() error {
_, err := uq.db.Exec(`CREATE TABLE IF NOT EXISTS user (
mxid VARCHAR(255) PRIMARY KEY,
client_id VARCHAR(255),
client_token VARCHAR(255),
server_token VARCHAR(255),
enc_key BLOB,
mac_key BLOB,
wid VARCHAR(255)
)`)
return err
}
func (uq *UserQuery) New() *User {
return &User{
db: uq.db,
log: uq.log,
}
}
func (uq *UserQuery) GetAll() (users []*User) {
rows, err := uq.db.Query("SELECT * FROM user")
if err != nil || rows == nil {
return nil
}
defer rows.Close()
for rows.Next() {
users = append(users, uq.New().Scan(rows))
}
return
}
func (uq *UserQuery) Get(userID string) *User {
row := uq.db.QueryRow("SELECT * FROM user WHERE mxid=?", userID)
if row == nil {
return nil
}
return uq.New().Scan(row)
}
type User struct {
db *Database
log *log.Sublogger
UserID string
session whatsapp.Session
}
func (user *User) Scan(row Scannable) *User {
err := row.Scan(&user.UserID, &user.session.ClientId, &user.session.ClientToken, &user.session.ServerToken,
&user.session.EncKey, &user.session.MacKey, &user.session.Wid)
if err != nil {
user.log.Fatalln("Database scan failed:", err)
}
return user
}
func (user *User) Insert() error {
_, err := user.db.Exec("INSERT INTO user VALUES (?, ?, ?, ?, ?, ?, ?)", user.UserID, user.session.ClientId,
user.session.ClientToken, user.session.ServerToken, user.session.EncKey, user.session.MacKey, user.session.Wid)
return err
}
func (user *User) Update() error {
_, err := user.db.Exec("UPDATE user SET client_id=?, client_token=?, server_token=?, enc_key=?, mac_key=?, wid=? WHERE mxid=?",
user.session.ClientId, user.session.ClientToken, user.session.ServerToken, user.session.EncKey, user.session.MacKey,
user.session.Wid, user.UserID)
return err
}