thread.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package main
  2. import (
  3. "maunium.net/go/mautrix/id"
  4. "go.mau.fi/mautrix-discord/database"
  5. )
  6. type Thread struct {
  7. *database.Thread
  8. Parent *Portal
  9. }
  10. func (br *DiscordBridge) GetThreadByID(id string, root *database.Message) *Thread {
  11. br.threadsLock.Lock()
  12. defer br.threadsLock.Unlock()
  13. thread, ok := br.threadsByID[id]
  14. if !ok {
  15. return br.loadThread(br.DB.Thread.GetByDiscordID(id), id, root)
  16. }
  17. return thread
  18. }
  19. func (br *DiscordBridge) GetThreadByRootMXID(mxid id.EventID) *Thread {
  20. br.threadsLock.Lock()
  21. defer br.threadsLock.Unlock()
  22. thread, ok := br.threadsByRootMXID[mxid]
  23. if !ok {
  24. return br.loadThread(br.DB.Thread.GetByMatrixRootMsg(mxid), "", nil)
  25. }
  26. return thread
  27. }
  28. func (br *DiscordBridge) loadThread(dbThread *database.Thread, id string, root *database.Message) *Thread {
  29. if dbThread == nil {
  30. if root == nil {
  31. return nil
  32. }
  33. dbThread = br.DB.Thread.New()
  34. dbThread.ID = id
  35. dbThread.RootDiscordID = root.DiscordID
  36. dbThread.RootMXID = root.MXID
  37. dbThread.ParentID = root.Channel.ChannelID
  38. dbThread.Insert()
  39. }
  40. thread := &Thread{
  41. Thread: dbThread,
  42. }
  43. thread.Parent = br.GetExistingPortalByID(database.NewPortalKey(thread.ParentID, ""))
  44. br.threadsByID[thread.ID] = thread
  45. br.threadsByRootMXID[thread.RootMXID] = thread
  46. return thread
  47. }