feat: Update open group avatars periodically (#807)

* feat: Update open group avatars periodically

* Updated timestamp

* Existing job check

* Refresh avatar on the conversation

* Remove println statement

* Update profile picture on recipient modified event
This commit is contained in:
ceokot
2021-12-15 08:11:55 +02:00
committed by GitHub
parent 5601da0e22
commit 44f5684b21
12 changed files with 135 additions and 27 deletions

View File

@@ -501,6 +501,7 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
}
updateSubtitle()
showOrHideInputIfNeeded()
profilePictureView.update(recipient, threadID)
}
}

View File

@@ -53,6 +53,7 @@ public class GroupDatabase extends Database implements LokiOpenGroupDatabaseProt
private static final String TIMESTAMP = "timestamp";
private static final String ACTIVE = "active";
private static final String MMS = "mms";
private static final String UPDATED = "updated";
// Loki
private static final String AVATAR_URL = "avatar_url";
@@ -83,11 +84,16 @@ public class GroupDatabase extends Database implements LokiOpenGroupDatabaseProt
private static final String[] GROUP_PROJECTION = {
GROUP_ID, TITLE, MEMBERS, ZOMBIE_MEMBERS, AVATAR, AVATAR_ID, AVATAR_KEY, AVATAR_CONTENT_TYPE, AVATAR_RELAY, AVATAR_DIGEST,
TIMESTAMP, ACTIVE, MMS, AVATAR_URL, ADMINS
TIMESTAMP, ACTIVE, MMS, AVATAR_URL, ADMINS, UPDATED
};
static final List<String> TYPED_GROUP_PROJECTION = Stream.of(GROUP_PROJECTION).map(columnName -> TABLE_NAME + "." + columnName).toList();
public static String getCreateUpdatedTimestampCommand() {
return "ALTER TABLE "+ TABLE_NAME + " " +
"ADD COLUMN " + UPDATED + " INTEGER DEFAULT 0;";
}
public GroupDatabase(Context context, SQLCipherOpenHelper databaseHelper) {
super(context, databaseHelper);
}
@@ -330,6 +336,13 @@ public class GroupDatabase extends Database implements LokiOpenGroupDatabaseProt
databaseHelper.getWritableDatabase().update(TABLE_NAME, contents, GROUP_ID + " = ?", new String[] {groupId});
}
public void updateTimestampUpdated(String groupId, Long updatedTimestamp) {
ContentValues contents = new ContentValues();
contents.put(UPDATED, updatedTimestamp);
databaseHelper.getWritableDatabase().update(TABLE_NAME, contents, GROUP_ID + " = ?", new String[] {groupId});
}
public void removeMember(String groupId, Address source) {
List<Address> currentMembers = getCurrentMembers(groupId, false);
currentMembers.remove(source);
@@ -439,7 +452,8 @@ public class GroupDatabase extends Database implements LokiOpenGroupDatabaseProt
cursor.getInt(cursor.getColumnIndexOrThrow(MMS)) == 1,
cursor.getString(cursor.getColumnIndexOrThrow(AVATAR_URL)),
cursor.getString(cursor.getColumnIndexOrThrow(ADMINS)),
cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP)));
cursor.getLong(cursor.getColumnIndexOrThrow(TIMESTAMP)),
cursor.getLong(cursor.getColumnIndexOrThrow(UPDATED)));
}
@Override

View File

