Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import org.session.libsignal.utilities.HTTP.isConnectedToNetwork
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.AppContext.configureKovenant
import org.thoughtcrime.securesms.debugmenu.DebugActivity
import org.thoughtcrime.securesms.debugmenu.DebugLogger
import org.thoughtcrime.securesms.dependencies.DatabaseComponent
import org.thoughtcrime.securesms.dependencies.DatabaseModule.init
import org.thoughtcrime.securesms.dependencies.OnAppStartupComponents
Expand Down Expand Up @@ -89,6 +90,7 @@ class ApplicationContext : Application(), DefaultLifecycleObserver, Configuratio

@Inject lateinit var startupComponents: Lazy<OnAppStartupComponents>
@Inject lateinit var persistentLogger: Lazy<PersistentLogger>
@Inject lateinit var debugLogger: Lazy<DebugLogger>
@Inject lateinit var textSecurePreferences: Lazy<TextSecurePreferences>
@Inject lateinit var migrationManager: Lazy<DatabaseMigrationManager>

Expand Down Expand Up @@ -208,7 +210,7 @@ class ApplicationContext : Application(), DefaultLifecycleObserver, Configuratio
}

private fun initializeLogging() {
Log.initialize(AndroidLogger(), persistentLogger.get())
Log.initialize(AndroidLogger(), persistentLogger.get(), debugLogger.get())
Logger.addLogger(object : Logger {
private val tag = "LibSession"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.thoughtcrime.securesms.attachments

import android.content.Context
import android.widget.Toast
import androidx.compose.ui.unit.IntSize
import androidx.hilt.work.HiltWorker
import androidx.work.BackoffPolicy
Expand All @@ -17,8 +16,6 @@ import dagger.Lazy
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import network.loki.messenger.BuildConfig
import okhttp3.HttpUrl.Companion.toHttpUrl
import okio.BufferedSource
Expand All @@ -31,8 +28,8 @@ import org.session.libsession.utilities.TextSecurePreferences
import org.session.libsession.utilities.recipients.RemoteFile.Companion.toRemoteFile
import org.session.libsignal.exceptions.NonRetryableException
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.debugmenu.DebugLogGroup
import org.thoughtcrime.securesms.util.BitmapUtil
import org.thoughtcrime.securesms.util.CurrentActivityObserver
import org.thoughtcrime.securesms.util.DateUtils.Companion.secondsToInstant
import org.thoughtcrime.securesms.util.ImageUtils
import java.time.Duration
Expand All @@ -54,23 +51,14 @@ class AvatarReuploadWorker @AssistedInject constructor(
private val configFactory: ConfigFactoryProtocol,
private val avatarUploadManager: Lazy<AvatarUploadManager>,
private val localEncryptedFileInputStreamFactory: LocalEncryptedFileInputStream.Factory,
private val fileServerApi: FileServerApi,
private val prefs: TextSecurePreferences,
private val currentActivityObserver: CurrentActivityObserver,
private val fileServerApi: FileServerApi
) : CoroutineWorker(context, params) {

/**
* Log the given message and show a toast if in debug mode
*/
private suspend inline fun logAndToast(message: String, e: Throwable? = null) {
Log.d(TAG, message, e)

val context = currentActivityObserver.currentActivity.value ?: return
if (prefs.debugAvatarReupload || BuildConfig.DEBUG) {
withContext(Dispatchers.Main) {
Toast.makeText(context, "AvatarReupload[debug only]: $message", Toast.LENGTH_SHORT).show()
}
}
private fun log(message: String, e: Throwable? = null) {
Log.d(DebugLogGroup.AVATAR.label, "Avatar Reupload: $message", e)
}

override suspend fun doWork(): Result {
Expand All @@ -79,13 +67,13 @@ class AvatarReuploadWorker @AssistedInject constructor(
}

if (profile == null) {
logAndToast("No profile picture set; nothing to do.")
log("No profile picture set; nothing to do.")
return Result.success()
}

val localFile = AvatarDownloadManager.computeFileName(context, profile)
if (!localFile.exists()) {
logAndToast("Avatar file is missing locally; nothing to do.")
log("Avatar file is missing locally; nothing to do.")
return Result.success()
}

Expand All @@ -94,7 +82,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
// Check if the file exists and whether we need to do reprocessing, if we do, we reprocess and re-upload
localEncryptedFileInputStreamFactory.create(localFile).use { stream ->
if (stream.meta.hasPermanentDownloadError) {
logAndToast("Permanent download error for current avatar; nothing to do.")
log("Permanent download error for current avatar; nothing to do.")
return Result.success()
}

Expand All @@ -103,7 +91,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
val source = stream.source().buffer()

if ((lastUpdated != null && needsReProcessing(source)) || lastUpdated == null) {
logAndToast("About to start reuploading avatar.")
log("About to start reuploading avatar.")
val attachment = attachmentProcessor.processAvatar(
data = source.use { it.readByteArray() },
) ?: return Result.failure()
Expand All @@ -118,22 +106,22 @@ class AvatarReuploadWorker @AssistedInject constructor(
} catch (e: CancellationException) {
throw e
} catch (e: NonRetryableException) {
logAndToast("Non-retryable error while reuploading avatar.", e)
log("Non-retryable error while reuploading avatar.", e)
return Result.failure()
} catch (e: Exception) {
logAndToast("Error while reuploading avatar.", e)
log("Error while reuploading avatar.", e)
return Result.retry()
}

logAndToast("Successfully reuploaded avatar.")
log("Successfully reuploaded avatar.")
return Result.success()
}
}

// Otherwise, we only need to renew the same avatar on the server
val parsed = fileServerApi.parseAttachmentUrl(profile.url.toHttpUrl())

logAndToast("Renewing user avatar on ${parsed.fileServer}")
log("Renewing user avatar on ${parsed.fileServer}")
try {
fileServerApi.renew(
fileId = parsed.fileId,
Expand All @@ -149,7 +137,7 @@ class AvatarReuploadWorker @AssistedInject constructor(
val now = Instant.now()
if (fileExpiry?.isBefore(now) == true ||
(lastUpdated?.isBefore(now.minus(Duration.ofDays(12)))) == true) {
logAndToast("FileServer renew failed, trying to upload", e)
log("FileServer renew failed, trying to upload", e)
val pictureData =
localEncryptedFileInputStreamFactory.create(localFile).use { stream ->
check(!stream.meta.hasPermanentDownloadError) {
Expand All @@ -166,18 +154,18 @@ class AvatarReuploadWorker @AssistedInject constructor(
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
logAndToast("Error while reuploading avatar after renew failed.", e)
log("Error while reuploading avatar after renew failed.", e)
return Result.failure()
}

logAndToast("Successfully reuploaded avatar after renew failed.")
log("Successfully reuploaded avatar after renew failed.")
} else {
logAndToast( "Not reuploading avatar after renew failed; last updated too recent.")
log( "Not reuploading avatar after renew failed; last updated too recent.")
}

return Result.success()
} else {
logAndToast("Error while renewing avatar. Retrying...", e)
log("Error while renewing avatar. Retrying...", e)
return Result.retry()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import org.session.libsession.utilities.Util
import org.session.libsession.utilities.recipients.RemoteFile
import org.session.libsession.utilities.recipients.RemoteFile.Companion.toRemoteFile
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.debugmenu.DebugLogGroup
import org.thoughtcrime.securesms.dependencies.ManagerScope
import org.thoughtcrime.securesms.dependencies.OnAppStartupComponent
import org.thoughtcrime.securesms.util.castAwayType
Expand All @@ -41,7 +42,7 @@ class AvatarUploadManager @Inject constructor(
@ManagerScope scope: CoroutineScope,
private val localEncryptedFileOutputStreamFactory: LocalEncryptedFileOutputStream.Factory,
private val fileServerApi: FileServerApi,
private val attachmentProcessor: AttachmentProcessor,
private val attachmentProcessor: AttachmentProcessor
) : OnAppStartupComponent {
init {
// Manage scheduling/cancellation of the AvatarReuploadWorker based on login state
Expand Down Expand Up @@ -99,7 +100,7 @@ class AvatarUploadManager @Inject constructor(
customExpiresDuration = DEBUG_AVATAR_TTL.takeIf { prefs.forcedShortTTL() }
)

Log.d(TAG, "Avatar upload finished with $uploadResult")
Log.d(DebugLogGroup.AVATAR.label, "Avatar upload finished with $uploadResult")

val remoteFile = RemoteFile.Encrypted(url = uploadResult.fileUrl, key = Bytes(result.key))

Expand All @@ -111,7 +112,7 @@ class AvatarUploadManager @Inject constructor(
it.write(pictureData)
}

Log.d(TAG, "Avatar file written to local storage")
Log.d(DebugLogGroup.AVATAR.label, "Avatar file written to local storage")

// Now that we have the file both locally and remotely, we can update the user profile
val oldPic = configFactory.withMutableUserConfigs {
Expand All @@ -134,7 +135,7 @@ class AvatarUploadManager @Inject constructor(
// If we had an old avatar, delete it from local storage
val oldFile = AvatarDownloadManager.computeFileName(application, oldPic)
if (oldFile.exists()) {
Log.d(TAG, "Deleting old avatar file: $oldFile")
Log.d(DebugLogGroup.AVATAR.label, "Deleting old avatar file: $oldFile")
oldFile.delete()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class DebugActivity : FullComposeActivity() {

@Composable
override fun ComposeContent() {
DebugMenuScreen(
onClose = { finish() }
DebugMenuNavHost(
onBack = { finish() }
)
}
}
Loading