@ -5,7 +5,7 @@ import org.session.libsession.messaging.open_groups.OpenGroupApi
import org.session.libsession.messaging.utilities.Data
import org.session.libsession.utilities.GroupUtil
class GroupAvatarDownloadJob ( val room: String , val server: String ) : Job {
class GroupAvatarDownloadJob ( val server: String , val room: String , val imageId: String ? ) : Job {
override var delegate : JobDelegate ? = null
override var id : String ? = null
@ -13,10 +13,30 @@ class GroupAvatarDownloadJob(val room: String, val server: String) : Job {
override val maxFailureCount : Int = 10
override fun execute ( dispatcherName : String ) {
if ( imageId == null ) {
delegate ?. handleJobFailedPermanently ( this , dispatcherName , Exception ( " GroupAvatarDownloadJob now requires imageId " ) )
return
}
val storage = MessagingModuleConfiguration . shared . storage
val imageId = storage . getOpenGroup ( room , server ) ?. imageId ?: return
val storedImageId = storage . getOpenGroup ( room , server ) ?. imageId
if ( storedImageId == null || storedImageId != imageId ) {
delegate ?. handleJobFailedPermanently ( this , dispatcherName , Exception ( " GroupAvatarDownloadJob imageId does not match the OpenGroup " ) )
return
}
try {
val bytes = OpenGroupApi . downloadOpenGroupProfilePicture ( server , room , imageId ) . get ( )
// Once the download is complete the imageId might no longer match, so we need to fetch it again just in case
val postDownloadStoredImageId = storage . getOpenGroup ( room , server ) ?. imageId
if ( postDownloadStoredImageId == null || postDownloadStoredImageId != imageId ) {
delegate ?. handleJobFailedPermanently ( this , dispatcherName , Exception ( " GroupAvatarDownloadJob imageId no longer matches the OpenGroup " ) )
return
}
val groupId = GroupUtil . getEncodedOpenGroupID ( " $server . $room " . toByteArray ( ) )
storage . updateProfilePicture ( groupId , bytes )
storage . updateTimestampUpdated ( groupId , System . currentTimeMillis ( ) )
@ -30,6 +50,7 @@ class GroupAvatarDownloadJob(val room: String, val server: String) : Job {
return Data . Builder ( )
. putString ( ROOM , room )
. putString ( SERVER , server )
. putString ( IMAGE _ID , imageId )
. build ( )
}
@ -40,6 +61,7 @@ class GroupAvatarDownloadJob(val room: String, val server: String) : Job {
private const val ROOM = " room "
private const val SERVER = " server "
private const val IMAGE _ID = " imageId "
}
class Factory : Job . Factory < GroupAvatarDownloadJob > {
@ -47,7 +69,8 @@ class GroupAvatarDownloadJob(val room: String, val server: String) : Job {
override fun create ( data : Data ) : GroupAvatarDownloadJob {
return GroupAvatarDownloadJob (
data . getString ( ROOM ) ,
data . getString ( SERVER )
data . getString ( SERVER ) ,
if ( data . hasString ( IMAGE _ID ) ) { data . getString ( IMAGE _ID ) } else { null }
)
}
}