Skip to content

Commit 86b1a17

Browse files
authored
Merge pull request #491 from classtranscribe/835-extend-caption-editing-area-ui
835 extend caption editing area UI
2 parents 042cc8d + d52c7e0 commit 86b1a17

File tree

3 files changed

+91
-23
lines changed

3 files changed

+91
-23
lines changed

ClassTranscribeServer/Controllers/CaptionsController.cs

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using ClassTranscribeDatabase;
22
using ClassTranscribeDatabase.Models;
3+
using ClassTranscribeServer.Utils;
34
using Microsoft.AspNetCore.Authorization;
45
using Microsoft.AspNetCore.Http;
56
using Microsoft.AspNetCore.Mvc;
@@ -23,14 +24,16 @@ public class CaptionsController : BaseController
2324
private readonly WakeDownloader _wakeDownloader;
2425
private readonly CaptionQueries _captionQueries;
2526
private readonly SubParser parser = new SubParser();
27+
private readonly UserUtils _userUtils;
2628

2729
public CaptionsController(WakeDownloader wakeDownloader,
2830
CTDbContext context,
29-
CaptionQueries captionQueries,
31+
CaptionQueries captionQueries, UserUtils userUtils,
3032
ILogger<CaptionsController> logger) : base(context, logger)
3133
{
3234
_captionQueries = captionQueries;
3335
_wakeDownloader = wakeDownloader;
36+
_userUtils = userUtils;
3437
}
3538

