Skip to content

Commit

Permalink
Added federation support for multiple LAOs in the same app
Browse files Browse the repository at this point in the history
  • Loading branch information
quadcopterman committed Jun 26, 2024
1 parent 71e63ad commit 05c3de9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import javax.inject.Singleton
class LinkedOrganizationsRepository @Inject constructor() {
private var challenge: Challenge? = null
private var onChallengeUpdatedCallback: ((Challenge) -> Unit)? = null
private var linkedLaos: MutableMap<String, Array<String>> = mutableMapOf()
private var linkedLaos: MutableMap<String, MutableMap<String, Array<String>>> = mutableMapOf()
private var onLinkedLaosUpdatedCallback: ((MutableMap<String, Array<String>>) -> Unit)? = null
private var newTokensNotifyFunction: ((String, String, Array<String>) -> Unit)? = null
var otherLaoId: String? = null
Expand All @@ -33,15 +33,22 @@ class LinkedOrganizationsRepository @Inject constructor() {
return challenge
}

fun addLinkedLao(lao_id: String, tokens: Array<String>) {
linkedLaos[lao_id] = tokens
onLinkedLaosUpdatedCallback?.invoke(linkedLaos)
fun addLinkedLao(laoId: String, otherLaoId: String, tokens: Array<String>) {
if (!linkedLaos.containsKey(laoId)) {
linkedLaos[laoId] = mutableMapOf()
}
linkedLaos[laoId]!![otherLaoId] = tokens
onLinkedLaosUpdatedCallback?.invoke(linkedLaos[laoId]!!)
}

fun updateAndNotifyLinkedLao(lao_id: String, tokens: Array<String>, rollCallId: String) {
linkedLaos[lao_id] = tokens
newTokensNotifyFunction?.invoke(lao_id, rollCallId, tokens)
onLinkedLaosUpdatedCallback?.invoke(linkedLaos)
fun updateAndNotifyLinkedLao(
laoId: String,
otherLaoId: String,
tokens: Array<String>,
rollCallId: String
) {
addLinkedLao(laoId, otherLaoId, tokens)
newTokensNotifyFunction?.invoke(laoId, rollCallId, tokens)
}

fun setOnLinkedLaosUpdatedCallback(callback: (MutableMap<String, Array<String>>) -> Unit) {
Expand All @@ -52,8 +59,12 @@ class LinkedOrganizationsRepository @Inject constructor() {
newTokensNotifyFunction = function
}

fun getLinkedLaos(): MutableMap<String, Array<String>> {
return linkedLaos
fun getLinkedLaos(laoId: String): MutableMap<String, Array<String>> {
return if (linkedLaos.containsKey(laoId)) {
linkedLaos[laoId]!!
} else {
mutableMapOf()
}
}

fun flush() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ constructor(
}

fun getLinkedLaosMap(): Map<String, Array<String>> {
return linkedOrgRepo.getLinkedLaos()
return linkedOrgRepo.getLinkedLaos(laoId)
}

fun isRepositoryValid(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,13 @@ constructor(
if (result.isSuccess()) {
if (result.challenge.data == linkedOrgRepo.getChallenge() &&
linkedOrgRepo.otherLaoId != null) {
linkedOrgRepo.addLinkedLao(linkedOrgRepo.otherLaoId!!, arrayOf())
val laoId = context.channel.extractLaoId()
linkedOrgRepo.addLinkedLao(laoId, linkedOrgRepo.otherLaoId!!, arrayOf())
laoRepo.addDisposable(
context.messageSender
.subscribe(Channel.getLaoChannel(linkedOrgRepo.otherLaoId!!))
.subscribe(
{ putRemoteLaoTokensInRepository() },
{ putRemoteLaoTokensInRepository(laoId) },
{ error: Throwable -> Timber.tag(TAG).e(error, "subscription error") }))
} else {
Timber.tag(TAG).d("Invalid FederationResult success")
Expand All @@ -67,7 +68,8 @@ constructor(
@Throws(UnknownLaoException::class)
fun handleTokensExchange(context: HandlerContext, tokenExchange: TokensExchange) {
// Adds the tokens in the repository
linkedOrgRepo.addLinkedLao(tokenExchange.laoId, tokenExchange.tokens)
linkedOrgRepo.addLinkedLao(
context.channel.extractLaoId(), tokenExchange.laoId, tokenExchange.tokens)

// Subscribes to social of the linked organization automatically
// Note that for now the participants of an LAO automatically subscribe to social of the other
Expand All @@ -84,12 +86,12 @@ constructor(
}
}

private fun putRemoteLaoTokensInRepository() {
private fun putRemoteLaoTokensInRepository(myLaoId: String) {
try {
val rollCall = rollCallRepo.getLastClosedRollCall(linkedOrgRepo.otherLaoId!!)
val attendees = rollCall.attendees.map { e -> e.encoded }.toTypedArray()
linkedOrgRepo.updateAndNotifyLinkedLao(
linkedOrgRepo.otherLaoId!!, attendees, rollCall.persistentId)
myLaoId, linkedOrgRepo.otherLaoId!!, attendees, rollCall.persistentId)
} catch (e: NoRollCallException) {
Timber.tag(TAG).d("No RollCall was found on the linked LAO")
}
Expand Down

0 comments on commit 05c3de9

Please sign in to comment.