mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-12-23 06:35:51 +00:00
[WIP] Stories
This commit is contained in:
@@ -6,12 +6,14 @@ import TelegramApi
|
||||
|
||||
func telegramMediaWebpageAttributeFromApiWebpageAttribute(_ attribute: Api.WebPageAttribute) -> TelegramMediaWebpageAttribute? {
|
||||
switch attribute {
|
||||
case let .webPageAttributeTheme(_, documents, settings):
|
||||
var files: [TelegramMediaFile] = []
|
||||
if let documents = documents {
|
||||
files = documents.compactMap { telegramMediaFileFromApiDocument($0) }
|
||||
}
|
||||
return .theme(TelegraMediaWebpageThemeAttribute(files: files, settings: settings.flatMap { TelegramThemeSettings(apiThemeSettings: $0) }))
|
||||
case let .webPageAttributeTheme(_, documents, settings):
|
||||
var files: [TelegramMediaFile] = []
|
||||
if let documents = documents {
|
||||
files = documents.compactMap { telegramMediaFileFromApiDocument($0) }
|
||||
}
|
||||
return .theme(TelegraMediaWebpageThemeAttribute(files: files, settings: settings.flatMap { TelegramThemeSettings(apiThemeSettings: $0) }))
|
||||
case .webPageAttributeStory:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,15 +40,22 @@ func telegramMediaWebpageFromApiWebpage(_ webpage: Api.WebPage, url: String?) ->
|
||||
if let document = document {
|
||||
file = telegramMediaFileFromApiDocument(document)
|
||||
}
|
||||
var story: TelegramMediaStory?
|
||||
var webpageAttributes: [TelegramMediaWebpageAttribute] = []
|
||||
if let attributes = attributes {
|
||||
webpageAttributes = attributes.compactMap(telegramMediaWebpageAttributeFromApiWebpageAttribute)
|
||||
for attribute in attributes {
|
||||
if case let .webPageAttributeStory(_, userId, id, _) = attribute {
|
||||
story = TelegramMediaStory(storyId: StoryId(peerId: PeerId(namespace: Namespaces.Peer.CloudUser, id: PeerId.Id._internalFromInt64Value(userId)), id: id))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var instantPage: InstantPage?
|
||||
if let cachedPage = cachedPage {
|
||||
instantPage = InstantPage(apiPage: cachedPage)
|
||||
}
|
||||
return TelegramMediaWebpage(webpageId: MediaId(namespace: Namespaces.Media.CloudWebpage, id: id), content: .Loaded(TelegramMediaWebpageLoadedContent(url: url, displayUrl: displayUrl, hash: hash, type: type, websiteName: siteName, title: title, text: description, embedUrl: embedUrl, embedType: embedType, embedSize: embedSize, duration: webpageDuration, author: author, image: image, file: file, attributes: webpageAttributes, instantPage: instantPage)))
|
||||
return TelegramMediaWebpage(webpageId: MediaId(namespace: Namespaces.Media.CloudWebpage, id: id), content: .Loaded(TelegramMediaWebpageLoadedContent(url: url, displayUrl: displayUrl, hash: hash, type: type, websiteName: siteName, title: title, text: description, embedUrl: embedUrl, embedType: embedType, embedSize: embedSize, duration: webpageDuration, author: author, image: image, file: file, story: story, attributes: webpageAttributes, instantPage: instantPage)))
|
||||
case .webPageEmpty:
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1254,6 +1254,8 @@ public final class AccountViewTracker {
|
||||
for media in message.media {
|
||||
if let storyMedia = media as? TelegramMediaStory {
|
||||
result.insert(storyMedia.storyId)
|
||||
} else if let webpage = media as? TelegramMediaWebpage, case let .Loaded(content) = webpage.content, let story = content.story {
|
||||
result.insert(story.storyId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,10 +85,11 @@ public final class TelegramMediaWebpageLoadedContent: PostboxCoding, Equatable {
|
||||
|
||||
public let image: TelegramMediaImage?
|
||||
public let file: TelegramMediaFile?
|
||||
public let story: TelegramMediaStory?
|
||||
public let attributes: [TelegramMediaWebpageAttribute]
|
||||
public let instantPage: InstantPage?
|
||||
|
||||
public init(url: String, displayUrl: String, hash: Int32, type: String?, websiteName: String?, title: String?, text: String?, embedUrl: String?, embedType: String?, embedSize: PixelDimensions?, duration: Int?, author: String?, image: TelegramMediaImage?, file: TelegramMediaFile?, attributes: [TelegramMediaWebpageAttribute], instantPage: InstantPage?) {
|
||||
public init(url: String, displayUrl: String, hash: Int32, type: String?, websiteName: String?, title: String?, text: String?, embedUrl: String?, embedType: String?, embedSize: PixelDimensions?, duration: Int?, author: String?, image: TelegramMediaImage?, file: TelegramMediaFile?, story: TelegramMediaStory?, attributes: [TelegramMediaWebpageAttribute], instantPage: InstantPage?) {
|
||||
self.url = url
|
||||
self.displayUrl = displayUrl
|
||||
self.hash = hash
|
||||
@@ -103,6 +104,7 @@ public final class TelegramMediaWebpageLoadedContent: PostboxCoding, Equatable {
|
||||
self.author = author
|
||||
self.image = image
|
||||
self.file = file
|
||||
self.story = story
|
||||
self.attributes = attributes
|
||||
self.instantPage = instantPage
|
||||
}
|
||||
@@ -141,6 +143,12 @@ public final class TelegramMediaWebpageLoadedContent: PostboxCoding, Equatable {
|
||||
self.file = nil
|
||||
}
|
||||
|
||||
if let story = decoder.decodeObjectForKey("stry") as? TelegramMediaStory {
|
||||
self.story = story
|
||||
} else {
|
||||
self.story = nil
|
||||
}
|
||||
|
||||
var effectiveAttributes: [TelegramMediaWebpageAttribute] = []
|
||||
if let attributes = decoder.decodeObjectArrayWithDecoderForKey("attr") as [TelegramMediaWebpageAttribute]? {
|
||||
effectiveAttributes.append(contentsOf: attributes)
|
||||
@@ -218,6 +226,11 @@ public final class TelegramMediaWebpageLoadedContent: PostboxCoding, Equatable {
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "fi")
|
||||
}
|
||||
if let story = self.story {
|
||||
encoder.encodeObject(story, forKey: "stry")
|
||||
} else {
|
||||
encoder.encodeNil(forKey: "stry")
|
||||
}
|
||||
|
||||
encoder.encodeObjectArray(self.attributes, forKey: "attr")
|
||||
|
||||
@@ -261,6 +274,14 @@ public func ==(lhs: TelegramMediaWebpageLoadedContent, rhs: TelegramMediaWebpage
|
||||
return false
|
||||
}
|
||||
|
||||
if let lhsStory = lhs.story, let rhsStory = rhs.story {
|
||||
if !lhsStory.isEqual(to: rhsStory) {
|
||||
return false
|
||||
}
|
||||
} else if (lhs.story == nil) != (rhs.story == nil) {
|
||||
return false
|
||||
}
|
||||
|
||||
if lhs.attributes.count != rhs.attributes.count {
|
||||
return false
|
||||
} else {
|
||||
@@ -289,6 +310,14 @@ public final class TelegramMediaWebpage: Media, Equatable {
|
||||
}
|
||||
public let peerIds: [PeerId] = []
|
||||
|
||||
public var storyIds: [StoryId] {
|
||||
if case let .Loaded(content) = self.content, let story = content.story {
|
||||
return story.storyIds
|
||||
} else {
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
public let webpageId: MediaId
|
||||
public let content: TelegramMediaWebpageContent
|
||||
|
||||
|
||||
@@ -62,6 +62,36 @@ func _internal_togglePeerMuted(account: Account, peerId: PeerId, threadId: Int64
|
||||
}
|
||||
}
|
||||
|
||||
func _internal_togglePeerStoriesMuted(account: Account, peerId: PeerId) -> Signal<Void, NoError> {
|
||||
return account.postbox.transaction { transaction -> Void in
|
||||
guard let peer = transaction.getPeer(peerId) else {
|
||||
return
|
||||
}
|
||||
|
||||
var notificationPeerId = peerId
|
||||
if let associatedPeerId = peer.associatedPeerId {
|
||||
notificationPeerId = associatedPeerId
|
||||
}
|
||||
|
||||
let currentSettings = transaction.getPeerNotificationSettings(id: notificationPeerId) as? TelegramPeerNotificationSettings
|
||||
let previousSettings: TelegramPeerNotificationSettings
|
||||
if let currentSettings = currentSettings {
|
||||
previousSettings = currentSettings
|
||||
} else {
|
||||
previousSettings = TelegramPeerNotificationSettings.defaultSettings
|
||||
}
|
||||
|
||||
let updatedSettings: TelegramPeerNotificationSettings
|
||||
if let previousStoriesMuted = previousSettings.storiesMuted {
|
||||
updatedSettings = previousSettings.withUpdatedStoriesMuted(!previousStoriesMuted)
|
||||
} else {
|
||||
updatedSettings = previousSettings.withUpdatedStoriesMuted(true)
|
||||
}
|
||||
|
||||
transaction.updatePendingPeerNotificationSettings(peerId: notificationPeerId, settings: updatedSettings)
|
||||
}
|
||||
}
|
||||
|
||||
func _internal_updatePeerMuteSetting(account: Account, peerId: PeerId, threadId: Int64?, muteInterval: Int32?) -> Signal<Void, NoError> {
|
||||
return account.postbox.transaction { transaction -> Void in
|
||||
_internal_updatePeerMuteSetting(account: account, transaction: transaction, peerId: peerId, threadId: threadId, muteInterval: muteInterval)
|
||||
|
||||
@@ -246,6 +246,11 @@ public extension TelegramEngine {
|
||||
public func togglePeerMuted(peerId: PeerId, threadId: Int64?) -> Signal<Void, NoError> {
|
||||
return _internal_togglePeerMuted(account: self.account, peerId: peerId, threadId: threadId)
|
||||
}
|
||||
|
||||
public func togglePeerStoriesMuted(peerId: EnginePeer.Id) -> Signal<Never, NoError> {
|
||||
return _internal_togglePeerStoriesMuted(account: self.account, peerId: peerId)
|
||||
|> ignoreValues
|
||||
}
|
||||
|
||||
public func updatePeerMuteSetting(peerId: PeerId, threadId: Int64?, muteInterval: Int32?) -> Signal<Void, NoError> {
|
||||
return _internal_updatePeerMuteSetting(account: self.account, peerId: peerId, threadId: threadId, muteInterval: muteInterval)
|
||||
|
||||
@@ -88,7 +88,7 @@ public func actualizedWebpage(postbox: Postbox, network: Network, webpage: Teleg
|
||||
return updatedWebpage
|
||||
}
|
||||
} else if let result = result, case let .webPageNotModified(_, viewsValue) = result, let views = viewsValue, case let .Loaded(content) = webpage.content {
|
||||
let updatedContent: TelegramMediaWebpageContent = .Loaded(TelegramMediaWebpageLoadedContent(url: content.url, displayUrl: content.displayUrl, hash: content.hash, type: content.type, websiteName: content.websiteName, title: content.title, text: content.text, embedUrl: content.embedUrl, embedType: content.embedType, embedSize: content.embedSize, duration: content.duration, author: content.author, image: content.image, file: content.file, attributes: content.attributes, instantPage: content.instantPage.flatMap({ InstantPage(blocks: $0.blocks, media: $0.media, isComplete: $0.isComplete, rtl: $0.rtl, url: $0.url, views: views) })))
|
||||
let updatedContent: TelegramMediaWebpageContent = .Loaded(TelegramMediaWebpageLoadedContent(url: content.url, displayUrl: content.displayUrl, hash: content.hash, type: content.type, websiteName: content.websiteName, title: content.title, text: content.text, embedUrl: content.embedUrl, embedType: content.embedType, embedSize: content.embedSize, duration: content.duration, author: content.author, image: content.image, file: content.file, story: content.story, attributes: content.attributes, instantPage: content.instantPage.flatMap({ InstantPage(blocks: $0.blocks, media: $0.media, isComplete: $0.isComplete, rtl: $0.rtl, url: $0.url, views: views) })))
|
||||
let updatedWebpage = TelegramMediaWebpage(webpageId: webpage.webpageId, content: updatedContent)
|
||||
return postbox.transaction { transaction -> TelegramMediaWebpage in
|
||||
updateMessageMedia(transaction: transaction, id: webpage.webpageId, media: updatedWebpage)
|
||||
|
||||
Reference in New Issue
Block a user