Esempio n. 1
0
        // ReSharper disable once SuggestBaseTypeForParameter
        /// <summary>
        /// Initializes a new instance of the <see cref="MatrixClient" /> class.
        /// </summary>
        /// <param name="loggerFactory">A factory to create logger instances.</param>
        /// <param name="httpClient">An instance of <see cref="HttpClient" /> to use for making API calls.</param>
        /// <param name="clientConfig">Client configuration monitor.</param>
        public MatrixClient(
            ILoggerFactory loggerFactory,
            HttpClient httpClient,
            IOptionsMonitor <MatrixClientConfiguration> clientConfig)
        {
            Log = loggerFactory.CreateLogger <MatrixClient>();
            var baseUri = clientConfig.CurrentValue.BaseUri ?? new Uri(DefaultBaseUrl);

            httpClient.BaseAddress = baseUri;

            _api = new RestClient(httpClient)
            {
                RequestPathParamSerializer  = new StringEnumRequestPathParamSerializer(),
                RequestQueryParamSerializer = new MatrixApiQueryParamSerializer()
            }.For <IMatrixClientServerApi>();

            _configMonitor  = clientConfig;
            _api.ApiVersion = _configMonitor.CurrentValue.ApiVersion ?? DefaultApiVersion;
            _api.SetBearerToken(_configMonitor.CurrentValue.AccessToken);

            _syncListener       = new SyncListener(loggerFactory.CreateLogger <SyncListener>(), _api);
            _syncListener.Sync += HandleSyncAsync;

            _rooms = new ConcurrentDictionary <string, Room>();
        }
        /// <summary>
        /// Download a thumbnail of the content from the content repository.
        /// </summary>
        /// <param name="api">An instance of <see cref="IMatrixClientServerApi" />.</param>
        /// <param name="serverName">The server name from the <c>mxc://</c> URI (the authority component).</param>
        /// <param name="mediaId">The media ID from the <c>mxc://</c> URI (the path component).</param>
        /// <param name="width">
        /// The desired width of the thumbnail, in pixels. The actual thumbnail may not match the size specified.
        /// </param>
        /// <param name="height">
        /// The desired height of the thumbnail, in pixels. The actual thumbnail may not match the size specified.
        /// </param>
        /// <param name="resizeMethod">The desired resizing method.</param>
        /// <param name="allowRemote">
        /// A value indicating whether the server should attempt to fetch the media if it is deemed remote.
        /// This is to prevent routing loops where the server contacts itself. Defaults to <c>true</c> if not provided.
        /// </param>
        /// <returns>The downloaded thumbnail.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="api" /> is <c>null</c>.</exception>
        public static async Task <Content> DownloadThumbnailContentAsync(
            [NotNull] this IMatrixClientServerApi api,
            string serverName,
            string mediaId,
            int width,
            int height,
            ResizeMethod resizeMethod = ResizeMethod.Scale,
            bool?allowRemote          = null)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            using (var response = await api.DownloadThumbnailAsync(
                       serverName,
                       mediaId,
                       width,
                       height,
                       resizeMethod,
                       allowRemote))
            {
                return(await CreateContentFromResponse(response).ConfigureAwait(false));
            }
        }
        /// <summary>
        /// Get an OpenID token object to verify the requester's identity.
        /// </summary>
        /// <param name="api">An instance of <see cref="IMatrixClientServerApi" /></param>
        /// <param name="userId">
        /// The ID of the user to request an OpenID token for. Should be the user who is authenticated for the request.
        /// </param>
        /// <returns>The OpenID token for the user.</returns>
        /// <remarks>
        /// <para>
        /// Gets an OpenID token object that the requester may supply to another service to verify their identity in
        /// Matrix. The generated token is only valid for exchanging for user information from the federation API
        /// for OpenID.
        /// </para>
        /// <para>
        /// The access token generated is only valid for the OpenID API. It cannot be used to request another OpenID
        /// access token or call <c>/sync</c>, for example.
        /// </para>
        /// </remarks>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="api" /> is <c>null</c>.</exception>
        public static Task <OpenIdToken> RequestOpenIdTokenAsync([NotNull] this IMatrixClientServerApi api, UserId userId)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            return(api.RequestOpenIdTokenAsync(userId, new object()));
        }
        /// <summary>
        /// Helper method to download a file from the Matrix API, choosing the relevant API method based on
        /// <paramref name="filename" /> being <c>null</c> or not.
        /// </summary>
        /// <param name="api">An instance of <see cref="IMatrixClientServerApi" />.</param>
        /// <param name="serverName">The domain part of a content URL.</param>
        /// <param name="mediaId">The ID of the media as extracted from a content URL.</param>
        /// <param name="filename">Name to assign to the file, can be left <c>null</c> if none is needed.</param>
        /// <param name="allowRemote">Allow the homeserver to fetch remote media if necessary.</param>
        /// <returns>
        /// An instance of <see cref="HttpResponseMessage" /> ready to be parsed.
        /// The returned object <em>must be disposed</em>.
        /// </returns>
        private static Task <HttpResponseMessage> DownloadAsync(
            IMatrixClientServerApi api,
            string serverName,
            string mediaId,
            [CanBeNull] string filename,
            bool?allowRemote)
        {
            // ReSharper disable once ConvertIfStatementToReturnStatement
            if (filename == null)
            {
                return(api.DownloadAsync(serverName, mediaId, allowRemote));
            }

            return(api.DownloadAsync(serverName, mediaId, filename, allowRemote));
        }
        /// <summary>
        /// Download content from the content repository.
        /// </summary>
        /// <param name="api">An instance of <see cref="IMatrixClientServerApi" />.</param>
        /// <param name="serverName">The server name from the <c>mxc://</c> URI (the authority component).</param>
        /// <param name="mediaId">The media ID from the <c>mxc://</c> URI (the path component).</param>
        /// <param name="filename">The filename to give in the <c>Content-Disposition</c> header.</param>
        /// <param name="allowRemote">
        /// A value indicating whether the server should attempt to fetch the media if it is deemed remote.
        /// This is to prevent routing loops where the server contacts itself. Defaults to <c>true</c> if not provided.
        /// </param>
        /// <returns>The downloaded content.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="api" /> is <c>null</c>.</exception>
        public static async Task <Content> DownloadContentAsync(
            [NotNull] this IMatrixClientServerApi api,
            string serverName,
            string mediaId,
            [CanBeNull] string filename = null,
            bool?allowRemote            = null)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            using (var response = await DownloadAsync(api, serverName, mediaId, filename, allowRemote))
            {
                return(await CreateContentFromResponse(response).ConfigureAwait(false));
            }
        }
        /// <summary>
        /// Download content from the content repository.
        /// </summary>
        /// <param name="api">An instance of <see cref="IMatrixClientServerApi" />.</param>
        /// <param name="mediaUri">The MXC URI to download.</param>
        /// <param name="filename">The filename to give in the <c>Content-Disposition</c> header.</param>
        /// <param name="allowRemote">
        /// A value indicating whether the server should attempt to fetch the media if it is deemed remote.
        /// This is to prevent routing loops where the server contacts itself. Defaults to <c>true</c> if not provided.
        /// </param>
        /// <returns>The downloaded content.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="api" /> is <c>null</c>.</exception>
        public static Task <Content> DownloadContentAsync(
            [NotNull] this IMatrixClientServerApi api,
            Uri mediaUri,
            [CanBeNull] string filename = null,
            bool?allowRemote            = null)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            var serverName = mediaUri.Authority;

            // Strip the leading slash (/)
            var mediaId = mediaUri.AbsolutePath.Substring(1);

            return(api.DownloadContentAsync(serverName, mediaId, filename, allowRemote));
        }
        /// <summary>
        /// Download a thumbnail of the content from the content repository.
        /// </summary>
        /// <param name="api">An instance of <see cref="IMatrixClientServerApi" />.</param>
        /// <param name="mediaUri">The MXC URI to download a thumbnail for.</param>
        /// <param name="width">
        /// The desired width of the thumbnail, in pixels. The actual thumbnail may not match the size specified.
        /// </param>
        /// <param name="height">
        /// The desired height of the thumbnail, in pixels. The actual thumbnail may not match the size specified.
        /// </param>
        /// <param name="resizeMethod">The desired resizing method.</param>
        /// <param name="allowRemote">
        /// A value indicating whether the server should attempt to fetch the media if it is deemed remote.
        /// This is to prevent routing loops where the server contacts itself. Defaults to <c>true</c> if not provided.
        /// </param>
        /// <returns>The downloaded thumbnail.</returns>
        /// <exception cref="ArgumentNullException">Thrown if <paramref name="api" /> is <c>null</c>.</exception>
        public static Task <Content> DownloadThumbnailContentAsync(
            [NotNull] this IMatrixClientServerApi api,
            Uri mediaUri,
            int width,
            int height,
            ResizeMethod resizeMethod = ResizeMethod.Scale,
            bool?allowRemote          = null)
        {
            if (api == null)
            {
                throw new ArgumentNullException(nameof(api));
            }

            var serverName = mediaUri.Authority;

            // Strip the leading slash (/)
            var mediaId = mediaUri.AbsolutePath.Substring(1);

            return(api.DownloadThumbnailContentAsync(serverName, mediaId, width, height, resizeMethod, allowRemote));
        }
Esempio n. 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SyncListener" /> class.
 /// </summary>
 /// <param name="log">Logger instance for this class.</param>
 /// <param name="api">API interface to use.</param>
 public SyncListener(ILogger <SyncListener> log, IMatrixClientServerApi api)
 {
     _log = log;
     _api = api;
 }