disappear.go 2.5 KB

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