Skip to content

Commit 9638c09

Browse files
committed
SF-3604 Fix download of historic drafts
1 parent e7a5145 commit 9638c09

File tree

2 files changed

+115
-13
lines changed

2 files changed

+115
-13
lines changed

src/SIL.XForge.Scripture/Services/MachineApiService.cs

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,25 +1545,63 @@ CancellationToken cancellationToken
15451545
cancellationToken
15461546
);
15471547

1548-
// First, see if the document exists in the realtime service, if the chapter is not 0
1548+
// First, see if the document exists in the realtime service, if we are not retrieving a custom draft configuration
15491549
IDocument<TextDocument>? textDocument = null;
1550-
if (chapterNum != 0 && draftUsfmConfig is null)
1550+
if (draftUsfmConfig is null)
15511551
{
1552-
textDocument = await connection.FetchAsync<TextDocument>(id);
1553-
if (textDocument.IsLoaded)
1552+
// Retrieve the chapters for this book from the realtime server, if the chapter is zero
1553+
if (chapterNum == 0)
15541554
{
1555-
// Retrieve the snapshot if it exists
1556-
Snapshot<TextDocument> snapshot = await connection.FetchSnapshotAsync<TextDocument>(
1557-
id,
1558-
latestTimestampForRevision
1559-
);
1560-
if (snapshot.Data is not null)
1555+
List<object> content = [];
1556+
foreach (Chapter chapter in project.Texts.SingleOrDefault(t => t.BookNum == bookNum)?.Chapters ?? [])
1557+
{
1558+
id = TextDocument.GetDocId(sfProjectId, bookNum, chapter.Number, TextDocument.Draft);
1559+
textDocument = await connection.FetchAsync<TextDocument>(id);
1560+
if (textDocument.IsLoaded)
1561+
{
1562+
// Retrieve the snapshot if it exists
1563+
Snapshot<TextDocument> snapshot = await connection.FetchSnapshotAsync<TextDocument>(
1564+
id,
1565+
latestTimestampForRevision
1566+
);
1567+
if (snapshot.Data?.Content is not null)
1568+
{
1569+
// Append the chapter to the book content
1570+
content.AddRange(snapshot.Data.Content);
1571+
}
1572+
}
1573+
}
1574+
1575+
// Return the USJ of the entire book, if present
1576+
if (content.Count > 0)
15611577
{
1562-
return snapshot.Data;
1578+
return new Usj
1579+
{
1580+
Type = Usj.UsjType,
1581+
Version = Usj.UsjVersion,
1582+
Content = content,
1583+
};
15631584
}
1585+
}
1586+
else
1587+
{
1588+
// Otherwise, retrieve the specific chapter from the realtime server
1589+
textDocument = await connection.FetchAsync<TextDocument>(id);
1590+
if (textDocument.IsLoaded)
1591+
{
1592+
// Retrieve the snapshot if it exists
1593+
Snapshot<TextDocument> snapshot = await connection.FetchSnapshotAsync<TextDocument>(
1594+
id,
1595+
latestTimestampForRevision
1596+
);
1597+
if (snapshot.Data is not null)
1598+
{
1599+
return snapshot.Data;
1600+
}
15641601

1565-
// There is no draft at the timestamp
1566-
throw new DataNotFoundException("A draft cannot be retrieved at that timestamp");
1602+
// There is no draft at the timestamp
1603+
throw new DataNotFoundException("A draft cannot be retrieved at that timestamp");
1604+
}
15671605
}
15681606
}
15691607

test/SIL.XForge.Scripture.Tests/Services/MachineApiServiceTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2634,6 +2634,70 @@ await env
26342634
);
26352635
}
26362636

2637+
[Test]
2638+
public async Task GetPreTranslationUsjAsync_ChapterZeroLoadsMultipleChaptersFromMongo()
2639+
{
2640+
// Set up test environment
2641+
var env = new TestEnvironment();
2642+
Usj testUsjChapterTwo = new Usj
2643+
{
2644+
Type = Usj.UsjType,
2645+
Version = Usj.UsjVersion,
2646+
Content =
2647+
[
2648+
new UsjMarker
2649+
{
2650+
Type = "chapter",
2651+
Marker = "c",
2652+
Number = "2",
2653+
},
2654+
new UsjMarker
2655+
{
2656+
Type = "verse",
2657+
Marker = "v",
2658+
Number = "1",
2659+
},
2660+
"Verse 1",
2661+
],
2662+
};
2663+
string id1 = TextDocument.GetDocId(Project01, 40, 1, TextDocument.Draft);
2664+
env.TextDocuments.Add(new TextDocument(id1, TestUsj));
2665+
string id2 = TextDocument.GetDocId(Project01, 40, 2, TextDocument.Draft);
2666+
env.TextDocuments.Add(new TextDocument(id2, testUsjChapterTwo));
2667+
2668+
// The chapters must be configured in the project document
2669+
await env.Projects.UpdateAsync(
2670+
Project01,
2671+
u =>
2672+
u.Add(
2673+
p => p.Texts,
2674+
new TextInfo { BookNum = 40, Chapters = [new Chapter { Number = 1 }, new Chapter { Number = 2 }] }
2675+
)
2676+
);
2677+
2678+
// SUT
2679+
IUsj actual = await env.Service.GetPreTranslationUsjAsync(
2680+
User01,
2681+
Project01,
2682+
40,
2683+
0,
2684+
false,
2685+
DateTime.UtcNow,
2686+
null,
2687+
CancellationToken.None
2688+
);
2689+
2690+
// The returned Usj will be chapter 1 and 2 combined in order
2691+
List<object> expectedContent = [.. TestUsj.Content, .. testUsjChapterTwo.Content];
2692+
Usj expected = new Usj
2693+
{
2694+
Type = Usj.UsjType,
2695+
Version = Usj.UsjVersion,
2696+
Content = expectedContent,
2697+
};
2698+
Assert.That(actual, Is.EqualTo(expected).UsingPropertiesComparer());
2699+
}
2700+
26372701
[Test]
26382702
public void GetPreTranslationUsjAsync_CorpusDoesNotSupportUsfm()
26392703
{

0 commit comments

Comments
 (0)