Set avatar on public chats.

This commit is contained in:
Mikunj 2019-11-29 13:47:41 +11:00
parent eafc2afe4a
commit 7494393473
2 changed files with 28 additions and 0 deletions

View File

@ -270,6 +270,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
void (^tryToUpdateService)(NSString *_Nullable, NSString *_Nullable) = ^(
NSString *_Nullable avatarUrlPath, NSString *_Nullable avatarFileName) {
[self updateServiceWithProfileName:profileName
avatarUrl:avatarUrlPath
success:^{
OWSUserProfile *userProfile = self.localUserProfile;
OWSAssertDebug(userProfile);
@ -447,6 +448,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
}
- (void)updateServiceWithProfileName:(nullable NSString *)localProfileName
avatarUrl:(nullable NSString *)avatarURL
success:(void (^)(void))successBlock
failure:(ProfileManagerFailureBlock)failureBlock {
OWSAssertDebug(successBlock);
@ -461,6 +463,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
for (NSString *server in servers) {
[[LKPublicChatAPI setDisplayName:localProfileName on:server] retainUntilComplete];
[[LKPublicChatAPI setAvatar:avatarURL profileKey:self.localProfileKey.keyData on:server] retainUntilComplete];
}
successBlock();
@ -636,6 +639,7 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
}
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
[self updateServiceWithProfileName:oldProfileName
avatarUrl:localUserProfile.avatarUrlPath
success:^{
OWSLogInfo(@"Update to profile name succeeded.");

View File

@ -12,6 +12,7 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
// MARK: Public Chat
private static let channelInfoType = "net.patter-app.settings"
private static let attachmentType = "net.app.core.oembed"
public static let avatarType = "network.loki.messenger.avatar"
@objc public static let publicChatMessageType = "network.loki.messenger.publicChat"
@objc public static let defaultChats: [LokiPublicChat] = {
@ -287,6 +288,24 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
}.retryingIfNeeded(maxRetryCount: 3)
}
public static func setAvatar(with url: String?, profileKey: Data, on server: String) -> Promise<Void> {
print("[Loki] Updating avatar on server: \(server).")
return getAuthToken(for: server).then(on: DispatchQueue.global()) { token -> Promise<Void> in
var annotation: JSON = ["type" : avatarType]
if let url = url {
annotation["value"] = ["profileKey": profileKey.base64EncodedString(), "url" : url]
}
let parameters: JSON = [ "annotations" : [annotation] ]
let url = URL(string: "\(server)/users/me")!
let request = TSRequest(url: url, method: "PATCH", parameters: parameters)
request.allHTTPHeaderFields = [ "Content-Type" : "application/json", "Authorization" : "Bearer \(token)" ]
return TSNetworkManager.shared().perform(request, withCompletionQueue: DispatchQueue.global()).map { _ in }.recover(on: DispatchQueue.global()) { error in
print("Couldn't update avatar due to error: \(error).")
throw error
}
}.retryingIfNeeded(maxRetryCount: 3)
}
public static func getInfo(for channel: UInt64, on server: String) -> Promise<LokiPublicChatInfo> {
let url = URL(string: "\(server)/channels/\(channel)?include_annotations=1")!
let request = TSRequest(url: url)
@ -329,4 +348,9 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
public static func objc_setDisplayName(to newDisplayName: String?, on server: String) -> AnyPromise {
return AnyPromise.from(setDisplayName(to: newDisplayName, on: server))
}
@objc(setAvatar:profileKey:on:)
public static func objc_setAvatar(with url: String?, profileKey: Data, on server: String) -> AnyPromise {
return AnyPromise.from(setAvatar(with: url, profileKey: profileKey, on: server))
}
}