Set userId as PK for user_voice table (user has only one mic, following Meteor current logic)

This commit is contained in:
Gustavo Trott 2023-04-04 09:02:41 -03:00
parent 3b9468696f
commit 94111058e5
4 changed files with 31 additions and 32 deletions

View File

@ -7,8 +7,8 @@ import scala.concurrent.ExecutionContext.Implicits.global
import scala.util.{Failure, Success }
case class UserVoiceDbModel(
voiceUserId: String,
userId: String,
voiceUserId: String,
callerName: String,
callerNum: String,
callingWith: String,
@ -27,11 +27,11 @@ case class UserVoiceDbModel(
class UserVoiceDbTableDef(tag: Tag) extends Table[UserVoiceDbModel](tag, None, "user_voice") {
override def * = (
voiceUserId, userId, callerName, callerNum, callingWith, joined, listenOnly,
userId, voiceUserId, callerName, callerNum, callingWith, joined, listenOnly,
muted, spoke, talking, floor, lastFloorTime, voiceConf, color, startTime, endTime
) <> (UserVoiceDbModel.tupled, UserVoiceDbModel.unapply)
val voiceUserId = column[String]("voiceUserId", O.PrimaryKey)
val userId = column[String]("userId")
val userId = column[String]("userId", O.PrimaryKey)
val voiceUserId = column[String]("voiceUserId")
val callerName = column[String]("callerName")
val callerNum = column[String]("callerNum")
val callingWith = column[String]("callingWith")
@ -53,11 +53,14 @@ object UserVoiceDAO {
// val usersTable = TableQuery[UserTableDef]
def insert(voiceUserState: VoiceUserState) = {
println("Will add!")
println(voiceUserState)
DatabaseConnection.db.run(
TableQuery[UserVoiceDbTableDef].forceInsert(
TableQuery[UserVoiceDbTableDef].insertOrUpdate(
UserVoiceDbModel(
voiceUserId = voiceUserState.voiceUserId,
userId = voiceUserState.intId,
voiceUserId = voiceUserState.voiceUserId,
callerName = voiceUserState.callerName,
callerNum = voiceUserState.callerNum,
callingWith = voiceUserState.callingWith,
@ -85,7 +88,7 @@ object UserVoiceDAO {
def update(voiceUserState: VoiceUserState) = {
DatabaseConnection.db.run(
TableQuery[UserVoiceDbTableDef]
.filter(_.voiceUserId === voiceUserState.voiceUserId)
.filter(_.userId === voiceUserState.intId)
.map(u => (u.listenOnly, u.muted, u.floor, u.lastFloorTime))
.update((voiceUserState.listenOnly, voiceUserState.muted, voiceUserState.floor, voiceUserState.lastFloorTime))
).onComplete {
@ -104,13 +107,13 @@ object UserVoiceDAO {
"spoke" = true,
"endTime" = null,
"startTime" = (case when "talking" is false then $now else "startTime" end)
WHERE "voiceUserId" = ${voiceUserState.voiceUserId}"""
WHERE "userId" = ${voiceUserState.intId}"""
} else {
sqlu"""UPDATE user_voice SET
"talking" = false,
"startTime" = null,
"endTime" = (case when "talking" is true then $now else "endTime" end)
WHERE "voiceUserId" = ${voiceUserState.voiceUserId}"""
WHERE "userId" = ${voiceUserState.intId}"""
}
DatabaseConnection.db.run(updateSql).onComplete {
@ -119,18 +122,14 @@ object UserVoiceDAO {
}
}
def delete(voiceUserId: String) = {
DatabaseConnection.db.run(
TableQuery[UserVoiceDbTableDef]
.filter(_.voiceUserId === voiceUserId)
.delete
).onComplete {
case Success(rowsAffected) => println(s"Voice ${voiceUserId} deleted")
case Failure(e) => println(s"Error deleting voice: $e")
}
}
def deleteUser(userId: String) = {
//Meteor sets this props instead of removing
// muted: false
// talking: false
// listenOnly: false
// joined: false
// spoke: false
DatabaseConnection.db.run(
TableQuery[UserVoiceDbTableDef]
.filter(_.userId === userId)

View File

@ -106,7 +106,7 @@ function App() {
<ChatPublicMessages />
<br />
<CursorsAll />
<br />
<br />
<TalkingStream />
<br />
<CursorsStream />

View File

@ -35,7 +35,7 @@ function UserList() {
clientType
leftFlag
loggedOut
voices {
voice {
joined
listenOnly
talking
@ -52,7 +52,7 @@ function UserList() {
isDefaultName
sequence
shortName
online
currentlyInRoom
}
}
}`
@ -99,13 +99,13 @@ function UserList() {
<td style={{backgroundColor: user.cameras.length > 0 ? '#A0DAA9' : ''}}>{user.cameras.length > 0 ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.presPagesWritable.length > 0 ? '#A0DAA9' : ''}}>{user.presPagesWritable.length > 0 ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.pinned === true ? '#A0DAA9' : ''}}>{user.pinned === true ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voices.length > 0 ? '#A0DAA9' : ''}}>{user.voices.length > 0 ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voices.filter(m => m.listenOnly === true).length > 0 ? '#A0DAA9' : ''}}>{user.voices.filter(m => m.listenOnly === true).length > 0 ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voices.filter(m => m.talking === true).length > 0 ? '#A0DAA9' : ''}}>{user.voices.filter(m => m.talking === true).length > 0 ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voices.filter(m => m.muted === true).length > 0 ? '#A0DAA9' : ''}}>{user.voices.filter(m => m.muted === true).length > 0 ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voice?.joined === true ? '#A0DAA9' : ''}}>{user.voice?.joined === true ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voice?.listenOnly === true ? '#A0DAA9' : ''}}>{user.voice?.listenOnly === true ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voice?.talking === true ? '#A0DAA9' : ''}}>{user.voice?.talking === true ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.voice?.muted === true ? '#A0DAA9' : ''}}>{user.voice?.muted === true ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.locked === true ? '#A0DAA9' : ''}}>{user.locked === true ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.lastBreakoutRoom?.online === true ? '#A0DAA9' : ''}}>
{user.lastBreakoutRoom?.shortName}{user.lastBreakoutRoom?.online == true ? ' (Online)' : ''}
<td style={{backgroundColor: user.lastBreakoutRoom?.currentlyInRoom === true ? '#A0DAA9' : ''}}>
{user.lastBreakoutRoom?.shortName}{user.lastBreakoutRoom?.currentlyInRoom == true ? ' (Online)' : ''}
</td>
<td style={{backgroundColor: user.leftFlag === true ? '#A0DAA9' : ''}}>{user.leftFlag === true ? 'Yes' : 'No'}</td>
<td style={{backgroundColor: user.loggedOut === true ? '#A0DAA9' : ''}}>{user.loggedOut === true ? 'Yes' : 'No'}</td>

View File

@ -177,8 +177,8 @@ CREATE TABLE public."user" (
CREATE INDEX "idx_user_meetingId" ON "user"("meetingId");
CREATE TABLE "user_voice" (
"voiceUserId" varchar(100) PRIMARY KEY,
"userId" varchar(50) NOT NULL REFERENCES "user"("userId") ON DELETE CASCADE,
"userId" varchar(50) PRIMARY KEY NOT NULL REFERENCES "user"("userId") ON DELETE CASCADE,
"voiceUserId" varchar(100),
"callerName" varchar(100),
"callerNum" varchar(100),
"callingWith" varchar(100),
@ -194,7 +194,7 @@ CREATE TABLE "user_voice" (
"endTime" bigint NULL,
"startTime" bigint NULL
);
CREATE INDEX "idx_user_voice_userId" ON "user_voice"("userId");
--CREATE INDEX "idx_user_voice_userId" ON "user_voice"("userId");
CREATE OR REPLACE VIEW "v_user_voice" AS
SELECT