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,31 +651,23 @@ 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: .boolFalse)) let pushStatusOnce = strongSelf.network.request(Api.functions.account.updateStatus(offline: online ? .boolFalse : .boolTrue))
|> 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 = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 + 60.0 * 60.0 * 24.0 * 356.0 let timestamp: Double
if online {
timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 + 60.0 * 60.0 * 24.0 * 356.0
} else {
timestamp = CFAbsoluteTimeGetCurrent() + NSTimeIntervalSince1970 - 1.0
}
modifier.updatePeerPresences([peerId: TelegramUserPresence(status: .present(until: Int32(timestamp)))]) modifier.updatePeerPresences([peerId: TelegramUserPresence(status: .present(until: Int32(timestamp)))])
} }
return combineLatest(pushStatusRepeatedly, updatePresenceLocally) return combineLatest(pushStatusRepeatedly, updatePresenceLocally)
|> mapToSignal { _ -> Signal<Void, NoError> in return .complete() } |> 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() }
}
} else { } else {
return .complete() return .complete()
} }

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,6 +222,22 @@ 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 {