Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@ package org.monogram.data.service
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import org.koin.android.ext.android.inject
import org.monogram.data.di.TdNotificationManager
import org.monogram.data.gateway.TelegramGateway
import org.monogram.data.push.PushSyncTrigger
import org.monogram.domain.repository.AppPreferencesProvider

class FcmPushService : FirebaseMessagingService() {
private val gateway: TelegramGateway by inject()
private val appPreferences: AppPreferencesProvider by inject()
private val notificationManager: TdNotificationManager by inject()
private val pushSyncTrigger: PushSyncTrigger by inject()
private val delegate by lazy {
BaseFcmPushService(
context = this,
gateway = gateway,
appPreferences = appPreferences
appPreferences = appPreferences,
notificationManager = notificationManager,
pushSyncTrigger = pushSyncTrigger
)
}

Expand Down
9 changes: 8 additions & 1 deletion data/src/main/java/org/monogram/data/chats/ChatCache.kt
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,14 @@ class ChatCache : ChatsCacheDataSource, UserCacheDataSource {
TdApi.ChatPosition(TdApi.ChatListArchive(), order, pinned, null)
}

"f" -> null
"f" -> {
if (parts.size < 4) return@mapNotNull null
val folderId = parts[1].toIntOrNull() ?: return@mapNotNull null
val order = parts[2].toLongOrNull() ?: return@mapNotNull null
if (order == 0L) return@mapNotNull null
val pinned = parts[3] == "1"
TdApi.ChatPosition(TdApi.ChatListFolder(folderId), order, pinned, null)
}

else -> null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class ChatModelFactory(
var personalAvatarPath: String? = null
var signMessages = false
var joinToSendMessages = false
var joinByRequest = false

val isArchived = chat.positions.any { it.list is TdApi.ChatListArchive }

Expand Down Expand Up @@ -143,6 +144,7 @@ class ChatModelFactory(
hasAutomaticTranslation = it.hasAutomaticTranslation
signMessages = it.signMessages
joinToSendMessages = it.joinToSendMessages
joinByRequest = it.joinByRequest
}
?: if (allowRemoteLookups && !cache.isSupergroupTemporarilyMissing(type.supergroupId)) lazyLoad(
cache.pendingSupergroups,
Expand Down Expand Up @@ -383,7 +385,9 @@ class ChatModelFactory(
hasAutomaticTranslation = hasAutomaticTranslation,
personalAvatarPath = personalAvatarPath,
signMessages = signMessages,
joinToSendMessages = joinToSendMessages
joinToSendMessages = joinToSendMessages,
pendingJoinRequestCount = chat.pendingJoinRequests?.totalCount ?: 0,
joinByRequest = joinByRequest
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class ChatPersistenceManager(
) {
private val lastSavedEntities = ConcurrentHashMap<Long, ChatEntity>()
private val pendingSaveJobs = ConcurrentHashMap<Long, Job>()
private val mainChatList = TdApi.ChatListMain()

fun rememberSavedEntity(entity: ChatEntity) {
lastSavedEntities[entity.id] = entity
Expand All @@ -38,7 +37,7 @@ class ChatPersistenceManager(
try {
delay(SINGLE_CHAT_SAVE_DEBOUNCE_MS)
val activeChatList = activeChatListProvider()
val position = resolvePersistPosition(chat, activeChatList)
val position = resolvePersistPosition(chat, activeChatList, listManager)
val model = modelFactory.mapChatToModel(
chat = chat,
order = position?.order ?: 0L,
Expand Down Expand Up @@ -149,7 +148,7 @@ class ChatPersistenceManager(
return chatMapper.mapToEntity(model)
}

val persistPosition = resolvePersistPosition(chat, activeChatList)
val persistPosition = resolvePersistPosition(chat, activeChatList, listManager)
val mapped = chatMapper.mapToEntity(chat, model)
return if (persistPosition != null &&
(persistPosition.order != mapped.order || persistPosition.isPinned != mapped.isPinned)
Expand All @@ -160,16 +159,6 @@ class ChatPersistenceManager(
}
}

private fun resolvePersistPosition(chat: TdApi.Chat, activeChatList: TdApi.ChatList): TdApi.ChatPosition? {
return chat.positions.find { pos ->
pos.order != 0L && listManager.isSameChatList(pos.list, mainChatList)
}
?: chat.positions.find { pos ->
pos.order != 0L && listManager.isSameChatList(pos.list, activeChatList)
}
?: chat.positions.firstOrNull { it.order != 0L }
}

private fun isEntityChanged(old: ChatEntity, new: ChatEntity): Boolean {
return old.withoutCreatedAt() != new.withoutCreatedAt()
}
Expand All @@ -181,4 +170,18 @@ class ChatPersistenceManager(
private const val SINGLE_CHAT_SAVE_DEBOUNCE_MS = 2000L
private const val SNAPSHOT_PERSIST_LIMIT = 1000
}
}
}

internal fun resolvePersistPosition(
chat: TdApi.Chat,
activeChatList: TdApi.ChatList,
listManager: ChatListManager
): TdApi.ChatPosition? {
return chat.positions.find { pos ->
pos.order != 0L && listManager.isSameChatList(pos.list, activeChatList)
}
?: chat.positions.find { pos ->
pos.order != 0L && listManager.isSameChatList(pos.list, TdApi.ChatListMain())
}
?: chat.positions.firstOrNull { it.order != 0L }
}
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ class ChatUpdateHandler(
is TdApi.UpdateFile -> {
if (fileManager.handleFileUpdate(update.file)) {
val chatId = fileManager.getChatIdByPhotoId(update.file.id)
if (chatId != null) {
onSaveChat(chatId)
}
onScheduleUpdate(chatId)
onRefreshForumTopics()
}
Expand Down Expand Up @@ -192,6 +195,7 @@ class ChatUpdateHandler(
cache.putUser(update.user)
val privateChatId = cache.userIdToChatId[update.user.id]
if (privateChatId != null) {
onSaveChat(privateChatId)
onTriggerUpdate(privateChatId)
}
onRefreshForumTopics()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ interface ChatRemoteSource {
suspend fun setChatPermissions(chatId: Long, permissions: ChatPermissionsModel)
suspend fun setChatHasProtectedContent(chatId: Long, hasProtectedContent: Boolean)
suspend fun setChatSignMessages(chatId: Long, signMessages: Boolean)
suspend fun setChatHasHiddenMembers(chatId: Long, hasHiddenMembers: Boolean)
suspend fun setChatHasAggressiveAntiSpamEnabled(chatId: Long, enabled: Boolean)
suspend fun setChatJoinToSendMessages(chatId: Long, joinToSendMessages: Boolean)
suspend fun setChatJoinByRequest(chatId: Long, joinByRequest: Boolean)
suspend fun setChatAvailableReactions(chatId: Long, availableReactions: List<String>)
suspend fun setChatSlowModeDelay(chatId: Long, slowModeDelay: Int)
suspend fun toggleChatIsForum(chatId: Long, isForum: Boolean)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ interface MessageRemoteDataSource : DraftLinkPreviewRemoteDataSource {
photoPath: String,
caption: String,
captionEntities: List<MessageEntity>,
showCaptionAboveMedia: Boolean,
replyToMsgId: Long?,
threadId: Long?,
sendOptions: MessageSendOptions
Expand All @@ -103,6 +104,7 @@ interface MessageRemoteDataSource : DraftLinkPreviewRemoteDataSource {
videoPath: String,
caption: String,
captionEntities: List<MessageEntity>,
showCaptionAboveMedia: Boolean,
replyToMsgId: Long?,
threadId: Long?,
sendOptions: MessageSendOptions
Expand Down Expand Up @@ -147,6 +149,7 @@ interface MessageRemoteDataSource : DraftLinkPreviewRemoteDataSource {
gifPath: String,
caption: String,
captionEntities: List<MessageEntity>,
showCaptionAboveMedia: Boolean,
replyToMsgId: Long?,
threadId: Long?,
sendOptions: MessageSendOptions
Expand All @@ -157,6 +160,7 @@ interface MessageRemoteDataSource : DraftLinkPreviewRemoteDataSource {
paths: List<String>,
caption: String,
captionEntities: List<MessageEntity>,
showCaptionAboveMedia: Boolean,
replyToMsgId: Long?,
threadId: Long?,
sendOptions: MessageSendOptions
Expand Down Expand Up @@ -289,6 +293,7 @@ interface MessageRemoteDataSource : DraftLinkPreviewRemoteDataSource {
limit: Int,
threadId: Long? = null
): List<MessageModel>
suspend fun getChatMessageByDate(chatId: Long, dateEpochSeconds: Int): MessageModel?

suspend fun getRemoteMessagesAround(
chatId: Long,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface SettingsRemoteDataSource {
suspend fun getActiveSessions(): TdApi.Sessions?
suspend fun getInstalledBackgrounds(forDarkTheme: Boolean): TdApi.Backgrounds?
suspend fun getStorageStatistics(chatLimit: Int): TdApi.StorageStatistics?
suspend fun getStorageStatisticsFast(): TdApi.StorageStatisticsFast?
suspend fun getNetworkStatistics(): TdApi.NetworkStatistics?
suspend fun getOption(name: String): TdApi.OptionValue?
suspend fun getChatNotificationSettingsExceptions(
Expand Down Expand Up @@ -44,7 +45,7 @@ interface SettingsRemoteDataSource {
chatIds: LongArray?,
returnDeletedFileStatistics: Boolean,
chatLimit: Int
): Boolean
): TdApi.StorageStatistics?
suspend fun resetNetworkStatistics(): Boolean

// Files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import org.monogram.data.compat.buildSearchPublicChats
import org.monogram.data.compat.buildTdChatPermissions
import org.monogram.data.core.coRunCatching
import org.monogram.data.gateway.TelegramGateway
import org.monogram.domain.repository.TelegramLinkRepository
import org.monogram.domain.models.ChatPermissionsModel
import org.monogram.domain.repository.TelegramLinkRepository

class TdChatRemoteSource(
private val gateway: TelegramGateway,
Expand Down Expand Up @@ -118,6 +118,27 @@ class TdChatRemoteSource(
}
}

override suspend fun setChatHasHiddenMembers(chatId: Long, hasHiddenMembers: Boolean) {
coRunCatching {
val chat = gateway.execute(TdApi.GetChat(chatId))
val supergroupId = (chat.type as? TdApi.ChatTypeSupergroup)?.supergroupId ?: return
gateway.execute(TdApi.ToggleSupergroupHasHiddenMembers(supergroupId, hasHiddenMembers))
}
}

override suspend fun setChatHasAggressiveAntiSpamEnabled(chatId: Long, enabled: Boolean) {
coRunCatching {
val chat = gateway.execute(TdApi.GetChat(chatId))
val supergroupId = (chat.type as? TdApi.ChatTypeSupergroup)?.supergroupId ?: return
gateway.execute(
TdApi.ToggleSupergroupHasAggressiveAntiSpamEnabled(
supergroupId,
enabled
)
)
}
}

override suspend fun setChatJoinToSendMessages(chatId: Long, joinToSendMessages: Boolean) {
coRunCatching {
val chat = gateway.execute(TdApi.GetChat(chatId))
Expand All @@ -131,6 +152,22 @@ class TdChatRemoteSource(
}
}

override suspend fun setChatJoinByRequest(chatId: Long, joinByRequest: Boolean) {
coRunCatching {
val chat = gateway.execute(TdApi.GetChat(chatId))
val supergroupId = (chat.type as? TdApi.ChatTypeSupergroup)?.supergroupId ?: return
val fullInfo = gateway.execute(TdApi.GetSupergroupFullInfo(supergroupId))
gateway.execute(
TdApi.ToggleSupergroupJoinByRequest(
supergroupId,
joinByRequest,
fullInfo.guardBotUserId,
true
)
)
}
}

override suspend fun setChatAvailableReactions(chatId: Long, availableReactions: List<String>) {
coRunCatching {
val chat = gateway.execute(TdApi.GetChat(chatId))
Expand Down
Loading