pull/65/head
Niels Andriesse 5 years ago committed by Maxim Shishmarev
parent 7644755d08
commit 7aa4e83700

@ -39,6 +39,7 @@ extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;
- (nullable NSString *)localProfileName;
- (nullable UIImage *)localProfileAvatarImage;
- (nullable NSData *)localProfileAvatarData;
- (nullable NSString *)profilePictureURL;
- (void)ensureLocalProfileCached;
// This method is used to update the "local profile" state on the client
@ -82,7 +83,7 @@ extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;
profileNameEncrypted:(nullable NSData *)profileNameEncrypted
avatarUrlPath:(nullable NSString *)avatarUrlPath;
- (void)setDisplayNameForContactWithID:(NSString *)contactID to:(NSString *)displayName with:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateProfileForContactWithID:(NSString *)contactID displayName:(NSString *)displayName profilePictureURL:(NSString *)profilePictureURL with:(YapDatabaseReadWriteTransaction *)transaction;
#pragma mark - User Interface

@ -340,6 +340,11 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
}
}
- (nullable NSString *)profilePictureURL
{
return self.localUserProfile.avatarUrlPath;
}
- (void)writeAvatarToDisk:(UIImage *)avatar
success:(void (^)(NSData *data, NSString *fileName))successBlock
failure:(ProfileManagerFailureBlock)failureBlock {
@ -1187,8 +1192,8 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
NSURLResponse *_Nonnull response, NSURL *_Nullable filePathParam, NSError *_Nullable error) {
// Ensure disk IO and decryption occurs off the main thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *_Nullable encryptedData = (error ? nil : [NSData dataWithContentsOfFile:tempFilePath]);
NSData *_Nullable decryptedData = [self decryptProfileData:encryptedData profileKey:profileKeyAtStart];
NSData *_Nullable encryptedData = [NSData dataWithContentsOfFile:tempFilePath];
NSData *_Nullable decryptedData = encryptedData;
UIImage *_Nullable image = nil;
if (decryptedData) {
BOOL success = [decryptedData writeToFile:filePath atomically:YES];
@ -1244,11 +1249,11 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
});
};
NSURL *avatarUrl = [NSURL URLWithString:userProfile.avatarUrlPath relativeToURL:self.avatarHTTPManager.baseURL];
NSString *profilePictureURL = userProfile.avatarUrlPath;
NSError *serializationError;
NSMutableURLRequest *request =
[self.avatarHTTPManager.requestSerializer requestWithMethod:@"GET"
URLString:avatarUrl.absoluteString
URLString:profilePictureURL
parameters:nil
error:&serializationError];
if (serializationError) {
@ -1256,7 +1261,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
return;
}
__block NSURLSessionDownloadTask *downloadTask = [self.avatarHTTPManager downloadTaskWithRequest:request
__block *downloadTask = [self.avatarHTTPManager downloadTaskWithRequest:request
progress:^(NSProgress *_Nonnull downloadProgress) {
OWSLogVerbose(
@"Downloading avatar for %@ %f", userProfile.recipientId, downloadProgress.fractionCompleted);
@ -1325,10 +1330,14 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
});
}
- (void)setDisplayNameForContactWithID:(NSString *)contactID to:(NSString *)displayName with:(YapDatabaseReadWriteTransaction *)transaction
- (void)updateProfileForContactWithID:(NSString *)contactID displayName:(NSString *)displayName profilePictureURL:(NSString *)profilePictureURL with:(YapDatabaseReadWriteTransaction *)transaction
{
OWSUserProfile *userProfile = [OWSUserProfile getOrBuildUserProfileForRecipientId:contactID transaction:transaction];
[userProfile updateWithProfileName:displayName avatarUrlPath:@"" avatarFileName:@"" transaction:transaction completion:nil];
NSString *oldProfilePictureURL = userProfile.avatarUrlPath;
[userProfile updateWithProfileName:displayName avatarUrlPath:profilePictureURL avatarFileName:@"" transaction:transaction completion:nil];
if (![oldProfilePictureURL isEqual:profilePictureURL]) {
[self downloadAvatarForUserProfile:userProfile];
}
}
- (BOOL)isNullableDataEqual:(NSData *_Nullable)left toData:(NSData *_Nullable)right

