From 48de7625c9a5c28f877e748862ef8f5395c76010 Mon Sep 17 00:00:00 2001
From: Richard Alam <ritzalam@gmail.com>
Date: Mon, 9 Apr 2018 12:22:00 -0700
Subject: [PATCH]  - user inactivity audit message sending and receiving

---
 .../UserInactivityAuditResponseMsgHdlr.scala  | 16 ++++++++--------
 .../bigbluebutton/core/models/Users2x.scala   | 11 ++++++-----
 .../core/running/MeetingActor.scala           | 19 ++++++++++++-------
 .../client/meeting/AllowedMessageNames.scala  |  1 +
 .../bigbluebutton/common2/msgs/UsersMgs.scala |  4 ++--
 5 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/apps/users/UserInactivityAuditResponseMsgHdlr.scala b/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/apps/users/UserInactivityAuditResponseMsgHdlr.scala
index 04a48d9916..d97f2a8398 100755
--- a/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/apps/users/UserInactivityAuditResponseMsgHdlr.scala
+++ b/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/apps/users/UserInactivityAuditResponseMsgHdlr.scala
@@ -5,13 +5,13 @@ import org.bigbluebutton.core.models.Users2x
 import org.bigbluebutton.core.running.MeetingActor
 
 trait UserInactivityAuditResponseMsgHdlr {
-	this: MeetingActor =>
+  this: MeetingActor =>
 
-	def handleUserInactivityAuditResponseMsg(msg: UserInactivityAuditResponseMsg):Unit = {
-		for {
-			user <- Users2x.findWithIntId(liveMeeting.users2x, msg.body.userId)
-		} yield {
-			Users2x.updateInactivityResponse(liveMeeting.users2x, user)
-		}
-	}
+  def handleUserInactivityAuditResponseMsg(msg: UserInactivityAuditResponseMsg): Unit = {
+    for {
+      user <- Users2x.findWithIntId(liveMeeting.users2x, msg.body.userId)
+    } yield {
+      Users2x.updateInactivityResponse(liveMeeting.users2x, user)
+    }
+  }
 }
diff --git a/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/models/Users2x.scala b/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/models/Users2x.scala
index ac45f072d4..15051a78c4 100755
--- a/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/models/Users2x.scala
+++ b/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/models/Users2x.scala
@@ -1,6 +1,7 @@
 package org.bigbluebutton.core.models
 
 import com.softwaremill.quicklens._
