public async Task <IActionResult> Create([FromBody] Api.Models.ChatBot model, CancellationToken cancellationToken)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            if (String.IsNullOrWhiteSpace(model.Name))
            {
                return(BadRequest(new ErrorMessage {
                    Message = "name cannot be null or whitespace!"
                }));
            }

            if (String.IsNullOrWhiteSpace(model.ConnectionString))
            {
                return(BadRequest(new ErrorMessage {
                    Message = "connection_string cannot be null or whitespace!"
                }));
            }

            if (!model.Provider.HasValue)
            {
                return(BadRequest(new ErrorMessage {
                    Message = "provider cannot be null!"
                }));
            }

            switch (model.Provider)
            {
            case ChatProvider.Discord:
            case ChatProvider.Irc:
                break;

            default:
                return(BadRequest(new ErrorMessage {
                    Message = "Invalid provider!"
                }));
            }

            if (model.ReconnectionInterval == 0)
            {
                return(BadRequest(new ErrorMessage {
                    Message = "ReconnectionInterval must not be zero!"
                }));
            }

            if (!model.ValidateProviderChannelTypes())
            {
                return(BadRequest(new ErrorMessage {
                    Message = "One or more of channels aren't formatted correctly for the given provider!"
                }));
            }

            model.Enabled = model.Enabled ?? false;
            model.ReconnectionInterval = model.ReconnectionInterval ?? 1;

            // try to update das db first
            var dbModel = new Models.ChatBot
            {
                Name                 = model.Name,
                ConnectionString     = model.ConnectionString,
                Enabled              = model.Enabled,
                Channels             = model.Channels?.Select(x => ConvertApiChatChannel(x)).ToList() ?? new List <Models.ChatChannel>(),    // important that this isn't null
                InstanceId           = Instance.Id,
                Provider             = model.Provider,
                ReconnectionInterval = model.ReconnectionInterval
            };

            DatabaseContext.ChatBots.Add(dbModel);

            await DatabaseContext.Save(cancellationToken).ConfigureAwait(false);

            try
            {
                try
                {
                    // try to create it
                    var instance = instanceManager.GetInstance(Instance);
                    await instance.Chat.ChangeSettings(dbModel, cancellationToken).ConfigureAwait(false);

                    if (dbModel.Channels.Count > 0)
                    {
                        await instance.Chat.ChangeChannels(dbModel.Id, dbModel.Channels, cancellationToken).ConfigureAwait(false);
                    }
                }
                catch
                {
                    // undo the add
                    DatabaseContext.ChatBots.Remove(dbModel);
                    await DatabaseContext.Save(default).ConfigureAwait(false);
Exemplo n.º 2
0
        public async Task <IActionResult> Create([FromBody] Api.Models.ChatBot model, CancellationToken cancellationToken)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var earlyOut = StandardModelChecks(model, true);

            if (earlyOut != null)
            {
                return(earlyOut);
            }

            var countOfExistingBotsInInstance = await DatabaseContext
                                                .ChatBots
                                                .AsQueryable()
                                                .Where(x => x.InstanceId == Instance.Id)
                                                .CountAsync(cancellationToken)
                                                .ConfigureAwait(false);

            if (countOfExistingBotsInInstance >= Instance.ChatBotLimit.Value)
            {
                return(Conflict(new ErrorMessage(ErrorCode.ChatBotMax)));
            }

            model.Enabled ??= false;
            model.ReconnectionInterval ??= 1;

            // try to update das db first
            var dbModel = new Models.ChatBot
            {
                Name                 = model.Name,
                ConnectionString     = model.ConnectionString,
                Enabled              = model.Enabled,
                Channels             = model.Channels?.Select(x => ConvertApiChatChannel(x)).ToList() ?? new List <Models.ChatChannel>(),    // important that this isn't null
                InstanceId           = Instance.Id,
                Provider             = model.Provider,
                ReconnectionInterval = model.ReconnectionInterval,
                ChannelLimit         = model.ChannelLimit
            };

            DatabaseContext.ChatBots.Add(dbModel);

            await DatabaseContext.Save(cancellationToken).ConfigureAwait(false);

            return(await WithComponentInstance(
                       async instance =>
            {
                try
                {
                    // try to create it
                    await instance.Chat.ChangeSettings(dbModel, cancellationToken).ConfigureAwait(false);

                    if (dbModel.Channels.Count > 0)
                    {
                        await instance.Chat.ChangeChannels(dbModel.Id, dbModel.Channels, cancellationToken).ConfigureAwait(false);
                    }
                }
                catch
                {
                    // undo the add
                    DatabaseContext.ChatBots.Remove(dbModel);

                    // DCTx2: Operations must always run
                    await DatabaseContext.Save(default).ConfigureAwait(false);