Add captions from PAD into audio_captions table

This commit is contained in:
Gustavo Trott 2023-10-19 13:26:04 -03:00
parent eb9d7c941b
commit a5f8a9d90c
5 changed files with 51 additions and 6 deletions

View File

@ -66,7 +66,7 @@ trait UpdateTranscriptPubMsgHdlr {
val transcript = AudioCaptions.parseTranscript(msg.body.transcript)
AudioCaptionDAO.insertOrUpdateAudioCaption(msg.body.transcriptId, meetingId, msg.header.userId, transcript, new Timestamp(System.currentTimeMillis()))
AudioCaptionDAO.insertOrUpdateAudioCaption(msg.body.transcriptId, meetingId, msg.header.userId, transcript)
broadcastEvent(
msg.header.userId,

View File

@ -2,6 +2,7 @@ package org.bigbluebutton.core.apps.pads
import org.bigbluebutton.common2.msgs._
import org.bigbluebutton.core.bus.MessageBus
import org.bigbluebutton.core.db.AudioCaptionDAO
import org.bigbluebutton.core.models.Pads
import org.bigbluebutton.core.running.LiveMeeting
@ -40,6 +41,7 @@ trait PadPatchSysMsgHdlr {
broadcastEditCaptionHistoryEvent(msg.body.userId, msg.body.start, msg.body.end, group.name, locale, msg.body.text)
val tail = liveMeeting.captionModel.getTextTail(group.name)
broadcastPadTailEvent(group.externalId, tail)
AudioCaptionDAO.insertOrUpdatePadCaption(liveMeeting.props.meetingProp.intId, locale, msg.body.userId, tail)
}
}
case _ =>

View File

@ -2,6 +2,7 @@ package org.bigbluebutton.core.db
import slick.jdbc.PostgresProfile.api._
import java.sql.Timestamp
import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{ Failure, Success }
@ -9,6 +10,7 @@ case class AudioCaptionDbModel(
transcriptId: String,
meetingId: String,
userId: String,
lang: String,
transcript: String,
createdAt: java.sql.Timestamp
)
@ -17,22 +19,24 @@ class AudioCaptionTableDef(tag: Tag) extends Table[AudioCaptionDbModel](tag, Non
val transcriptId = column[String]("transcriptId", O.PrimaryKey)
val meetingId = column[String]("meetingId")
val userId = column[String]("userId")
val lang = column[String]("lang")
val transcript = column[String]("transcript")
val createdAt = column[java.sql.Timestamp]("createdAt")
def * = (transcriptId, meetingId, userId, transcript, createdAt) <> (AudioCaptionDbModel.tupled, AudioCaptionDbModel.unapply)
def * = (transcriptId, meetingId, userId, lang, transcript, createdAt) <> (AudioCaptionDbModel.tupled, AudioCaptionDbModel.unapply)
}
object AudioCaptionDAO {
def insertOrUpdateAudioCaption(transcriptId: String, meetingId: String, userId: String, transcript: String, createdAt: java.sql.Timestamp) = {
def insertOrUpdateAudioCaption(transcriptId: String, meetingId: String, userId: String, transcript: String) = {
DatabaseConnection.db.run(
TableQuery[AudioCaptionTableDef].insertOrUpdate(
AudioCaptionDbModel(
transcriptId = transcriptId,
meetingId = meetingId,
userId = userId,
lang = "",
transcript = transcript,
createdAt = createdAt
createdAt = new java.sql.Timestamp(System.currentTimeMillis())
)
)
).onComplete {
@ -40,4 +44,42 @@ object AudioCaptionDAO {
case Failure(e) => DatabaseConnection.logger.debug(s"Error upserting audio caption on AudioCaption: $e")
}
}
def insertOrUpdatePadCaption(meetingId: String, locale: String, userId: String, transcript: String) = {
val lines: Array[String] = transcript.split("\\r?\\n").filter(_.trim.nonEmpty)
val lastTwoLines = lines.takeRight(2)
val actions: Seq[DBIO[Int]] = lastTwoLines.map { line =>
sqlu"""
WITH upsert AS (
UPDATE audio_caption
SET transcript=${line},
"createdAt" = current_timestamp
WHERE "meetingId" = ${meetingId}
AND (${line} LIKE transcript || '%' or transcript LIKE ${line} || '%')
AND ${line} != transcript
AND lang = ${locale}
AND "createdAt" > current_timestamp - interval '15 seconds'
RETURNING *)
INSERT INTO audio_caption ("transcriptId", "meetingId", "userId", "lang", "transcript", "createdAt")
SELECT md5(random()::text || clock_timestamp()::text), ${meetingId}, ${userId}, 'en', ${line}, current_timestamp
WHERE NOT EXISTS (SELECT * FROM upsert)
AND ${line} NOT IN (SELECT transcript
FROM audio_caption
WHERE "meetingId" = ${meetingId}
AND lang = ${locale}
order by "createdAt" desc
limit 2
)"""
}
DatabaseConnection.db.run(DBIO.sequence(actions).transactionally).onComplete {
case Success(rowsAffected) =>
val total = rowsAffected.sum
DatabaseConnection.logger.debug(s"$total row(s) affected in audio_caption table!")
case Failure(e) =>
DatabaseConnection.logger.error(s"Error executing action: ", e)
}
}
}

View File

@ -21,8 +21,8 @@
"eslint-config-google": "^0.14.0"
},
"engines": {
"node": ">=16",
"npm": ">=8.5"
"node": "^18.16.0",
"npm": "^9.5.0"
}
},
"node_modules/@eslint/eslintrc": {

View File

@ -1465,6 +1465,7 @@ CREATE TABLE "audio_caption" (
"transcriptId" varchar(100) NOT NULL PRIMARY KEY,
"meetingId" varchar(100) REFERENCES "meeting"("meetingId") ON DELETE CASCADE,
"userId" varchar(50) REFERENCES "user"("userId") ON DELETE CASCADE,
"lang" varchar(15),
"transcript" text,
"createdAt" timestamp with time zone
);