mediabackfillrequest.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 Tulir Asokan, Sumner Evans
  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. "database/sql"
  19. "errors"
  20. _ "github.com/mattn/go-sqlite3"
  21. "go.mau.fi/util/dbutil"
  22. log "maunium.net/go/maulogger/v2"
  23. "maunium.net/go/mautrix/id"
  24. )
  25. type MediaBackfillRequestStatus int
  26. const (
  27. MediaBackfillRequestStatusNotRequested MediaBackfillRequestStatus = iota
  28. MediaBackfillRequestStatusRequested
  29. MediaBackfillRequestStatusRequestFailed
  30. )
  31. type MediaBackfillRequestQuery struct {
  32. db *Database
  33. log log.Logger
  34. }
  35. type MediaBackfillRequest struct {
  36. db *Database
  37. log log.Logger
  38. UserID id.UserID
  39. PortalKey *PortalKey
  40. EventID id.EventID
  41. MediaKey []byte
  42. Status MediaBackfillRequestStatus
  43. Error string
  44. }
  45. func (mbrq *MediaBackfillRequestQuery) newMediaBackfillRequest() *MediaBackfillRequest {
  46. return &MediaBackfillRequest{
  47. db: mbrq.db,
  48. log: mbrq.log,
  49. PortalKey: &PortalKey{},
  50. }
  51. }
  52. func (mbrq *MediaBackfillRequestQuery) NewMediaBackfillRequestWithValues(userID id.UserID, portalKey *PortalKey, eventID id.EventID, mediaKey []byte) *MediaBackfillRequest {
  53. return &MediaBackfillRequest{
  54. db: mbrq.db,
  55. log: mbrq.log,
  56. UserID: userID,
  57. PortalKey: portalKey,
  58. EventID: eventID,
  59. MediaKey: mediaKey,
  60. Status: MediaBackfillRequestStatusNotRequested,
  61. }
  62. }
  63. const (
  64. getMediaBackfillRequestsForUser = `
  65. SELECT user_mxid, portal_jid, portal_receiver, event_id, media_key, status, error
  66. FROM media_backfill_requests
  67. WHERE user_mxid=$1
  68. AND status=0
  69. `
  70. )
  71. func (mbr *MediaBackfillRequest) Upsert() {
  72. _, err := mbr.db.Exec(`
  73. INSERT INTO media_backfill_requests (user_mxid, portal_jid, portal_receiver, event_id, media_key, status, error)
  74. VALUES ($1, $2, $3, $4, $5, $6, $7)
  75. ON CONFLICT (user_mxid, portal_jid, portal_receiver, event_id)
  76. DO UPDATE SET
  77. media_key=EXCLUDED.media_key,
  78. status=EXCLUDED.status,
  79. error=EXCLUDED.error`,
  80. mbr.UserID,
  81. mbr.PortalKey.JID.String(),
  82. mbr.PortalKey.Receiver.String(),
  83. mbr.EventID,
  84. mbr.MediaKey,
  85. mbr.Status,
  86. mbr.Error)
  87. if err != nil {
  88. mbr.log.Warnfln("Failed to insert media backfill request %s/%s/%s: %v", mbr.UserID, mbr.PortalKey.String(), mbr.EventID, err)
  89. }
  90. }
  91. func (mbr *MediaBackfillRequest) Scan(row dbutil.Scannable) *MediaBackfillRequest {
  92. err := row.Scan(&mbr.UserID, &mbr.PortalKey.JID, &mbr.PortalKey.Receiver, &mbr.EventID, &mbr.MediaKey, &mbr.Status, &mbr.Error)
  93. if err != nil {
  94. if !errors.Is(err, sql.ErrNoRows) {
  95. mbr.log.Errorln("Database scan failed:", err)
  96. }
  97. return nil
  98. }
  99. return mbr
  100. }
  101. func (mbrq *MediaBackfillRequestQuery) GetMediaBackfillRequestsForUser(userID id.UserID) (requests []*MediaBackfillRequest) {
  102. rows, err := mbrq.db.Query(getMediaBackfillRequestsForUser, userID)
  103. defer rows.Close()
  104. if err != nil || rows == nil {
  105. return nil
  106. }
  107. for rows.Next() {
  108. requests = append(requests, mbrq.newMediaBackfillRequest().Scan(rows))
  109. }
  110. return
  111. }
  112. func (mbrq *MediaBackfillRequestQuery) DeleteAllMediaBackfillRequests(userID id.UserID) {
  113. _, err := mbrq.db.Exec("DELETE FROM media_backfill_requests WHERE user_mxid=$1", userID)
  114. if err != nil {
  115. mbrq.log.Warnfln("Failed to delete media backfill requests for %s: %v", userID, err)
  116. }
  117. }