Class MatrixService

java.lang.Object
io.meeds.chat.service.MatrixService

@Service public class MatrixService extends Object
  • Field Details

    • CHAT_APPLICATION_ID

      public static final String CHAT_APPLICATION_ID
      See Also:
    • USER_MATRIX_ID_CACHE_NAME

      public static final String USER_MATRIX_ID_CACHE_NAME
      See Also:
    • defaultAuthorizedSpaceTemplates

      @Value("#{'${meeds.chat.authorized.space.templates:project,circle}'.split(',')}") public List<String> defaultAuthorizedSpaceTemplates
    • USER_ACCESS_TOKEN_CACHE_NAME

      public static final String USER_ACCESS_TOKEN_CACHE_NAME
      See Also:
  • Constructor Details

    • MatrixService

      public MatrixService(MatrixRoomStorage matrixRoomStorage, org.exoplatform.social.core.manager.IdentityManager identityManager, org.exoplatform.social.core.storage.api.IdentityStorage identityStorage, org.exoplatform.services.organization.OrganizationService organizationService, MatrixHttpClient matrixHttpClient, org.exoplatform.services.cache.CacheService cacheService, org.exoplatform.commons.api.settings.SettingService settingService, io.meeds.social.space.template.service.SpaceTemplateService spaceTemplateService)
  • Method Details

    • init

      @PostConstruct public void init()
      Kicks off the connection to the Matrix server on a background thread so that the (possibly slow, possibly retried) handshake never blocks the WAR deployment / server startup. See AsyncTaskUtils.runAsync(String, Runnable) for the escape hatch used by tests to run it synchronously instead.
    • isServiceEnabled

      public boolean isServiceEnabled()
      Checks if the Chat feature is enabled and that the Matrix service is available
      Returns:
      the status fo the chat service and feature
    • getUserFullMatrixID

      public String getUserFullMatrixID(String userName)
      Convert the user id into the full Matrix ID format
      Parameters:
      userName - the username
      Returns:
      formatted username
    • getAccessToken

      public String getAccessToken(String jwtToken) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Retrieves an access token for a user using a JWT token
      Parameters:
      jwtToken - the Json Web Token
      Returns:
      String the access token
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • invalidateAccessToken

      public boolean invalidateAccessToken(String accessToken)
      Invalidate a specific access token
      Parameters:
      accessToken - the access token to be invalidated
      Returns:
      true if success
    • updateUserDisplayName

      public void updateUserDisplayName(String matrixFullID, String newDisplayName)
    • getRoomBySpace

      public Room getRoomBySpace(org.exoplatform.social.core.space.model.Space space)
      Returns the ID of the room linked to a space
      Parameters:
      space - the space
      Returns:
      the roomId linked to the space
    • getRoomBySpace

      public Room getRoomBySpace(org.exoplatform.social.core.space.model.Space space, boolean includeDisabled)
      Returns the ID of the room linked to a space
      Parameters:
      space - the space
      Returns:
      the roomId linked to the space
    • getRoomBySpaceId

      public Room getRoomBySpaceId(Long spaceId)
      Returns the ID of the room linked to a space
      Parameters:
      spaceId - the space Id
      Returns:
      the roomId linked to the space
    • getRoomBySpaceId

      public Room getRoomBySpaceId(Long spaceId, boolean includeDisabled)
      Returns the ID of the room linked to a space
      Parameters:
      spaceId - the space Id
      Returns:
      the roomId linked to the space
    • getById

      public Room getById(String roomId)
      Get a room by its technical ID
      Parameters:
      roomId - the room technical ID
      Returns:
      Room
    • getById

      public Room getById(String roomId, boolean includeDisabled)
      Get a room by its technical ID
      Parameters:
      roomId - the room technical ID
      includeDisabled - return room even if it is disabled
      Returns:
      Room
    • linkSpaceToMatrixRoom

      public Room linkSpaceToMatrixRoom(org.exoplatform.social.core.space.model.Space space, String roomId)
      records the matrix ID of the room linked to the space
      Parameters:
      space - the Space
      roomId - the ID of the matrix room
      Returns:
      the room ID
    • linkSpaceToMatrixRoom

      public Room linkSpaceToMatrixRoom(long spaceId, String roomId, RoomStatus roomStatus)
      records the matrix ID of the room linked to the space
      Parameters:
      spaceId - the Space identifier
      roomId - the ID of the matrix room
      Returns:
      the room ID
    • createRoomForSpaceOnMatrix

      public String createRoomForSpaceOnMatrix(org.exoplatform.social.core.space.model.Space space) throws Exception
      Creates a room on Matrix for the space
      Parameters:
      space - the space
      Returns:
      String representing the room id
      Throws:
      Exception - when creating the room fails on Matrix
    • getMatrixIdForUser

      public String getMatrixIdForUser(String userName)
      Get the matrix ID of a defined user
      Parameters:
      userName - of the user
      Returns:
      the matrix ID
    • getJWTSessionToken

      public String getJWTSessionToken(String userNameOnMatrix)
      Returns the JWT for user authentication
      Parameters:
      userNameOnMatrix - the username of the current user
      Returns:
      String
    • saveUserAccount

      public String saveUserAccount(org.exoplatform.social.core.identity.model.Identity user, boolean isNew) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Saves a new user on Matrix
      Parameters:
      user - the user identity
      isNew - if the user has been just created
      Returns:
      the matrix user ID
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • saveUserAccount

      public String saveUserAccount(org.exoplatform.social.core.identity.model.Identity user, boolean isNew, boolean isEnableUserOperation, boolean isUserEnabled) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Saves a new user on Matrix
      Parameters:
      user - the user to create on Matrix
      isNew - boolean if the user is new, then true
      Returns:
      String the matrix user ID
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • uploadFileOnMatrix

      public String uploadFileOnMatrix(String fileName, String mimeType, byte[] fileBytes) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • updateUserAvatar

      public void updateUserAvatar(org.exoplatform.social.core.identity.model.Profile profile, String userMatrixID) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • disableAccount

      public void disableAccount(String matrixUsername) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • updateRoomDescription

      public boolean updateRoomDescription(String roomId, String description) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • updateRoomAvatar

      public void updateRoomAvatar(org.exoplatform.social.core.space.model.Space space, String roomId) throws Exception
      Throws:
      Exception
    • getRoomSettings

      public MatrixRoomPermissions getRoomSettings(String roomId) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • updateRoomSettings

      public boolean updateRoomSettings(String roomId, MatrixRoomPermissions matrixRoomPermissions) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • kickUserFromRoom

      public void kickUserFromRoom(String roomId, String matrixIdOfUser, String message) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • joinUserToRoom

      public void joinUserToRoom(String roomId, String matrixIdOfUser) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • isUserMemberOfRoom

      public boolean isUserMemberOfRoom(String roomId, String matrixIdOfUser) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Checks if the user is a member of the room
      Parameters:
      roomId - the room ID
      matrixIdOfUser - the ID of the user on Matrix
      Returns:
      true if the user is a member of the room, false otherwise
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • renameRoom

      public void renameRoom(String roomId, String spaceDisplayName) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • makeUserAdminInRoom

      public void makeUserAdminInRoom(String matrixRoomId, String matrixIdOfUser) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • createRoom

      public String createRoom(org.exoplatform.social.core.space.model.Space space) throws Exception
      This function do : - Create a room on Matrix - Links the room to the space on Meeds - Update room permissions
      Parameters:
      space - The space
      Returns:
      The room ID
      Throws:
      Exception
    • getDirectMessagingRoom

      public Room getDirectMessagingRoom(String firstParticipant, String secondParticipant)
    • deleteRoom

      public void deleteRoom(String roomId) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Delete a Matrix room
      Parameters:
      roomId - the room identifier
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException
      IOException
      InterruptedException
    • createDirectMessagingRoom

      public Room createDirectMessagingRoom(Room directMessagingRoom) throws org.exoplatform.commons.ObjectAlreadyExistsException
      Creates a one to one room
      Parameters:
      directMessagingRoom - the Object representation of a one to one room
      Returns:
      the created Room
      Throws:
      org.exoplatform.commons.ObjectAlreadyExistsException - in case a room between the same users is already created
    • getOrCreateDirectMessagingRoom

      public Room getOrCreateDirectMessagingRoom(String userName, String recipientUserName) throws Exception
      Returns the direct messaging room between a user and another one, and creates it when it does not exist yet, as the web chat does: the room is created on Matrix as the user, the other participant invited, then it is recorded on the platform and the user's m.direct account data is updated. When a parallel call records the pair while the room is created on Matrix, its room is returned and the user's m.direct updated to it. The room just created is not recorded, but it stays on Matrix: the user is joined to it and the other participant holds a pending direct-message invite to it, as for the web chat's own duplicate. As in createDirectMessagingRoom(Room), only the database check and save remain unguarded. The user may open a direct message only where the chat lets them: direct messages are enabled, both users may use the chat (isChatUser(String)), and the other participant is an enabled user with a chat account.
      Parameters:
      userName - the username of the user opening the conversation
      recipientUserName - the username of the other participant
      Returns:
      the direct messaging room
      Throws:
      org.exoplatform.commons.exception.ObjectNotFoundException - when no user has that username
      IllegalAccessException - when the user may not message that user, the message saying why: direct messages disabled, a user outside the chat, a disabled or deleted recipient, or one with no chat account
      IllegalArgumentException - when a username is blank or both are the same
      Exception - any failure of the room creation on Matrix
    • sendDirectMessage

      public String sendDirectMessage(String userName, String recipientUserName, String text) throws Exception
      Sends a text message to another user as the given user, in their direct messaging room, which is created first when they have none. Used by the send_direct_message MCP tool.
      Parameters:
      userName - the username of the user sending the message
      recipientUserName - the username of the recipient
      text - the plain text message to send
      Returns:
      the created event id, or null when the text is blank or the message could not be sent
      Throws:
      org.exoplatform.commons.exception.ObjectNotFoundException - when no user has that username
      IllegalAccessException - when the user may not message that user, the message saying why
      IllegalArgumentException - when a username is blank or both are the same
      Exception - any failure of the room creation on Matrix
    • getMatrixDMRoomsOfUser

      public List<Room> getMatrixDMRoomsOfUser(String user)
      Loads all the one to one rooms of a defined user
      Parameters:
      user - the username of the user
      Returns:
      List of Room
    • isUserMemberOfGroups

      public boolean isUserMemberOfGroups(String userName, String... groups) throws Exception
      Checks if the user is a member of a group defined by its name
      Parameters:
      userName - the userName
      groups - the list of groups
      Returns:
      true if the user is a member of the group
      Throws:
      Exception - error finding the user
    • getSpaceRooms

      public List<Room> getSpaceRooms()
      Returns a list of all space rooms
      Returns:
      List of Space rooms
    • extractUserId

      public String extractUserId(String fullMatrixUserId)
      Extracts the user ID from the full User Id on Matrix
      Parameters:
      fullMatrixUserId - the full Matrix user Id
      Returns:
      the user Identifier
    • extractRoomId

      public String extractRoomId(String fullMatrixUserId)
      Extracts the room ID from the full room Id on Matrix
      Parameters:
      fullMatrixUserId - the full Matrix user Id
      Returns:
      the user Identifier
    • updateUserPresence

      public String updateUserPresence(String userIdOnMatrix, String presence, String statusMessage)
      update the user presence status on Matrix
      Parameters:
      userIdOnMatrix - the user Id on Matrix
      presence - the presence value: online , unavailable, offline
      statusMessage - a personalized status message
    • canAccess

      public boolean canAccess(Room room, String userName)
      Checks if the user able to access the room
      Parameters:
      room - the room
      userName - the username of the user
      Returns:
      true if he has access, false otherwise
    • enableSpaceChat

      public Room enableSpaceChat(org.exoplatform.social.core.space.model.Space space, boolean enable) throws org.exoplatform.commons.exception.ObjectNotFoundException
      Enable the chat for the space
      Parameters:
      space - the space where the chat will be disabled/enabled
      enable - true to enable the room, false to disable it
      Returns:
      Room the updated room
      Throws:
      org.exoplatform.commons.exception.ObjectNotFoundException
    • overrideAdminRateLimit

      public void overrideAdminRateLimit(String adminUserId)
      Overrides the rate limits for the current administrator to unlimited
      Parameters:
      adminUserId - the admin username on Matrix
    • getRoomEvent

      public MatrixMessage getRoomEvent(String eventId, String roomId, String token)
      Retrieves the message related to the event ID
      Parameters:
      eventId - the event ID
      roomId - the room ID
      Returns:
      MatrixMessage the received message
    • getRoomEventOrThrow

      public MatrixMessage getRoomEventOrThrow(String eventId, String roomId, Duration timeout) throws Exception
      Reads one message event with the administrator account, every request bounded by timeout: the administrator login, when its token is not cached yet, and the read. Propagates every failure. A room ID stored as its local part is completed with the server name, as the send and history reads do; a full room ID is read as given.
      Parameters:
      eventId - the event ID
      roomId - the room ID, as its local part or in full
      timeout - the timeout of each request
      Returns:
      the message, null when the event is missing or not an m.room.message
      Throws:
      Exception - when the login or the read fails or times out
    • findUserByMatrixIdOrThrow

      public String findUserByMatrixIdOrThrow(String userMatrixId, Duration timeout) throws Exception
      Finds the platform user of a Matrix id like findUserByMatrixId(String), every request bounded by timeout (the administrator login, when its token is not cached yet, and the user read) and every failure propagated.
      Parameters:
      userMatrixId - the Matrix id
      timeout - the timeout of each request
      Returns:
      the platform login, null when no platform user has the Matrix account's email
      Throws:
      Exception - when the login or the read fails or times out
    • getRoomEvent

      public MatrixMessage getRoomEvent(String eventId, String roomId)
      Retrieves the message related to the event ID
      Parameters:
      eventId - the event ID
      roomId - the room ID
      Returns:
      MatrixMessage the received message
    • findSpaceMemberByMatrixId

      public org.exoplatform.social.core.identity.model.Identity findSpaceMemberByMatrixId(String matrixId, org.exoplatform.social.core.space.model.Space space)
      Finds the identity of a user based on its Matrix ID and a space where he is a member
      Parameters:
      matrixId - the Matrix identifier of the user
      space - the space
      Returns:
      the identity of the space member
    • findUserByMatrixId

      public String findUserByMatrixId(String userMatrixId)
      find the user on Meeds based on his id on Matrix
      Parameters:
      userMatrixId - the ID of the user on Matrix
      Returns:
      the user Identity
    • cleanMatrixUsername

      public String cleanMatrixUsername(String userName)
      Cleans the username to be compatible with the usernames on Matrix server
      Parameters:
      userName - the user identifier
      Returns:
      String the cleaned username
    • isChatUser

      public boolean isChatUser(String userName) throws Exception
      Tells whether a user may have a chat account: chat is restricted to no group, the user is a member of one of the restricted groups, or the user's account is of a kind an add-on declares (a digital worker), which uses chat wherever chat is restricted.
      Parameters:
      userName - the user name
      Returns:
      true when the user may have a chat account
      Throws:
      Exception - when the user's memberships cannot be read
    • getAccountKindGroups

      public List<String> getAccountKindGroups()
      Lists the groups of the account kinds the add-ons declare.
      Returns:
      the groups' ids, never null
    • getRestrictedGroups

      public String[] getRestrictedGroups()
      Returns the restricted group of users if it is configured
      Returns:
      Array of group names
    • getMediaInfo

      public Optional<MediaInfo> getMediaInfo(String mediaId) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Returns synapse media info by its ID
      Parameters:
      mediaId - synapse media id
      Returns:
      MediaInfo wrapped in Optional, or Optional.empty() if mediaId is null or blank
      Throws:
      org.exoplatform.ws.frameworks.json.impl.JsonException - if there is an error parsing the JSON response from the server
      IOException - if a network or I/O error occurs during the request
      InterruptedException - if the thread executing the request is interrupted
    • deleteMedia

      public void deleteMedia(String mediaId) throws org.exoplatform.ws.frameworks.json.impl.JsonException, IOException, InterruptedException
      Deletes synapse media by its ID
      Parameters:
      mediaId - synapse media id
      Throws:
      IllegalArgumentException - if mediaId is null or blank
      org.exoplatform.ws.frameworks.json.impl.JsonException - if there is an error parsing the JSON response from the server
      IOException - if a network or I/O error occurs during the request
      InterruptedException - if the thread executing the request is interrupted
    • loadChatSettings

      public ChatSettingsEntity loadChatSettings()
      retrieves the chat settings
      Returns:
      the status of the chat feature
    • loadChatSettings

      public ChatSettingsEntity loadChatSettings(String userName, Locale locale)
      retrieves the chat settings
      Parameters:
      userName - the caller username
      locale - the caller Locale
      Returns:
      the Chat settings
    • saveChatSettings

      public void saveChatSettings(ChatSettings chatSettings)
      Sets the settings of the chat feature
      Parameters:
      chatSettings - the new status fo the chat enabled/disabled
    • isChatAuthorizedForSpace

      public boolean isChatAuthorizedForSpace(Long spaceId)
      Check if Chat application is authorized for the space based on its template
      Parameters:
      spaceId - The space identifier
      Returns:
      boolean : true if the chat is authorized
    • isChatAuthorizedForSpace

      public boolean isChatAuthorizedForSpace(org.exoplatform.social.core.space.model.Space space)
      Check if Chat application is authorized for the space based on its template
      Parameters:
      space - The space
      Returns:
      boolean : true if the chat is authorized
    • isChatAuthorizedForSpaceTemplate

      public boolean isChatAuthorizedForSpaceTemplate(org.exoplatform.social.core.space.model.Space space)
      Checks if the Chat is authorized for the template used to create the space
      Parameters:
      space - The given space
      Returns:
      True if the chat was authorized for this space template
    • isChatAuthorizedByAdministration

      public boolean isChatAuthorizedByAdministration(org.exoplatform.social.core.space.model.Space space)
      Checks if the Chat is authorized from the administration for the space
      Parameters:
      space - The given space
      Returns:
      True if the chat was authorized for this space
    • isChatEnabledByDefault

      public boolean isChatEnabledByDefault(org.exoplatform.social.core.space.model.Space space)
      Check if the chat is enabled and synched be default for the provided space
      Parameters:
      space - the related space
      Returns:
      boolean True if the chat is enabled by default
    • changeChatRoomReadability

      public void changeChatRoomReadability(String roomId, boolean readOnly)
      Sets the room to readOnly
      Parameters:
      roomId - the room identifier
      readOnly - true if the room will be set readonly
    • getUserConversations

      public List<ChatConversation> getUserConversations(String userName)
      Lists the conversations the given user participates in: their direct messaging rooms and the rooms of the spaces they are a member of. Used by the list_chat_conversations MCP tool to give AI agents the user's chat context.
      Parameters:
      userName - the Meeds username
      Returns:
      the user's conversations, never null
    • getUserConversations

      public List<ChatConversation> getUserConversations(String userName, String type, String query, Integer limit)
      Lists the conversations the given user participates in, as getUserConversations(String) does, keeping those of a type and those whose title, or for a direct message the other participant's username, contains a text. The match ignores case and accents.
      Parameters:
      userName - the Meeds username
      type - "dm" or "space" to keep one type only, blank to keep both
      query - the text to look for, blank to keep every conversation
      limit - the maximum number of conversations to return, null for no maximum
      Returns:
      the matching conversations, never null
    • getRoomEventAsUser

      public MatrixMessage getRoomEventAsUser(String userName, String roomId, String eventId)
      Reads one event back with the user's own Matrix identity, so an event of a room the administrator account cannot see (a direct message) resolves too. A room ID stored as its local part is completed with the server name.
      Parameters:
      userName - the platform user
      roomId - the room (local or full id)
      eventId - the event to read back
      Returns:
      the event, or null when it cannot be read
    • markRoomAsRead

      public boolean markRoomAsRead(String userName, String roomId, String eventId)
      Marks a room as read for a user up to an event, with the user's own Matrix identity (server-side read anchor): posts the m.read receipt so every client of the user converges through Matrix sync.
      Parameters:
      userName - the platform user
      roomId - the room (local or full id)
      eventId - the event read up to (high-water mark)
      Returns:
      true when the receipt was posted
    • callAsUserOrThrow

      public <T> T callAsUserOrThrow(String userName, Duration timeout, MatrixUserCall<T> operation) throws Exception
      Executes a Matrix operation on behalf of the given user like callAsUser(String, T, MatrixUserCall), but propagates every failure instead of answering a fallback, so a caller tells a timeout or an error from an empty result. A login it needs is bounded by timeout; the operation bounds its own requests. A token Synapse rejects (HTTP 401) is refreshed once and the operation retried once.
      Type Parameters:
      T - the operation result type
      Parameters:
      userName - the Meeds username acting
      timeout - the login request timeout
      operation - the operation to run with a valid access token
      Returns:
      the operation result
      Throws:
      MatrixException - when the user has no Matrix account
      Exception - any failure of the login or of the operation
    • callAsUserOrThrow

      public <T> T callAsUserOrThrow(String userName, String matrixId, Duration timeout, MatrixUserCall<T> operation) throws Exception
      Executes a Matrix operation on behalf of the given user like callAsUserOrThrow(String, Duration, MatrixUserCall), with the user's Matrix id as the caller knows it: a listener's request may not have committed the profile write that stores it yet.
      Type Parameters:
      T - the operation result type
      Parameters:
      userName - the Meeds username acting
      matrixId - the user's Matrix id, null to read it from the profile
      timeout - the login request timeout
      operation - the operation to run with a valid access token
      Returns:
      the operation result
      Throws:
      MatrixException - when the user has no Matrix account
      Exception - any failure of the login or of the operation
    • getRoomMessages

      public List<ChatMessage> getRoomMessages(String userName, String roomId, int limit)
      Returns the most recent messages of a conversation, in chronological order, read as the given user so that Synapse enforces exactly what that user is allowed to see (membership, history visibility, redactions). A cheap participant pre-check avoids reading conversations the user is obviously not part of. Used by the get_chat_messages MCP tool.
      Parameters:
      userName - the Meeds username acting
      roomId - the Matrix room id of the conversation
      limit - the maximum number of messages to return (clamped to [1, 200])
      Returns:
      the conversation messages, oldest first, never null
    • getRoomMessages

      public List<ChatMessage> getRoomMessages(String userName, String roomId, int limit, Duration timeout) throws Exception
      Returns the most recent messages of a conversation like getRoomMessages(String, String, int), every request bounded by timeout and every failure propagated: a timeout, an unreachable Matrix or a user without a Matrix account is thrown, so the caller tells it from a conversation with nothing to read. A user who does not participate in the conversation reads nothing, as in the unbounded read.
      Parameters:
      userName - the Meeds username acting
      roomId - the Matrix room id of the conversation, local part or full id
      limit - the maximum number of messages to return (clamped to [1, 200])
      timeout - the bound of each request
      Returns:
      the conversation messages, edits merged, oldest first, never null
      Throws:
      Exception - any failure of the login or of the read
    • getUnreadConversations

      public List<ChatUnread> getUnreadConversations(String userName)
      Returns the unread conversations of the given user, read as that user via a Matrix /sync, so only the rooms the user has joined are considered. Each entry carries the unread count and the recent messages, edits merged into the message they replace as in the room read, with a human readable title resolved from the user's conversations. Used by the get_unread_chat_messages MCP tool.
      Parameters:
      userName - the Meeds username acting
      Returns:
      the user's unread conversations, never null
    • sendMessage

      public String sendMessage(String userName, String roomId, String text)
      Sends a textual message to a conversation as the given user, after checking that the user participates in it. Synapse additionally enforces that the user is allowed to post. Used by the send_chat_message MCP tool, e.g. to let an AI agent post a real-time reply on the user's behalf.
      Parameters:
      userName - the Meeds username acting
      roomId - the Matrix room id of the conversation
      text - the plain text message to send
      Returns:
      the created event id, or null when the message could not be sent
    • sendReply

      public String sendReply(String userName, String roomId, String body, String formattedBody, String inReplyToEventId, List<String> mentionedMatrixIds, Duration timeout) throws Exception
      Posts a reply as the given user, quoting the event it answers and mentioning the given Matrix users, with the content the web composer sends: m.in_reply_to, an HTML formatted_body and m.mentions. The mentions are exactly mentionedMatrixIds, chosen by the caller, never read from the text. The transaction id is derived from the replied-to event and the user, so Synapse posts a reply retried with the same access token once; a retry after the token was refreshed is a new device, and Synapse posts it again. The request is bounded by timeout and a failure is thrown.
      Parameters:
      userName - the Meeds username replying
      roomId - the Matrix room id of the conversation, local part or full id
      body - the plain text body
      formattedBody - the HTML body, null or blank to send the plain body only
      inReplyToEventId - the event id the reply answers
      mentionedMatrixIds - the Matrix ids the reply mentions
      timeout - the bound of each request
      Returns:
      the created event id, or null when nothing was sent because an argument is blank or the user does not participate in the conversation
      Throws:
      Exception - any failure of the login or of the send
    • searchChatMessages

      public List<ChatSearchResult> searchChatMessages(String userName, String query, String conversationId, int limit)
      Runs a full-text search of message bodies as the given user, either across all their conversations (when conversationId is blank) or scoped to a single conversation. Synapse enforces the user's Matrix visibility, and the hits are then restricted to the conversations that are tracked by the platform and accessible to the user — the same rooms the chat list shows and the row can open — so a room they still belong to on Matrix but not in the chat (created from another Matrix client, left behind by a failed space kick) is never returned. Each hit is resolved to a human readable conversation title. Backs the search_chat_messages MCP tool and the chat UI search.
      Parameters:
      userName - the Meeds username acting
      query - the free-text search term
      conversationId - the Matrix room id to scope the search to, or null/blank to search across all the user's conversations
      limit - the maximum number of hits to return (clamped to [1, 100])
      Returns:
      the matching messages, most recent first, never null