Add CallLink Observed event and handling.

This commit is contained in:
Alex Hart
2024-06-06 10:39:50 -03:00
parent d3eb480d31
commit f572eb5322
7 changed files with 160 additions and 40 deletions

View File

@@ -10,6 +10,7 @@ import org.signal.core.util.SqlUtil
import org.signal.core.util.count
import org.signal.core.util.delete
import org.signal.core.util.deleteAll
import org.signal.core.util.exists
import org.signal.core.util.flatten
import org.signal.core.util.insertInto
import org.signal.core.util.isAbsent
@@ -580,6 +581,22 @@ class CallTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTabl
AppDependencies.databaseObserver.notifyCallUpdateObservers()
}
fun insertOrUpdateAdHocCallFromObserveEvent(
callRecipient: Recipient,
timestamp: Long,
callId: Long
) {
handleCallLinkUpdate(callRecipient, timestamp, CallId(callId), Direction.INCOMING)
}
fun insertAdHocCallFromObserveEvent(
callRecipient: Recipient,
timestamp: Long,
eraId: String
): Boolean {
return handleCallLinkUpdate(callRecipient, timestamp, CallId.fromEra(eraId), Direction.INCOMING)
}
fun insertOrUpdateGroupCallFromExternalEvent(
groupRecipientId: RecipientId,
sender: RecipientId,
@@ -606,7 +623,7 @@ class CallTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTabl
) {
val recipient = Recipient.resolved(groupRecipientId)
if (recipient.isCallLink) {
handleCallLinkUpdate(recipient, timestamp, peekGroupCallEraId)
handleCallLinkUpdate(recipient, timestamp, peekGroupCallEraId?.let { CallId.fromEra(it) })
} else {
handleGroupUpdate(recipient, sender, timestamp, peekGroupCallEraId, peekJoinedUuids, isCallFull)
}
@@ -676,35 +693,53 @@ class CallTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTabl
}
}
/**
* @return Whether or not a new row was inserted.
*/
private fun handleCallLinkUpdate(
callLinkRecipient: Recipient,
timestamp: Long,
peekGroupCallEraId: String?
) {
callId: CallId?,
direction: Direction = Direction.OUTGOING,
skipTimestampUpdate: Boolean = false
): Boolean {
check(callLinkRecipient.isCallLink)
val callId = peekGroupCallEraId?.let { CallId.fromEra(it).longValue() } ?: return
writableDatabase.withinTransaction { db ->
db.delete(TABLE_NAME)
.where("$PEER = ?", callLinkRecipient.id.serialize())
.run()
db.insertInto(TABLE_NAME)
.values(
CALL_ID to callId,
MESSAGE_ID to null,
PEER to callLinkRecipient.id.toLong(),
EVENT to Event.serialize(Event.GENERIC_GROUP_CALL),
TYPE to Type.serialize(Type.AD_HOC_CALL),
DIRECTION to Direction.serialize(Direction.OUTGOING),
TIMESTAMP to timestamp,
RINGER to null
).run(SQLiteDatabase.CONFLICT_ABORT)
if (callId == null) {
return false
}
val didInsert = writableDatabase.withinTransaction { db ->
val exists = db.exists(TABLE_NAME)
.where("$PEER = ? AND $CALL_ID = ?", callLinkRecipient.id.serialize(), callId)
.run()
if (exists && !skipTimestampUpdate) {
db.update(TABLE_NAME)
.values(TIMESTAMP to timestamp)
.where("$PEER = ? AND $CALL_ID = ? AND $TIMESTAMP < ?", callLinkRecipient.id.serialize(), callId, timestamp)
.run()
false
} else if (!exists) {
db.insertInto(TABLE_NAME)
.values(
CALL_ID to callId,
MESSAGE_ID to null,
PEER to callLinkRecipient.id.toLong(),
EVENT to Event.serialize(Event.GENERIC_GROUP_CALL),
TYPE to Type.serialize(Type.AD_HOC_CALL),
DIRECTION to Direction.serialize(direction),
TIMESTAMP to timestamp,
RINGER to null
).run(SQLiteDatabase.CONFLICT_ABORT)
Log.d(TAG, "Inserted new call event for call link. Call Id: $callId")
true
} else false
}
Log.d(TAG, "Inserted new call event for call link. Call Id: $callId")
AppDependencies.databaseObserver.notifyCallUpdateObservers()
return didInsert
}
private fun insertCallEventFromGroupUpdate(
@@ -1648,7 +1683,7 @@ class CallTable(context: Context, databaseHelper: SignalDatabase) : DatabaseTabl
@JvmStatic
fun from(event: CallEvent.Event?): Event? {
return when (event) {
null, CallEvent.Event.UNKNOWN_ACTION -> null
null, CallEvent.Event.UNKNOWN_ACTION, CallEvent.Event.OBSERVED -> null
CallEvent.Event.ACCEPTED -> ACCEPTED
CallEvent.Event.NOT_ACCEPTED -> NOT_ACCEPTED
CallEvent.Event.DELETE -> DELETE