public async Task <IRpcMethodResult> UpdateSettings(string projectId, SFProjectSettings settings)
        {
            try
            {
                await _projectService.UpdateSettingsAsync(UserId, projectId, settings);

                return(Ok());
            }
            catch (ForbiddenException)
            {
                return(ForbiddenError());
            }
            catch (DataNotFoundException dnfe)
            {
                return(NotFoundError(dnfe.Message));
            }
        }
Ejemplo n.º 2
0
        public async Task UpdateSettingsAsync(string curUserId, string projectId, SFProjectSettings settings)
        {
            using (IConnection conn = await RealtimeService.ConnectAsync(curUserId))
            {
                IDocument <SFProject> projectDoc = await conn.FetchAsync <SFProject>(projectId);

                if (!projectDoc.IsLoaded)
                {
                    throw new DataNotFoundException("The project does not exist.");
                }
                if (!IsProjectAdmin(projectDoc.Data, curUserId))
                {
                    throw new ForbiddenException();
                }

                // Get the source - any creation or permission updates are handled in GetTranslateSourceAsync
                TranslateSource source = null;
                if (settings.SourceParatextId != null)
                {
                    Attempt <UserSecret> userSecretAttempt = await _userSecrets.TryGetAsync(curUserId);

                    if (!userSecretAttempt.TryResult(out UserSecret userSecret))
                    {
                        throw new DataNotFoundException("The user does not exist.");
                    }

                    IReadOnlyList <ParatextProject> ptProjects = await _paratextService.GetProjectsAsync(userSecret);

                    source = await GetTranslateSourceAsync(curUserId, userSecret, settings.SourceParatextId,
                                                           ptProjects, projectDoc.Data.UserRoles);

                    if (source.ProjectRef == projectId)
                    {
                        // A project cannot reference itself
                        source = null;
                    }
                }

                await projectDoc.SubmitJson0OpAsync(op =>
                {
                    UpdateSetting(op, p => p.TranslateConfig.TranslationSuggestionsEnabled,
                                  settings.TranslationSuggestionsEnabled);
                    UpdateSetting(op, p => p.TranslateConfig.Source, source);

                    UpdateSetting(op, p => p.CheckingConfig.CheckingEnabled, settings.CheckingEnabled);
                    UpdateSetting(op, p => p.CheckingConfig.UsersSeeEachOthersResponses,
                                  settings.UsersSeeEachOthersResponses);
                    UpdateSetting(op, p => p.CheckingConfig.ShareEnabled, settings.ShareEnabled);
                    UpdateSetting(op, p => p.CheckingConfig.ShareLevel, settings.ShareLevel);
                });

                bool suggestionsEnabledSet = settings.TranslationSuggestionsEnabled != null;
                bool sourceParatextIdSet   = settings.SourceParatextId != null;
                bool checkingEnabledSet    = settings.CheckingEnabled != null;
                // check if a sync needs to be run
                if (suggestionsEnabledSet || sourceParatextIdSet || checkingEnabledSet)
                {
                    bool trainEngine = false;
                    if (suggestionsEnabledSet || sourceParatextIdSet)
                    {
                        if (projectDoc.Data.TranslateConfig.TranslationSuggestionsEnabled &&
                            projectDoc.Data.TranslateConfig.Source != null)
                        {
                            // translate task was enabled or source project changed

                            // recreate Machine project only if source project changed
                            if (!suggestionsEnabledSet && sourceParatextIdSet)
                            {
                                await _engineService.RemoveProjectAsync(projectId);
                            }
                            var machineProject = new MachineProject
                            {
                                Id = projectId,
                                SourceLanguageTag = projectDoc.Data.TranslateConfig.Source.WritingSystem.Tag,
                                TargetLanguageTag = projectDoc.Data.WritingSystem.Tag
                            };
                            await _engineService.AddProjectAsync(machineProject);

                            trainEngine = true;
                        }
                        else
                        {
                            // translate task was disabled or source project set to null
                            await _engineService.RemoveProjectAsync(projectId);
                        }
                    }

                    await _syncService.SyncAsync(curUserId, projectId, trainEngine);
                }
            }
        }