+import org.bigbluebutton.core.util.TimeUtil
 
 object Users2x {
   def findWithIntId(users: Users2x, intId: String): Option[UserState] = {
@@ -26,11 +27,11 @@ object Users2x {
     users.toVector.filter(u => !u.presenter)
   }
 
-	def updateInactivityResponse(users: Users2x, u: UserState): UserState = {
-		val newUserState = modify(u)(_.inactivityResponseOn).setTo(System.currentTimeMillis())
-		users.save(newUserState)
-		newUserState
-	}
+  def updateInactivityResponse(users: Users2x, u: UserState): UserState = {
+    val newUserState = modify(u)(_.inactivityResponseOn).setTo(TimeUtil.timeNowInMs())
+    users.save(newUserState)
+    newUserState
+  }
 
   def changeRole(users: Users2x, u: UserState, newRole: String): UserState = {
     val newUserState = modify(u)(_.role).setTo(newRole).modify(_.roleChangedOn).setTo(System.currentTimeMillis())
diff --git a/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/running/MeetingActor.scala b/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/running/MeetingActor.scala
index 4de5528273..00a97f8517 100755
--- a/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/running/MeetingActor.scala
+++ b/akka-bbb-apps/src/main/scala/org/bigbluebutton/core/running/MeetingActor.scala
@@ -4,6 +4,7 @@ import java.io.{ PrintWriter, StringWriter }
 
 import akka.actor._
 import akka.actor.SupervisorStrategy.Resume
+import org.bigbluebutton.SystemConfiguration
 import org.bigbluebutton.core.apps.groupchats.{ GroupChatApp, GroupChatHdlrs }
 import org.bigbluebutton.core.apps.presentationpod._
 import org.bigbluebutton.core.apps.users._
@@ -54,6 +55,7 @@ class MeetingActor(
   val liveMeeting: LiveMeeting
 )
     extends BaseMeetingActor
+    with SystemConfiguration
     with GuestsApp
     with LayoutApp2x
     with VoiceApp2x
@@ -79,7 +81,7 @@ class MeetingActor(
     with SyncGetMeetingInfoRespMsgHdlr
     with ClientToServerLatencyTracerMsgHdlr
     with ValidateConnAuthTokenSysMsgHdlr
-		with UserInactivityAuditResponseMsgHdlr {
+    with UserInactivityAuditResponseMsgHdlr {
 
   override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10, withinTimeRange = 1 minute) {
     case e: Exception => {
@@ -348,7 +350,7 @@ class MeetingActor(
 
       case m: ValidateConnAuthTokenSysMsg => handleValidateConnAuthTokenSysMsg(m)
 
-				case m: UserInactivityAuditResponseMsg => handleUserInactivityAuditResponseMsg(m)
+      case m: UserInactivityAuditResponseMsg => handleUserInactivityAuditResponseMsg(m)
 
       case _ => log.warning("***** Cannot handle " + msg.envelope.name)
     }
@@ -515,24 +517,27 @@ class MeetingActor(
 
   def processUserInactivityAudit(): Unit = {
     val now = TimeUtil.timeNowInMs()
-    if (now - lastUserInactivitySentOn > 30000) {
+    val auditTimerMs = TimeUtil.minutesToMillis(userInactivityAuditTimer)
+    if (now - lastUserInactivitySentOn > auditTimerMs) {
       lastUserInactivitySentOn = now
       checkInactiveUsers = true
       val event = buildUserInactivityAuditMsg(liveMeeting.props.meetingProp.intId)
       outGW.send(event)
     }
 
-    if (checkInactiveUsers && now - lastUserInactivitySentOn > 10000) {
+    val auditResponseMs = TimeUtil.minutesToMillis(userInactivityAuditResponseDuration)
+    if (checkInactiveUsers && now - lastUserInactivitySentOn > auditResponseMs) {
       checkInactiveUsers = false
       checkForInactiveUsers()
     }
   }
 
   def checkForInactiveUsers(): Unit = {
-    val now = TimeUtil.timeNowInMs()
+    val auditResponseMs = TimeUtil.minutesToMillis(userInactivityAuditResponseDuration)
     val users = Users2x.findAll(liveMeeting.users2x)
     users foreach { u =>
-      if (now - u.inactivityResponseOn > 2000) {
+      val respondedOntIme = lastUserInactivitySentOn < u.inactivityResponseOn && (lastUserInactivitySentOn + auditResponseMs) > u.inactivityResponseOn
+      if (!respondedOntIme) {
         UsersApp.ejectUserFromMeeting(outGW, liveMeeting, u.intId, SystemUser.ID, "User inactive for too long.", EjectReasonCode.USER_INACTIVITY)
         Sender.sendDisconnectClientSysMsg(liveMeeting.props.meetingProp.intId, u.intId, SystemUser.ID, EjectReasonCode.USER_INACTIVITY, outGW)
       }
@@ -542,7 +547,7 @@ class MeetingActor(
   def buildUserInactivityAuditMsg(meetingId: String): BbbCommonEnvCoreMsg = {
     val routing = Routing.addMsgToClientRouting(MessageTypes.BROADCAST_TO_MEETING, meetingId, "system")
     val envelope = BbbCoreEnvelope(UserInactivityAuditMsg.NAME, routing)
-    val body = UserInactivityAuditMsgBody(meetingId)
+    val body = UserInactivityAuditMsgBody(meetingId, TimeUtil.minutesToSeconds(userInactivityAuditResponseDuration))
     val header = BbbClientMsgHeader(UserInactivityAuditMsg.NAME, meetingId, "system")
     val event = UserInactivityAuditMsg(header, body)
 
diff --git a/bbb-apps-common/src/main/scala/org/bigbluebutton/client/meeting/AllowedMessageNames.scala b/bbb-apps-common/src/main/scala/org/bigbluebutton/client/meeting/AllowedMessageNames.scala
index 6b322b5b34..07dac9fb09 100755
--- a/bbb-apps-common/src/main/scala/org/bigbluebutton/client/meeting/AllowedMessageNames.scala
+++ b/bbb-apps-common/src/main/scala/org/bigbluebutton/client/meeting/AllowedMessageNames.scala
@@ -34,6 +34,7 @@ object AllowedMessageNames {
       GetGuestPolicyReqMsg.NAME,
       SetGuestPolicyCmdMsg.NAME,
       GuestsWaitingApprovedMsg.NAME,
+      UserInactivityAuditResponseMsg.NAME,
 
       // Webcams
       GetWebcamsOnlyForModeratorReqMsg.NAME,
diff --git a/bbb-common-message/src/main/scala/org/bigbluebutton/common2/msgs/UsersMgs.scala b/bbb-common-message/src/main/scala/org/bigbluebutton/common2/msgs/UsersMgs.scala
index acfbd844e5..3abf0819f0 100755
--- a/bbb-common-message/src/main/scala/org/bigbluebutton/common2/msgs/UsersMgs.scala
+++ b/bbb-common-message/src/main/scala/org/bigbluebutton/common2/msgs/UsersMgs.scala
@@ -362,8 +362,8 @@ case class GetPresenterGroupRespMsgBody(presenterGroup: Vector[String], requeste
 
 object UserInactivityAuditMsg { val NAME = "UserInactivityAuditMsg" }
 case class UserInactivityAuditMsg(header: BbbClientMsgHeader, body: UserInactivityAuditMsgBody) extends StandardMsg
-case class UserInactivityAuditMsgBody(meetingId: String)
+case class UserInactivityAuditMsgBody(meetingId: String, responseDuration: Long)
 
 object UserInactivityAuditResponseMsg { val NAME = "UserInactivityAuditResponseMsg" }
 case class UserInactivityAuditResponseMsg(header: BbbClientMsgHeader, body: UserInactivityAuditResponseMsgBody) extends StandardMsg
-case class UserInactivityAuditResponseMsgBody(meetingId: String, userId: String)
\ No newline at end of file
+case class UserInactivityAuditResponseMsgBody(userId: String)
-- 
GitLab