|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 New Vector Ltd |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.element.android.libraries.matrix.impl.timeline |
| 18 | + |
| 19 | +import org.matrix.rustcomponents.sdk.EventItemOrigin |
| 20 | +import org.matrix.rustcomponents.sdk.TimelineChange |
| 21 | +import org.matrix.rustcomponents.sdk.TimelineDiff |
| 22 | +import org.matrix.rustcomponents.sdk.TimelineItem |
| 23 | + |
| 24 | +/** |
| 25 | + * Tries to get an event origin from the TimelineDiff. |
| 26 | + * If there is multiple events in the diff, uses the first one as it should be a good indicator. |
| 27 | + */ |
| 28 | +internal fun TimelineDiff.eventOrigin(): EventItemOrigin? { |
| 29 | + return when (change()) { |
| 30 | + TimelineChange.APPEND -> { |
| 31 | + append()?.first()?.eventOrigin() |
| 32 | + } |
| 33 | + TimelineChange.PUSH_BACK -> { |
| 34 | + pushBack()?.eventOrigin() |
| 35 | + } |
| 36 | + TimelineChange.PUSH_FRONT -> { |
| 37 | + pushFront()?.eventOrigin() |
| 38 | + } |
| 39 | + TimelineChange.SET -> { |
| 40 | + set()?.item?.eventOrigin() |
| 41 | + } |
| 42 | + TimelineChange.INSERT -> { |
| 43 | + insert()?.item?.eventOrigin() |
| 44 | + } |
| 45 | + TimelineChange.RESET -> { |
| 46 | + reset()?.first()?.eventOrigin() |
| 47 | + } |
| 48 | + else -> null |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +private fun TimelineItem.eventOrigin(): EventItemOrigin? { |
| 53 | + return asEvent()?.origin() |
| 54 | +} |
0 commit comments