mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2025-03-16 03:08:58 +03:00
PreviewUrl: extract url from Content
This commit is contained in:
parent
dd150c6d7e
commit
be20f9b455
3 changed files with 146 additions and 2 deletions
|
@ -0,0 +1,91 @@
|
|||
/*
|
||||
* Copyright 2020 The Matrix.org Foundation C.I.C.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.internal.session.media
|
||||
|
||||
import androidx.test.ext.junit.runners.AndroidJUnit4
|
||||
import org.amshove.kluent.shouldBeEqualTo
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.matrix.android.sdk.InstrumentedTest
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.events.model.toContent
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageTextContent
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageType
|
||||
|
||||
@RunWith(AndroidJUnit4::class)
|
||||
internal class UrlsExtractorTest : InstrumentedTest {
|
||||
|
||||
private val urlsExtractor = UrlsExtractor()
|
||||
|
||||
@Test
|
||||
fun wrongEventTypeTest() {
|
||||
createEvent(body = "https://matrix.org")
|
||||
.copy(type = EventType.STATE_ROOM_GUEST_ACCESS)
|
||||
.let { urlsExtractor.extract(it) }
|
||||
.size shouldBeEqualTo 0
|
||||
}
|
||||
|
||||
@Test
|
||||
fun oneUrlTest() {
|
||||
createEvent(body = "https://matrix.org")
|
||||
.let { urlsExtractor.extract(it) }
|
||||
.let { result ->
|
||||
result.size shouldBeEqualTo 1
|
||||
result[0] shouldBeEqualTo "https://matrix.org"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun oneUrlWithParamTest() {
|
||||
createEvent(body = "https://matrix.org?foo=bar")
|
||||
.let { urlsExtractor.extract(it) }
|
||||
.let { result ->
|
||||
result.size shouldBeEqualTo 1
|
||||
result[0] shouldBeEqualTo "https://matrix.org?foo=bar"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun oneUrlInlinedTest() {
|
||||
createEvent(body = "Hello https://matrix.org, how are you?")
|
||||
.let { urlsExtractor.extract(it) }
|
||||
.let { result ->
|
||||
result.size shouldBeEqualTo 1
|
||||
result[0] shouldBeEqualTo "https://matrix.org"
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun twoUrlsTest() {
|
||||
createEvent(body = "https://matrix.org https://example.org")
|
||||
.let { urlsExtractor.extract(it) }
|
||||
.let { result ->
|
||||
result.size shouldBeEqualTo 2
|
||||
result[0] shouldBeEqualTo "https://matrix.org"
|
||||
result[1] shouldBeEqualTo "https://example.org"
|
||||
}
|
||||
}
|
||||
|
||||
private fun createEvent(body: String): Event = Event(
|
||||
type = EventType.MESSAGE,
|
||||
content = MessageTextContent(
|
||||
msgType = MessageType.MSGTYPE_TEXT,
|
||||
body = body
|
||||
).toContent()
|
||||
)
|
||||
}
|
|
@ -16,6 +16,7 @@
|
|||
|
||||
package org.matrix.android.sdk.internal.session.media
|
||||
|
||||
import androidx.collection.LruCache
|
||||
import org.matrix.android.sdk.api.cache.CacheStrategy
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.api.session.media.MediaService
|
||||
|
@ -26,12 +27,23 @@ import javax.inject.Inject
|
|||
internal class DefaultMediaService @Inject constructor(
|
||||
private val clearPreviewUrlCacheTask: ClearPreviewUrlCacheTask,
|
||||
private val getPreviewUrlTask: GetPreviewUrlTask,
|
||||
private val getRawPreviewUrlTask: GetRawPreviewUrlTask
|
||||
private val getRawPreviewUrlTask: GetRawPreviewUrlTask,
|
||||
private val urlsExtractor: UrlsExtractor
|
||||
) : MediaService {
|
||||
// Cache of extracted URLs
|
||||
private val extractedUrlsCache = LruCache<String, List<String>>(1_000)
|
||||
|
||||
override fun extractUrls(event: Event): List<String> {
|
||||
TODO("Not yet implemented")
|
||||
val cacheKey = event.cacheKey()
|
||||
return extractedUrlsCache.get(cacheKey)
|
||||
?: let {
|
||||
urlsExtractor.extract(event)
|
||||
.also { extractedUrlsCache.put(cacheKey, it) }
|
||||
}
|
||||
}
|
||||
|
||||
private fun Event.cacheKey() = "${eventId ?: ""}-${roomId ?: ""}"
|
||||
|
||||
override suspend fun getRawPreviewUrl(url: String, timestamp: Long?): JsonDict {
|
||||
return getRawPreviewUrlTask.execute(GetRawPreviewUrlTask.Params(url, timestamp))
|
||||
}
|
||||
|
@ -41,6 +53,7 @@ internal class DefaultMediaService @Inject constructor(
|
|||
}
|
||||
|
||||
override suspend fun clearCache() {
|
||||
extractedUrlsCache.evictAll()
|
||||
clearPreviewUrlCacheTask.execute(Unit)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
/*
|
||||
* Copyright (c) 2020 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.matrix.android.sdk.internal.session.media
|
||||
|
||||
import android.util.Patterns
|
||||
import org.matrix.android.sdk.api.session.events.model.Event
|
||||
import org.matrix.android.sdk.api.session.events.model.EventType
|
||||
import org.matrix.android.sdk.api.session.events.model.toModel
|
||||
import org.matrix.android.sdk.api.session.room.model.message.MessageContent
|
||||
import javax.inject.Inject
|
||||
|
||||
internal class UrlsExtractor @Inject constructor() {
|
||||
private val urlRegex = Patterns.WEB_URL.toRegex()
|
||||
|
||||
fun extract(event: Event): List<String> {
|
||||
return event.takeIf { it.getClearType() == EventType.MESSAGE }
|
||||
?.getClearContent()
|
||||
?.toModel<MessageContent>()
|
||||
?.body
|
||||
?.let { urlRegex.findAll(it) }
|
||||
?.map { it.value }
|
||||
?.distinct()
|
||||
?.toList()
|
||||
.orEmpty()
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue