[WIP] Topic APIs

This commit is contained in:
Ali
2022-09-28 12:15:06 +02:00
parent d7d3b1b9cb
commit d01a7853fa
60 changed files with 4264 additions and 1861 deletions

View File

@@ -243,7 +243,7 @@ public extension TelegramEngine.EngineData.Item {
guard let view = view as? ChatListIndexView else {
preconditionFailure()
}
return view.chatListIndex
return view.chatListIndex.flatMap(EngineChatList.Item.Index.chatList)
}
}

View File

@@ -1,6 +1,6 @@
import Postbox
public final class EngineChatList {
public final class EngineChatList: Equatable {
public enum Group {
case root
case archive
@@ -17,7 +17,7 @@ public final class EngineChatList {
case earlier(than: EngineChatList.Item.Index?)
}
public struct Draft {
public struct Draft: Equatable {
public var text: String
public var entities: [MessageTextEntity]
@@ -27,14 +27,52 @@ public final class EngineChatList {
}
}
public final class Item {
public typealias Index = ChatListIndex
public final class Item: Equatable {
public enum Id: Hashable {
case chatList(EnginePeer.Id)
case forum(Int64)
}
public enum Index: Equatable, Comparable {
public typealias ChatList = ChatListIndex
case chatList(ChatListIndex)
case forum(timestamp: Int32, threadId: Int64, namespace: EngineMessage.Id.Namespace, id: EngineMessage.Id.Id)
public static func <(lhs: Index, rhs: Index) -> Bool {
switch lhs {
case let .chatList(lhsIndex):
if case let .chatList(rhsIndex) = rhs {
return lhsIndex < rhsIndex
} else {
return true
}
case let .forum(lhsTimestamp, lhsThreadId, lhsNamespace, lhsId):
if case let .forum(rhsTimestamp, rhsThreadId, rhsNamespace, rhsId) = rhs {
if lhsTimestamp != rhsTimestamp {
return lhsTimestamp < rhsTimestamp
}
if lhsThreadId != rhsThreadId {
return lhsThreadId < rhsThreadId
}
if lhsNamespace != rhsNamespace {
return lhsNamespace < rhsNamespace
}
return lhsId < rhsId
} else {
return false
}
}
}
}
public let id: Id
public let index: Index
public let messages: [EngineMessage]
public let readCounters: EnginePeerReadCounters?
public let isMuted: Bool
public let draft: Draft?
public let threadInfo: EngineMessageHistoryThreads.Info?
public let renderedPeer: EngineRenderedPeer
public let presence: EnginePeer.Presence?
public let hasUnseenMentions: Bool
@@ -43,11 +81,13 @@ public final class EngineChatList {
public let isContact: Bool
public init(
id: Id,
index: Index,
messages: [EngineMessage],
readCounters: EnginePeerReadCounters?,
isMuted: Bool,
draft: Draft?,
threadInfo: EngineMessageHistoryThreads.Info?,
renderedPeer: EngineRenderedPeer,
presence: EnginePeer.Presence?,
hasUnseenMentions: Bool,
@@ -55,11 +95,13 @@ public final class EngineChatList {
hasFailed: Bool,
isContact: Bool
) {
self.id = id
self.index = index
self.messages = messages
self.readCounters = readCounters
self.isMuted = isMuted
self.draft = draft
self.threadInfo = threadInfo
self.renderedPeer = renderedPeer
self.presence = presence
self.hasUnseenMentions = hasUnseenMentions
@@ -67,6 +109,49 @@ public final class EngineChatList {
self.hasFailed = hasFailed
self.isContact = isContact
}
public static func ==(lhs: Item, rhs: Item) -> Bool {
if lhs.id != rhs.id {
return false
}
if lhs.index != rhs.index {
return false
}
if lhs.messages != rhs.messages {
return false
}
if lhs.readCounters != rhs.readCounters {
return false
}
if lhs.isMuted != rhs.isMuted {
return false
}
if lhs.draft != rhs.draft {
return false
}
if lhs.threadInfo != rhs.threadInfo {
return false
}
if lhs.renderedPeer != rhs.renderedPeer {
return false
}
if lhs.presence != rhs.presence {
return false
}
if lhs.hasUnseenMentions != rhs.hasUnseenMentions {
return false
}
if lhs.hasUnseenReactions != rhs.hasUnseenReactions {
return false
}
if lhs.hasFailed != rhs.hasFailed {
return false
}
if lhs.isContact != rhs.isContact {
return false
}
return true
}
}
public final class GroupItem: Equatable {
@@ -127,9 +212,9 @@ public final class EngineChatList {
}
}
public final class AdditionalItem {
public final class PromoInfo {
public enum Content {
public final class AdditionalItem: Equatable {
public final class PromoInfo: Equatable {
public enum Content: Equatable {
case proxy
case psa(type: String, message: String?)
}
@@ -139,6 +224,14 @@ public final class EngineChatList {
public init(content: Content) {
self.content = content
}
public static func ==(lhs: PromoInfo, rhs: PromoInfo) -> Bool {
if lhs.content != rhs.content {
return false
}
return true
}
}
public let item: Item
@@ -148,6 +241,17 @@ public final class EngineChatList {
self.item = item
self.promoInfo = promoInfo
}
public static func ==(lhs: AdditionalItem, rhs: AdditionalItem) -> Bool {
if lhs.item != rhs.item {
return false
}
if lhs.promoInfo != rhs.promoInfo {
return false
}
return true
}
}
public let items: [Item]
@@ -157,7 +261,7 @@ public final class EngineChatList {
public let hasLater: Bool
public let isLoading: Bool
init(
public init(
items: [Item],
groupItems: [GroupItem],
additionalItems: [AdditionalItem],
@@ -172,6 +276,29 @@ public final class EngineChatList {
self.hasLater = hasLater
self.isLoading = isLoading
}
public static func ==(lhs: EngineChatList, rhs: EngineChatList) -> Bool {
if lhs.items != rhs.items {
return false
}
if lhs.groupItems != rhs.groupItems {
return false
}
if lhs.additionalItems != rhs.additionalItems {
return false
}
if lhs.hasEarlier != rhs.hasEarlier {
return false
}
if lhs.hasLater != rhs.hasLater {
return false
}
if lhs.isLoading != rhs.isLoading {
return false
}
return true
}
}
public extension EngineChatList.Group {
@@ -199,17 +326,23 @@ public extension EngineChatList.RelativePosition {
init(_ position: ChatListRelativePosition) {
switch position {
case let .earlier(than):
self = .earlier(than: than)
self = .earlier(than: than.flatMap(EngineChatList.Item.Index.chatList))
case let .later(than):
self = .later(than: than)
self = .later(than: than.flatMap(EngineChatList.Item.Index.chatList))
}
}
func _asPosition() -> ChatListRelativePosition {
func _asPosition() -> ChatListRelativePosition? {
switch self {
case let .earlier(than):
guard case let .chatList(than) = than else {
return nil
}
return .earlier(than: than)
case let .later(than):
guard case let .chatList(than) = than else {
return nil
}
return .later(than: than)
}
}
@@ -245,11 +378,13 @@ extension EngineChatList.Item {
}
self.init(
index: index,
id: .chatList(index.messageIndex.id.peerId),
index: .chatList(index),
messages: messages.map(EngineMessage.init),
readCounters: readState.flatMap(EnginePeerReadCounters.init),
isMuted: isRemovedFromTotalUnreadCount,
draft: draft,
threadInfo: nil,
renderedPeer: EngineRenderedPeer(renderedPeer),
presence: presence.flatMap(EnginePeer.Presence.init),
hasUnseenMentions: hasUnseenMentions,

View File

@@ -1,6 +1,6 @@
import Postbox
public final class EngineMessage {
public final class EngineMessage: Equatable {
public typealias Id = MessageId
public typealias Index = MessageIndex
public typealias Tags = MessageTags
@@ -148,4 +148,50 @@ public final class EngineMessage {
public func _asMessage() -> Message {
return self.impl
}
public static func ==(lhs: EngineMessage, rhs: EngineMessage) -> Bool {
if lhs.id != rhs.id {
return false
}
if lhs.globallyUniqueId != rhs.globallyUniqueId {
return false
}
if lhs.groupingKey != rhs.groupingKey {
return false
}
if lhs.groupInfo != rhs.groupInfo {
return false
}
if lhs.threadId != rhs.threadId {
return false
}
if lhs.timestamp != rhs.timestamp {
return false
}
if lhs.flags != rhs.flags {
return false
}
if lhs.tags != rhs.tags {
return false
}
if lhs.globalTags != rhs.globalTags {
return false
}
if lhs.localTags != rhs.localTags {
return false
}
if lhs.forwardInfo != rhs.forwardInfo {
return false
}
if lhs.author != rhs.author {
return false
}
if lhs.text != rhs.text {
return false
}
if !areMediaArraysEqual(lhs.media, rhs.media) {
return false
}
return true
}
}

View File

@@ -440,8 +440,11 @@ public extension TelegramEngine {
}
public func getRelativeUnreadChatListIndex(filtered: Bool, position: EngineChatList.RelativePosition, groupId: EngineChatList.Group) -> Signal<EngineChatList.Item.Index?, NoError> {
guard let position = position._asPosition() else {
return .single(nil)
}
return self.account.postbox.transaction { transaction -> EngineChatList.Item.Index? in
return transaction.getRelativeUnreadChatListIndex(filtered: filtered, position: position._asPosition(), groupId: groupId._asGroup())
return transaction.getRelativeUnreadChatListIndex(filtered: filtered, position: position, groupId: groupId._asGroup()).flatMap(EngineChatList.Item.Index.chatList)
}
}

View File

@@ -61,6 +61,17 @@ private func createChannel(account: Account, title: String, description: String?
}
|> castError(CreateChannelError.self)
|> timeout(5.0, queue: Queue.concurrentDefaultQueue(), alternate: .fail(.generic))
|> mapToSignal { peerId -> Signal<PeerId, CreateChannelError> in
if title.contains("*forum") {
return _internal_setChannelForumMode(account: account, peerId: peerId, isForum: true)
|> castError(CreateChannelError.self)
|> map { _ -> PeerId in
}
|> then(.single(peerId))
} else {
return .single(peerId)
}
}
} else {
return .fail(.generic)
}

View File

@@ -776,6 +776,10 @@ public extension TelegramEngine {
}
|> ignoreValues
}
public func setChannelForumMode(id: EnginePeer.Id, isForum: Bool) -> Signal<Never, NoError> {
return _internal_setChannelForumMode(account: self.account, peerId: id, isForum: isForum)
}
}
}