Skip to content

Commit

Permalink
Use message.attributedBody
Browse files Browse the repository at this point in the history
  • Loading branch information
tagavari committed Jul 29, 2022
1 parent 77a911c commit 33fae0f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
26 changes: 24 additions & 2 deletions AirMessage/Database/DatabaseConverter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class DatabaseConverter {
}

//Message-specific parameters
let text = cleanMessageText(row[indices["message.text"]!] as! String?)
let text = parseAttributedBody(row[indices["message.attributedBody"]!] as! SQLite.Blob?, withLogID: guid)
let subject = row[indices["message.subject"]!] as! String?
let sendEffect: String?
if #available(macOS 10.12, *) {
Expand Down Expand Up @@ -260,7 +260,7 @@ class DatabaseConverter {
.components(separatedBy: ",")

let lastMessageDate = row[indices["message.date"]!] as! Int64
let lastMessageText = cleanMessageText(row[indices["message.text"]!] as! String?)
let lastMessageText = parseAttributedBody(row[indices["message.attributedBody"]!] as! SQLite.Blob?)
let lastMessageSendStyle: String?
if #available(macOS 10.12, *) {
lastMessageSendStyle = row[indices["message.expressive_send_style_id"]!] as! String?
Expand Down Expand Up @@ -445,6 +445,28 @@ class DatabaseConverter {

//MARK: Helpers

///Parses an attributed body string and cleans it
static func parseAttributedBody(_ body: SQLite.Blob?, withLogID logID: String? = nil) -> String? {
//Skip if the body is nil
guard let body = body else {
return nil
}

//Create an unarchiver
guard let unarchiver = NSUnarchiver(forReadingWith: Data.fromDatatypeValue(body)) else {
LogManager.log("Failed to create unarchiver for message \(logID ?? "unknown")", level: .notice)
return nil
}

//Retrieve the archive contents
guard let attributedString = unarchiver.decodeObject() as? NSAttributedString else {
LogManager.log("Failed to decode object for message \(logID ?? "unknown")", level: .notice)
return nil
}

return cleanMessageText(attributedString.string)
}

///Cleans a message string found in the database
static func cleanMessageText(_ message: String?) -> String? {
//Skip if the message is nil
Expand Down
2 changes: 1 addition & 1 deletion AirMessage/Database/DatabaseManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ class DatabaseManager {
"message.date",
"message.item_type",
"message.group_action_type",
"message.text",
"message.attributedBody",
"message.subject",
"message.error",
"message.date_read",
Expand Down
2 changes: 1 addition & 1 deletion AirMessage/Database/SQL/QueryAllChatSummary.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ SELECT
chat.guid AS "chat.guid",
chat.display_name AS "chat.display_name",
chat.service_name AS "chat.service_name",
message.text AS "message.text",
message.attributedBody AS "message.attributedBody",
message.date AS "message.date",
message.is_from_me AS "message.is_from_me",
handle.id AS "handle.id",
Expand Down

0 comments on commit 33fae0f

Please sign in to comment.