mediabackfillrequest.go 3.5 KB

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