Skip to content

Commit

Permalink
fix: fix get messages
Browse files Browse the repository at this point in the history
refactor MessageV2 to Message
  • Loading branch information
Gbogboade1 committed Jun 14, 2024
1 parent 1ce17a8 commit c4fd982
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 189 deletions.
4 changes: 2 additions & 2 deletions Sources/Chat/Conversation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ extension PushChat {
message: messages[i], privateKeyArmored: pgpPrivateKey, env: env)

if decryptedObj != nil {
messages[i].messageObj = decryptedObj
messages[i].messageObj = MessageObj(content: decryptedObj)
}
messages[i].messageContent = decryptedMsg
}
Expand Down Expand Up @@ -151,7 +151,7 @@ extension PushChat {
var messageObj: String? = nil
if let msgObj = message.messageObj {
messageObj = try? decryptMessage(
msgObj, secretKey: secretKey)
msgObj.content!, secretKey: secretKey)
}

let decMsg = try decryptMessage(
Expand Down
2 changes: 1 addition & 1 deletion Sources/Chat/Inbox.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public struct PushChat {
var toDecrypt: Bool = false
var page: Int = CONSTANTS.PAGINATION.INITIAL_PAGE
var limit: Int = CONSTANTS.PAGINATION.LIMIT
var env: ENV = ENV.STAGING
var env: ENV = .STAGING

public init(
account: String,
Expand Down
52 changes: 44 additions & 8 deletions Sources/Chat/SendV2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ extension PushChat {
}
}

public static func sendV2(chatOptions: SendOptionsV2) async throws -> MessageV2 {
public static func sendV2(chatOptions: SendOptionsV2) async throws -> Message {
let computedOptions = try computeOptions(chatOptions)

try validateSendOptions(options: computedOptions)
Expand All @@ -151,7 +151,8 @@ extension PushChat {

var messageContent: String
if computedOptions.messageType == .Reply ||
computedOptions.messageType == .Composite {
computedOptions.messageType == .Composite
{
messageContent = "MessageType Not Supported by this sdk version. Plz upgrade !!!"
} else {
messageContent = computedOptions.messageObj.content
Expand All @@ -167,7 +168,8 @@ extension PushChat {
messageObj: computedOptions.messageObj,
group: groupInfo,
isGroup: isGroup,
env: computedOptions.env)
env: computedOptions.env
)

// if isIntent {
// return try await sendIntentService(payload: sendMessagePayload, env: computedOptions.env)
Expand Down Expand Up @@ -198,7 +200,7 @@ extension PushChat {
}

do {
return try JSONDecoder().decode(MessageV2.self, from: data)
return try JSONDecoder().decode(Message.self, from: data)
} catch {
print("[Push SDK] - API \(error.localizedDescription)")
throw error
Expand All @@ -216,7 +218,8 @@ extension PushChat {
isGroup: Bool,
env: ENV
) async throws
-> SendMessagePayloadV2 {
-> SendMessagePayloadV2
{
var secretKey: String

if isGroup, group != nil, group?.encryptedSecret != nil, group?.sessionKey != nil {
Expand Down Expand Up @@ -285,8 +288,8 @@ extension PushChat {
("sessionKey", body.sessionKey ?? "null"),
("encryptedSecret", body.encryptedSecret ?? "null"),
])
bodyToBehashed = bodyToBehashed.replacingOccurrences(of: "\"\(messageObjKey)\"",
with: encryptionType == "PlainText" ? try messageObj.toJson() : "\"\(encryptedMessageObj)\"")
bodyToBehashed = try bodyToBehashed.replacingOccurrences(of: "\"\(messageObjKey)\"",
with: encryptionType == "PlainText" ? messageObj.toJson() : "\"\(encryptedMessageObj)\"")

let hash = generateSHA256Hash(msg: bodyToBehashed)

Expand Down Expand Up @@ -354,7 +357,7 @@ extension PushChat {
}
}

public struct MessageV2: Codable {
public struct Message: Codable {
public var fromCAIP10: String
public var toCAIP10: String
public var fromDID: String
Expand All @@ -371,6 +374,39 @@ public struct MessageV2: Codable {
public var cid: String?
public var sessionKey: String?

public init(fromCAIP10: String,
toCAIP10: String,
fromDID: String,
toDID: String,
messageType: String,
messageContent: String,
messageObj: MessageObj? = nil, // Define a type that can represent both String and JSON
signature: String,
sigType: String,
timestamp: Int? = nil,
encType: String,
encryptedSecret: String? = nil,
link: String? = nil,
cid: String? = nil,
sessionKey: String? = nil)
{
self.fromCAIP10 = fromCAIP10
self.toCAIP10 = toCAIP10
self.fromDID = fromDID
self.toDID = toDID
self.messageType = messageType
self.messageContent = messageContent
self.messageObj = messageObj
self.signature = signature
self.sigType = sigType
self.timestamp = timestamp
self.encType = encType
self.encryptedSecret = encryptedSecret
self.link = link
self.cid = cid
self.sessionKey = sessionKey
}

// Implement a custom init(from:) initializer for decoding
public init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
Expand Down
19 changes: 10 additions & 9 deletions Sources/Helpers/Chat/GetInboxList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ struct BodyToHashInboxList: Encodable {
let messageType: String
}

func verifySignature(messageContent: String, signatureArmored: String, publicKeyArmored: String)
func verifySignature(messageContent _: String, signatureArmored _: String, publicKeyArmored _: String)
throws
{
// TODO: Implement
Expand All @@ -17,14 +17,15 @@ func verifySignature(messageContent: String, signatureArmored: String, publicKey
private func decryptAndVerifySignature(
cipherText: String,
encryptedSecretKey: String,
publicKeyArmored: String,
signatureArmored: String,
publicKeyArmored _: String,
signatureArmored _: String,
privateKeyArmored: String,
message: Message
message _: Message
) async throws -> String {
do {
let secretKey: String = try Pgp.pgpDecrypt(
cipherText: encryptedSecretKey, toPrivateKeyArmored: privateKeyArmored)
cipherText: encryptedSecretKey, toPrivateKeyArmored: privateKeyArmored
)

guard let userMsg = try AESCBCHelper.decrypt(cipherText: cipherText, secretKey: secretKey)
else {
Expand All @@ -40,14 +41,13 @@ private func decryptAndVerifySignature(

private func decryptFeeds(
feeds: [PushChat.Feeds],
connectedUser: PushUser,
connectedUser _: PushUser,
pgpPrivateKey: String?,
env: ENV
) async throws -> [PushChat.Feeds] {
var updatedFeeds: [PushChat.Feeds] = []
for feed in feeds {
var currentFeed = feed
// print(feed.chatId, feed.publicKey?.count, feed.msg?.encType)
if currentFeed.msg == nil {
updatedFeeds.append(currentFeed)
continue
Expand All @@ -58,7 +58,8 @@ private func decryptFeeds(
}

let (decryptedMsg, _) = try await PushChat.decryptMessage(
message: currentFeed.msg!, privateKeyArmored: pgpPrivateKey!, env: env)
message: currentFeed.msg!, privateKeyArmored: pgpPrivateKey!, env: env
)
currentFeed.msg?.messageContent = decryptedMsg
}
updatedFeeds.append(currentFeed)
Expand Down Expand Up @@ -92,12 +93,12 @@ public func getInboxLists(
messageContent: "",
signature: "",
sigType: "",
timestamp: nil,
encType: "PlainText",
encryptedSecret: "",
link: ""
)
}

feeds.append(
PushChat.Feeds(
msg: message,
Expand Down
40 changes: 1 addition & 39 deletions Sources/Helpers/Ipfs/Cid.swift
Original file line number Diff line number Diff line change
@@ -1,43 +1,5 @@
import Foundation

public struct Message: Codable {
public var fromCAIP10: String
public var toCAIP10: String
public var fromDID: String
public var toDID: String
public var messageType: String
public var messageContent: String
public var messageObj: String?
public var signature: String
public var sigType: String
public var timestamp: Int?
public var encType: String
public var encryptedSecret: String?
public var link: String?
public var cid: String?
public var sessionKey: String?
}

extension Message {
enum CodingKeys: String, CodingKey {
case fromCAIP10
case toCAIP10
case fromDID
case toDID
case messageType
case messageContent
case messageObj
case signature
case sigType
case timestamp
case encType
case encryptedSecret
case link
case cid
case sessionKey
}
}

public func getCID(env: ENV, cid: String) async throws -> Message {
let url: URL = PushEndpoint.getCID(env: env, cid: cid).url

Expand All @@ -47,7 +9,7 @@ public func getCID(env: ENV, cid: String) async throws -> Message {
throw URLError(.badServerResponse)
}

guard (200...299).contains(httpResponse.statusCode) else {
guard (200 ... 299).contains(httpResponse.statusCode) else {
throw URLError(.badServerResponse)
}

Expand Down
14 changes: 1 addition & 13 deletions Sources/PushAPI/Chat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,20 +118,8 @@ public struct Chat {

try await PushUser.updateUserProfile(account: account, pgpPrivateKey: decryptedPgpPvtKey, newProfile: profile!, env: env)
}

// public func send(target: String, messageContent: String, messageType: String = "Text") async throws -> Message {
// return try await Push.PushChat.sendV2(
// chatOptions: PushChat.SendOptions(
//
// messageContent: messageContent,
// messageType: messageType,
// receiverAddress: target,
// account: account,
// pgpPrivateKey: decryptedPgpPvtKey
// ))
// }

public func send(target: String, message: PushChat.SendMessage) async throws -> MessageV2 {
public func send(target: String, message: PushChat.SendMessage) async throws -> Message {

let sendOption = PushChat.SendOptionsV2(
to: target,
Expand Down
Loading

0 comments on commit c4fd982

Please sign in to comment.