Skip to content

Commit

Permalink
Set a captions_file_id post prop to avoid indirection when fetching c…
Browse files Browse the repository at this point in the history
…aptions file for video preview (#581)
  • Loading branch information
streamer45 authored Dec 4, 2023
1 parent dc74531 commit d9ed7a7
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 22 deletions.
50 changes: 42 additions & 8 deletions server/bot_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,49 @@ func (p *Plugin) handleBotPostTranscriptions(w http.ResponseWriter, r *http.Requ

// We update the metadata with the file and post IDs for the transcription.
transcriptions, ok := post.GetProp("transcriptions").(map[string]any)
if ok {
var tm jobMetadata
tm.fromMap(transcriptions[info.JobID])
tm.FileID = info.FileIDs[0]
tm.PostID = trPost.Id
transcriptions[info.JobID] = tm.toMap()
post.AddProp("transcriptions", transcriptions)
if !ok {
res.Err = "unexpected data found in transcriptions post prop"
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}

var tm jobMetadata
tm.fromMap(transcriptions[info.JobID])
tm.FileID = info.FileIDs[0]
tm.PostID = trPost.Id
transcriptions[info.JobID] = tm.toMap()
post.AddProp("transcriptions", transcriptions)

// We retrieve the related recording info (if any) so that we can save the file id
// for the VTT captions in the props of the recording post that will
// eventually render them on top of the video player.
recordings, ok := post.GetProp("recordings").(map[string]any)
if !ok {
res.Err = "unexpected data found in recordings post prop"
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}
var rm jobMetadata
rm.fromMap(recordings[tm.RecID])
if rm.PostID != "" {
recPost, appErr := p.API.GetPost(rm.PostID)
if appErr != nil {
res.Err = "failed to get recording post: " + appErr.Error()
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}
recPost.AddProp("captions_file_id", tm.FileID)
if _, appErr := p.API.UpdatePost(recPost); appErr != nil {
res.Err = "failed to update recording post: " + appErr.Error()
res.Code = http.StatusInternalServerError
p.LogError(res.Err, "trID", info.JobID)
return
}
} else {
p.LogError("unexpected data found in transcriptions post prop", "trID", info.JobID)
p.LogWarn("unexpected missing recording post ID", "trID", info.JobID)
}

_, appErr = p.API.UpdatePost(post)
Expand Down
7 changes: 4 additions & 3 deletions server/job_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ type jobMetadata struct {
FileID string
// PostID is the Post.Id that holds the job's artifacts (e.g. recording post).
PostID string
// RecID is the recording ID.
// RecID is the recording job ID.
RecID string
// TrID is the transcription ID.
// TrID is the transcription job ID.
TrID string
}

Expand Down Expand Up @@ -96,7 +96,8 @@ func (p *Plugin) saveRecordingMetadata(postID, recID, trID string) error {
post.AddProp("recordings", recordings)

// This is where we map a transcription to a recording.
// This information will be used by the client when rendering the recording preview.
// This information will be used when the transcribing job completes to populate
// the recording post props with the captions file id.
if trID != "" {
tm := jobMetadata{
RecID: recID,
Expand Down
14 changes: 3 additions & 11 deletions webapp/src/components/recordings_file_preview.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import {FileInfo} from '@mattermost/types/files';
import {Post} from '@mattermost/types/posts';
import {GlobalState} from '@mattermost/types/store';
import {Client4} from 'mattermost-redux/client';
import {getPost} from 'mattermost-redux/selectors/entities/posts';
import React, {useMemo} from 'react';
import {useSelector} from 'react-redux';
import styled from 'styled-components';

type Props = {
Expand All @@ -13,12 +10,7 @@ type Props = {
}

const RecordingsFilePreview = ({fileInfo, post}: Props) => {
const callPost = useSelector((state: GlobalState) => getPost(state, post.props?.call_post_id));

const recording = callPost?.props?.recordings?.[post.props?.recording_id];
const transcription = callPost?.props?.transcriptions?.[recording?.tr_id];

const now = useMemo(() => Date.now(), [recording, transcription]);
const now = useMemo(() => Date.now(), [post.props.captions_file_id]);

return (
<Video
Expand All @@ -31,13 +23,13 @@ const RecordingsFilePreview = ({fileInfo, post}: Props) => {
src={Client4.getFileUrl(fileInfo.id, now)}
type={fileInfo.mime_type}
/>
{ transcription?.file_id &&
{ post.props.captions_file_id &&
<track
data-testid='calls-recording-transcription'
label='Transcription'
kind='subtitles'
srcLang='en'
src={Client4.getFileUrl(transcription.file_id, now)}
src={Client4.getFileUrl(post.props.captions_file_id, now)}
default={true}
/>
}
Expand Down

0 comments on commit d9ed7a7

Please sign in to comment.