Skip to content

Commit 183ca18

Browse files
authored
Merge pull request #4401 from vector-im/feature/adm/single-transaction-push
Single `PushTriggerListener` dispatch
2 parents 3760401 + 88fbb8f commit 183ca18

File tree

17 files changed

+551
-304
lines changed

17 files changed

+551
-304
lines changed

changelog.d/4401.removal

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Breaking SDK API change to PushRuleListener, the separated callbacks have been merged into one with a data class which includes all the previously separated push information
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2021 The Matrix.org Foundation C.I.C.
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+
package org.matrix.android.sdk.api.pushrules
17+
18+
import org.matrix.android.sdk.api.pushrules.rest.PushRule
19+
import org.matrix.android.sdk.api.session.events.model.Event
20+
21+
data class PushEvents(
22+
val matchedEvents: List<Pair<Event, PushRule>>,
23+
val roomsJoined: Collection<String>,
24+
val roomsLeft: Collection<String>,
25+
val redactedEventIds: List<String>
26+
)

matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/pushrules/PushRuleService.kt

+1-5
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,7 @@ interface PushRuleService {
5151
// fun fulfilledBingRule(event: Event, rules: List<PushRule>): PushRule?
5252

5353
interface PushRuleListener {
54-
fun onMatchRule(event: Event, actions: List<Action>)
55-
fun onRoomJoined(roomId: String)
56-
fun onRoomLeft(roomId: String)
57-
fun onEventRedacted(redactedEventId: String)
58-
fun batchFinish()
54+
fun onEvents(pushEvents: PushEvents)
5955
}
6056

6157
fun getKeywords(): LiveData<Set<String>>

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/notification/DefaultPushRuleService.kt

+13-73
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import androidx.lifecycle.LiveData
1919
import androidx.lifecycle.Transformations
2020
import com.zhuinden.monarchy.Monarchy
2121
import org.matrix.android.sdk.api.pushrules.Action
22+
import org.matrix.android.sdk.api.pushrules.PushEvents
2223
import org.matrix.android.sdk.api.pushrules.PushRuleService
2324
import org.matrix.android.sdk.api.pushrules.RuleKind
2425
import org.matrix.android.sdk.api.pushrules.RuleScope
@@ -142,79 +143,6 @@ internal class DefaultPushRuleService @Inject constructor(
142143
return pushRuleFinder.fulfilledBingRule(event, rules)?.getActions().orEmpty()
143144
}
144145

145-
// fun processEvents(events: List<Event>) {
146-
// var hasDoneSomething = false
147-
// events.forEach { event ->
148-
// fulfilledBingRule(event)?.let {
149-
// hasDoneSomething = true
150-
// dispatchBing(event, it)
151-
// }
152-
// }
153-
// if (hasDoneSomething)
154-
// dispatchFinish()
155-
// }
156-
157-
fun dispatchBing(event: Event, rule: PushRule) {
158-
synchronized(listeners) {
159-
val actionsList = rule.getActions()
160-
listeners.forEach {
161-
try {
162-
it.onMatchRule(event, actionsList)
163-
} catch (e: Throwable) {
164-
Timber.e(e, "Error while dispatching bing")
165-
}
166-
}
167-
}
168-
}
169-
170-
fun dispatchRoomJoined(roomId: String) {
171-
synchronized(listeners) {
172-
listeners.forEach {
173-
try {
174-
it.onRoomJoined(roomId)
175-
} catch (e: Throwable) {
176-
Timber.e(e, "Error while dispatching room joined")
177-
}
178-
}
179-
}
180-
}
181-
182-
fun dispatchRoomLeft(roomId: String) {
183-
synchronized(listeners) {
184-
listeners.forEach {
185-
try {
186-
it.onRoomLeft(roomId)
187-
} catch (e: Throwable) {
188-
Timber.e(e, "Error while dispatching room left")
189-
}
190-
}
191-
}
192-
}
193-
194-
fun dispatchRedactedEventId(redactedEventId: String) {
195-
synchronized(listeners) {
196-
listeners.forEach {
197-
try {
198-
it.onEventRedacted(redactedEventId)
199-
} catch (e: Throwable) {
200-
Timber.e(e, "Error while dispatching redacted event")
201-
}
202-
}
203-
}
204-
}
205-
206-
fun dispatchFinish() {
207-
synchronized(listeners) {
208-
listeners.forEach {
209-
try {
210-
it.batchFinish()
211-
} catch (e: Throwable) {
212-
Timber.e(e, "Error while dispatching finish")
213-
}
214-
}
215-
}
216-
}
217-
218146
override fun getKeywords(): LiveData<Set<String>> {
219147
// Keywords are all content rules that don't start with '.'
220148
val liveData = monarchy.findAllMappedWithChanges(
@@ -229,4 +157,16 @@ internal class DefaultPushRuleService @Inject constructor(
229157
results.firstOrNull().orEmpty().toSet()
230158
}
231159
}
160+
161+
fun dispatchEvents(pushEvents: PushEvents) {
162+
synchronized(listeners) {
163+
listeners.forEach {
164+
try {
165+
it.onEvents(pushEvents)
166+
} catch (e: Throwable) {
167+
Timber.e(e, "Error while dispatching push events")
168+
}
169+
}
170+
}
171+
}
232172
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/notification/ProcessEventForPushTask.kt

+11-15
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.matrix.android.sdk.internal.session.notification
1818

19+
import org.matrix.android.sdk.api.pushrules.PushEvents
1920
import org.matrix.android.sdk.api.pushrules.rest.PushRule
2021
import org.matrix.android.sdk.api.session.events.model.EventType
2122
import org.matrix.android.sdk.api.session.events.model.isInvitation
@@ -39,14 +40,6 @@ internal class DefaultProcessEventForPushTask @Inject constructor(
3940
) : ProcessEventForPushTask {
4041

4142
override suspend fun execute(params: ProcessEventForPushTask.Params) {
42-
// Handle left rooms
43-
params.syncResponse.leave.keys.forEach {
44-
defaultPushRuleService.dispatchRoomLeft(it)
45-
}
46-
// Handle joined rooms
47-
params.syncResponse.join.keys.forEach {
48-
defaultPushRuleService.dispatchRoomJoined(it)
49-
}
5043
val newJoinEvents = params.syncResponse.join
5144
.mapNotNull { (key, value) ->
5245
value.timeline?.events?.mapNotNull {
@@ -74,10 +67,10 @@ internal class DefaultProcessEventForPushTask @Inject constructor(
7467
}
7568
Timber.v("[PushRules] Found ${allEvents.size} out of ${(newJoinEvents + inviteEvents).size}" +
7669
" to check for push rules with ${params.rules.size} rules")
77-
allEvents.forEach { event ->
70+
val matchedEvents = allEvents.mapNotNull { event ->
7871
pushRuleFinder.fulfilledBingRule(event, params.rules)?.let {
7972
Timber.v("[PushRules] Rule $it match for event ${event.eventId}")
80-
defaultPushRuleService.dispatchBing(event, it)
73+
event to it
8174
}
8275
}
8376

@@ -91,10 +84,13 @@ internal class DefaultProcessEventForPushTask @Inject constructor(
9184

9285
Timber.v("[PushRules] Found ${allRedactedEvents.size} redacted events")
9386

94-
allRedactedEvents.forEach { redactedEventId ->
95-
defaultPushRuleService.dispatchRedactedEventId(redactedEventId)
96-
}
97-
98-
defaultPushRuleService.dispatchFinish()
87+
defaultPushRuleService.dispatchEvents(
88+
PushEvents(
89+
matchedEvents = matchedEvents,
90+
roomsJoined = params.syncResponse.join.keys,
91+
roomsLeft = params.syncResponse.leave.keys,
92+
redactedEventIds = allRedactedEvents
93+
)
94+
)
9995
}
10096
}

vector/src/gplay/java/im/vector/app/gplay/push/fcm/VectorFirebaseMessagingService.kt

+1-2
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ class VectorFirebaseMessagingService : FirebaseMessagingService() {
201201
resolvedEvent
202202
?.also { Timber.tag(loggerTag.value).d("Fast lane: notify drawer") }
203203
?.let {
204-
notificationDrawerManager.onNotifiableEventReceived(it)
205-
notificationDrawerManager.refreshNotificationDrawer()
204+
notificationDrawerManager.updateEvents { it.onNotifiableEventReceived(resolvedEvent) }
206205
}
207206
}
208207
}

vector/src/main/java/im/vector/app/features/home/room/detail/RoomDetailFragment.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -2099,12 +2099,12 @@ class RoomDetailFragment @Inject constructor(
20992099
// VectorInviteView.Callback
21002100

21012101
override fun onAcceptInvite() {
2102-
notificationDrawerManager.clearMemberShipNotificationForRoom(roomDetailArgs.roomId)
2102+
notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(roomDetailArgs.roomId) }
21032103
roomDetailViewModel.handle(RoomDetailAction.AcceptInvite)
21042104
}
21052105

21062106
override fun onRejectInvite() {
2107-
notificationDrawerManager.clearMemberShipNotificationForRoom(roomDetailArgs.roomId)
2107+
notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(roomDetailArgs.roomId) }
21082108
roomDetailViewModel.handle(RoomDetailAction.RejectInvite)
21092109
}
21102110

vector/src/main/java/im/vector/app/features/home/room/list/RoomListFragment.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ class RoomListFragment @Inject constructor(
479479
}
480480

481481
override fun onAcceptRoomInvitation(room: RoomSummary) {
482-
notificationDrawerManager.clearMemberShipNotificationForRoom(room.roomId)
482+
notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(room.roomId) }
483483
roomListViewModel.handle(RoomListAction.AcceptInvitation(room))
484484
}
485485

@@ -492,7 +492,7 @@ class RoomListFragment @Inject constructor(
492492
}
493493

494494
override fun onRejectRoomInvitation(room: RoomSummary) {
495-
notificationDrawerManager.clearMemberShipNotificationForRoom(room.roomId)
495+
notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(room.roomId) }
496496
roomListViewModel.handle(RoomListAction.RejectInvitation(room))
497497
}
498498
}

vector/src/main/java/im/vector/app/features/notifications/NotificationBroadcastReceiver.kt

+12-13
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,26 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
4949
NotificationUtils.SMART_REPLY_ACTION ->
5050
handleSmartReply(intent, context)
5151
NotificationUtils.DISMISS_ROOM_NOTIF_ACTION ->
52-
intent.getStringExtra(KEY_ROOM_ID)?.let {
53-
notificationDrawerManager.clearMessageEventOfRoom(it)
52+
intent.getStringExtra(KEY_ROOM_ID)?.let { roomId ->
53+
notificationDrawerManager.updateEvents { it.clearMessagesForRoom(roomId) }
5454
}
5555
NotificationUtils.DISMISS_SUMMARY_ACTION ->
5656
notificationDrawerManager.clearAllEvents()
5757
NotificationUtils.MARK_ROOM_READ_ACTION ->
58-
intent.getStringExtra(KEY_ROOM_ID)?.let {
59-
notificationDrawerManager.clearMessageEventOfRoom(it)
60-
handleMarkAsRead(it)
58+
intent.getStringExtra(KEY_ROOM_ID)?.let { roomId ->
59+
notificationDrawerManager.updateEvents { it.clearMessagesForRoom(roomId) }
60+
handleMarkAsRead(roomId)
6161
}
6262
NotificationUtils.JOIN_ACTION -> {
63-
intent.getStringExtra(KEY_ROOM_ID)?.let {
64-
notificationDrawerManager.clearMemberShipNotificationForRoom(it)
65-
handleJoinRoom(it)
63+
intent.getStringExtra(KEY_ROOM_ID)?.let { roomId ->
64+
notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(roomId) }
65+
handleJoinRoom(roomId)
6666
}
6767
}
6868
NotificationUtils.REJECT_ACTION -> {
69-
intent.getStringExtra(KEY_ROOM_ID)?.let {
70-
notificationDrawerManager.clearMemberShipNotificationForRoom(it)
71-
handleRejectRoom(it)
69+
intent.getStringExtra(KEY_ROOM_ID)?.let { roomId ->
70+
notificationDrawerManager.updateEvents { it.clearMemberShipNotificationForRoom(roomId) }
71+
handleRejectRoom(roomId)
7272
}
7373
}
7474
}
@@ -145,8 +145,7 @@ class NotificationBroadcastReceiver : BroadcastReceiver() {
145145
canBeReplaced = false
146146
)
147147

148-
notificationDrawerManager.onNotifiableEventReceived(notifiableMessageEvent)
149-
notificationDrawerManager.refreshNotificationDrawer()
148+
notificationDrawerManager.updateEvents { it.onNotifiableEventReceived(notifiableMessageEvent) }
150149

151150
/*
152151
// TODO Error cannot be managed the same way than in Riot

0 commit comments

Comments
 (0)