Ejemplo n.º 1
0
        private async Task ClearSelectedSegmentChecksum(IConnection conn, string projectId, string userId)
        {
            IDocument <SFProjectUserConfig> config = await conn.FetchAsync <SFProjectUserConfig>(
                SFProjectUserConfig.GetDocId(projectId, userId));

            await config.SubmitJson0OpAsync(op => op.Unset(puc => puc.SelectedSegmentChecksum));
        }
Ejemplo n.º 2
0
        protected override async Task RemoveUserFromProjectAsync(IConnection conn, IDocument <SFProject> projectDoc,
                                                                 IDocument <User> userDoc)
        {
            await base.RemoveUserFromProjectAsync(conn, projectDoc, userDoc);

            IDocument <SFProjectUserConfig> projectUserConfigDoc = await conn.FetchAsync <SFProjectUserConfig>(
                SFProjectUserConfig.GetDocId(projectDoc.Id, userDoc.Id));

            await projectUserConfigDoc.DeleteAsync();
        }
Ejemplo n.º 3
0
        protected override async Task AddUserToProjectAsync(IConnection conn, IDocument <SFProject> projectDoc,
                                                            IDocument <User> userDoc, string projectRole, bool removeShareKeys = true)
        {
            await conn.CreateAsync <SFProjectUserConfig>(SFProjectUserConfig.GetDocId(projectDoc.Id, userDoc.Id),
                                                         new SFProjectUserConfig { ProjectRef = projectDoc.Id, OwnerRef = userDoc.Id });

            // Listeners can now assume the ProjectUserConfig is ready when the user is added.
            await base.AddUserToProjectAsync(conn, projectDoc, userDoc, projectRole, removeShareKeys);

            // Add to the source project, if required
            bool   translationSuggestionsEnabled = projectDoc.Data.TranslateConfig.TranslationSuggestionsEnabled;
            string sourceProjectId  = projectDoc.Data.TranslateConfig.Source?.ProjectRef;
            string sourceParatextId = projectDoc.Data.TranslateConfig.Source?.ParatextId;

            if (translationSuggestionsEnabled &&
                !string.IsNullOrWhiteSpace(sourceProjectId) &&
                !string.IsNullOrWhiteSpace(sourceParatextId))
            {
                // Load the source project role from MongoDB
                IDocument <SFProject> sourceProjectDoc = await TryGetProjectDocAsync(sourceProjectId, conn);

                if (sourceProjectDoc == null)
                {
                    return;
                }
                if (sourceProjectDoc.IsLoaded && !sourceProjectDoc.Data.UserRoles.ContainsKey(userDoc.Id))
                {
                    // Not found in Mongo, so load the project role from Paratext
                    Attempt <string> attempt = await TryGetProjectRoleAsync(sourceProjectDoc.Data, userDoc.Id);

                    if (attempt.TryResult(out string sourceProjectRole))
                    {
                        // If they are in Paratext, add the user to the source project
                        await this.AddUserToProjectAsync(conn, sourceProjectDoc, userDoc, sourceProjectRole,
                                                         removeShareKeys);
                    }
                }
            }
        }
            public TestEnvironment()
            {
                RealtimeService = new SFMemoryRealtimeService();
                RealtimeService.AddRepository("users", OTType.Json0, new MemoryRepository <User>(new[]
                {
                    new User
                    {
                        Id    = User01,
                        Email = "*****@*****.**",
                        Sites = new Dictionary <string, Site>
                        {
                            { SiteId, new Site {
                                  Projects = { Project01, Project03 }
                              } }
                        }
                    },
                    new User
                    {
                        Id    = User02,
                        Email = "*****@*****.**",
                        Sites = new Dictionary <string, Site>
                        {
                            { SiteId, new Site {
                                  Projects = { Project01, Project02, Project03 }
                              } }
                        }
                    },
                    new User
                    {
                        Id    = User03,
                        Email = "*****@*****.**",
                        Sites = new Dictionary <string, Site> {
                            { SiteId, new Site() }
                        }
                    },
                    new User
                    {
                        Id    = User04,
                        Email = "*****@*****.**",
                        Sites = new Dictionary <string, Site>(),
                        Role  = SystemRole.SystemAdmin
                    }
                }));
                RealtimeService.AddRepository("sf_projects", OTType.Json0, new MemoryRepository <SFProject>(
                                                  new[]
                {
                    new SFProject
                    {
                        Id              = Project01,
                        ParatextId      = "paratext_" + Project01,
                        Name            = "project01",
                        ShortName       = "P01",
                        TranslateConfig = new TranslateConfig
                        {
                            TranslationSuggestionsEnabled = true,
                            Source = new TranslateSource
                            {
                                ParatextId    = "paratextId",
                                Name          = "Source",
                                ShortName     = "SRC",
                                WritingSystem = new WritingSystem
                                {
                                    Tag = "qaa"
                                }
                            }
                        },
                        CheckingConfig = new CheckingConfig
                        {
                            ShareEnabled = false
                        },
                        UserRoles = new Dictionary <string, string>
                        {
                            { User01, SFProjectRole.Administrator },
                            { User02, SFProjectRole.CommunityChecker }
                        },
                        Texts =
                        {
                            new TextInfo
                            {
                                BookNum  = 40,
                                Chapters =
                                {
                                    new Chapter {
                                        Number = 1, LastVerse = 3, IsValid = true, Permissions ={                                                  }
                                    }
                                }
                            },
                            new TextInfo
                            {
                                BookNum  = 41,
                                Chapters =
                                {
                                    new Chapter {
                                        Number = 1, LastVerse = 3, IsValid = true, Permissions ={                                                  }
                                    },
                                    new Chapter {
                                        Number = 2, LastVerse = 3, IsValid = true, Permissions ={                                                  }
                                    }
                                }
                            }
                        }
                    },
                    new SFProject
                    {
                        Id             = Project02,
                        Name           = "project02",
                        ShortName      = "P02",
                        CheckingConfig = new CheckingConfig
                        {
                            ShareEnabled = true,
                            ShareLevel   = CheckingShareLevel.Anyone
                        },
                        UserRoles =
                        {
                            { User02, SFProjectRole.Administrator }
                        },
                    },
                    new SFProject
                    {
                        Id             = Project03,
                        Name           = "project03",
                        ShortName      = "P03",
                        CheckingConfig = new CheckingConfig
                        {
                            ShareEnabled = true,
                            ShareLevel   = CheckingShareLevel.Specific
                        },
                        UserRoles =
                        {
                            { User01, SFProjectRole.Administrator },
                            { User02, SFProjectRole.Translator    }
                        }
                    },
                    new SFProject
                    {
                        Id         = Resource01,
                        ParatextId = "resid_is_16_char",
                        Name       = "resource project",
                        ShortName  = "RES",
                    }
                }));
                RealtimeService.AddRepository("sf_project_user_configs", OTType.Json0,
                                              new MemoryRepository <SFProjectUserConfig>(new[]
                {
                    new SFProjectUserConfig {
                        Id = SFProjectUserConfig.GetDocId(Project01, User01)
                    },
                    new SFProjectUserConfig {
                        Id = SFProjectUserConfig.GetDocId(Project01, User02)
                    },
                    new SFProjectUserConfig {
                        Id = SFProjectUserConfig.GetDocId(Project02, User02)
                    },
                    new SFProjectUserConfig {
                        Id = SFProjectUserConfig.GetDocId(Project03, User01)
                    },
                    new SFProjectUserConfig {
                        Id = SFProjectUserConfig.GetDocId(Project03, User02)
                    }
                }));
                var siteOptions = Substitute.For <IOptions <SiteOptions> >();

                siteOptions.Value.Returns(new SiteOptions
                {
                    Id      = SiteId,
                    Name    = "xForge",
                    Origin  = new Uri("http://localhost"),
                    SiteDir = "xforge"
                });
                var audioService = Substitute.For <IAudioService>();

                EmailService   = Substitute.For <IEmailService>();
                ProjectSecrets = new MemoryRepository <SFProjectSecret>(new[]
                {
                    new SFProjectSecret {
                        Id = Project01
                    },
                    new SFProjectSecret {
                        Id = Project02
                    },
                    new SFProjectSecret
                    {
                        Id        = Project03,
                        ShareKeys = new List <ShareKey>
                        {
                            new ShareKey {
                                Email = "*****@*****.**", Key = "key1111"
                            },
                            new ShareKey {
                                Email = "*****@*****.**", Key = "key1234"
                            },
                            new ShareKey {
                                Email = "*****@*****.**", Key = "key2222"
                            }
                        }
                    },
                });
                EngineService = Substitute.For <IEngineService>();
                SyncService   = Substitute.For <ISyncService>();
                SyncService.SyncAsync(Arg.Any <string>(), Arg.Any <string>(), Arg.Any <bool>())
                .Returns(Task.CompletedTask);
                ParatextService = Substitute.For <IParatextService>();
                IReadOnlyList <ParatextProject> ptProjects = new[]
                {
                    new ParatextProject
                    {
                        ParatextId  = "changedId",
                        Name        = "NewSource",
                        LanguageTag = "qaa"
                    },
                    new ParatextProject
                    {
                        ParatextId = GetProject(Project01).ParatextId
                    },
                    new ParatextProject
                    {
                        ParatextId = "ptProject123"
                    }
                };

                ParatextService.GetProjectsAsync(Arg.Any <UserSecret>()).Returns(Task.FromResult(ptProjects));
                IReadOnlyList <ParatextResource> ptResources = new[]
                {
                    new ParatextResource
                    {
                        ParatextId  = "resource_project",
                        Name        = "ResourceProject",
                        LanguageTag = "qaa"
                    },
                    new ParatextResource
                    {
                        ParatextId = GetProject(Resource01).ParatextId
                    }
                };

                ParatextService.GetResources(Arg.Any <UserSecret>()).Returns(ptResources);
                var userSecrets = new MemoryRepository <UserSecret>(new[]
                {
                    new UserSecret {
                        Id = User01
                    },
                    new UserSecret {
                        Id = User02
                    },
                    new UserSecret {
                        Id = User03
                    }
                });
                var translateMetrics = new MemoryRepository <TranslateMetrics>();

                FileSystemService = Substitute.For <IFileSystemService>();
                var options = Options.Create(new LocalizationOptions {
                    ResourcesPath = "Resources"
                });
                var factory = new ResourceManagerStringLocalizerFactory(options, NullLoggerFactory.Instance);

                Localizer       = new StringLocalizer <SharedResource>(factory);
                SecurityService = Substitute.For <ISecurityService>();
                SecurityService.GenerateKey().Returns("1234abc");
                var transceleratorService = Substitute.For <ITransceleratorService>();

                Service = new SFProjectService(RealtimeService, siteOptions, audioService, EmailService, ProjectSecrets,
                                               SecurityService, FileSystemService, EngineService, SyncService, ParatextService, userSecrets,
                                               translateMetrics, Localizer, transceleratorService);
            }