@ -247,6 +247,7 @@ message DataMessage {
// Loki: A custom message for our profile
message LokiProfile {
optional string displayName = 1;
optional string profilePicture = 2;
}
optional string body = 1;

@ -1103,15 +1103,15 @@ NSString *NSStringForOutgoingMessageRecipientState(OWSOutgoingMessageRecipientSt
[ProtoUtils addLocalProfileKeyIfNecessary:self.thread recipientId:recipientId dataMessageBuilder:builder];
// Loki: Set display name if needed
// Loki: Set display name & profile picture
id<ProfileManagerProtocol> profileManager = SSKEnvironment.shared.profileManager;
NSString *displayName = profileManager.localProfileName;
if (displayName != nil) {
SSKProtoDataMessageLokiProfileBuilder *profileBuilder = [SSKProtoDataMessageLokiProfile builder];
[profileBuilder setDisplayName:displayName];
SSKProtoDataMessageLokiProfile *profile = [profileBuilder buildAndReturnError:nil];
[builder setProfile:profile];
}
NSString *profilePictureURL = profileManager.profilePictureURL;
SSKProtoDataMessageLokiProfileBuilder *profileBuilder = [SSKProtoDataMessageLokiProfile builder];
[profileBuilder setDisplayName:displayName];
[profileBuilder setProfilePicture:profilePictureURL ?: @""];
SSKProtoDataMessageLokiProfile *profile = [profileBuilder buildAndReturnError:nil];
[builder setProfile:profile];
NSError *error;
SSKProtoDataMessage *_Nullable dataProto = [builder buildAndReturnError:&error];

@ -1525,15 +1525,18 @@ NS_ASSUME_NONNULL_BEGIN
linkPreview:linkPreview
serverTimestamp:serverTimestamp
wasReceivedByUD:wasReceivedByUD];
// Loki: Handle display name update if needed
NSString *rawDisplayName = dataMessage.profile.displayName;
NSString *displayName = nil;
if (rawDisplayName != nil && rawDisplayName.length > 0) {
NSString *displayName = [NSString stringWithFormat:@"%@ (...%@)", rawDisplayName, [incomingMessage.authorId substringFromIndex:incomingMessage.authorId.length - 8]];
[self.profileManager setDisplayNameForContactWithID:masterThread.contactIdentifier to:displayName with:transaction];
} else {
[self.profileManager setDisplayNameForContactWithID:masterThread.contactIdentifier to:nil with:transaction];
displayName = [NSString stringWithFormat:@"%@ (...%@)", rawDisplayName, [incomingMessage.authorId substringFromIndex:incomingMessage.authorId.length - 8]];
}
NSString *rawProfilePictureURL = dataMessage.profile.profilePicture;
NSString *profilePictureURL = nil;
if (rawProfilePictureURL != nil && rawProfilePictureURL.length > 0) {
profilePictureURL = rawProfilePictureURL;
}
[self.profileManager updateProfileForContactWithID:thread.contactIdentifier displayName:displayName profilePictureURL:profilePictureURL with:transaction];
// Loki: Parse Loki specific properties if needed
if (envelope.isPtpMessage) { incomingMessage.isP2P = YES; }

@ -14,6 +14,7 @@ NS_ASSUME_NONNULL_BEGIN
- (nullable NSString *)localProfileName;
- (nullable NSString *)profileNameForRecipientId:(NSString *)recipientId;
- (nullable NSString *)profilePictureURL;
- (nullable NSData *)profileKeyDataForRecipientId:(NSString *)recipientId;
- (void)setProfileKeyData:(NSData *)profileKeyData forRecipientId:(NSString *)recipientId;
@ -29,7 +30,7 @@ NS_ASSUME_NONNULL_BEGIN
- (void)fetchProfileForRecipientId:(NSString *)recipientId;
- (void)setDisplayNameForContactWithID:(NSString *)contactID to:(NSString *)displayName with:(YapDatabaseReadWriteTransaction *)transaction;
- (void)updateProfileForContactWithID:(NSString *)contactID displayName:(NSString *)displayName profilePictureURL:(NSString *)profilePictureURL with:(YapDatabaseReadWriteTransaction *)transaction;
@end

@ -3328,6 +3328,9 @@ extension SSKProtoDataMessagePreview.SSKProtoDataMessagePreviewBuilder {
if let _value = displayName {
builder.setDisplayName(_value)
}
if let _value = profilePicture {
builder.setProfilePicture(_value)
}
return builder
}
@ -3341,6 +3344,10 @@ extension SSKProtoDataMessagePreview.SSKProtoDataMessagePreviewBuilder {
proto.displayName = valueParam
}
@objc public func setProfilePicture(_ valueParam: String) {
proto.profilePicture = valueParam
}
@objc public func build() throws -> SSKProtoDataMessageLokiProfile {
return try SSKProtoDataMessageLokiProfile.parseProto(proto)
}
@ -3362,6 +3369,16 @@ extension SSKProtoDataMessagePreview.SSKProtoDataMessagePreviewBuilder {
return proto.hasDisplayName
}
@objc public var profilePicture: String? {
guard proto.hasProfilePicture else {
return nil
}
return proto.profilePicture
}
@objc public var hasProfilePicture: Bool {
return proto.hasProfilePicture
}
private init(proto: SignalServiceProtos_DataMessage.LokiProfile) {
self.proto = proto
}

@ -1507,11 +1507,21 @@ struct SignalServiceProtos_DataMessage {
/// Clears the value of `displayName`. Subsequent reads from it will return its default value.
mutating func clearDisplayName() {self._displayName = nil}
var profilePicture: String {
get {return _profilePicture ?? String()}
set {_profilePicture = newValue}
}
/// Returns true if `profilePicture` has been explicitly set.
var hasProfilePicture: Bool {return self._profilePicture != nil}
/// Clears the value of `profilePicture`. Subsequent reads from it will return its default value.
mutating func clearProfilePicture() {self._profilePicture = nil}
var unknownFields = SwiftProtobuf.UnknownStorage()
init() {}
fileprivate var _displayName: String? = nil
fileprivate var _profilePicture: String? = nil
}
init() {}
@ -4102,12 +4112,14 @@ extension SignalServiceProtos_DataMessage.LokiProfile: SwiftProtobuf.Message, Sw
static let protoMessageName: String = SignalServiceProtos_DataMessage.protoMessageName + ".LokiProfile"
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1: .same(proto: "displayName"),
2: .same(proto: "profilePicture"),
]
mutating func decodeMessage<D: SwiftProtobuf.Decoder>(decoder: inout D) throws {
while let fieldNumber = try decoder.nextFieldNumber() {
switch fieldNumber {
case 1: try decoder.decodeSingularStringField(value: &self._displayName)
case 2: try decoder.decodeSingularStringField(value: &self._profilePicture)
default: break
}
}
@ -4117,11 +4129,15 @@ extension SignalServiceProtos_DataMessage.LokiProfile: SwiftProtobuf.Message, Sw
if let v = self._displayName {
try visitor.visitSingularStringField(value: v, fieldNumber: 1)
}
if let v = self._profilePicture {
try visitor.visitSingularStringField(value: v, fieldNumber: 2)
}
try unknownFields.traverse(visitor: &visitor)
}
static func ==(lhs: SignalServiceProtos_DataMessage.LokiProfile, rhs: SignalServiceProtos_DataMessage.LokiProfile) -> Bool {
if lhs._displayName != rhs._displayName {return false}
if lhs._profilePicture != rhs._profilePicture {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}

Loading…
Cancel
Save