mediabackfillrequest.go 3.8 KB

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