You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.6 KiB

  1. package clients
  2. import (
  3. "errors"
  4. "maunium.net/go/mautrix"
  5. "maunium.net/go/mautrix/event"
  6. "maunium.net/go/mautrix/id"
  7. )
  8. // NebStateStore implements the StateStore interface for OlmMachine.
  9. // It is used to determine which rooms are encrypted and which rooms are shared with a user.
  10. // The state is updated by /sync responses.
  11. type NebStateStore struct {
  12. Storer *mautrix.InMemoryStore
  13. }
  14. // GetEncryptionEvent returns the encryption event for a room.
  15. func (ss *NebStateStore) GetEncryptionEvent(roomID id.RoomID) *event.EncryptionEventContent {
  16. room := ss.Storer.LoadRoom(roomID)
  17. if room == nil {
  18. return nil
  19. }
  20. if evts, ok := room.State[event.StateEncryption]; ok {
  21. if evt, ok := evts[""]; ok {
  22. return evt.Content.AsEncryption()
  23. }
  24. }
  25. return nil
  26. }
  27. // IsEncrypted returns whether a room has been encrypted.
  28. func (ss *NebStateStore) IsEncrypted(roomID id.RoomID) bool {
  29. room := ss.Storer.LoadRoom(roomID)
  30. if room == nil {
  31. return false
  32. }
  33. _, ok := room.State[event.StateEncryption]
  34. return ok
  35. }
  36. // FindSharedRooms returns a list of room IDs that the given user ID is also a member of.
  37. func (ss *NebStateStore) FindSharedRooms(userID id.UserID) []id.RoomID {
  38. sharedRooms := make([]id.RoomID, 0)
  39. for roomID, room := range ss.Storer.Rooms {
  40. if room.GetMembershipState(userID) != event.MembershipLeave {
  41. sharedRooms = append(sharedRooms, roomID)
  42. }
  43. }
  44. return sharedRooms
  45. }
  46. // UpdateStateStore updates the internal state of NebStateStore from a /sync response.
  47. func (ss *NebStateStore) UpdateStateStore(resp *mautrix.RespSync) {
  48. for roomID, evts := range resp.Rooms.Join {
  49. room := ss.Storer.LoadRoom(roomID)
  50. if room == nil {
  51. room = mautrix.NewRoom(roomID)
  52. ss.Storer.SaveRoom(room)
  53. }
  54. for _, i := range evts.State.Events {
  55. room.UpdateState(i)
  56. }
  57. for _, i := range evts.Timeline.Events {
  58. if i.Type.IsState() {
  59. room.UpdateState(i)
  60. }
  61. }
  62. }
  63. }
  64. // GetJoinedMembers returns a list of members that are currently in a room.
  65. func (ss *NebStateStore) GetJoinedMembers(roomID id.RoomID) ([]id.UserID, error) {
  66. joinedMembers := make([]id.UserID, 0)
  67. room := ss.Storer.LoadRoom(roomID)
  68. if room == nil {
  69. return nil, errors.New("unknown roomID")
  70. }
  71. memberEvents := room.State[event.StateMember]
  72. if memberEvents == nil {
  73. return nil, errors.New("no state member events found")
  74. }
  75. for stateKey, stateEvent := range memberEvents {
  76. if stateEvent == nil {
  77. continue
  78. }
  79. stateEvent.Content.ParseRaw(event.StateMember)
  80. if stateEvent.Content.AsMember().Membership == event.MembershipJoin {
  81. joinedMembers = append(joinedMembers, id.UserID(stateKey))
  82. }
  83. }
  84. return joinedMembers, nil
  85. }