@@ -3,13 +3,17 @@ package org.thoughtcrime.securesms.database
import android.content.ContentValues
import android.content.Context
import net.sqlcipher.Cursor
import org.session.libsession.messaging.jobs.*
import org.session.libsession.messaging.jobs.AttachmentUploadJob
import org.session.libsession.messaging.jobs.GroupAvatarDownloadJob
import org.session.libsession.messaging.jobs.Job
import org.session.libsession.messaging.jobs.MessageReceiveJob
import org.session.libsession.messaging.jobs.MessageSendJob
import org.session.libsession.messaging.jobs.SessionJobInstantiator
import org.session.libsession.messaging.jobs.SessionJobManagerFactories
import org.session.libsession.messaging.utilities.Data
import org.session.libsignal.utilities.Log
import org.thoughtcrime.securesms.database.*
import org.thoughtcrime.securesms.database.helpers.SQLCipherOpenHelper
import org.thoughtcrime.securesms.jobmanager.impl.JsonDataSerializer
import org.thoughtcrime.securesms.util.*
class SessionJobDatabase(context: Context, helper: SQLCipherOpenHelper) : Database(context, helper) {
@@ -78,6 +82,13 @@ class SessionJobDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa
}
}
fun getGroupAvatarDownloadJob(server: String, room: String): GroupAvatarDownloadJob? {
val database = databaseHelper.readableDatabase
return database.getAll(sessionJobTable, "$jobType = ?", arrayOf(GroupAvatarDownloadJob.KEY)) {
jobFromCursor(it) as GroupAvatarDownloadJob?
}.filterNotNull().find { it.server == server && it.room == room }
}
fun cancelPendingMessageSendJobs(threadID: Long) {
val database = databaseHelper.writableDatabase
val attachmentUploadJobKeys = mutableListOf<String>()

View File

@@ -185,6 +185,10 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
return DatabaseComponent.get(context).sessionJobDatabase().getMessageReceiveJob(messageReceiveJobID)
}
override fun getGroupAvatarDownloadJob(server: String, room: String): GroupAvatarDownloadJob? {
return DatabaseComponent.get(context).sessionJobDatabase().getGroupAvatarDownloadJob(server, room)
}
override fun resumeMessageSendJobIfNeeded(messageSendJobID: String) {
val job = DatabaseComponent.get(context).sessionJobDatabase().getMessageSendJob(messageSendJobID) ?: return
JobQueue.shared.resumePendingSendMessage(job)
@@ -468,6 +472,11 @@ class Storage(context: Context, helper: SQLCipherOpenHelper) : Database(context,
.updateFormationTimestamp(groupID, formationTimestamp)
}
override fun updateTimestampUpdated(groupID: String, updatedTimestamp: Long) {
DatabaseComponent.get(context).groupDatabase()
.updateTimestampUpdated(groupID, updatedTimestamp)
}
override fun setExpirationTimer(groupID: String, duration: Int) {
val recipient = Recipient.from(context, fromSerialized(groupID), false)
DatabaseComponent.get(context).recipientDatabase().setExpireMessages(recipient, duration);

View File

@@ -61,9 +61,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
private static final int lokiV27 = 48;
private static final int lokiV28 = 49;
private static final int lokiV29 = 50;
private static final int lokiV30 = 51;
// Loki - onUpgrade(...) must be updated to use Loki version numbers if Signal makes any database changes
private static final int DATABASE_VERSION = lokiV29;
private static final int DATABASE_VERSION = lokiV30;
private static final String DATABASE_NAME = "signal.db";
private final Context context;
@@ -136,6 +137,7 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
db.execSQL(SessionContactDatabase.getCreateSessionContactTableCommand());
db.execSQL(RecipientDatabase.getCreateNotificationTypeCommand());
db.execSQL(ThreadDatabase.getCreatePinnedCommand());
db.execSQL(GroupDatabase.getCreateUpdatedTimestampCommand());
executeStatements(db, SmsDatabase.CREATE_INDEXS);
executeStatements(db, MmsDatabase.CREATE_INDEXS);
@@ -314,6 +316,10 @@ public class SQLCipherOpenHelper extends SQLiteOpenHelper {
db.execSQL(ThreadDatabase.getCreatePinnedCommand());
}
if (oldVersion < lokiV30) {
db.execSQL(GroupDatabase.getCreateUpdatedTimestampCommand());
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();

View File

@@ -1,7 +1,6 @@
package org.thoughtcrime.securesms.groups
import android.content.Context
import android.graphics.Bitmap
import androidx.annotation.WorkerThread
import okhttp3.HttpUrl
import org.session.libsession.messaging.MessagingModuleConfiguration
@@ -72,18 +71,9 @@ object OpenGroupManager {
OpenGroupAPIV2.getAuthToken(room, server).get()
// Get group info
val info = OpenGroupAPIV2.getInfo(room, server).get()
// Download the group image
// FIXME: Don't wait for the image to download
val image: Bitmap?
// Create the group locally if not available already
if (threadID < 0) {
val profilePictureAsByteArray = try {
OpenGroupAPIV2.downloadOpenGroupProfilePicture(info.id, server).get()
} catch (e: Exception) {
null
}
image = BitmapUtil.fromByteArray(profilePictureAsByteArray)
// Create the group locally
threadID = GroupManager.createOpenGroup(openGroupID, context, image, info.name).threadId
threadID = GroupManager.createOpenGroup(openGroupID, context, null, info.name).threadId
}
val openGroup = OpenGroupV2(server, room, info.name, publicKey)
threadDB.setOpenGroupChat(openGroup, threadID)