disappear.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2022 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 main
  17. import (
  18. "fmt"
  19. "time"
  20. "maunium.net/go/mautrix"
  21. "maunium.net/go/mautrix/id"
  22. "maunium.net/go/mautrix/util/dbutil"
  23. "maunium.net/go/mautrix-whatsapp/database"
  24. )
  25. func (portal *Portal) MarkDisappearing(txn dbutil.Execable, eventID id.EventID, expiresIn uint32, startNow bool) {
  26. if expiresIn == 0 || (!portal.bridge.Config.Bridge.DisappearingMessagesInGroups && portal.IsGroupChat()) {
  27. return
  28. }
  29. msg := portal.bridge.DB.DisappearingMessage.NewWithValues(portal.MXID, eventID, time.Duration(expiresIn)*time.Second, startNow)
  30. msg.Insert(txn)
  31. if startNow {
  32. go portal.sleepAndDelete(msg)
  33. }
  34. }
  35. func (portal *Portal) ScheduleDisappearing() {
  36. if !portal.bridge.Config.Bridge.DisappearingMessagesInGroups && portal.IsGroupChat() {
  37. return
  38. }
  39. nowPlusHour := time.Now().Add(1 * time.Hour)
  40. for _, msg := range portal.bridge.DB.DisappearingMessage.StartAllUnscheduledInRoom(portal.MXID) {
  41. if msg.ExpireAt.Before(nowPlusHour) {
  42. go portal.sleepAndDelete(msg)
  43. }
  44. }
  45. }
  46. func (br *WABridge) SleepAndDeleteUpcoming() {
  47. for _, msg := range br.DB.DisappearingMessage.GetUpcomingScheduled(1 * time.Hour) {
  48. portal := br.GetPortalByMXID(msg.RoomID)
  49. if portal == nil {
  50. msg.Delete()
  51. } else {
  52. go portal.sleepAndDelete(msg)
  53. }
  54. }
  55. }
  56. func (portal *Portal) sleepAndDelete(msg *database.DisappearingMessage) {
  57. sleepTime := msg.ExpireAt.Sub(time.Now())
  58. portal.log.Debugfln("Sleeping for %s to make %s disappear", sleepTime, msg.EventID)
  59. time.Sleep(sleepTime)
  60. _, err := portal.MainIntent().RedactEvent(msg.RoomID, msg.EventID, mautrix.ReqRedact{
  61. Reason: "Message expired",
  62. TxnID: fmt.Sprintf("mxwa_disappear_%s", msg.EventID),
  63. })
  64. if err != nil {
  65. portal.log.Warnfln("Failed to make %s disappear: %v", msg.EventID, err)
  66. } else {
  67. portal.log.Debugfln("Disappeared %s", msg.EventID)
  68. }
  69. msg.Delete()
  70. }