-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ReceiveMms.kt
96 lines (89 loc) · 4.45 KB
/
ReceiveMms.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/*
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
*
* This file is part of QKSMS.
*
* QKSMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* QKSMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
*/
package com.moez.QKSMS.interactor
import android.net.Uri
import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.extensions.mapNotNull
import com.moez.QKSMS.manager.ActiveConversationManager
import com.moez.QKSMS.manager.NotificationManager
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.repository.MessageRepository
import com.moez.QKSMS.repository.SyncRepository
import com.moez.QKSMS.util.Preferences
import io.reactivex.Flowable
import timber.log.Timber
import javax.inject.Inject
class ReceiveMms @Inject constructor(
private val activeConversationManager: ActiveConversationManager,
private val conversationRepo: ConversationRepository,
private val blockingClient: BlockingClient,
private val prefs: Preferences,
private val syncManager: SyncRepository,
private val messageRepo: MessageRepository,
private val notificationManager: NotificationManager,
private val updateBadge: UpdateBadge
) : Interactor<Uri>() {
override fun buildObservable(params: Uri): Flowable<*> {
return Flowable.just(params)
.mapNotNull(syncManager::syncMessage) // Sync the message
.doOnNext { message ->
// TODO: Ideally this is done when we're saving the MMS to ContentResolver
// This change can be made once we move the MMS storing code to the Data module
if (activeConversationManager.getActiveConversation() == message.threadId) {
messageRepo.markRead(message.threadId)
}
}
.mapNotNull { message ->
// Because we use the smsmms library for receiving and storing MMS, we'll need
// to check if it should be blocked after we've pulled it into realm. If it
// turns out that it should be dropped, then delete it
// TODO Don't store blocked messages in the first place
val action = blockingClient.getAction(message.address).blockingGet()
val shouldDrop = prefs.drop.get()
Timber.v("block=$action, drop=$shouldDrop")
if (action == BlockingClient.Action.BLOCK && shouldDrop) {
messageRepo.deleteMessages(message.id)
return@mapNotNull null
}
when (action) {
BlockingClient.Action.BLOCK -> {
messageRepo.markRead(message.threadId)
conversationRepo.markBlocked(message.threadId)
}
BlockingClient.Action.UNBLOCK -> conversationRepo.markUnblocked(message.threadId)
else -> Unit
}
message
}
.doOnNext { message ->
conversationRepo.updateConversations(message.threadId) // Update the conversation
}
.mapNotNull { message ->
conversationRepo.getOrCreateConversation(message.threadId) // Map message to conversation
}
.filter { conversation -> !conversation.blocked } // Don't notify for blocked conversations
.doOnNext { conversation ->
// Unarchive conversation if necessary
if (conversation.archived) conversationRepo.markUnarchived(conversation.id)
}
.map { conversation -> conversation.id } // Map to the id because [delay] will put us on the wrong thread
.doOnNext(notificationManager::update) // Update the notification
.flatMap { updateBadge.buildObservable(Unit) } // Update the badge
}
}