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 |> distinctUntilChanged
|> mapToSignal { [weak self] online -> Signal<Void, NoError> in |> mapToSignal { [weak self] online -> Signal<Void, NoError> in
if let strongSelf = self { if let strongSelf = self {
if online { let delayRequest: Signal<Void, NoError> = .complete() |> delay(60.0, queue: Queue.concurrentDefaultQueue())
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))
let pushStatusOnce = strongSelf.network.request(Api.functions.account.updateStatus(offline: .boolFalse)) |> retryRequest
|> retryRequest |> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() } let pushStatusRepeatedly = (pushStatusOnce |> then(delayRequest)) |> restart
let pushStatusRepeatedly = (pushStatusOnce |> then(delayRequest)) |> restart let peerId = strongSelf.peerId
let peerId = strongSelf.peerId let updatePresenceLocally = strongSelf.postbox.modify { modifier -> Void in
let updatePresenceLocally = strongSelf.postbox.modify { modifier -> Void in let timestamp: Double
let timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 + 60.0 * 60.0 * 24.0 * 356.0 if online {
modifier.updatePeerPresences([peerId: TelegramUserPresence(status: .present(until: Int32(timestamp)))]) timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 + 60.0 * 60.0 * 24.0 * 356.0
} else {
timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 - 1.0
} }
return combineLatest(pushStatusRepeatedly, updatePresenceLocally) modifier.updatePeerPresences([peerId: TelegramUserPresence(status: .present(until: Int32(timestamp)))])
|> 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() }
} }
return combineLatest(pushStatusRepeatedly, updatePresenceLocally)
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() }
} else { } else {
return .complete() return .complete()
} }
} }
self.updatedPresenceDisposable.set(updatedPresence.start()) self.updatedPresenceDisposable.set(updatedPresence.start())
self.deviceContactListDisposable.set(managedDeviceContacts(postbox: self.postbox, network: self.network, deviceContacts: self.deviceContactList.get()).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 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): case let .updateUserPhone(userId, phone):
updatedState.updatePeer(PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), { peer in updatedState.updatePeer(PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), { peer in
if let user = peer as? TelegramUser { if let user = peer as? TelegramUser {

View File

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

View File

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

View File

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

View File

@ -7,6 +7,7 @@ import Foundation
public enum TelegramMediaRemoteImageReference { public enum TelegramMediaRemoteImageReference {
case remoteImage(imageId: Int64, accessHash: Int64) case remoteImage(imageId: Int64, accessHash: Int64)
case none
} }
public final class TelegramMediaImage: Media, Equatable { 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 extension TelegramUser {
public convenience init(user: Api.User) { public convenience init(user: Api.User) {
switch user { switch user {
case let .user(flags, id, accessHash, firstName, lastName, username, phone, photo, _, _, _, botInlinePlaceholder, _): case let .user(flags, id, accessHash, firstName, lastName, username, phone, photo, _, _, _, botInlinePlaceholder, _):
var telegramPhoto: [TelegramMediaImageRepresentation] = [] var telegramPhoto: [TelegramMediaImageRepresentation] = []
if let photo = photo { if let photo = photo {
switch photo { switch photo {
case let .userProfilePhoto(_, photoSmall, photoBig): case let .userProfilePhoto(_, photoSmall, photoBig):
if let smallResource = mediaResourceFromApiFileLocation(photoSmall, size: nil), let largeResource = mediaResourceFromApiFileLocation(photoBig, size: nil) { 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: 80.0, height: 80.0), resource: smallResource))
telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 640.0, height: 640.0), resource: largeResource)) telegramPhoto.append(TelegramMediaImageRepresentation(dimensions: CGSize(width: 640.0, height: 640.0), resource: largeResource))
}
case .userProfilePhotoEmpty:
break
} }
case .userProfilePhotoEmpty:
break
} }
}
var userFlags: UserInfoFlags = [] var userFlags: UserInfoFlags = []
if (flags & (1 << 17)) != 0 { if (flags & (1 << 17)) != 0 {
userFlags.insert(.isVerified) userFlags.insert(.isVerified)
} }
var botInfo: BotUserInfo? var botInfo: BotUserInfo?
if (flags & (1 << 14)) != 0 { if (flags & (1 << 14)) != 0 {
var botFlags = BotUserInfoFlags() var botFlags = BotUserInfoFlags()
if (flags & (1 << 15)) != 0 { if (flags & (1 << 15)) != 0 {
botFlags.insert(.hasAccessToChatHistory) 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)
} }
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) if (flags & (1 << 16)) == 0 {
case let .userEmpty(id): botFlags.insert(.worksWithGroups)
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 << 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: [])
} }
} }