portal.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2018 Tulir Asokan
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package database
  17. import (
  18. log "maunium.net/go/maulogger"
  19. "maunium.net/go/mautrix-whatsapp/types"
  20. )
  21. type PortalQuery struct {
  22. db *Database
  23. log log.Logger
  24. }
  25. func (pq *PortalQuery) CreateTable() error {
  26. _, err := pq.db.Exec(`CREATE TABLE IF NOT EXISTS portal (
  27. jid VARCHAR(255),
  28. owner VARCHAR(255),
  29. mxid VARCHAR(255) NOT NULL UNIQUE,
  30. PRIMARY KEY (jid, owner),
  31. FOREIGN KEY owner REFERENCES user(mxid)
  32. )`)
  33. return err
  34. }
  35. func (pq *PortalQuery) New() *Portal {
  36. return &Portal{
  37. db: pq.db,
  38. log: pq.log,
  39. }
  40. }
  41. func (pq *PortalQuery) GetAll(owner types.MatrixUserID) (portals []*Portal) {
  42. rows, err := pq.db.Query("SELECT * FROM portal WHERE owner=?", owner)
  43. if err != nil || rows == nil {
  44. return nil
  45. }
  46. defer rows.Close()
  47. for rows.Next() {
  48. portals = append(portals, pq.New().Scan(rows))
  49. }
  50. return
  51. }
  52. func (pq *PortalQuery) GetByJID(owner types.MatrixUserID, jid types.WhatsAppID) *Portal {
  53. return pq.get("SELECT * FROM portal WHERE jid=? AND owner=?", jid, owner)
  54. }
  55. func (pq *PortalQuery) GetByMXID(mxid types.MatrixRoomID) *Portal {
  56. return pq.get("SELECT * FROM portal WHERE mxid=?", mxid)
  57. }
  58. func (pq *PortalQuery) get(query string, args ...interface{}) *Portal {
  59. row := pq.db.QueryRow(query, args...)
  60. if row == nil {
  61. return nil
  62. }
  63. return pq.New().Scan(row)
  64. }
  65. type Portal struct {
  66. db *Database
  67. log log.Logger
  68. JID types.WhatsAppID
  69. MXID types.MatrixRoomID
  70. Owner types.MatrixUserID
  71. }
  72. func (portal *Portal) Scan(row Scannable) *Portal {
  73. err := row.Scan(&portal.JID, &portal.MXID, &portal.Owner)
  74. if err != nil {
  75. portal.log.Fatalln("Database scan failed:", err)
  76. }
  77. return portal
  78. }
  79. func (portal *Portal) Insert() error {
  80. _, err := portal.db.Exec("INSERT INTO portal VALUES (?, ?, ?)", portal.JID, portal.Owner, portal.MXID)
  81. return err
  82. }
  83. func (portal *Portal) Update() error {
  84. _, err := portal.db.Exec("UPDATE portal SET mxid=? WHERE jid=? AND owner=?", portal.MXID, portal.JID, portal.Owner)
  85. return err
  86. }