3639
// GET: api/Captions/ByTranscription/5
@@ -72,6 +75,9 @@ public async Task<ActionResult<string>> GetTranscriptionFile(string Transcriptio
7275
[HttpGet]
7376
public async Task<ActionResult<Caption>> GetCaption(string transcriptionId, int index)
7477
{
78+
// Note here that captions are effectively "stacked" on top of all other captions with the
79+
// same transcription Id and Index.
80+
// Then, getting a caption only returns the top caption of the stack with the newest creation date.
7581
var captions = await _context.Captions.Where(c => c.TranscriptionId == transcriptionId && c.Index == index)
7682
.OrderByDescending(c => c.CreatedAt).ToListAsync();
7783
if (captions == null || captions.Count == 0)
@@ -86,32 +92,83 @@ public async Task<ActionResult<Caption>> GetCaption(string transcriptionId, int
8692

8793
// POST: api/Captions
8894
[HttpPost]
95+
[Authorize]
8996
public async Task<ActionResult<Caption>> PostCaption(Caption modifiedCaption)
9097
{
98+
// This endpoint should handle deletion as well, which is represented by posting a caption
99+
// with the empty string as text.
100+
101+
// This endpoint should be accessible only for people who are logged in
102+
var user = await _userUtils.GetUser(User);
103+
if (user == null)
104+
{
105+
return Unauthorized();
106+
}
107+
91108
if (modifiedCaption == null || modifiedCaption.Id == null)
92109
{
93110
return BadRequest("modifiedCaption.Id not present");
94111
}
112+
95113
Caption oldCaption = await _context.Captions.FindAsync(modifiedCaption.Id);
96114
if (oldCaption == null)
97115
{
98116
return NotFound();
99117
}
100118
Caption newCaption = new Caption
101119
{
102-
Begin = oldCaption.Begin,
103-
End = oldCaption.End,
120+
Begin = modifiedCaption.Begin,
121+
End = modifiedCaption.End,
104122
Index = oldCaption.Index,
105123
CaptionType = oldCaption.CaptionType,
106124
Text = modifiedCaption.Text,
107-
TranscriptionId = oldCaption.TranscriptionId
125+
TranscriptionId = oldCaption.TranscriptionId,
126+
LastUpdatedBy = user.Id,
127+
CreatedBy = oldCaption.CreatedBy
108128
};
109129
_context.Captions.Add(newCaption);
110130
await _context.SaveChangesAsync();
111-
// nope _wakeDownloader.UpdateVTTFile(oldCaption.TranscriptionId);
112131
return newCaption;
113132
}
114133

134+
// POST: api/Captions/Add
135+
[HttpPost("Add")]
136+
[Authorize]
137+
public async Task<ActionResult<Caption>> AddCaption(Caption newCaption)
138+
{
139+
// This endpoint should be accessible only for people who are logged in
140+
var user = await _userUtils.GetUser(User);
141+
if (user == null)
142+
{
143+
return Unauthorized();
144+
}
145+
146+
if (newCaption == null)
147+
{
148+
return BadRequest("newCaption not present");
149+
}
150+
151+
var allCaptions = await _context.Captions.Where(c => c.TranscriptionId == newCaption.TranscriptionId).ToListAsync();
152+
153+
// Every new caption must have a unique index to avoid conflicts with existing indices.
154+
var newIndex = allCaptions.Max(c => c.Index) + 1;
155+
156+
Caption addedCaption = new Caption
157+
{
158+
Begin = newCaption.Begin,
159+
End = newCaption.End,
160+
Index = newIndex,
161+
CaptionType = newCaption.CaptionType,
162+
Text = newCaption.Text,
163+
TranscriptionId = newCaption.TranscriptionId,
164+
LastUpdatedBy = user.Id,
165+
CreatedBy = user.Id
166+
};
167+
_context.Captions.Add(addedCaption);
168+
await _context.SaveChangesAsync();
169+
return addedCaption;
170+
}
171+
115172
// POST: api/Captions/UpVote
116173
[HttpPost("UpVote")]
117174
public async Task<ActionResult<Caption>> UpVote(string id)

TaskEngine.Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,11 @@ apt-get install -y netcat-traditional && apt-get -q update
3838
# Microsoft 8.0 issue: https://github.com/Azure-Samples/cognitive-services-speech-sdk/issues/2204
3939
# This will install OpenSSL 1.1.1 because it is needed by the Speech SDK.
4040
# RUN ARCH=$(dpkg --print-architecture)
41-
COPY ./install-speech-hack-libssl1.sh /
42-
RUN /install-speech-hack-libssl1.sh
41+
# COPY ./install-speech-hack-libssl1.sh /
42+
# RUN wget /install-speech-hack-libssl1.sh
43+
44+
RUN wget http://archive.ubuntu.com/ubuntu/pool/main/o/openssl/libssl1.1_1.1.0g-2ubuntu4_amd64.deb
45+
RUN dpkg -i libssl1.1_1.1.0g-2ubuntu4_amd64.deb
4346

4447

4548
FROM publish_base as publish

UnitTests/ClassTranscribeServer/ControllerTests/CaptionsControllerTest.cs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,15 @@ public class CaptionsControllerTest : BaseControllerTest
2121
public CaptionsControllerTest(GlobalFixture fixture) : base(fixture)
2222
{
2323
_controller = new CaptionsController(
24-
(WakeDownloader) fixture._serviceProvider.GetService(typeof(WakeDownloader)),
24+
(WakeDownloader)fixture._serviceProvider.GetService(typeof(WakeDownloader)),
2525
_context,
2626
new CaptionQueries(_context),
27+
_userUtils,
2728
null
28-
);
29+
)
30+
{
31+
ControllerContext = fixture._controllerContext
32+
};
2933
}
3034

3135
[Fact]
@@ -146,7 +150,7 @@ public async Task Post_Caption_Success()
146150
Text = "foo bar",
147151
CaptionType = CaptionType.TextCaption
148152
};
149-
153+
_context.Users.Add(new ApplicationUser { Id = TestGlobals.TEST_USER_ID });
150154
_context.Captions.Add(caption);
151155
_context.SaveChanges();
152156

@@ -162,6 +166,9 @@ public async Task Post_Caption_Success()
162166
[Fact]
163167
public async Task Post_Caption_Fail()
164168
{
169+
_context.Users.Add(new ApplicationUser { Id = TestGlobals.TEST_USER_ID });
170+
_context.SaveChanges();
171+
165172
var result = await _controller.PostCaption(null);
166173
Assert.IsType<BadRequestObjectResult>(result.Result);
167174

@@ -320,7 +327,7 @@ public async Task Search_In_Offering_Fail()
320327
public async Task Search_In_Offering()
321328
{
322329
var video = new Video { Id = "789" };
323-
330+
324331
var transcriptions = new List<Transcription>()
325332
{
326333
new Transcription
@@ -359,9 +366,9 @@ public async Task Search_In_Offering()
359366
};
360367
var course = new Course { Id = "cid1" };
361368
var offering = new Offering { Id = "oid8" };
362-
var CourseOffering = new CourseOffering { Id = "2123000", CourseId = "cid1", OfferingId = "oid8"};
363-
var playlist = new Playlist { Id = "2456" , OfferingId = offering.Id, Name = "Playlist 1"};
364-
var media = new Media { Id = "2678", PlaylistId = playlist.Id , VideoId = video.Id, Name = "Media 1"};
369+
var CourseOffering = new CourseOffering { Id = "2123000", CourseId = "cid1", OfferingId = "oid8" };
370+
var playlist = new Playlist { Id = "2456", OfferingId = offering.Id, Name = "Playlist 1" };
371+
var media = new Media { Id = "2678", PlaylistId = playlist.Id, VideoId = video.Id, Name = "Media 1" };
365372
_context.Courses.Add(course);
366373
_context.Offerings.Add(offering);
367374
_context.CourseOfferings.Add(CourseOffering);
@@ -379,23 +386,24 @@ public async Task Search_In_Offering()
379386
var onlyEnglishResults = await _controller.SearchInOffering(offering.Id, "fortran");
380387
Assert.Single(onlyEnglishResults.Value);
381388

382-
var bothResults = await _controller.SearchInOffering(offering.Id, "fortran","");
383-
389+
var bothResults = await _controller.SearchInOffering(offering.Id, "fortran", "");
390+
384391
List<SearchedCaptionDTO> bothResultList = bothResults.Value.ToList();
385392
Assert.Equal(captions.Count, bothResultList.Count());
386-
for (int i = 0; i < captions.Count; i++) {
393+
for (int i = 0; i < captions.Count; i++)
394+
{
387395
Assert.Equal(captions[i].Text, bothResultList[i].Caption.Text);
388-
Assert.Equal(transcriptions[i].Language, bothResultList[i].Language );
396+
Assert.Equal(transcriptions[i].Language, bothResultList[i].Language);
389397
}
390398

391399
var oneFrenchResult = await _controller.SearchInOffering(offering.Id, "fortran", "fr");
392-
400+
393401
Assert.Single(oneFrenchResult.Value);
394-
var c = oneFrenchResult.Value.First();
395-
Assert.Equal( captions[1].Text, c.Caption.Text);
402+
var c = oneFrenchResult.Value.First();
403+
Assert.Equal(captions[1].Text, c.Caption.Text);
396404
Assert.Null(c.Caption.Transcription);
397-
398-
Assert.Equal(media.Id, c.MediaId );
405+
406+
Assert.Equal(media.Id, c.MediaId);
399407
Assert.Equal(playlist.Id, c.PlaylistId);
400408
Assert.Equal(media.Name, c.MediaName);
401409
Assert.Equal(playlist.Name, c.PlaylistName);

0 commit comments

Comments
 (0)