mirror of https://github.com/tasks/tasks
Add Invite property
parent
c42b22ebe9
commit
24437cb6ce
@ -0,0 +1,40 @@
|
||||
package org.tasks.caldav.property
|
||||
|
||||
import at.bitfire.dav4jvm.Property
|
||||
import at.bitfire.dav4jvm.PropertyFactory
|
||||
import at.bitfire.dav4jvm.XmlUtils
|
||||
import at.bitfire.dav4jvm.XmlUtils.propertyName
|
||||
import org.tasks.BuildConfig
|
||||
import org.xmlpull.v1.XmlPullParser
|
||||
|
||||
data class Invite(val sharees: List<Sharee>): Property {
|
||||
|
||||
companion object {
|
||||
@JvmField
|
||||
val NAME = Property.Name(XmlUtils.NS_WEBDAV, "invite")
|
||||
|
||||
val SHAREE = Property.Name(XmlUtils.NS_WEBDAV, "sharee")
|
||||
}
|
||||
|
||||
|
||||
class Factory : PropertyFactory {
|
||||
|
||||
override fun getName() = NAME
|
||||
|
||||
override fun create(parser: XmlPullParser): Invite {
|
||||
val depth = parser.depth
|
||||
var eventType = parser.eventType
|
||||
val sharees = ArrayList<Sharee>()
|
||||
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
|
||||
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1) {
|
||||
if (parser.propertyName() == SHAREE) {
|
||||
sharees.add(Sharee(parser))
|
||||
}
|
||||
}
|
||||
eventType = parser.next()
|
||||
}
|
||||
if (BuildConfig.DEBUG && parser.depth != depth) { error("Assertion failed") }
|
||||
return Invite(sharees)
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
package org.tasks.caldav.property
|
||||
|
||||
import at.bitfire.dav4jvm.DavResource
|
||||
import at.bitfire.dav4jvm.Property
|
||||
import at.bitfire.dav4jvm.PropertyRegistry
|
||||
import at.bitfire.dav4jvm.XmlUtils
|
||||
import at.bitfire.dav4jvm.XmlUtils.propertyName
|
||||
import org.tasks.BuildConfig
|
||||
import org.xmlpull.v1.XmlPullParser
|
||||
|
||||
class Sharee(parser: XmlPullParser) {
|
||||
lateinit var href: String
|
||||
private set
|
||||
lateinit var access: ShareAccess
|
||||
private set
|
||||
lateinit var response: Property.Name
|
||||
private set
|
||||
var comment: String? = null
|
||||
private set
|
||||
val properties = ArrayList<Property>()
|
||||
|
||||
init {
|
||||
val depth = parser.depth
|
||||
var eventType = parser.eventType
|
||||
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
|
||||
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1) {
|
||||
when (val name = parser.propertyName()) {
|
||||
DavResource.HREF ->
|
||||
XmlUtils.readText(parser)?.let { href = it }
|
||||
ShareAccess.NAME ->
|
||||
access = PropertyRegistry.create(ShareAccess.NAME, parser) as ShareAccess
|
||||
COMMENT ->
|
||||
comment = XmlUtils.readText(parser)
|
||||
INVITE_ACCEPTED, INVITE_DECLINED, INVITE_NORESPONSE, INVITE_INVALID ->
|
||||
response = name
|
||||
DavResource.PROP ->
|
||||
properties.addAll(Property.parse(parser))
|
||||
}
|
||||
}
|
||||
eventType = parser.next()
|
||||
}
|
||||
if (BuildConfig.DEBUG && parser.depth != depth) { error("Assertion failed") }
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "Sharee(href='$href', access=$access, response=$response, comment=$comment, properties=$properties)"
|
||||
}
|
||||
|
||||
companion object {
|
||||
val COMMENT = Property.Name(XmlUtils.NS_WEBDAV, "comment")
|
||||
val INVITE_ACCEPTED = Property.Name(XmlUtils.NS_WEBDAV, "invite-accepted")
|
||||
val INVITE_DECLINED = Property.Name(XmlUtils.NS_WEBDAV, "invite-declined")
|
||||
val INVITE_NORESPONSE = Property.Name(XmlUtils.NS_WEBDAV, "invite-noresponse")
|
||||
val INVITE_INVALID = Property.Name(XmlUtils.NS_WEBDAV, "invite-invalid")
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,62 @@
|
||||
package org.tasks.caldav.property
|
||||
|
||||
import at.bitfire.dav4jvm.PropertyRegistry
|
||||
import org.junit.Assert.*
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.tasks.caldav.property.PropertyUtils.toProperty
|
||||
import org.tasks.caldav.property.ShareAccess.Companion.SHARED_OWNER
|
||||
|
||||
class InviteTest {
|
||||
@Before
|
||||
fun setUp() {
|
||||
PropertyRegistry.register(listOf(
|
||||
ShareAccess.Factory(),
|
||||
Invite.Factory()
|
||||
))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun emptyInvite() {
|
||||
val invite: Invite = "<d:invite />".toProperty()
|
||||
|
||||
assertTrue(invite.sharees.isEmpty())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shareeAccess() {
|
||||
assertEquals(ShareAccess(SHARED_OWNER), sharee(SHARE_OWNER).access)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun shareeHref() {
|
||||
assertEquals("/principals/102967489186752069531", sharee(SHARE_OWNER).href)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun inviteStatus() {
|
||||
assertEquals(Sharee.INVITE_ACCEPTED, sharee(SHARE_OWNER).response)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun noComment() {
|
||||
assertNull(sharee(SHARE_OWNER).comment)
|
||||
}
|
||||
|
||||
private fun sharee(xml: String): Sharee = xml.toProperty<Invite>().sharees.first()
|
||||
|
||||
companion object {
|
||||
val SHARE_OWNER = """
|
||||
<d:invite>
|
||||
<d:sharee>
|
||||
<d:href>/principals/102967489186752069531</d:href>
|
||||
<d:prop />
|
||||
<d:share-access>
|
||||
<d:shared-owner />
|
||||
</d:share-access>
|
||||
<d:invite-accepted />
|
||||
</d:sharee>
|
||||
</d:invite>
|
||||
""".trimIndent()
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue