Chat.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 let lock: String
  26. public init(pin: String) {
  27. self.fpin = ""
  28. self.pin = pin
  29. self.messageId = ""
  30. self.counter = ""
  31. self.messageText = ""
  32. self.serverDate = ""
  33. self.image = ""
  34. self.video = ""
  35. self.file = ""
  36. self.attachmentFlag = ""
  37. self.messageScope = ""
  38. self.name = ""
  39. self.profile = ""
  40. self.official = ""
  41. self.status = ""
  42. self.credential = ""
  43. self.lock = ""
  44. }
  45. 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) {
  46. self.fpin = fpin
  47. self.pin = pin
  48. self.messageId = messageId
  49. self.counter = counter
  50. self.messageText = messageText
  51. self.serverDate = serverDate
  52. self.image = image
  53. self.video = video
  54. self.file = file
  55. self.attachmentFlag = attachmentFlag
  56. self.messageScope = messageScope
  57. self.name = name
  58. self.profile = profile
  59. self.official = official
  60. self.status = status
  61. self.credential = credential
  62. self.lock = lock
  63. }
  64. public static func == (lhs: Chat, rhs: Chat) -> Bool {
  65. return lhs.pin == rhs.pin
  66. }
  67. public var description: String {
  68. return ""
  69. }
  70. public static func getMessageFromSearch(text: String = "") -> [String] {
  71. var messages: [String] = []
  72. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  73. let query = "select message_id FROM MESSAGE where message_text LIKE '%\(text)%'"
  74. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  75. while cursorData.next() {
  76. let data = cursorData.string(forColumnIndex: 0) ?? ""
  77. messages.append(data)
  78. }
  79. cursorData.close()
  80. }
  81. })
  82. return messages
  83. }
  84. public static func getData(messageId: String = "") -> [Chat] {
  85. var chats: [Chat] = []
  86. Database.shared.database?.inTransaction({ (fmdb, rollback) in
  87. let query = """
  88. 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 from MESSAGE_SUMMARY ms, MESSAGE m, BUDDY b where ms.message_id = m.message_id and ms.l_pin = b.f_pin \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")
  89. union
  90. 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 from MESSAGE_SUMMARY ms, MESSAGE m where ms.message_id = m.message_id and ms.l_pin = '-999' \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")
  91. union
  92. 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.f_name || ' (\("Lounge".localized()))', b.image_id profile, b.official, m.status, m.credential, m.lock from MESSAGE_SUMMARY ms, MESSAGE m, GROUPZ b where ms.message_id = m.message_id and ms.l_pin = b.group_id \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")
  93. union
  94. 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, c.f_name || ' (' || b.title || ')', c.image_id profile, '', m.status, m.credential, m.lock from MESSAGE_SUMMARY ms, MESSAGE m, DISCUSSION_FORUM b, GROUPZ c where ms.message_id = m.message_id and ms.l_pin = b.chat_id and b.group_id = c.group_id \(messageId.isEmpty ? "" : " and m.message_id = '\(messageId)'")
  95. order by 6 desc
  96. """
  97. if let cursorData = Database.shared.getRecords(fmdb: fmdb, query: query) {
  98. while cursorData.next() {
  99. let chat = Chat(fpin: cursorData.string(forColumnIndex: 0) ?? "",
  100. pin: cursorData.string(forColumnIndex: 1) ?? "",
  101. messageId: cursorData.string(forColumnIndex: 2) ?? "",
  102. counter: cursorData.string(forColumnIndex: 3) ?? "",
  103. messageText: cursorData.string(forColumnIndex: 4) ?? "",
  104. serverDate: cursorData.string(forColumnIndex: 5) ?? "",
  105. image: cursorData.string(forColumnIndex: 6) ?? "",
  106. video: cursorData.string(forColumnIndex: 7) ?? "",
  107. file: cursorData.string(forColumnIndex: 8) ?? "",
  108. attachmentFlag: cursorData.string(forColumnIndex: 9) ?? "",
  109. messageScope: cursorData.string(forColumnIndex: 10) ?? "",
  110. name: cursorData.string(forColumnIndex: 11) ?? "",
  111. profile: cursorData.string(forColumnIndex: 12) ?? "",
  112. official: cursorData.string(forColumnIndex: 13) ?? "",
  113. status: cursorData.string(forColumnIndex: 14) ?? "",
  114. credential: cursorData.string(forColumnIndex: 15) ?? "",
  115. lock: cursorData.string(forColumnIndex: 16) ?? "")
  116. chats.append(chat)
  117. }
  118. cursorData.close()
  119. // if chats.count == 0 {
  120. // if let cursorCounter = Database.shared.getRecords(fmdb: fmdb, query: "SELECT SUM(counter) FROM MESSAGE_SUMMARY"), cursorCounter.next() {
  121. // if cursorCounter.int(forColumnIndex: 0) != 0 {
  122. // _ = Database.shared.updateAllRecord(fmdb: fmdb, table: "MESSAGE_SUMMARY", cvalues: [
  123. // "counter" : 0
  124. // ])
  125. // NotificationCenter.default.post(name: NSNotification.Name(rawValue: "reloadTabChats"), object: nil, userInfo: nil)
  126. // }
  127. // cursorCounter.close()
  128. // }
  129. // }
  130. }
  131. })
  132. return chats
  133. }
  134. }