Chat.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 let 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 init(pin: String) {
  29. self.fpin = ""
  30. self.pin = pin
  31. self.messageId = ""
  32. self.counter = ""
  33. self.messageText = ""
  34. self.serverDate = ""
  35. self.image = ""
  36. self.video = ""
  37. self.file = ""
  38. self.attachmentFlag = ""
  39. self.messageScope = ""
  40. self.name = ""
  41. self.profile = ""
  42. self.official = ""
  43. self.status = ""
  44. self.credential = ""
  45. self.lock = ""
  46. self.thumb = ""
  47. self.audio = ""
  48. }
  49. 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 = "") {
  50. self.fpin = fpin
  51. self.pin = pin
  52. self.messageId = messageId
  53. self.counter = counter
  54. self.messageText = messageText
  55. self.serverDate = serverDate
  56. self.image = image
  57. self.video = video
  58. self.file = file
  59. self.attachmentFlag = attachmentFlag
  60. self.messageScope = messageScope
  61. self.name = name
  62. self.profile = profile
  63. self.official = official
  64. self.status = status
  65. self.credential = credential
  66. self.lock = lock
  67. self.thumb = thumb
  68. self.audio = audio
  69. }
  70. public static func == (lhs: Chat, rhs: Chat) -> Bool {
  71. return lhs.pin == rhs.pin
  72. }
  73. public var description: String {
  74. return ""
  75. }
  76. public static func getMessageFromSearch(text: String = "") -> [String] {
  77. var messages: [String] = []
  78. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  79. do {
  80. let query = "select message_id FROM MESSAGE where message_text LIKE '%\(text)%'"
  81. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  82. while cursorData.next() {
  83. let data = cursorData.string(forColumnIndex: 0) ?? ""
  84. messages.append(data)
  85. }
  86. cursorData.close()
  87. }
  88. } catch {
  89. rollback.pointee = true
  90. print("Access database error: \(error.localizedDescription)")
  91. }
  92. })
  93. return messages
  94. }
  95. // public static func getUcList() {
  96. // Database.shared.database?.inTransaction({ (fmdb, rollback) in
  97. // let query = " select ms.message_id from MESSAGE_SUMMARY ms"
  98. // if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  99. // while cursorData.next() {
  100. // print("HMMKAMPRET2 \(cursorData.string(forColumnIndex: 0))")
  101. // }
  102. // cursorData.close()
  103. // }
  104. // })
  105. // }
  106. public static func getData(messageId: String = "", isImage: Bool = false, isDoc: Bool = false, isVideo: Bool = false, isGIF: Bool = false, isLink: Bool = false, isAudio: Bool = false) -> [Chat] {
  107. var chats: [Chat] = []
  108. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  109. do {
  110. var lastQuery = ""
  111. if isImage {
  112. lastQuery = "m.image_id IS NOT NULL AND m.image_id != ''"
  113. } else if isDoc {
  114. lastQuery = "m.file_id IS NOT NULL AND m.file_id != ''"
  115. } else if isVideo {
  116. lastQuery = "m.video_id IS NOT NULL AND m.video_id != ''"
  117. } else if isGIF {
  118. lastQuery = "m.file_id IS NOT NULL AND m.file_id != ''"
  119. } else if isLink {
  120. lastQuery = "m.message_text IS NOT NULL AND m.message_text != '' AND (m.message_text LIKE '%https://%' OR m.message_text LIKE '%www.%')"
  121. } else if isAudio {
  122. lastQuery = "m.audio_id IS NOT NULL AND m.audio_id != ''"
  123. }
  124. let query = """
  125. select m.f_pin, \(!lastQuery.isEmpty ? "m.l_pin, m.message_id" : "ms.l_pin, ms.message_id"), \(!lastQuery.isEmpty ? "m.thumb_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 from MESSAGE_SUMMARY ms, MESSAGE m, BUDDY b where ms.l_pin = b.f_pin and \(!lastQuery.isEmpty ? lastQuery : "ms.message_id = m.message_id \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")")
  126. union
  127. select m.f_pin, \(!lastQuery.isEmpty ? "m.l_pin, m.message_id" : "ms.l_pin, ms.message_id"), \(!lastQuery.isEmpty ? "m.thumb_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 from MESSAGE_SUMMARY ms, MESSAGE m where ms.l_pin = '-999' and \(!lastQuery.isEmpty ? lastQuery : "ms.message_id = m.message_id \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")")
  128. union
  129. select m.f_pin, \(!lastQuery.isEmpty ? "m.l_pin, m.message_id" : "ms.l_pin, ms.message_id"), \(!lastQuery.isEmpty ? "m.thumb_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.f_name || ' (\("Lounge".localized()))', b.image_id profile, b.official, m.status, m.credential, m.lock, m.audio_id from MESSAGE_SUMMARY ms, MESSAGE m, GROUPZ b where ms.l_pin = b.group_id and \(!lastQuery.isEmpty ? lastQuery : "ms.message_id = m.message_id \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")")
  130. union
  131. select m.f_pin, \(!lastQuery.isEmpty ? "m.l_pin, m.message_id" : "ms.l_pin, ms.message_id"), \(!lastQuery.isEmpty ? "m.thumb_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, c.f_name || ' (' || b.title || ')', c.image_id profile, '', m.status, m.credential, m.lock, m.audio_id 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 \(!lastQuery.isEmpty ? lastQuery : "ms.message_id = m.message_id \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")")
  132. order by 6 desc
  133. """
  134. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  135. while cursorData.next() {
  136. // if !lastQuery.isEmpty {
  137. // for columnIndex in 0..<cursorData.columnCount {
  138. // if let columnName = cursorData.columnName(for: columnIndex) {
  139. // if let value = cursorData.object(forColumn: columnName) {
  140. // print("\(columnName): \(value)")
  141. // } else {
  142. // print("\(columnName): nil")
  143. // }
  144. // }
  145. // }
  146. // print("---------------------")
  147. // }
  148. let chat = Chat(fpin: cursorData.string(forColumnIndex: 0) ?? "",
  149. pin: cursorData.string(forColumnIndex: 1) ?? "",
  150. messageId: cursorData.string(forColumnIndex: 2) ?? "",
  151. counter: !lastQuery.isEmpty ? "0" : cursorData.string(forColumnIndex: 3) ?? "",
  152. messageText: cursorData.string(forColumnIndex: 4) ?? "",
  153. serverDate: cursorData.string(forColumnIndex: 5) ?? "",
  154. image: cursorData.string(forColumnIndex: 6) ?? "",
  155. video: cursorData.string(forColumnIndex: 7) ?? "",
  156. file: cursorData.string(forColumnIndex: 8) ?? "",
  157. attachmentFlag: cursorData.string(forColumnIndex: 9) ?? "",
  158. messageScope: cursorData.string(forColumnIndex: 10) ?? "",
  159. name: cursorData.string(forColumnIndex: 11) ?? "",
  160. profile: cursorData.string(forColumnIndex: 12) ?? "",
  161. official: cursorData.string(forColumnIndex: 13) ?? "",
  162. status: cursorData.string(forColumnIndex: 14) ?? "",
  163. credential: cursorData.string(forColumnIndex: 15) ?? "",
  164. lock: cursorData.string(forColumnIndex: 16) ?? "",
  165. thumb: lastQuery.isEmpty ? "" : cursorData.string(forColumnIndex: 3) ?? "",
  166. audio: cursorData.string(forColumnIndex: 17) ?? "")
  167. chats.append(chat)
  168. }
  169. cursorData.close()
  170. // if chats.count == 0 {
  171. // if let cursorCounter = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursorCounter.next() {
  172. // if cursorCounter.int(forColumnIndex: 0) != 0 {
  173. // _ = Database.shared.updateAllRecord(fmdb: fmdb, table: "MESSAGE_SUMMARY", cvalues: [
  174. // "counter" : 0
  175. // ])
  176. // NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil, userInfo: nil)
  177. // }
  178. // cursorCounter.close()
  179. // }
  180. // }
  181. }
  182. } catch {
  183. rollback.pointee = true
  184. print("Access database error: \(error.localizedDescription)")
  185. }
  186. })
  187. return chats
  188. }
  189. }