@ -19,7 +19,8 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
// MARK: C o n v e n i e n c e
private static var userDisplayName : String {
return SSKEnvironment . shared . contactsManager . displayName ( forPhoneIdentifier : getUserHexEncodedPublicKey ( ) ) ? ? " Anonymous "
let userPublicKey = UserDefaults . standard [ . masterHexEncodedPublicKey ] ? ? getUserHexEncodedPublicKey ( )
return SSKEnvironment . shared . profileManager . profileNameForRecipient ( withID : userPublicKey ) ? ? " Anonymous "
}
// MARK: D a t a b a s e
@ -329,6 +330,51 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
}
}
static func updateProfileIfNeeded ( for channel : UInt64 , on server : String , from info : LokiPublicChatInfo ) {
let storage = OWSPrimaryStorage . shared ( )
let publicChatID = " \( server ) . \( channel ) "
try ! Storage . writeSync { transaction in
// U p d a t e u s e r c o u n t
storage . setUserCount ( info . memberCount , forPublicChatWithID : publicChatID , in : transaction )
let groupThread = TSGroupThread . getOrCreateThread ( withGroupId : publicChatID . data ( using : . utf8 ) ! , groupType : . openGroup , transaction : transaction )
// U p d a t e d i s p l a y n a m e i f n e e d e d
let groupModel = groupThread . groupModel
if groupModel . groupName != info . displayName {
let newGroupModel = TSGroupModel ( title : info . displayName , memberIds : groupModel . groupMemberIds , image : groupModel . groupImage , groupId : groupModel . groupId , groupType : groupModel . groupType , adminIds : groupModel . groupAdminIds )
groupThread . groupModel = newGroupModel
groupThread . save ( with : transaction )
}
// D o w n l o a d a n d u p d a t e p r o f i l e p i c t u r e i f n e e d e d
let oldProfilePictureURL = storage . getProfilePictureURL ( forPublicChatWithID : publicChatID , in : transaction )
if oldProfilePictureURL != info . profilePictureURL || groupModel . groupImage = = nil {
storage . setProfilePictureURL ( info . profilePictureURL , forPublicChatWithID : publicChatID , in : transaction )
if let avatarURL = info . profilePictureURL {
let configuration = URLSessionConfiguration . default
let manager = AFURLSessionManager . init ( sessionConfiguration : configuration )
let url = URL ( string : " \( server ) \( avatarURL ) " ) !
let request = URLRequest ( url : url )
let task = manager . downloadTask ( with : request , progress : nil ,
destination : { ( targetPath : URL , response : URLResponse ) -> URL in
let tempFilePath = URL ( fileURLWithPath : OWSTemporaryDirectoryAccessibleAfterFirstAuth ( ) ) . appendingPathComponent ( UUID ( ) . uuidString )
return tempFilePath
} ,
completionHandler : { ( response : URLResponse , filePath : URL ? , error : Error ? ) in
if let error = error {
print ( " [Loki] Couldn't download profile picture for public chat channel with ID: \( channel ) on server: \( server ) . " )
return
}
if let filePath = filePath , let avatarData = try ? Data . init ( contentsOf : filePath ) {
let attachmentStream = TSAttachmentStream ( contentType : OWSMimeTypeImageJpeg , byteCount : UInt32 ( avatarData . count ) , sourceFilename : nil , caption : nil , albumMessageId : nil )
try ! attachmentStream . write ( avatarData )
groupThread . updateAvatar ( with : attachmentStream )
}
} )
task . resume ( )
}
}
}
}
// MARK: J o i n i n g & L e a v i n g
@objc ( getInfoForChannelWithID : onServer : )
public static func objc_getInfo ( for channel : UInt64 , on server : String ) -> AnyPromise {
@ -348,6 +394,7 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
let annotation = annotations . first ,
let info = annotation [ " value " ] as ? JSON ,
let displayName = info [ " name " ] as ? String ,
let profilePictureURL = info [ " avatar " ] as ? String ,
let countInfo = data [ " counts " ] as ? JSON ,
let memberCount = countInfo [ " subscribers " ] as ? Int else {
print ( " [Loki] Couldn't parse info for public chat channel with ID: \( channel ) on server: \( server ) from: \( rawResponse ) . " )
@ -357,8 +404,9 @@ public final class LokiPublicChatAPI : LokiDotNetAPI {
try ! Storage . writeSync { transaction in
storage . setUserCount ( memberCount , forPublicChatWithID : " \( server ) . \( channel ) " , in : transaction )
}
// TODO: U s e t h i s t o u p d a t e o p e n g r o u p n a m e s a s n e e d e d
return LokiPublicChatInfo ( displayName : displayName , memberCount : memberCount )
let publicChatInfo = LokiPublicChatInfo ( displayName : displayName , profilePictureURL : profilePictureURL , memberCount : memberCount )
updateProfileIfNeeded ( for : channel , on : server , from : publicChatInfo )
return publicChatInfo
}
} . handlingInvalidAuthTokenIfNeeded ( for : server )
}