-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathChatContextQuery.swift
241 lines (211 loc) · 9 KB
/
ChatContextQuery.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
import Foundation
import SwiftSignalKit
import TextFormat
import TelegramCore
import AccountContext
public struct PossibleContextQueryTypes: OptionSet {
public var rawValue: Int32
public init() {
self.rawValue = 0
}
public init(rawValue: Int32) {
self.rawValue = rawValue
}
public static let emoji = PossibleContextQueryTypes(rawValue: (1 << 0))
public static let hashtag = PossibleContextQueryTypes(rawValue: (1 << 1))
public static let mention = PossibleContextQueryTypes(rawValue: (1 << 2))
public static let command = PossibleContextQueryTypes(rawValue: (1 << 3))
public static let contextRequest = PossibleContextQueryTypes(rawValue: (1 << 4))
public static let emojiSearch = PossibleContextQueryTypes(rawValue: (1 << 5))
}
private func scalarCanPrependQueryControl(_ c: UnicodeScalar?) -> Bool {
if let c = c {
if c == " " || c == "\n" || c == "." || c == "," {
return true
}
return false
} else {
return true
}
}
private func makeScalar(_ c: Character) -> Character {
return c
}
private let spaceScalar = " " as UnicodeScalar
private let newlineScalar = "\n" as UnicodeScalar
private let hashScalar = "#" as UnicodeScalar
private let atScalar = "@" as UnicodeScalar
private let slashScalar = "/" as UnicodeScalar
private let colonScalar = ":" as UnicodeScalar
private let alphanumerics = CharacterSet.alphanumerics
public func textInputStateContextQueryRangeAndType(_ inputState: ChatTextInputState) -> [(NSRange, PossibleContextQueryTypes, NSRange?)] {
return textInputStateContextQueryRangeAndType(inputText: inputState.inputText, selectionRange: inputState.selectionRange)
}
public func textInputStateContextQueryRangeAndType(inputText: NSAttributedString, selectionRange: Range<Int>) -> [(NSRange, PossibleContextQueryTypes, NSRange?)] {
if selectionRange.count != 0 {
return []
}
let inputString: NSString = inputText.string as NSString
var results: [(NSRange, PossibleContextQueryTypes, NSRange?)] = []
let inputLength = inputString.length
if inputLength != 0 {
if inputString.hasPrefix("@") && inputLength != 1 {
let startIndex = 1
var index = startIndex
var contextAddressRange: NSRange?
while true {
if index == inputLength {
break
}
if let c = UnicodeScalar(inputString.character(at: index)) {
if c == " " {
if index != startIndex {
contextAddressRange = NSRange(location: startIndex, length: index - startIndex)
index += 1
}
break
} else {
if !((c >= "a" && c <= "z") || (c >= "A" && c <= "Z") || (c >= "0" && c <= "9") || c == "_") {
break
}
}
if index == inputLength {
break
} else {
index += 1
}
} else {
index += 1
}
}
if let contextAddressRange = contextAddressRange {
results.append((contextAddressRange, [.contextRequest], NSRange(location: index, length: inputLength - index)))
}
}
let maxIndex = min(selectionRange.lowerBound, inputLength)
if maxIndex == 0 {
return results
}
var index = maxIndex - 1
var possibleQueryRange: NSRange?
let string = (inputString as String)
let trimmedString = string.trimmingTrailingSpaces()
if string.count < 3, trimmedString.isSingleEmoji {
if inputText.attribute(ChatTextInputAttributes.customEmoji, at: 0, effectiveRange: nil) == nil {
return [(NSRange(location: 0, length: inputString.length - (string.count - trimmedString.count)), [.emoji], nil)]
}
} else {
/*let activeString = inputText.attributedSubstring(from: NSRange(location: 0, length: inputState.selectionRange.upperBound))
if let lastCharacter = activeString.string.last, String(lastCharacter).isSingleEmoji {
let matchLength = (String(lastCharacter) as NSString).length
if activeString.attribute(ChatTextInputAttributes.customEmoji, at: activeString.length - matchLength, effectiveRange: nil) == nil {
return [(NSRange(location: inputState.selectionRange.upperBound - matchLength, length: matchLength), [.emojiSearch], nil)]
}
}*/
}
var possibleTypes = PossibleContextQueryTypes([.command, .mention, .hashtag, .emojiSearch])
var definedType = false
while true {
var previousC: UnicodeScalar?
if index != 0 {
previousC = UnicodeScalar(inputString.character(at: index - 1))
}
if let c = UnicodeScalar(inputString.character(at: index)) {
if c == spaceScalar || c == newlineScalar {
possibleTypes = []
} else if c == hashScalar {
if scalarCanPrependQueryControl(previousC) {
possibleTypes = possibleTypes.intersection([.hashtag])
definedType = true
index += 1
possibleQueryRange = NSRange(location: index, length: maxIndex - index)
}
break
} else if c == atScalar {
if scalarCanPrependQueryControl(previousC) {
possibleTypes = possibleTypes.intersection([.mention])
definedType = true
index += 1
possibleQueryRange = NSRange(location: index, length: maxIndex - index)
}
break
} else if c == slashScalar {
if scalarCanPrependQueryControl(previousC) {
possibleTypes = possibleTypes.intersection([.command])
definedType = true
index += 1
possibleQueryRange = NSRange(location: index, length: maxIndex - index)
}
break
} else if c == colonScalar {
if scalarCanPrependQueryControl(previousC) {
possibleTypes = possibleTypes.intersection([.emojiSearch])
definedType = true
index += 1
possibleQueryRange = NSRange(location: index, length: maxIndex - index)
}
break
}
}
if index == 0 {
break
} else {
index -= 1
possibleQueryRange = NSRange(location: index, length: maxIndex - index)
}
}
if let possibleQueryRange = possibleQueryRange, definedType && !possibleTypes.isEmpty {
results.append((possibleQueryRange, possibleTypes, nil))
}
}
return results
}
public enum ChatPresentationInputQueryKind: Int32 {
case emoji
case hashtag
case mention
case command
case contextRequest
case emojiSearch
}
public struct ChatInputQueryMentionTypes: OptionSet, Hashable {
public var rawValue: Int32
public init(rawValue: Int32) {
self.rawValue = rawValue
}
public static let contextBots = ChatInputQueryMentionTypes(rawValue: 1 << 0)
public static let members = ChatInputQueryMentionTypes(rawValue: 1 << 1)
public static let accountPeer = ChatInputQueryMentionTypes(rawValue: 1 << 2)
}
public enum ChatPresentationInputQuery: Hashable, Equatable {
case emoji(String)
case hashtag(String)
case mention(query: String, types: ChatInputQueryMentionTypes)
case command(String)
case emojiSearch(query: String, languageCode: String, range: NSRange)
case contextRequest(addressName: String, query: String)
public var kind: ChatPresentationInputQueryKind {
switch self {
case .emoji:
return .emoji
case .hashtag:
return .hashtag
case .mention:
return .mention
case .command:
return .command
case .contextRequest:
return .contextRequest
case .emojiSearch:
return .emojiSearch
}
}
}
public enum ChatContextQueryError {
case generic
case inlineBotLocationRequest(EnginePeer.Id)
}
public enum ChatContextQueryUpdate {
case remove
case update(ChatPresentationInputQuery, Signal<(ChatPresentationInputQueryResult?) -> ChatPresentationInputQueryResult?, ChatContextQueryError>)
}