historysync.go 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. // mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge.
  2. // Copyright (C) 2021 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. "crypto/sha256"
  19. "encoding/base64"
  20. "fmt"
  21. "time"
  22. "maunium.net/go/mautrix/util/variationselector"
  23. waProto "go.mau.fi/whatsmeow/binary/proto"
  24. "go.mau.fi/whatsmeow/types"
  25. "maunium.net/go/mautrix"
  26. "maunium.net/go/mautrix/appservice"
  27. "maunium.net/go/mautrix/bridge/bridgeconfig"
  28. "maunium.net/go/mautrix/event"
  29. "maunium.net/go/mautrix/id"
  30. "maunium.net/go/mautrix/util/dbutil"
  31. "maunium.net/go/mautrix-whatsapp/config"
  32. "maunium.net/go/mautrix-whatsapp/database"
  33. )
  34. // region User history sync handling
  35. type wrappedInfo struct {
  36. *types.MessageInfo
  37. Type database.MessageType
  38. Error database.MessageErrorType
  39. SenderMXID id.UserID
  40. ReactionTarget types.MessageID
  41. MediaKey []byte
  42. ExpirationStart time.Time
  43. ExpiresIn time.Duration
  44. }
  45. func (user *User) handleHistorySyncsLoop() {
  46. if !user.bridge.Config.Bridge.HistorySync.Backfill {
  47. return
  48. }
  49. batchSend := user.bridge.SpecVersions.Supports(mautrix.BeeperFeatureBatchSending)
  50. if batchSend {
  51. // Start the backfill queue.
  52. user.BackfillQueue = &BackfillQueue{
  53. BackfillQuery: user.bridge.DB.Backfill,
  54. reCheckChannels: []chan bool{},
  55. log: user.log.Sub("BackfillQueue"),
  56. }
  57. forwardAndImmediate := []database.BackfillType{database.BackfillImmediate, database.BackfillForward}
  58. // Immediate backfills can be done in parallel
  59. for i := 0; i < user.bridge.Config.Bridge.HistorySync.Immediate.WorkerCount; i++ {
  60. go user.HandleBackfillRequestsLoop(forwardAndImmediate, []database.BackfillType{})
  61. }
  62. // Deferred backfills should be handled synchronously so as not to
  63. // overload the homeserver. Users can configure their backfill stages
  64. // to be more or less aggressive with backfilling at this stage.
  65. go user.HandleBackfillRequestsLoop([]database.BackfillType{database.BackfillDeferred}, forwardAndImmediate)
  66. }
  67. if user.bridge.Config.Bridge.HistorySync.MediaRequests.AutoRequestMedia &&
  68. user.bridge.Config.Bridge.HistorySync.MediaRequests.RequestMethod == config.MediaRequestMethodLocalTime {
  69. go user.dailyMediaRequestLoop()
  70. }
  71. // Always save the history syncs for the user. If they want to enable
  72. // backfilling in the future, we will have it in the database.
  73. for {
  74. select {
  75. case evt := <-user.historySyncs:
  76. if evt == nil {
  77. return
  78. }
  79. user.storeHistorySync(evt.Data)
  80. case <-user.enqueueBackfillsTimer.C:
  81. if batchSend {
  82. user.enqueueAllBackfills()
  83. } else {
  84. user.backfillAll()
  85. }
  86. }
  87. }
  88. }
  89. const EnqueueBackfillsDelay = 30 * time.Second
  90. func (user *User) enqueueAllBackfills() {
  91. nMostRecent := user.bridge.DB.HistorySync.GetNMostRecentConversations(user.MXID, user.bridge.Config.Bridge.HistorySync.MaxInitialConversations)
  92. if len(nMostRecent) > 0 {
  93. user.log.Infofln("%v has passed since the last history sync blob, enqueueing backfills for %d chats", EnqueueBackfillsDelay, len(nMostRecent))
  94. // Find the portals for all the conversations.
  95. portals := []*Portal{}
  96. for _, conv := range nMostRecent {
  97. jid, err := types.ParseJID(conv.ConversationID)
  98. if err != nil {
  99. user.log.Warnfln("Failed to parse chat JID '%s' in history sync: %v", conv.ConversationID, err)
  100. continue
  101. }
  102. portals = append(portals, user.GetPortalByJID(jid))
  103. }
  104. user.EnqueueImmediateBackfills(portals)
  105. user.EnqueueForwardBackfills(portals)
  106. user.EnqueueDeferredBackfills(portals)
  107. // Tell the queue to check for new backfill requests.
  108. user.BackfillQueue.ReCheck()
  109. }
  110. }
  111. func (user *User) backfillAll() {
  112. conversations := user.bridge.DB.HistorySync.GetNMostRecentConversations(user.MXID, -1)
  113. if len(conversations) > 0 {
  114. user.zlog.Info().
  115. Int("conversation_count", len(conversations)).
  116. Msg("Probably received all history sync blobs, now backfilling conversations")
  117. // Find the portals for all the conversations.
  118. for i, conv := range conversations {
  119. jid, err := types.ParseJID(conv.ConversationID)
  120. if err != nil {
  121. user.zlog.Warn().Err(err).
  122. Str("conversation_id", conv.ConversationID).
  123. Msg("Failed to parse chat JID in history sync")
  124. continue
  125. }
  126. portal := user.GetPortalByJID(jid)
  127. if portal.MXID != "" {
  128. user.zlog.Debug().
  129. Str("portal_jid", portal.Key.JID.String()).
  130. Msg("Chat already has a room, deleting messages from database")
  131. user.bridge.DB.HistorySync.DeleteAllMessagesForPortal(user.MXID, portal.Key)
  132. } else if i < user.bridge.Config.Bridge.HistorySync.MaxInitialConversations {
  133. err = portal.CreateMatrixRoom(user, nil, true, true)
  134. if err != nil {
  135. user.zlog.Err(err).Msg("Failed to create Matrix room for backfill")
  136. }
  137. }
  138. }
  139. }
  140. }
  141. func (portal *Portal) legacyBackfill(user *User) {
  142. defer portal.latestEventBackfillLock.Unlock()
  143. // This should only be called from CreateMatrixRoom which locks latestEventBackfillLock before creating the room.
  144. if portal.latestEventBackfillLock.TryLock() {
  145. panic("legacyBackfill() called without locking latestEventBackfillLock")
  146. }
  147. // TODO use portal.zlog instead of user.zlog
  148. log := user.zlog.With().
  149. Str("portal_jid", portal.Key.JID.String()).
  150. Str("action", "legacy backfill").
  151. Logger()
  152. messages := user.bridge.DB.HistorySync.GetMessagesBetween(user.MXID, portal.Key.JID.String(), nil, nil, portal.bridge.Config.Bridge.HistorySync.MessageCount)
  153. log.Debug().Int("message_count", len(messages)).Msg("Got messages to backfill from database")
  154. for i := len(messages) - 1; i >= 0; i-- {
  155. msgEvt, err := user.Client.ParseWebMessage(portal.Key.JID, messages[i])
  156. if err != nil {
  157. log.Warn().Err(err).
  158. Int("msg_index", i).
  159. Str("msg_id", messages[i].GetKey().GetId()).
  160. Uint64("msg_time_seconds", messages[i].GetMessageTimestamp()).
  161. Msg("Dropping historical message due to parse error")
  162. continue
  163. }
  164. portal.handleMessage(user, msgEvt)
  165. }
  166. log.Debug().Msg("Backfill complete, deleting leftover messages from database")
  167. user.bridge.DB.HistorySync.DeleteAllMessagesForPortal(user.MXID, portal.Key)
  168. }
  169. func (user *User) dailyMediaRequestLoop() {
  170. // Calculate when to do the first set of media retry requests
  171. now := time.Now()
  172. userTz, err := time.LoadLocation(user.Timezone)
  173. if err != nil {
  174. userTz = now.Local().Location()
  175. }
  176. tonightMidnight := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, userTz)
  177. midnightOffset := time.Duration(user.bridge.Config.Bridge.HistorySync.MediaRequests.RequestLocalTime) * time.Minute
  178. requestStartTime := tonightMidnight.Add(midnightOffset)
  179. // If the request time for today has already happened, we need to start the
  180. // request loop tomorrow instead.
  181. if requestStartTime.Before(now) {
  182. requestStartTime = requestStartTime.AddDate(0, 0, 1)
  183. }
  184. // Wait to start the loop
  185. user.log.Infof("Waiting until %s to do media retry requests", requestStartTime)
  186. time.Sleep(time.Until(requestStartTime))
  187. for {
  188. mediaBackfillRequests := user.bridge.DB.MediaBackfillRequest.GetMediaBackfillRequestsForUser(user.MXID)
  189. user.log.Infof("Sending %d media retry requests", len(mediaBackfillRequests))
  190. // Send all of the media backfill requests for the user at once
  191. for _, req := range mediaBackfillRequests {
  192. portal := user.GetPortalByJID(req.PortalKey.JID)
  193. _, err := portal.requestMediaRetry(user, req.EventID, req.MediaKey)
  194. if err != nil {
  195. user.log.Warnf("Failed to send media retry request for %s / %s", req.PortalKey.String(), req.EventID)
  196. req.Status = database.MediaBackfillRequestStatusRequestFailed
  197. req.Error = err.Error()
  198. } else {
  199. user.log.Debugfln("Sent media retry request for %s / %s", req.PortalKey.String(), req.EventID)
  200. req.Status = database.MediaBackfillRequestStatusRequested
  201. }
  202. req.MediaKey = nil
  203. req.Upsert()
  204. }
  205. // Wait for 24 hours before making requests again
  206. time.Sleep(24 * time.Hour)
  207. }
  208. }
  209. func (user *User) backfillInChunks(req *database.Backfill, conv *database.HistorySyncConversation, portal *Portal) {
  210. portal.backfillLock.Lock()
  211. defer portal.backfillLock.Unlock()
  212. if !user.shouldCreatePortalForHistorySync(conv, portal) {
  213. return
  214. }
  215. backfillState := user.bridge.DB.Backfill.GetBackfillState(user.MXID, &portal.Key)
  216. if backfillState == nil {
  217. backfillState = user.bridge.DB.Backfill.NewBackfillState(user.MXID, &portal.Key)
  218. }
  219. backfillState.SetProcessingBatch(true)
  220. defer backfillState.SetProcessingBatch(false)
  221. var forwardPrevID id.EventID
  222. var timeEnd *time.Time
  223. var isLatestEvents, shouldMarkAsRead, shouldAtomicallyMarkAsRead bool
  224. portal.latestEventBackfillLock.Lock()
  225. if req.BackfillType == database.BackfillForward {
  226. // TODO this overrides the TimeStart set when enqueuing the backfill
  227. // maybe the enqueue should instead include the prev event ID
  228. lastMessage := portal.bridge.DB.Message.GetLastInChat(portal.Key)
  229. forwardPrevID = lastMessage.MXID
  230. start := lastMessage.Timestamp.Add(1 * time.Second)
  231. req.TimeStart = &start
  232. // Sending events at the end of the room (= latest events)
  233. isLatestEvents = true
  234. } else {
  235. firstMessage := portal.bridge.DB.Message.GetFirstInChat(portal.Key)
  236. if firstMessage != nil {
  237. end := firstMessage.Timestamp.Add(-1 * time.Second)
  238. timeEnd = &end
  239. user.log.Debugfln("Limiting backfill to end at %v", end)
  240. } else {
  241. // Portal is empty -> events are latest
  242. isLatestEvents = true
  243. }
  244. }
  245. if !isLatestEvents {
  246. // We'll use normal batch sending, so no need to keep blocking new message processing
  247. portal.latestEventBackfillLock.Unlock()
  248. } else {
  249. // This might involve sending events at the end of the room as non-historical events,
  250. // make sure we don't process messages until this is done.
  251. defer portal.latestEventBackfillLock.Unlock()
  252. isUnread := conv.MarkedAsUnread || conv.UnreadCount > 0
  253. isTooOld := user.bridge.Config.Bridge.HistorySync.UnreadHoursThreshold > 0 && conv.LastMessageTimestamp.Before(time.Now().Add(time.Duration(-user.bridge.Config.Bridge.HistorySync.UnreadHoursThreshold)*time.Hour))
  254. shouldMarkAsRead = !isUnread || isTooOld
  255. shouldAtomicallyMarkAsRead = shouldMarkAsRead && user.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareHungry
  256. }
  257. allMsgs := user.bridge.DB.HistorySync.GetMessagesBetween(user.MXID, conv.ConversationID, req.TimeStart, timeEnd, req.MaxTotalEvents)
  258. sendDisappearedNotice := false
  259. // If expired messages are on, and a notice has not been sent to this chat
  260. // about it having disappeared messages at the conversation timestamp, send
  261. // a notice indicating so.
  262. if len(allMsgs) == 0 && conv.EphemeralExpiration != nil && *conv.EphemeralExpiration > 0 {
  263. lastMessage := portal.bridge.DB.Message.GetLastInChat(portal.Key)
  264. if lastMessage == nil || conv.LastMessageTimestamp.After(lastMessage.Timestamp) {
  265. sendDisappearedNotice = true
  266. }
  267. }
  268. if !sendDisappearedNotice && len(allMsgs) == 0 {
  269. user.log.Debugfln("Not backfilling %s: no bridgeable messages found", portal.Key.JID)
  270. return
  271. }
  272. if len(portal.MXID) == 0 {
  273. user.log.Debugln("Creating portal for", portal.Key.JID, "as part of history sync handling")
  274. err := portal.CreateMatrixRoom(user, nil, true, false)
  275. if err != nil {
  276. user.log.Errorfln("Failed to create room for %s during backfill: %v", portal.Key.JID, err)
  277. return
  278. }
  279. }
  280. // Update the backfill status here after the room has been created.
  281. portal.updateBackfillStatus(backfillState)
  282. if sendDisappearedNotice {
  283. user.log.Debugfln("Sending notice to %s that there are disappeared messages ending at %v", portal.Key.JID, conv.LastMessageTimestamp)
  284. resp, err := portal.sendMessage(portal.MainIntent(), event.EventMessage, &event.MessageEventContent{
  285. MsgType: event.MsgNotice,
  286. Body: portal.formatDisappearingMessageNotice(),
  287. }, nil, conv.LastMessageTimestamp.UnixMilli())
  288. if err != nil {
  289. portal.log.Errorln("Error sending disappearing messages notice event")
  290. return
  291. }
  292. msg := portal.bridge.DB.Message.New()
  293. msg.Chat = portal.Key
  294. msg.MXID = resp.EventID
  295. msg.JID = types.MessageID(resp.EventID)
  296. msg.Timestamp = conv.LastMessageTimestamp
  297. msg.SenderMXID = portal.MainIntent().UserID
  298. msg.Sent = true
  299. msg.Type = database.MsgFake
  300. msg.Insert(nil)
  301. user.markSelfReadFull(portal)
  302. return
  303. }
  304. user.log.Infofln("Backfilling %d messages in %s, %d messages at a time (queue ID: %d)", len(allMsgs), portal.Key.JID, req.MaxBatchEvents, req.QueueID)
  305. toBackfill := allMsgs[0:]
  306. var insertionEventIds []id.EventID
  307. for len(toBackfill) > 0 {
  308. var msgs []*waProto.WebMessageInfo
  309. if len(toBackfill) <= req.MaxBatchEvents || req.MaxBatchEvents < 0 {
  310. msgs = toBackfill
  311. toBackfill = nil
  312. } else {
  313. msgs = toBackfill[:req.MaxBatchEvents]
  314. toBackfill = toBackfill[req.MaxBatchEvents:]
  315. }
  316. if len(msgs) > 0 {
  317. time.Sleep(time.Duration(req.BatchDelay) * time.Second)
  318. user.log.Debugfln("Backfilling %d messages in %s (queue ID: %d)", len(msgs), portal.Key.JID, req.QueueID)
  319. resp := portal.backfill(user, msgs, req.BackfillType == database.BackfillForward, isLatestEvents, shouldAtomicallyMarkAsRead, forwardPrevID)
  320. if resp != nil && (resp.BaseInsertionEventID != "" || !isLatestEvents) {
  321. insertionEventIds = append(insertionEventIds, resp.BaseInsertionEventID)
  322. }
  323. }
  324. }
  325. user.log.Debugfln("Finished backfilling %d messages in %s (queue ID: %d)", len(allMsgs), portal.Key.JID, req.QueueID)
  326. if len(insertionEventIds) > 0 {
  327. portal.sendPostBackfillDummy(
  328. time.Unix(int64(allMsgs[0].GetMessageTimestamp()), 0),
  329. insertionEventIds[0])
  330. }
  331. user.log.Debugfln("Deleting %d history sync messages after backfilling (queue ID: %d)", len(allMsgs), req.QueueID)
  332. err := user.bridge.DB.HistorySync.DeleteMessages(user.MXID, conv.ConversationID, allMsgs)
  333. if err != nil {
  334. user.log.Warnfln("Failed to delete %d history sync messages after backfilling (queue ID: %d): %v", len(allMsgs), req.QueueID, err)
  335. }
  336. if req.TimeStart == nil {
  337. // If the time start is nil, then there's no more history to backfill.
  338. backfillState.BackfillComplete = true
  339. if conv.EndOfHistoryTransferType == waProto.Conversation_COMPLETE_BUT_MORE_MESSAGES_REMAIN_ON_PRIMARY {
  340. // Since there are more messages on the phone, but we can't
  341. // backfill any more of them, indicate that the last timestamp
  342. // that we expect to be backfilled is the oldest one that was just
  343. // backfilled.
  344. backfillState.FirstExpectedTimestamp = allMsgs[len(allMsgs)-1].GetMessageTimestamp()
  345. } else if conv.EndOfHistoryTransferType == waProto.Conversation_COMPLETE_AND_NO_MORE_MESSAGE_REMAIN_ON_PRIMARY {
  346. // Since there are no more messages left on the phone, we've
  347. // backfilled everything. Indicate so by setting the expected
  348. // timestamp to 0 which means that the backfill goes to the
  349. // beginning of time.
  350. backfillState.FirstExpectedTimestamp = 0
  351. }
  352. backfillState.Upsert()
  353. portal.updateBackfillStatus(backfillState)
  354. }
  355. if isLatestEvents && !shouldAtomicallyMarkAsRead {
  356. if shouldMarkAsRead {
  357. user.markSelfReadFull(portal)
  358. } else if conv.MarkedAsUnread && user.bridge.Config.Bridge.SyncManualMarkedUnread {
  359. user.markUnread(portal, true)
  360. }
  361. }
  362. }
  363. func (user *User) shouldCreatePortalForHistorySync(conv *database.HistorySyncConversation, portal *Portal) bool {
  364. if len(portal.MXID) > 0 {
  365. if !user.bridge.AS.StateStore.IsInRoom(portal.MXID, user.MXID) {
  366. portal.ensureUserInvited(user)
  367. }
  368. // Portal exists, let backfill continue
  369. return true
  370. } else if !user.bridge.Config.Bridge.HistorySync.CreatePortals {
  371. user.log.Debugfln("Not creating portal for %s: creating rooms from history sync is disabled", portal.Key.JID)
  372. return false
  373. } else {
  374. // Portal doesn't exist, but should be created
  375. return true
  376. }
  377. }
  378. func (user *User) storeHistorySync(evt *waProto.HistorySync) {
  379. if evt == nil || evt.SyncType == nil {
  380. return
  381. }
  382. log := user.bridge.ZLog.With().
  383. Str("method", "User.storeHistorySync").
  384. Str("user_id", user.MXID.String()).
  385. Str("sync_type", evt.GetSyncType().String()).
  386. Uint32("chunk_order", evt.GetChunkOrder()).
  387. Uint32("progress", evt.GetProgress()).
  388. Logger()
  389. if evt.GetGlobalSettings() != nil {
  390. log.Debug().Interface("global_settings", evt.GetGlobalSettings()).Msg("Got global settings in history sync")
  391. }
  392. if evt.GetSyncType() == waProto.HistorySync_INITIAL_STATUS_V3 || evt.GetSyncType() == waProto.HistorySync_PUSH_NAME || evt.GetSyncType() == waProto.HistorySync_NON_BLOCKING_DATA {
  393. log.Debug().
  394. Int("conversation_count", len(evt.GetConversations())).
  395. Int("pushname_count", len(evt.GetPushnames())).
  396. Int("status_count", len(evt.GetStatusV3Messages())).
  397. Int("recent_sticker_count", len(evt.GetRecentStickers())).
  398. Int("past_participant_count", len(evt.GetPastParticipants())).
  399. Msg("Ignoring history sync")
  400. return
  401. }
  402. log.Info().
  403. Int("conversation_count", len(evt.GetConversations())).
  404. Int("past_participant_count", len(evt.GetPastParticipants())).
  405. Msg("Storing history sync")
  406. successfullySavedTotal := 0
  407. totalMessageCount := 0
  408. for _, conv := range evt.GetConversations() {
  409. jid, err := types.ParseJID(conv.GetId())
  410. if err != nil {
  411. totalMessageCount += len(conv.GetMessages())
  412. log.Warn().Err(err).
  413. Str("chat_jid", conv.GetId()).
  414. Int("msg_count", len(conv.GetMessages())).
  415. Msg("Failed to parse chat JID in history sync")
  416. continue
  417. } else if jid.Server == types.BroadcastServer {
  418. log.Debug().Str("chat_jid", jid.String()).Msg("Skipping broadcast list in history sync")
  419. continue
  420. }
  421. totalMessageCount += len(conv.GetMessages())
  422. portal := user.GetPortalByJID(jid)
  423. log := log.With().
  424. Str("chat_jid", portal.Key.JID.String()).
  425. Int("msg_count", len(conv.GetMessages())).
  426. Logger()
  427. historySyncConversation := user.bridge.DB.HistorySync.NewConversationWithValues(
  428. user.MXID,
  429. conv.GetId(),
  430. &portal.Key,
  431. getConversationTimestamp(conv),
  432. conv.GetMuteEndTime(),
  433. conv.GetArchived(),
  434. conv.GetPinned(),
  435. conv.GetDisappearingMode().GetInitiator(),
  436. conv.GetEndOfHistoryTransferType(),
  437. conv.EphemeralExpiration,
  438. conv.GetMarkedAsUnread(),
  439. conv.GetUnreadCount())
  440. historySyncConversation.Upsert()
  441. var minTime, maxTime time.Time
  442. var minTimeIndex, maxTimeIndex int
  443. successfullySaved := 0
  444. unsupportedTypes := 0
  445. for i, rawMsg := range conv.GetMessages() {
  446. // Don't store messages that will just be skipped.
  447. msgEvt, err := user.Client.ParseWebMessage(portal.Key.JID, rawMsg.GetMessage())
  448. if err != nil {
  449. log.Warn().Err(err).
  450. Int("msg_index", i).
  451. Str("msg_id", rawMsg.GetMessage().GetKey().GetId()).
  452. Uint64("msg_time_seconds", rawMsg.GetMessage().GetMessageTimestamp()).
  453. Msg("Dropping historical message due to parse error")
  454. continue
  455. }
  456. if minTime.IsZero() || msgEvt.Info.Timestamp.Before(minTime) {
  457. minTime = msgEvt.Info.Timestamp
  458. minTimeIndex = i
  459. }
  460. if maxTime.IsZero() || msgEvt.Info.Timestamp.After(maxTime) {
  461. maxTime = msgEvt.Info.Timestamp
  462. maxTimeIndex = i
  463. }
  464. msgType := getMessageType(msgEvt.Message)
  465. if msgType == "unknown" || msgType == "ignore" || msgType == "unknown_protocol" {
  466. unsupportedTypes++
  467. continue
  468. }
  469. // Don't store unsupported messages.
  470. if !containsSupportedMessage(msgEvt.Message) {
  471. unsupportedTypes++
  472. continue
  473. }
  474. message, err := user.bridge.DB.HistorySync.NewMessageWithValues(user.MXID, conv.GetId(), msgEvt.Info.ID, rawMsg)
  475. if err != nil {
  476. log.Error().Err(err).
  477. Int("msg_index", i).
  478. Str("msg_id", msgEvt.Info.ID).
  479. Time("msg_time", msgEvt.Info.Timestamp).
  480. Msg("Failed to save historical message")
  481. continue
  482. }
  483. err = message.Insert()
  484. if err != nil {
  485. log.Error().Err(err).
  486. Int("msg_index", i).
  487. Str("msg_id", msgEvt.Info.ID).
  488. Time("msg_time", msgEvt.Info.Timestamp).
  489. Msg("Failed to save historical message")
  490. }
  491. successfullySaved++
  492. }
  493. successfullySavedTotal += successfullySaved
  494. log.Debug().
  495. Int("saved_count", successfullySaved).
  496. Int("unsupported_msg_type_count", unsupportedTypes).
  497. Time("lowest_time", minTime).
  498. Int("lowest_time_index", minTimeIndex).
  499. Time("highest_time", maxTime).
  500. Int("highest_time_index", maxTimeIndex).
  501. Msg("Saved messages from history sync conversation")
  502. }
  503. log.Info().
  504. Int("total_saved_count", successfullySavedTotal).
  505. Int("total_message_count", totalMessageCount).
  506. Msg("Finished storing history sync")
  507. // If this was the initial bootstrap, enqueue immediate backfills for the
  508. // most recent portals. If it's the last history sync event, start
  509. // backfilling the rest of the history of the portals.
  510. if user.bridge.Config.Bridge.HistorySync.Backfill {
  511. user.enqueueBackfillsTimer.Reset(EnqueueBackfillsDelay)
  512. }
  513. }
  514. func getConversationTimestamp(conv *waProto.Conversation) uint64 {
  515. convTs := conv.GetConversationTimestamp()
  516. if convTs == 0 && len(conv.GetMessages()) > 0 {
  517. convTs = conv.Messages[0].GetMessage().GetMessageTimestamp()
  518. }
  519. return convTs
  520. }
  521. func (user *User) EnqueueImmediateBackfills(portals []*Portal) {
  522. for priority, portal := range portals {
  523. maxMessages := user.bridge.Config.Bridge.HistorySync.Immediate.MaxEvents
  524. initialBackfill := user.bridge.DB.Backfill.NewWithValues(user.MXID, database.BackfillImmediate, priority, &portal.Key, nil, maxMessages, maxMessages, 0)
  525. initialBackfill.Insert()
  526. }
  527. }
  528. func (user *User) EnqueueDeferredBackfills(portals []*Portal) {
  529. numPortals := len(portals)
  530. for stageIdx, backfillStage := range user.bridge.Config.Bridge.HistorySync.Deferred {
  531. for portalIdx, portal := range portals {
  532. var startDate *time.Time = nil
  533. if backfillStage.StartDaysAgo > 0 {
  534. startDaysAgo := time.Now().AddDate(0, 0, -backfillStage.StartDaysAgo)
  535. startDate = &startDaysAgo
  536. }
  537. backfillMessages := user.bridge.DB.Backfill.NewWithValues(
  538. user.MXID, database.BackfillDeferred, stageIdx*numPortals+portalIdx, &portal.Key, startDate, backfillStage.MaxBatchEvents, -1, backfillStage.BatchDelay)
  539. backfillMessages.Insert()
  540. }
  541. }
  542. }
  543. func (user *User) EnqueueForwardBackfills(portals []*Portal) {
  544. for priority, portal := range portals {
  545. lastMsg := user.bridge.DB.Message.GetLastInChat(portal.Key)
  546. if lastMsg == nil {
  547. continue
  548. }
  549. backfill := user.bridge.DB.Backfill.NewWithValues(
  550. user.MXID, database.BackfillForward, priority, &portal.Key, &lastMsg.Timestamp, -1, -1, 0)
  551. backfill.Insert()
  552. }
  553. }
  554. // endregion
  555. // region Portal backfilling
  556. func (portal *Portal) deterministicEventID(sender types.JID, messageID types.MessageID, partName string) id.EventID {
  557. data := fmt.Sprintf("%s/whatsapp/%s/%s", portal.MXID, sender.User, messageID)
  558. if partName != "" {
  559. data += "/" + partName
  560. }
  561. sum := sha256.Sum256([]byte(data))
  562. return id.EventID(fmt.Sprintf("$%s:whatsapp.com", base64.RawURLEncoding.EncodeToString(sum[:])))
  563. }
  564. var (
  565. PortalCreationDummyEvent = event.Type{Type: "fi.mau.dummy.portal_created", Class: event.MessageEventType}
  566. PreBackfillDummyEvent = event.Type{Type: "fi.mau.dummy.pre_backfill", Class: event.MessageEventType}
  567. HistorySyncMarker = event.Type{Type: "org.matrix.msc2716.marker", Class: event.MessageEventType}
  568. BackfillStatusEvent = event.Type{Type: "com.beeper.backfill_status", Class: event.StateEventType}
  569. )
  570. func (portal *Portal) backfill(source *User, messages []*waProto.WebMessageInfo, isForward, isLatest, atomicMarkAsRead bool, prevEventID id.EventID) *mautrix.RespBatchSend {
  571. var req mautrix.ReqBatchSend
  572. var infos []*wrappedInfo
  573. if !isForward {
  574. if portal.FirstEventID != "" || portal.NextBatchID != "" {
  575. req.PrevEventID = portal.FirstEventID
  576. req.BatchID = portal.NextBatchID
  577. } else {
  578. portal.log.Warnfln("Can't backfill %d messages through %s to chat: first event ID not known", len(messages), source.MXID)
  579. return nil
  580. }
  581. } else {
  582. req.PrevEventID = prevEventID
  583. }
  584. req.BeeperNewMessages = isLatest && req.BatchID == ""
  585. if atomicMarkAsRead {
  586. req.BeeperMarkReadBy = source.MXID
  587. }
  588. beforeFirstMessageTimestampMillis := (int64(messages[len(messages)-1].GetMessageTimestamp()) * 1000) - 1
  589. req.StateEventsAtStart = make([]*event.Event, 0)
  590. addedMembers := make(map[id.UserID]struct{})
  591. addMember := func(puppet *Puppet) {
  592. if portal.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareHungry {
  593. // Hungryserv doesn't need state_events_at_start, it can figure out memberships automatically
  594. return
  595. } else if _, alreadyAdded := addedMembers[puppet.MXID]; alreadyAdded {
  596. return
  597. }
  598. mxid := puppet.MXID.String()
  599. content := event.MemberEventContent{
  600. Membership: event.MembershipJoin,
  601. Displayname: puppet.Displayname,
  602. AvatarURL: puppet.AvatarURL.CUString(),
  603. }
  604. inviteContent := content
  605. inviteContent.Membership = event.MembershipInvite
  606. req.StateEventsAtStart = append(req.StateEventsAtStart, &event.Event{
  607. Type: event.StateMember,
  608. Sender: portal.MainIntent().UserID,
  609. StateKey: &mxid,
  610. Timestamp: beforeFirstMessageTimestampMillis,
  611. Content: event.Content{Parsed: &inviteContent},
  612. }, &event.Event{
  613. Type: event.StateMember,
  614. Sender: puppet.MXID,
  615. StateKey: &mxid,
  616. Timestamp: beforeFirstMessageTimestampMillis,
  617. Content: event.Content{Parsed: &content},
  618. })
  619. addedMembers[puppet.MXID] = struct{}{}
  620. }
  621. portal.log.Infofln("Processing history sync with %d messages (forward: %t, latest: %t, prev: %s, batch: %s)", len(messages), isForward, isLatest, req.PrevEventID, req.BatchID)
  622. // The messages are ordered newest to oldest, so iterate them in reverse order.
  623. for i := len(messages) - 1; i >= 0; i-- {
  624. webMsg := messages[i]
  625. msgEvt, err := source.Client.ParseWebMessage(portal.Key.JID, webMsg)
  626. if err != nil {
  627. continue
  628. }
  629. msgType := getMessageType(msgEvt.Message)
  630. if msgType == "unknown" || msgType == "ignore" || msgType == "unknown_protocol" {
  631. if msgType != "ignore" {
  632. portal.log.Debugfln("Skipping message %s with unknown type in backfill", msgEvt.Info.ID)
  633. }
  634. continue
  635. }
  636. if webMsg.GetPushName() != "" && webMsg.GetPushName() != "-" {
  637. existingContact, _ := source.Client.Store.Contacts.GetContact(msgEvt.Info.Sender)
  638. if !existingContact.Found || existingContact.PushName == "" {
  639. changed, _, err := source.Client.Store.Contacts.PutPushName(msgEvt.Info.Sender, webMsg.GetPushName())
  640. if err != nil {
  641. source.log.Errorfln("Failed to save push name of %s from historical message in device store: %v", msgEvt.Info.Sender, err)
  642. } else if changed {
  643. source.log.Debugfln("Got push name %s for %s from historical message", webMsg.GetPushName(), msgEvt.Info.Sender)
  644. }
  645. }
  646. }
  647. puppet := portal.getMessagePuppet(source, &msgEvt.Info)
  648. if puppet == nil {
  649. continue
  650. }
  651. intent := puppet.IntentFor(portal)
  652. if intent.IsCustomPuppet && !portal.bridge.Config.CanDoublePuppetBackfill(puppet.CustomMXID) {
  653. intent = puppet.DefaultIntent()
  654. }
  655. converted := portal.convertMessage(intent, source, &msgEvt.Info, msgEvt.Message, true)
  656. if converted == nil {
  657. portal.log.Debugfln("Skipping unsupported message %s in backfill", msgEvt.Info.ID)
  658. continue
  659. }
  660. if !intent.IsCustomPuppet && !portal.bridge.StateStore.IsInRoom(portal.MXID, puppet.MXID) {
  661. addMember(puppet)
  662. }
  663. if converted.ReplyTo != nil {
  664. portal.SetReply(converted.Content, converted.ReplyTo, true)
  665. }
  666. err = portal.appendBatchEvents(source, converted, &msgEvt.Info, webMsg, &req.Events, &infos)
  667. if err != nil {
  668. portal.log.Errorfln("Error handling message %s during backfill: %v", msgEvt.Info.ID, err)
  669. }
  670. }
  671. portal.log.Infofln("Made %d Matrix events from messages in batch", len(req.Events))
  672. if len(req.Events) == 0 {
  673. return nil
  674. }
  675. if len(req.BatchID) == 0 || isForward {
  676. portal.log.Debugln("Sending a dummy event to avoid forward extremity errors with backfill")
  677. _, err := portal.MainIntent().SendMessageEvent(portal.MXID, PreBackfillDummyEvent, struct{}{})
  678. if err != nil {
  679. portal.log.Warnln("Error sending pre-backfill dummy event:", err)
  680. }
  681. }
  682. resp, err := portal.MainIntent().BatchSend(portal.MXID, &req)
  683. if err != nil {
  684. portal.log.Errorln("Error batch sending messages:", err)
  685. return nil
  686. } else {
  687. txn, err := portal.bridge.DB.Begin()
  688. if err != nil {
  689. portal.log.Errorln("Failed to start transaction to save batch messages:", err)
  690. return nil
  691. }
  692. // Do the following block in the transaction
  693. {
  694. portal.finishBatch(txn, resp.EventIDs, infos)
  695. portal.NextBatchID = resp.NextBatchID
  696. portal.Update(txn)
  697. }
  698. err = txn.Commit()
  699. if err != nil {
  700. portal.log.Errorln("Failed to commit transaction to save batch messages:", err)
  701. return nil
  702. }
  703. if portal.bridge.Config.Bridge.HistorySync.MediaRequests.AutoRequestMedia {
  704. go portal.requestMediaRetries(source, resp.EventIDs, infos)
  705. }
  706. return resp
  707. }
  708. }
  709. func (portal *Portal) requestMediaRetries(source *User, eventIDs []id.EventID, infos []*wrappedInfo) {
  710. for i, info := range infos {
  711. if info != nil && info.Error == database.MsgErrMediaNotFound && info.MediaKey != nil {
  712. switch portal.bridge.Config.Bridge.HistorySync.MediaRequests.RequestMethod {
  713. case config.MediaRequestMethodImmediate:
  714. err := source.Client.SendMediaRetryReceipt(info.MessageInfo, info.MediaKey)
  715. if err != nil {
  716. portal.log.Warnfln("Failed to send post-backfill media retry request for %s: %v", info.ID, err)
  717. } else {
  718. portal.log.Debugfln("Sent post-backfill media retry request for %s", info.ID)
  719. }
  720. case config.MediaRequestMethodLocalTime:
  721. req := portal.bridge.DB.MediaBackfillRequest.NewMediaBackfillRequestWithValues(source.MXID, &portal.Key, eventIDs[i], info.MediaKey)
  722. req.Upsert()
  723. }
  724. }
  725. }
  726. }
  727. func (portal *Portal) appendBatchEvents(source *User, converted *ConvertedMessage, info *types.MessageInfo, raw *waProto.WebMessageInfo, eventsArray *[]*event.Event, infoArray *[]*wrappedInfo) error {
  728. if portal.bridge.Config.Bridge.CaptionInMessage {
  729. converted.MergeCaption()
  730. }
  731. mainEvt, err := portal.wrapBatchEvent(info, converted.Intent, converted.Type, converted.Content, converted.Extra, "")
  732. if err != nil {
  733. return err
  734. }
  735. expirationStart := info.Timestamp
  736. if raw.GetEphemeralStartTimestamp() > 0 {
  737. expirationStart = time.Unix(int64(raw.GetEphemeralStartTimestamp()), 0)
  738. }
  739. mainInfo := &wrappedInfo{
  740. MessageInfo: info,
  741. Type: database.MsgNormal,
  742. SenderMXID: mainEvt.Sender,
  743. Error: converted.Error,
  744. MediaKey: converted.MediaKey,
  745. ExpirationStart: expirationStart,
  746. ExpiresIn: converted.ExpiresIn,
  747. }
  748. if converted.Caption != nil {
  749. captionEvt, err := portal.wrapBatchEvent(info, converted.Intent, converted.Type, converted.Caption, nil, "caption")
  750. if err != nil {
  751. return err
  752. }
  753. *eventsArray = append(*eventsArray, mainEvt, captionEvt)
  754. *infoArray = append(*infoArray, mainInfo, nil)
  755. } else {
  756. *eventsArray = append(*eventsArray, mainEvt)
  757. *infoArray = append(*infoArray, mainInfo)
  758. }
  759. if converted.MultiEvent != nil {
  760. for i, subEvtContent := range converted.MultiEvent {
  761. subEvt, err := portal.wrapBatchEvent(info, converted.Intent, converted.Type, subEvtContent, nil, fmt.Sprintf("multi-%d", i))
  762. if err != nil {
  763. return err
  764. }
  765. *eventsArray = append(*eventsArray, subEvt)
  766. *infoArray = append(*infoArray, nil)
  767. }
  768. }
  769. // Sending reactions in the same batch requires deterministic event IDs, so only do it on hungryserv
  770. if portal.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareHungry {
  771. for _, reaction := range raw.GetReactions() {
  772. reactionEvent, reactionInfo := portal.wrapBatchReaction(source, reaction, mainEvt.ID, info.Timestamp)
  773. if reactionEvent != nil {
  774. *eventsArray = append(*eventsArray, reactionEvent)
  775. *infoArray = append(*infoArray, &wrappedInfo{
  776. MessageInfo: reactionInfo,
  777. SenderMXID: reactionEvent.Sender,
  778. ReactionTarget: info.ID,
  779. Type: database.MsgReaction,
  780. })
  781. }
  782. }
  783. }
  784. return nil
  785. }
  786. func (portal *Portal) wrapBatchReaction(source *User, reaction *waProto.Reaction, mainEventID id.EventID, mainEventTS time.Time) (reactionEvent *event.Event, reactionInfo *types.MessageInfo) {
  787. var senderJID types.JID
  788. if reaction.GetKey().GetFromMe() {
  789. senderJID = source.JID.ToNonAD()
  790. } else if reaction.GetKey().GetParticipant() != "" {
  791. senderJID, _ = types.ParseJID(reaction.GetKey().GetParticipant())
  792. } else if portal.IsPrivateChat() {
  793. senderJID = portal.Key.JID
  794. }
  795. if senderJID.IsEmpty() {
  796. return
  797. }
  798. reactionInfo = &types.MessageInfo{
  799. MessageSource: types.MessageSource{
  800. Chat: portal.Key.JID,
  801. Sender: senderJID,
  802. IsFromMe: reaction.GetKey().GetFromMe(),
  803. IsGroup: portal.IsGroupChat(),
  804. },
  805. ID: reaction.GetKey().GetId(),
  806. Timestamp: mainEventTS,
  807. }
  808. puppet := portal.getMessagePuppet(source, reactionInfo)
  809. if puppet == nil {
  810. return
  811. }
  812. intent := puppet.IntentFor(portal)
  813. content := event.ReactionEventContent{
  814. RelatesTo: event.RelatesTo{
  815. Type: event.RelAnnotation,
  816. EventID: mainEventID,
  817. Key: variationselector.Add(reaction.GetText()),
  818. },
  819. }
  820. if rawTS := reaction.GetSenderTimestampMs(); rawTS >= mainEventTS.UnixMilli() && rawTS <= time.Now().UnixMilli() {
  821. reactionInfo.Timestamp = time.UnixMilli(rawTS)
  822. }
  823. wrappedContent := event.Content{Parsed: &content}
  824. intent.AddDoublePuppetValue(&wrappedContent)
  825. reactionEvent = &event.Event{
  826. ID: portal.deterministicEventID(senderJID, reactionInfo.ID, ""),
  827. Type: event.EventReaction,
  828. Content: wrappedContent,
  829. Sender: intent.UserID,
  830. Timestamp: reactionInfo.Timestamp.UnixMilli(),
  831. }
  832. return
  833. }
  834. func (portal *Portal) wrapBatchEvent(info *types.MessageInfo, intent *appservice.IntentAPI, eventType event.Type, content *event.MessageEventContent, extraContent map[string]interface{}, partName string) (*event.Event, error) {
  835. wrappedContent := event.Content{
  836. Parsed: content,
  837. Raw: extraContent,
  838. }
  839. newEventType, err := portal.encrypt(intent, &wrappedContent, eventType)
  840. if err != nil {
  841. return nil, err
  842. }
  843. intent.AddDoublePuppetValue(&wrappedContent)
  844. var eventID id.EventID
  845. if portal.bridge.Config.Homeserver.Software == bridgeconfig.SoftwareHungry {
  846. eventID = portal.deterministicEventID(info.Sender, info.ID, partName)
  847. }
  848. return &event.Event{
  849. ID: eventID,
  850. Sender: intent.UserID,
  851. Type: newEventType,
  852. Timestamp: info.Timestamp.UnixMilli(),
  853. Content: wrappedContent,
  854. }, nil
  855. }
  856. func (portal *Portal) finishBatch(txn dbutil.Transaction, eventIDs []id.EventID, infos []*wrappedInfo) {
  857. for i, info := range infos {
  858. if info == nil {
  859. continue
  860. }
  861. eventID := eventIDs[i]
  862. portal.markHandled(txn, nil, info.MessageInfo, eventID, info.SenderMXID, true, false, info.Type, info.Error)
  863. if info.Type == database.MsgReaction {
  864. portal.upsertReaction(txn, nil, info.ReactionTarget, info.Sender, eventID, info.ID)
  865. }
  866. if info.ExpiresIn > 0 {
  867. portal.MarkDisappearing(txn, eventID, info.ExpiresIn, info.ExpirationStart)
  868. }
  869. }
  870. portal.log.Infofln("Successfully sent %d events", len(eventIDs))
  871. }
  872. func (portal *Portal) sendPostBackfillDummy(lastTimestamp time.Time, insertionEventId id.EventID) {
  873. resp, err := portal.MainIntent().SendMessageEvent(portal.MXID, HistorySyncMarker, map[string]interface{}{
  874. "org.matrix.msc2716.marker.insertion": insertionEventId,
  875. //"m.marker.insertion": insertionEventId,
  876. })
  877. if err != nil {
  878. portal.log.Errorln("Error sending post-backfill dummy event:", err)
  879. return
  880. }
  881. msg := portal.bridge.DB.Message.New()
  882. msg.Chat = portal.Key
  883. msg.MXID = resp.EventID
  884. msg.SenderMXID = portal.MainIntent().UserID
  885. msg.JID = types.MessageID(resp.EventID)
  886. msg.Timestamp = lastTimestamp.Add(1 * time.Second)
  887. msg.Sent = true
  888. msg.Type = database.MsgFake
  889. msg.Insert(nil)
  890. }
  891. func (portal *Portal) updateBackfillStatus(backfillState *database.BackfillState) {
  892. backfillStatus := "backfilling"
  893. if backfillState.BackfillComplete {
  894. backfillStatus = "complete"
  895. }
  896. _, err := portal.MainIntent().SendStateEvent(portal.MXID, BackfillStatusEvent, "", map[string]interface{}{
  897. "status": backfillStatus,
  898. "first_timestamp": backfillState.FirstExpectedTimestamp * 1000,
  899. })
  900. if err != nil {
  901. portal.log.Errorln("Error sending backfill status event:", err)
  902. }
  903. }
  904. // endregion