add util for permissions

util uses bitwise operations to access every single permission via variable

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2022-05-10 14:11:39 +02:00
parent 99e9bcb0e8
commit d9d6af2b99
No known key found for this signature in database
GPG key ID: C793F8B59F43CE7B
2 changed files with 69 additions and 0 deletions

View file

@ -0,0 +1,42 @@
package com.nextcloud.talk.utils
/**
* see https://nextcloud-talk.readthedocs.io/en/latest/constants/#attendee-permissions
*/
class AttendeePermissionsUtil(flag: Int) {
var isDefault: Boolean = false
var isCustom: Boolean = false
var canStartCall: Boolean = false
var canJoinCall: Boolean = false
var canIgnoreLobby: Boolean = false
var canPublishAudio: Boolean = false
var canPublishVideo: Boolean = false
var canPublishScreen: Boolean = false
var canPostChatShareItemsDoReaction: Boolean = false
init {
isDefault = (flag and DEFAULT) == DEFAULT
isCustom = (flag and CUSTOM) == CUSTOM
canStartCall = (flag and START_CALL) == START_CALL
canJoinCall = (flag and JOIN_CALL) == JOIN_CALL
canIgnoreLobby = (flag and CAN_IGNORE_LOBBY) == CAN_IGNORE_LOBBY
canPublishAudio = (flag and PUBLISH_AUDIO) == PUBLISH_AUDIO
canPublishVideo = (flag and PUBLISH_VIDEO) == PUBLISH_VIDEO
canPublishScreen = (flag and PUBLISH_SCREEN) == PUBLISH_SCREEN
canPostChatShareItemsDoReaction =
(flag and POST_CHAT_SHARE_ITEMS_DO_REACTIONS) == POST_CHAT_SHARE_ITEMS_DO_REACTIONS
}
companion object {
const val DEFAULT = 0
const val CUSTOM = 1
const val START_CALL = 2
const val JOIN_CALL = 4
const val CAN_IGNORE_LOBBY = 8
const val PUBLISH_AUDIO = 16
const val PUBLISH_VIDEO = 32
const val PUBLISH_SCREEN = 64
const val POST_CHAT_SHARE_ITEMS_DO_REACTIONS = 128
}
}

View file

@ -0,0 +1,27 @@
package com.nextcloud.talk.utils
import junit.framework.TestCase
import org.junit.Test
class AttendeePermissionsUtilTest : TestCase() {
@Test
fun test_areFlagsSet() {
val attendeePermissionsUtil = AttendeePermissionsUtil(
AttendeePermissionsUtil.PUBLISH_SCREEN
or AttendeePermissionsUtil.JOIN_CALL
or AttendeePermissionsUtil.DEFAULT)
assert(attendeePermissionsUtil.canPublishScreen)
assert(attendeePermissionsUtil.canJoinCall)
assert(attendeePermissionsUtil.isDefault) // if AttendeePermissionsUtil.DEFAULT is not set with setFlags and
// checked with assertFalse, the logic fails somehow?!
assertFalse(attendeePermissionsUtil.isCustom)
assertFalse(attendeePermissionsUtil.canStartCall)
assertFalse(attendeePermissionsUtil.canIgnoreLobby)
assertFalse(attendeePermissionsUtil.canPublishAudio)
assertFalse(attendeePermissionsUtil.canPublishVideo)
assertFalse(attendeePermissionsUtil.canPostChatShareItemsDoReaction)
}
}