Browse Source

Update contact model when legacy update methods are invoked

pull/320/head
Niels Andriesse 2 years ago
parent
commit
ec2d784c20
  1. 14
      SessionMessagingKit/Contacts/Contact.swift
  2. 2
      SessionMessagingKit/Database/Storage+Contacts.swift
  3. 49
      SessionMessagingKit/To Do/OWSUserProfile.m

14
SessionMessagingKit/Contacts/Contact.swift

@ -1,19 +1,19 @@
@objc(SNContact)
public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
public let sessionID: String
@objc public let sessionID: String
/// The display name of the contact.
///
/// - Note: In open groups use `openGroupDisplayName`.
public var displayName: String?
@objc public var displayName: String?
/// The URL from which to fetch the contact's profile picture.
public var profilePictureURL: String?
@objc public var profilePictureURL: String?
/// The file name of the contact's profile picture on local storage.
public var profilePictureFileName: String?
@objc public var profilePictureFileName: String?
/// The key with which the profile picture is encrypted.
public var profilePictureEncryptionKey: OWSAES256Key?
@objc public var profilePictureEncryptionKey: OWSAES256Key?
/// The ID of the thread associated with this contact.
public var threadID: String?
@objc public var threadID: String?
/// In open groups, where it's more likely that multiple users have the same name, we display a bit of the Session ID after
/// a user's display name for added context.
@ -25,7 +25,7 @@ public class Contact : NSObject, NSCoding { // NSObject/NSCoding conformance is
}
// MARK: Initialization
public init(sessionID: String) {
@objc public init(sessionID: String) {
self.sessionID = sessionID
super.init()
}

2
SessionMessagingKit/Database/Storage+Contacts.swift

@ -4,6 +4,7 @@ extension Storage {
private static let contactCollection = "LokiContactCollection"
@objc(getContactWithSessionID:)
public func getContact(with sessionID: String) -> Contact? {
var result: Contact?
Storage.read { transaction in
@ -12,6 +13,7 @@ extension Storage {
return result
}
@objc(setContact:usingTransaction:)
public func setContact(_ contact: Contact, using transaction: Any) {
(transaction as! YapDatabaseReadWriteTransaction).setObject(contact, forKey: contact.sessionID, inCollection: Storage.contactCollection)
}

49
SessionMessagingKit/To Do/OWSUserProfile.m

@ -13,6 +13,8 @@
#import <SignalCoreKit/NSString+OWS.h>
#import <YapDatabase/YapDatabaseConnection.h>
#import <YapDatabase/YapDatabaseTransaction.h>
#import <Curve25519Kit/Curve25519.h>
#import <SessionMessagingKit/SessionMessagingKit-Swift.h>
NS_ASSUME_NONNULL_BEGIN
@ -117,6 +119,15 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
#pragma mark -
- (NSString *)sessionID
{
if ([self.recipientId isEqual:kLocalProfileUniqueId]) {
return OWSIdentityManager.sharedManager.identityKeyPair.hexEncodedPublicKey;
} else {
return self.recipientId;
}
}
- (nullable NSString *)avatarUrlPath
{
@synchronized(self)
@ -270,6 +281,12 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
functionName:__PRETTY_FUNCTION__
transaction:transaction
completion:completion];
SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID];
contact.displayName = [profileName ows_stripped];
contact.profilePictureURL = avatarUrlPath;
contact.profilePictureFileName = avatarFileName;
[LKStorage.shared setContact:contact usingTransaction:transaction];
}
- (void)updateWithProfileName:(nullable NSString *)profileName
@ -288,6 +305,14 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
functionName:__PRETTY_FUNCTION__
dbConnection:dbConnection
completion:completion];
SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID];
contact.displayName = [profileName ows_stripped];
contact.profilePictureURL = avatarUrlPath;
contact.profilePictureFileName = avatarFileName;
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[LKStorage.shared setContact:contact usingTransaction:transaction];
}];
}
- (void)updateWithProfileName:(nullable NSString *)profileName
@ -302,6 +327,13 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
functionName:__PRETTY_FUNCTION__
dbConnection:dbConnection
completion:completion];
SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID];
contact.displayName = [profileName ows_stripped];
contact.profilePictureURL = avatarUrlPath;
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[LKStorage.shared setContact:contact usingTransaction:transaction];
}];
}
- (void)updateWithAvatarUrlPath:(nullable NSString *)avatarUrlPath
@ -318,6 +350,13 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
functionName:__PRETTY_FUNCTION__
dbConnection:dbConnection
completion:completion];
SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID];
contact.profilePictureURL = avatarUrlPath;
contact.profilePictureFileName = avatarFileName;
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[LKStorage.shared setContact:contact usingTransaction:transaction];
}];
}
- (void)updateWithAvatarFileName:(nullable NSString *)avatarFileName
@ -330,6 +369,12 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
functionName:__PRETTY_FUNCTION__
dbConnection:dbConnection
completion:completion];
SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID];
contact.profilePictureFileName = avatarFileName;
[LKStorage writeSyncWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[LKStorage.shared setContact:contact usingTransaction:transaction];
}];
}
- (void)clearWithProfileKey:(OWSAES256Key *)profileKey
@ -368,6 +413,10 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
functionName:__PRETTY_FUNCTION__
transaction:transaction
completion:completion];
SNContact *contact = [LKStorage.shared getContactWithSessionID:self.sessionID] ?: [[SNContact alloc] initWithSessionID:self.sessionID];
contact.profilePictureEncryptionKey = profileKey;
[LKStorage.shared setContact:contact usingTransaction:transaction];
}
#pragma mark - Database Connection Accessors

Loading…
Cancel
Save