public async Task delete([QueryField] string username)
        {
            if (username == null)
            {
                throw HttpException.BadRequest();
            }

            if (!validated)
            {
                throw HttpException.Forbidden();
            }

            var user = await getUser(username);

            if (user == null)
            {
                throw HttpException.NotFound();
            }

            var result = await accountCollection.DeleteOneAsync(
                Builders <AccountModel> .Filter.Eq(x => x.id, user.id));

            if (result.DeletedCount != 1)
            {
                throw HttpException.InternalServerError();
            }
        }
Example #2
0
        private async static Task OfferAsync(IHttpContext context)
        {
            var offer = await context.GetRequestDataAsync <RTCSessionDescriptionInit>();

            logger.LogDebug($"SDP Offer={offer.sdp}");

            var jsonOptions = new JsonSerializerOptions();

            jsonOptions.Converters.Add(new System.Text.Json.Serialization.JsonStringEnumConverter());

            CancellationTokenSource cts = new CancellationTokenSource();

            cts.CancelAfter(MAX_TEST_DURATION_SECONDS * 1000);

            var janusClient = new JanusRestClient(JANUS_REST_URL, logger, cts.Token);

            var startSessionResult = await janusClient.StartSession();

            if (!startSessionResult.Success)
            {
                throw HttpException.InternalServerError("Failed to create Janus session.");
            }
            else
            {
                var waitForAnswer = new TaskCompletionSource <string>();

                janusClient.OnJanusEvent += (resp) =>
                {
                    if (resp.jsep != null)
                    {
                        logger.LogDebug($"janus get event jsep={resp.jsep.type}.");
                        logger.LogDebug($"janus SDP Answer: {resp.jsep.sdp}");

                        waitForAnswer.SetResult(resp.jsep.sdp);
                    }
                };

                var pluginResult = await janusClient.StartEcho(offer.sdp);

                if (!pluginResult.Success)
                {
                    await janusClient.DestroySession();

                    waitForAnswer.SetCanceled();
                    cts.Cancel();
                    throw HttpException.InternalServerError("Failed to create Janus Echo Test Plugin instance.");
                }
                else
                {
                    using (cts.Token.Register(() =>
                    {
                        // This callback will be executed if the token is cancelled.
                        waitForAnswer.TrySetCanceled();
                    }))
                    {
                        // At this point we're waiting for a response on the Janus long poll thread that
                        // contains the SDP answer to send back to the client.
                        try
                        {
                            var answer = await waitForAnswer.Task;
                            logger.LogDebug("SDP answer ready, sending to client.");

                            if (answer != null)
                            {
                                var answerInit = new RTCSessionDescriptionInit
                                {
                                    type = RTCSdpType.answer,
                                    sdp  = answer
                                };

                                context.Response.ContentType = "application/json";
                                using (var responseStm = context.OpenResponseStream(false, false))
                                {
                                    await JsonSerializer.SerializeAsync(responseStm, answerInit, jsonOptions);
                                }
                            }
                        }
                        catch (TaskCanceledException)
                        {
                            await janusClient.DestroySession();

                            throw HttpException.InternalServerError("Janus operation timed out waiting for Echo Test plugin SDP answer.");
                        }
                    }
                }
            }
        }