Chat.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. //
  2. // Chat.swift
  3. // Qmera
  4. //
  5. // Created by Yayan Dwi on 14/10/21.
  6. //
  7. import Foundation
  8. public class Chat: Model {
  9. public let fpin: String
  10. public let pin: String
  11. public let messageId: String
  12. public var counter: String
  13. public var messageText: String
  14. public let serverDate: String
  15. public let image: String
  16. public let video: String
  17. public let file: String
  18. public let attachmentFlag: String
  19. public let messageScope: String
  20. public let name: String
  21. public let profile: String
  22. public let official: String
  23. public let status: String
  24. public let credential: String
  25. public var lock: String
  26. public let thumb: String
  27. public let audio: String
  28. public let gif: String
  29. public let groupId: String
  30. public let groupName: String
  31. public var isSelected: Bool
  32. public var isParent: Bool
  33. public var pinned: Int64
  34. public var isBot: Int
  35. public var isFolPinned: Bool
  36. public init(pin: String) {
  37. self.fpin = ""
  38. self.pin = pin
  39. self.messageId = ""
  40. self.counter = ""
  41. self.messageText = ""
  42. self.serverDate = ""
  43. self.image = ""
  44. self.video = ""
  45. self.file = ""
  46. self.attachmentFlag = ""
  47. self.messageScope = ""
  48. self.name = ""
  49. self.profile = ""
  50. self.official = ""
  51. self.status = ""
  52. self.credential = ""
  53. self.lock = ""
  54. self.thumb = ""
  55. self.audio = ""
  56. self.gif = ""
  57. self.groupId = ""
  58. self.groupName = ""
  59. self.isSelected = false
  60. self.isParent = false
  61. self.pinned = 0
  62. self.isBot = 0
  63. self.isFolPinned = false
  64. }
  65. public init(profile: String, groupName: String, counter: String, groupId: String) {
  66. self.fpin = ""
  67. self.pin = ""
  68. self.messageId = ""
  69. self.counter = counter
  70. self.messageText = ""
  71. self.serverDate = ""
  72. self.image = ""
  73. self.video = ""
  74. self.file = ""
  75. self.attachmentFlag = ""
  76. self.messageScope = ""
  77. self.name = ""
  78. self.profile = profile
  79. self.official = ""
  80. self.status = ""
  81. self.credential = ""
  82. self.lock = ""
  83. self.thumb = ""
  84. self.audio = ""
  85. self.gif = ""
  86. self.groupId = groupId
  87. self.groupName = groupName
  88. self.isSelected = false
  89. self.isParent = false
  90. self.pinned = 0
  91. self.isBot = 0
  92. self.isFolPinned = false
  93. }
  94. public init(fpin:String, pin: String, messageId: String, counter: String, messageText: String, serverDate: String, image: String, video: String, file: String, attachmentFlag: String, messageScope: String, name: String, profile: String, official: String, status: String, credential: String, lock: String, thumb: String = "", audio: String = "", gif: String = "", groupId: String = "", groupName: String = "", isSelected: Bool = false, isParent: Bool = false, pinned: Int64 = 0, isBot: Int = 0, isFolPinned: Bool = false) {
  95. self.fpin = fpin
  96. self.pin = pin
  97. self.messageId = messageId
  98. self.counter = counter
  99. self.messageText = messageText
  100. self.serverDate = serverDate
  101. self.image = image
  102. self.video = video
  103. self.file = file
  104. self.attachmentFlag = attachmentFlag
  105. self.messageScope = messageScope
  106. self.name = name
  107. self.profile = profile
  108. self.official = official
  109. self.status = status
  110. self.credential = credential
  111. self.lock = lock
  112. self.thumb = thumb
  113. self.audio = audio
  114. self.gif = gif
  115. self.groupId = groupId
  116. self.groupName = groupName
  117. self.isSelected = isSelected
  118. self.isParent = isParent
  119. self.pinned = pinned
  120. self.isBot = isBot
  121. self.isFolPinned = isFolPinned
  122. }
  123. public static func == (lhs: Chat, rhs: Chat) -> Bool {
  124. return lhs.pin == rhs.pin
  125. }
  126. public func copy() -> Chat {
  127. return Chat(fpin: self.fpin, pin: self.pin, messageId: self.messageId, counter: self.counter, messageText: self.messageText, serverDate: self.serverDate, image: self.image, video: self.video, file: self.file, attachmentFlag: self.attachmentFlag, messageScope: self.messageScope, name: self.name, profile: self.profile, official: self.official, status: self.status, credential: self.credential, lock: self.lock, thumb: self.thumb, audio: self.audio, gif: self.gif, groupId: self.groupId, groupName: self.groupName, isSelected: self.isSelected, isParent: self.isParent)
  128. }
  129. public var description: String {
  130. return ""
  131. }
  132. public static func getCountSearchMessage(key: String, pin: String, chatId:String = "", isPersonal: Bool) -> Int {
  133. var query = ""
  134. var count = 0
  135. if isPersonal {
  136. query = "select message_id FROM MESSAGE where message_text LIKE '%\(key)%' and (l_pin = '\(pin)' or f_pin = '\(pin)')"
  137. } else {
  138. query = "select message_id FROM MESSAGE where message_text LIKE '%\(key)%' and l_pin = '\(pin)' and chat_id = '\(chatId)'"
  139. }
  140. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  141. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  142. while cursorData.next() {
  143. count+=1
  144. }
  145. }
  146. })
  147. return count
  148. }
  149. public static func getMessageFromSearch(text: String = "") -> [Chat] {
  150. var messages: [Chat] = []
  151. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  152. do {
  153. let query = "select m.f_pin, m.l_pin, m.message_id, ms.counter, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, b.first_name || ' ' || ifnull(b.last_name, '') name, b.image_id profile, b.official_account, m.status, m.credential, m.lock, m.audio_id, m.gif_id, '' group_id, '' group_name from MESSAGE m, BUDDY b where m.l_pin = b.f_pin and m.is_call_center = 0 and message_text LIKE '%\(text)%'"
  154. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  155. while cursorData.next() {
  156. let data = cursorData.string(forColumnIndex: 0) ?? ""
  157. let chat = Chat(fpin: cursorData.string(forColumnIndex: 0) ?? "",
  158. pin: cursorData.string(forColumnIndex: 1) ?? "",
  159. messageId: cursorData.string(forColumnIndex: 2) ?? "",
  160. counter: cursorData.string(forColumnIndex: 3) ?? "",
  161. messageText: cursorData.string(forColumnIndex: 4) ?? "",
  162. serverDate: cursorData.string(forColumnIndex: 5) ?? "",
  163. image: cursorData.string(forColumnIndex: 6) ?? "",
  164. video: cursorData.string(forColumnIndex: 7) ?? "",
  165. file: cursorData.string(forColumnIndex: 8) ?? "",
  166. attachmentFlag: cursorData.string(forColumnIndex: 9) ?? "",
  167. messageScope: cursorData.string(forColumnIndex: 10) ?? "",
  168. name: cursorData.string(forColumnIndex: 11) ?? "",
  169. profile: cursorData.string(forColumnIndex: 12) ?? "",
  170. official: cursorData.string(forColumnIndex: 13) ?? "",
  171. status: cursorData.string(forColumnIndex: 14) ?? "",
  172. credential: cursorData.string(forColumnIndex: 15) ?? "",
  173. lock: cursorData.string(forColumnIndex: 16) ?? "",
  174. thumb: "",
  175. audio: cursorData.string(forColumnIndex: 17) ?? "",
  176. gif: cursorData.string(forColumnIndex: 18) ?? "",
  177. groupId: cursorData.string(forColumnIndex: 19) ?? "",
  178. groupName: cursorData.string(forColumnIndex: 20) ?? "")
  179. messages.append(chat)
  180. }
  181. cursorData.close()
  182. }
  183. } catch {
  184. rollback.pointee = true
  185. print("Access database error: \(error.localizedDescription)")
  186. }
  187. })
  188. return messages
  189. }
  190. public static func getMessageFromId(message_id: String = "") -> [Chat] {
  191. var messages: [Chat] = []
  192. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  193. do {
  194. let query = """
  195. select m.f_pin, m.l_pin, m.message_id, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, b.first_name || ' ' || ifnull(b.last_name, '') name, b.image_id profile, b.official_account, m.status, m.credential, m.lock, m.audio_id, m.gif_id, '' group_id, '' group_name, m.thumb_id from MESSAGE m, BUDDY b where (m.l_pin = b.f_pin OR m.f_pin = b.f_pin) and m.attachment_flag = '3' and m.message_id = '\(message_id)' and m.is_call_center = 0
  196. union
  197. select m.f_pin, m.l_pin, m.message_id, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, 'Bot' name, '' profile, '', m.status, m.credential, m.lock, m.audio_id, m.gif_id, '' group_id, '' group_name, m.thumb_id from MESSAGE m where m.f_pin = '-999' and m.message_id = '\(message_id)' and m.is_call_center = 0
  198. union
  199. select m.f_pin, m.l_pin, m.message_id, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, 'GPT SmartBot' name, '' profile, '', m.status, m.credential, m.lock, m.audio_id, m.gif_id, '' group_id, '' group_name, m.thumb_id from MESSAGE m where m.f_pin = '-997' and m.message_id = '\(message_id)' and m.is_call_center = 0
  200. union
  201. select m.f_pin, m.l_pin, m.message_id, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, '\("Lounge".localized())' name, b.image_id profile, b.official, m.status, m.credential, m.lock, m.audio_id, m.gif_id, b.group_id, b.f_name group_name, m.thumb_id from MESSAGE m, GROUPZ b where m.l_pin = b.group_id and m.message_id = '\(message_id)' and m.is_call_center = 0
  202. union
  203. select m.f_pin, m.l_pin, m.message_id, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, b.title, c.image_id profile, '', m.status, m.credential, m.lock, m.audio_id, m.gif_id, c.group_id, c.f_name group_name, m.thumb_id from MESSAGE m, DISCUSSION_FORUM b, GROUPZ c where b.group_id = c.group_id and m.l_pin = b.chat_id and m.message_id = '\(message_id)' and m.is_call_center = 0
  204. order by 6 desc
  205. """
  206. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  207. while cursorData.next() {
  208. let chat = Chat(fpin: cursorData.string(forColumnIndex: 0) ?? "",
  209. pin: cursorData.string(forColumnIndex: 1) ?? "",
  210. messageId: cursorData.string(forColumnIndex: 2) ?? "",
  211. counter: "0",
  212. messageText: cursorData.string(forColumnIndex: 3) ?? "",
  213. serverDate: cursorData.string(forColumnIndex: 4) ?? "",
  214. image: cursorData.string(forColumnIndex: 5) ?? "",
  215. video: cursorData.string(forColumnIndex: 6) ?? "",
  216. file: cursorData.string(forColumnIndex: 7) ?? "",
  217. attachmentFlag: cursorData.string(forColumnIndex: 8) ?? "",
  218. messageScope: cursorData.string(forColumnIndex: 9) ?? "",
  219. name: cursorData.string(forColumnIndex: 10) ?? "",
  220. profile: cursorData.string(forColumnIndex: 11) ?? "",
  221. official: cursorData.string(forColumnIndex: 12) ?? "",
  222. status: cursorData.string(forColumnIndex: 13) ?? "",
  223. credential: cursorData.string(forColumnIndex: 14) ?? "",
  224. lock: cursorData.string(forColumnIndex: 15) ?? "",
  225. thumb: cursorData.string(forColumnIndex: 20) ?? "",
  226. audio: cursorData.string(forColumnIndex: 16) ?? "",
  227. gif: cursorData.string(forColumnIndex: 17) ?? "",
  228. groupId: cursorData.string(forColumnIndex: 18) ?? "",
  229. groupName: cursorData.string(forColumnIndex: 19) ?? "")
  230. messages.append(chat)
  231. }
  232. cursorData.close()
  233. }
  234. } catch {
  235. rollback.pointee = true
  236. print("Access database error: \(error.localizedDescription)")
  237. }
  238. })
  239. return messages
  240. }
  241. // public static func getUcList() {
  242. // Database.shared.database?.inTransaction({ (fmdb, rollback) in
  243. // let query = " select ms.message_id from MESSAGE_SUMMARY ms"
  244. // if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  245. // while cursorData.next() {
  246. // print("HMMKAMPRET2 \(cursorData.string(forColumnIndex: 0))")
  247. // }
  248. // cursorData.close()
  249. // }
  250. // })
  251. // }
  252. public static func getData(isImage: Bool = false, isDoc: Bool = false, isVideo: Bool = false, isGIF: Bool = false, isLink: Bool = false, isAudio: Bool = false, isArchived: Bool = false) -> [Chat] {
  253. var chats: [Chat] = []
  254. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  255. do {
  256. var lastQuery = ""
  257. if isImage {
  258. lastQuery = "m.image_id IS NOT NULL AND m.image_id != ''"
  259. } else if isDoc {
  260. lastQuery = "m.file_id IS NOT NULL AND m.file_id != ''"
  261. } else if isVideo {
  262. lastQuery = "m.video_id IS NOT NULL AND m.video_id != ''"
  263. } else if isGIF {
  264. lastQuery = "m.gif_id IS NOT NULL AND m.gif_id != ''"
  265. } else if isLink {
  266. lastQuery = "m.message_text IS NOT NULL AND m.message_text != '' AND (m.message_text LIKE '%https://%' OR m.message_text LIKE '%www.%')"
  267. } else if isAudio {
  268. lastQuery = "m.audio_id IS NOT NULL AND m.audio_id != ''"
  269. }
  270. var query = """
  271. select m.f_pin, ms.l_pin, ms.message_id, ms.counter, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, b.first_name || ' ' || ifnull(b.last_name, '') name, b.image_id profile, b.official_account, m.status, m.credential, m.lock, m.audio_id, m.gif_id, '' group_id, '' group_name, ms.pinned, m.is_bot from MESSAGE_SUMMARY ms, MESSAGE m, BUDDY b where ms.l_pin = b.f_pin and ms.message_id = m.message_id and m.is_call_center = 0 \(isArchived ? "and ms.archived <> 0" : "and ms.archived = 0")
  272. union
  273. select m.f_pin, ms.l_pin, ms.message_id, ms.counter, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, 'Bot' name, '' profile, '', m.status, m.credential, m.lock, m.audio_id, m.gif_id, '' group_id, '' group_name, ms.pinned, m.is_bot from MESSAGE_SUMMARY ms, MESSAGE m where ms.l_pin = '-999' and ms.message_id = m.message_id \(isArchived ? "and ms.archived <> 0" : "and ms.archived = 0")
  274. union
  275. select m.f_pin, ms.l_pin, ms.message_id, ms.counter, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, 'GPT SmartBot' name, '' profile, '', m.status, m.credential, m.lock, m.audio_id, m.gif_id, '' group_id, '' group_name, ms.pinned, m.is_bot from MESSAGE_SUMMARY ms, MESSAGE m where ms.l_pin = '-997' and ms.message_id = m.message_id \(isArchived ? "and ms.archived <> 0" : "and ms.archived = 0")
  276. union
  277. select m.f_pin, ms.l_pin, ms.message_id, ms.counter, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, '\("Lounge".localized())' name, b.image_id profile, b.official, m.status, m.credential, m.lock, m.audio_id, m.gif_id, b.group_id, b.f_name group_name, ms.pinned, m.is_bot from MESSAGE_SUMMARY ms, MESSAGE m, GROUPZ b where ms.l_pin = b.group_id and ms.message_id = m.message_id and m.is_call_center = 0 \(isArchived ? "and ms.archived <> 0" : "and ms.archived = 0")
  278. union
  279. select m.f_pin, ms.l_pin, ms.message_id, ms.counter, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, b.title, c.image_id profile, '', m.status, m.credential, m.lock, m.audio_id, m.gif_id, c.group_id, c.f_name group_name, ms.pinned, m.is_bot from MESSAGE_SUMMARY ms, MESSAGE m, DISCUSSION_FORUM b, GROUPZ c where b.group_id = c.group_id and ms.l_pin = b.chat_id and ms.message_id = m.message_id and m.is_call_center = 0 \(isArchived ? "and ms.archived <> 0" : "and ms.archived = 0")
  280. order by 6 desc
  281. """
  282. if !lastQuery.isEmpty {
  283. query = "select m.f_pin, m.opposite_pin, m.message_id, m.thumb_id, m.message_text, m.server_date, m.image_id, m.video_id, m.file_id, m.attachment_flag, m.message_scope_id, b.first_name || ' ' || ifnull(b.last_name, '') name, b.image_id profile, b.official_account, m.credential, m.lock, m.audio_id, m.gif_id from MESSAGE m JOIN BUDDY b ON m.f_pin = b.f_pin where \(lastQuery) order by 6 desc"
  284. }
  285. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  286. while cursorData.next() {
  287. // if !lastQuery.isEmpty {
  288. // for columnIndex in 0..<cursorData.columnCount {
  289. // if let columnName = cursorData.columnName(for: columnIndex) {
  290. // if let value = cursorData.object(forColumn: columnName) {
  291. // print("\(columnName): \(value)")
  292. // } else {
  293. // print("\(columnName): nil")
  294. // }
  295. // }
  296. // }
  297. // print("---------------------")
  298. // }
  299. let chat = Chat(fpin: cursorData.string(forColumnIndex: 0) ?? "",
  300. pin: cursorData.string(forColumnIndex: 1) ?? "",
  301. messageId: cursorData.string(forColumnIndex: 2) ?? "",
  302. counter: !lastQuery.isEmpty ? "0" : cursorData.string(forColumnIndex: 3) ?? "",
  303. messageText: cursorData.string(forColumnIndex: 4) ?? "",
  304. serverDate: cursorData.string(forColumnIndex: 5) ?? "",
  305. image: cursorData.string(forColumnIndex: 6) ?? "",
  306. video: cursorData.string(forColumnIndex: 7) ?? "",
  307. file: cursorData.string(forColumnIndex: 8) ?? "",
  308. attachmentFlag: cursorData.string(forColumnIndex: 9) ?? "",
  309. messageScope: cursorData.string(forColumnIndex: 10) ?? "",
  310. name: cursorData.string(forColumnIndex: 11) ?? "",
  311. profile: cursorData.string(forColumnIndex: 12) ?? "",
  312. official: cursorData.string(forColumnIndex: 13) ?? "",
  313. status: cursorData.string(forColumnIndex: 14) ?? "",
  314. credential: !lastQuery.isEmpty ? cursorData.string(forColumnIndex: 11) ?? "" : cursorData.string(forColumnIndex: 15) ?? "",
  315. lock: !lastQuery.isEmpty ? cursorData.string(forColumnIndex: 12) ?? "" : cursorData.string(forColumnIndex: 16) ?? "",
  316. thumb: !lastQuery.isEmpty ? cursorData.string(forColumnIndex: 3) ?? "" : "",
  317. audio: !lastQuery.isEmpty ? cursorData.string(forColumnIndex: 13) ?? "" : cursorData.string(forColumnIndex: 17) ?? "",
  318. gif: !lastQuery.isEmpty ? cursorData.string(forColumnIndex: 17) ?? "" : cursorData.string(forColumnIndex: 18) ?? "",
  319. groupId: cursorData.string(forColumnIndex: 19) ?? "",
  320. groupName: cursorData.string(forColumnIndex: 20) ?? "",
  321. pinned: cursorData.longLongInt(forColumnIndex: 21),
  322. isBot: Int(cursorData.string(forColumnIndex: 22) ?? "0") ?? 0)
  323. chats.append(chat)
  324. }
  325. cursorData.close()
  326. }
  327. } catch {
  328. rollback.pointee = true
  329. print("Access database error: \(error.localizedDescription)")
  330. }
  331. })
  332. return chats
  333. }
  334. }