mediabackfillrequest.go 3.6 KB

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