no message

This commit is contained in:
overtake 2017-09-15 16:12:52 +03:00
parent e894b8d94a
commit a1779a5467
7 changed files with 82 additions and 59 deletions

View File

@ -651,35 +651,27 @@ public class Account {
|> distinctUntilChanged
|> mapToSignal { [weak self] online -> Signal<Void, NoError> in
if let strongSelf = self {
if online {
let delayRequest: Signal<Void, NoError> = .complete() |> delay(60.0, queue: Queue.concurrentDefaultQueue())
let pushStatusOnce = strongSelf.network.request(Api.functions.account.updateStatus(offline: .boolFalse))
|> retryRequest
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
let pushStatusRepeatedly = (pushStatusOnce |> then(delayRequest)) |> restart
let peerId = strongSelf.peerId
let updatePresenceLocally = strongSelf.postbox.modify { modifier -> Void in
let timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 + 60.0 * 60.0 * 24.0 * 356.0
modifier.updatePeerPresences([peerId: TelegramUserPresence(status: .present(until: Int32(timestamp)))])
let delayRequest: Signal<Void, NoError> = .complete() |> delay(60.0, queue: Queue.concurrentDefaultQueue())
let pushStatusOnce = strongSelf.network.request(Api.functions.account.updateStatus(offline: online ? .boolFalse : .boolTrue))
|> retryRequest
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
let pushStatusRepeatedly = (pushStatusOnce |> then(delayRequest)) |> restart
let peerId = strongSelf.peerId
let updatePresenceLocally = strongSelf.postbox.modify { modifier -> Void in
let timestamp: Double
if online {
timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 + 60.0 * 60.0 * 24.0 * 356.0
} else {
timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 - 1.0
}
return combineLatest(pushStatusRepeatedly, updatePresenceLocally)
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
} else {
let pushStatusOnce = strongSelf.network.request(Api.functions.account.updateStatus(offline: .boolTrue))
|> retryRequest
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
let peerId = strongSelf.peerId
let updatePresenceLocally = strongSelf.postbox.modify { modifier -> Void in
let timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 - 1.0
modifier.updatePeerPresences([peerId: TelegramUserPresence(status: .present(until: Int32(timestamp)))])
}
return combineLatest(pushStatusOnce, updatePresenceLocally)
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
modifier.updatePeerPresences([peerId: TelegramUserPresence(status: .present(until: Int32(timestamp)))])
}
return combineLatest(pushStatusRepeatedly, updatePresenceLocally)
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
} else {
return .complete()
}
}
}
self.updatedPresenceDisposable.set(updatedPresence.start())
self.deviceContactListDisposable.set(managedDeviceContacts(postbox: self.postbox, network: self.network, deviceContacts: self.deviceContactList.get()).start())

View File

@ -936,6 +936,14 @@ private func finalStateWithUpdates(account: Account, state: AccountMutableState,
return peer
}
})
case let .updateUserPhoto(userId, _, photo, _):
updatedState.updatePeer(PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), { peer in
if let user = peer as? TelegramUser {
return user.withUpdatedPhoto(parsedTelegramProfilePhoto(photo))
} else {
return peer
}
})
case let .updateUserPhone(userId, phone):
updatedState.updatePeer(PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), { peer in
if let user = peer as? TelegramUser {

View File

@ -18,6 +18,7 @@ public func convertGroupToSupergroup(account: Account, peerId: PeerId) -> Signal
|> mapError { _ -> ConvertGroupToSupergroupError in
return .generic
}
|> timeout(5.0, queue: Queue.concurrentDefaultQueue(), alternate: .fail(.generic))
|> mapToSignal { updates -> Signal<PeerId, ConvertGroupToSupergroupError> in
account.stateManager.addUpdates(updates)
var createdPeerId: PeerId?

View File

@ -455,6 +455,7 @@ private final class NetworkHelper: NSObject, MTContextChangeListener {
}
public final class Network: NSObject, MTRequestMessageServiceDelegate {
private let queue: Queue = Queue()
let datacenterId: Int
let context: MTContext
let mtProto: MTProto
@ -516,7 +517,7 @@ public final class Network: NSObject, MTRequestMessageServiceDelegate {
}))
requestService.delegate = self
let shouldKeepConnectionSignal = self.shouldKeepConnection.get()
let shouldKeepConnectionSignal = self.shouldKeepConnection.get() |> deliverOn(queue)
|> distinctUntilChanged
self.shouldKeepConnectionDisposable.set(shouldKeepConnectionSignal.start(next: { [weak self] value in
if let strongSelf = self {

View File

@ -13,6 +13,7 @@ import Foundation
public struct TelegramPeerPhoto {
public let image: TelegramMediaImage
public let reference:TelegramMediaRemoteImageReference
public let index:Int
public let totalCount:Int
}
@ -45,13 +46,16 @@ public func requestPeerPhotos(account:Account, peerId:PeerId) -> Signal<[Telegra
for i in 0 ..< photos.count {
let photo = photos[i]
let image:TelegramMediaImage
let reference: TelegramMediaRemoteImageReference
switch photo {
case let .photo(data):
image = TelegramMediaImage(imageId: MediaId(namespace: Namespaces.Media.CloudImage, id: data.id), representations: telegramMediaImageRepresentationsFromApiSizes(data.sizes))
reference = .remoteImage(imageId: data.id, accessHash: data.accessHash)
case let .photoEmpty(id: id):
image = TelegramMediaImage(imageId: MediaId(namespace: Namespaces.Media.CloudImage, id: id), representations: [])
reference = .remoteImage(imageId: id, accessHash: 0)
}
images.append(TelegramPeerPhoto(image: image, index: i, totalCount: totalCount))
images.append(TelegramPeerPhoto(image: image, reference: reference, index: i, totalCount: totalCount))
}
return images
@ -116,7 +120,7 @@ public func requestPeerPhotos(account:Account, peerId:PeerId) -> Signal<[Telegra
switch media.action {
case let .photoUpdated(image):
if let image = image {
photos.append(TelegramPeerPhoto(image: image, index: index, totalCount: messages.count))
photos.append(TelegramPeerPhoto(image: image, reference: .none, index: index, totalCount: messages.count))
}
default:
break

View File

@ -7,6 +7,7 @@ import Foundation
public enum TelegramMediaRemoteImageReference {
case remoteImage(imageId: Int64, accessHash: Int64)
case none
}
public final class TelegramMediaImage: Media, Equatable {

View File

@ -222,45 +222,61 @@ public final class TelegramUser: Peer {
}
}
func parsedTelegramProfilePhoto(_ photo: Api.UserProfilePhoto?) -> [TelegramMediaImageRepresentation] {
var telegramPhoto: [TelegramMediaImageRepresentation] = []
if let photo = photo {
switch photo {
case let .userProfilePhoto(_, photoSmall, photoBig):
if let smallResource = mediaResourceFromApiFileLocation(photoSmall, size: nil), let largeResource = mediaResourceFromApiFileLocation(photoBig, size: nil) {
telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 80.0, height: 80.0), resource: smallResource))
telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 640.0, height: 640.0), resource: largeResource))
}
case .userProfilePhotoEmpty:
break
}
}
return telegramPhoto
}
public extension TelegramUser {
public convenience init(user: Api.User) {
switch user {
case let .user(flags, id, accessHash, firstName, lastName, username, phone, photo, _, _, _, botInlinePlaceholder, _):
var telegramPhoto: [TelegramMediaImageRepresentation] = []
if let photo = photo {
switch photo {
case let .userProfilePhoto(_, photoSmall, photoBig):
if let smallResource = mediaResourceFromApiFileLocation(photoSmall, size: nil), let largeResource = mediaResourceFromApiFileLocation(photoBig, size: nil) {
telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 80.0, height: 80.0), resource: smallResource))
telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 640.0, height: 640.0), resource: largeResource))
}
case .userProfilePhotoEmpty:
break
case let .user(flags, id, accessHash, firstName, lastName, username, phone, photo, _, _, _, botInlinePlaceholder, _):
var telegramPhoto: [TelegramMediaImageRepresentation] = []
if let photo = photo {
switch photo {
case let .userProfilePhoto(_, photoSmall, photoBig):
if let smallResource = mediaResourceFromApiFileLocation(photoSmall, size: nil), let largeResource = mediaResourceFromApiFileLocation(photoBig, size: nil) {
telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 80.0, height: 80.0), resource: smallResource))
telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 640.0, height: 640.0), resource: largeResource))
}
case .userProfilePhotoEmpty:
break
}
}
var userFlags: UserInfoFlags = []
if (flags & (1 << 17)) != 0 {
userFlags.insert(.isVerified)
}
var userFlags: UserInfoFlags = []
if (flags & (1 << 17)) != 0 {
userFlags.insert(.isVerified)
}
var botInfo: BotUserInfo?
if (flags & (1 << 14)) != 0 {
var botFlags = BotUserInfoFlags()
if (flags & (1 << 15)) != 0 {
botFlags.insert(.hasAccessToChatHistory)
}
if (flags & (1 << 16)) == 0 {
botFlags.insert(.worksWithGroups)
}
if (flags & (1 << 21)) == 0 {
botFlags.insert(.requiresGeolocationForInlineRequests)
}
botInfo = BotUserInfo(flags: botFlags, inlinePlaceholder: botInlinePlaceholder)
var botInfo: BotUserInfo?
if (flags & (1 << 14)) != 0 {
var botFlags = BotUserInfoFlags()
if (flags & (1 << 15)) != 0 {
botFlags.insert(.hasAccessToChatHistory)
}
self.init(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: id), accessHash: accessHash, firstName: firstName, lastName: lastName, username: username, phone: phone, photo: telegramPhoto, botInfo: botInfo, flags: userFlags)
case let .userEmpty(id):
self.init(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: id), accessHash: nil, firstName: nil, lastName: nil, username: nil, phone: nil, photo: [], botInfo: nil, flags: [])
if (flags & (1 << 16)) == 0 {
botFlags.insert(.worksWithGroups)
}
if (flags & (1 << 21)) == 0 {
botFlags.insert(.requiresGeolocationForInlineRequests)
}
botInfo = BotUserInfo(flags: botFlags, inlinePlaceholder: botInlinePlaceholder)
}
self.init(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: id), accessHash: accessHash, firstName: firstName, lastName: lastName, username: username, phone: phone, photo: telegramPhoto, botInfo: botInfo, flags: userFlags)
case let .userEmpty(id):
self.init(id: PeerId(namespace: Namespaces.Peer.CloudUser, id: id), accessHash: nil, firstName: nil, lastName: nil, username: nil, phone: nil, photo: [], botInfo: nil, flags: [])
}
}