Skip to content

Commit fe87978

Browse files
committed
feat: add profile high level sdk
1 parent f7d7567 commit fe87978

File tree

5 files changed

+226
-0
lines changed

5 files changed

+226
-0
lines changed

Sources/PushAPI/Chat.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public struct Chat {
2+
private var account: String
3+
private var decryptedPgpPvtKey: String
4+
private var env: ENV
5+
6+
init(
7+
account: String,
8+
decryptedPgpPvtKey: String,
9+
env: ENV
10+
) {
11+
self.account = account
12+
self.decryptedPgpPvtKey = decryptedPgpPvtKey
13+
self.env = env
14+
}
15+
16+
public func list(type: ChatListType, page: Int = 1, limit: Int = 10, overrideAccount: String? = nil) async throws -> [PushChat.Feeds] {
17+
if type == .CHAT {
18+
let options = PushChat.GetChatsOptions(
19+
account: account,
20+
pgpPrivateKey: decryptedPgpPvtKey,
21+
page: page, limit: limit, env: env)
22+
23+
return try await PushChat.getChats(
24+
options: options
25+
)
26+
27+
} else {
28+
let options = PushChat.RequestOptionsType(
29+
account: account,
30+
pgpPrivateKey: decryptedPgpPvtKey,
31+
page: page, limit: limit, env: env)
32+
return try await PushChat.requests(options: options)
33+
}
34+
}
35+
}
36+
37+
public enum ChatListType {
38+
case CHAT
39+
case REQUESTS
40+
}

Sources/PushAPI/Profile.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import Foundation
2+
3+
public struct Profile {
4+
private var account: String
5+
private var decryptedPgpPvtKey: String
6+
private var env: ENV
7+
8+
init(
9+
account: String,
10+
decryptedPgpPvtKey: String,
11+
env: ENV
12+
) {
13+
self.account = account
14+
self.decryptedPgpPvtKey = decryptedPgpPvtKey
15+
self.env = env
16+
}
17+
18+
public func info(overrideAccount: String? = nil) async throws -> PushUser? {
19+
return try? await PushUser.get(account: overrideAccount ?? account, env: env)
20+
}
21+
22+
public func update(name: String? = nil, desc: String? = nil, picture: String? = nil) async throws {
23+
let info = try? await info()
24+
var profile = info!.profile
25+
profile.name = name ?? profile.name
26+
profile.desc = desc ?? profile.desc
27+
profile.picture = picture ?? profile.picture
28+
29+
try await PushUser.updateUserProfile(account: account, pgpPrivateKey: decryptedPgpPvtKey, newProfile: profile, env: env)
30+
}
31+
}

Sources/PushAPI/PushAPI.swift

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import Foundation
2+
3+
public struct PushAPI {
4+
private var env: ENV
5+
private var account: String
6+
private var readMode: Bool
7+
private var decryptedPgpPvtKey: String
8+
private var pgpPublicKey: String?
9+
private var signer: Signer
10+
11+
public var chat: Chat
12+
public var profile: Profile
13+
14+
init(env: ENV,
15+
account: String,
16+
readMode: Bool,
17+
decryptedPgpPvtKey: String,
18+
pgpPublicKey: String?,
19+
signer: Signer) {
20+
self.env = env
21+
self.account = account
22+
self.readMode = readMode
23+
self.decryptedPgpPvtKey = decryptedPgpPvtKey
24+
self.pgpPublicKey = pgpPublicKey
25+
self.signer = signer
26+
27+
chat = Chat(account: account, decryptedPgpPvtKey: decryptedPgpPvtKey, env: env)
28+
profile = Profile(account: account, decryptedPgpPvtKey: decryptedPgpPvtKey, env: env)
29+
}
30+
31+
public static func initializePush(signer: Signer, options: PushAPIInitializeOptions) async throws -> PushAPI {
32+
// Get account
33+
// Derives account from signer if not provided
34+
let derivedAccount = try await signer.getAddress()
35+
36+
var decryptedPGPPrivateKey: String?
37+
var pgpPublicKey: String?
38+
39+
/**
40+
* Decrypt PGP private key
41+
* If user exists, decrypts the PGP private key
42+
* If user does not exist, creates a new user and returns the decrypted PGP private key
43+
*/
44+
if let user = try await PushUser.get(account: derivedAccount, env: options.env) {
45+
decryptedPGPPrivateKey = try await PushUser.DecryptPGPKey(encryptedPrivateKey: user.encryptedPrivateKey, signer: signer)
46+
pgpPublicKey = user.publicKey
47+
} else {
48+
let newUser = try await PushUser.create(
49+
options: PushUser.CreateUserOptions(
50+
env: options.env,
51+
signer: signer,
52+
progressHook: nil
53+
))
54+
55+
decryptedPGPPrivateKey = try await PushUser.DecryptPGPKey(encryptedPrivateKey: newUser.encryptedPrivateKey, signer: signer)
56+
pgpPublicKey = newUser.publicKey
57+
}
58+
59+
return PushAPI(env: options.env,
60+
account: derivedAccount,
61+
readMode: true,
62+
decryptedPgpPvtKey: decryptedPGPPrivateKey!,
63+
pgpPublicKey: pgpPublicKey,
64+
signer: signer)
65+
}
66+
}
67+
68+
extension PushAPI {
69+
public struct PushAPIInitializeOptions {
70+
var env: ENV
71+
var version: ENCRYPTION_TYPE
72+
var versionMeta: [String: [String: String]]?
73+
var autoUpgrade: Bool
74+
var origin: String?
75+
76+
public init(
77+
env: ENV = .PROD,
78+
version: ENCRYPTION_TYPE = .PGP_V3,
79+
versionMeta: [String: [String: String]]? = nil,
80+
autoUpgrade: Bool = true,
81+
origin: String? = nil) {
82+
self.env = env
83+
self.version = version
84+
self.versionMeta = versionMeta
85+
self.autoUpgrade = autoUpgrade
86+
self.origin = origin
87+
}
88+
}
89+
}

Tests/PushAPI/ChatTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import Push
2+
import XCTest
3+
4+
class ChatTests: XCTestCase {
5+
6+
func testChatLists() async throws {
7+
let userPk = getRandomAccount()
8+
let signer = try SignerPrivateKey(privateKey: userPk)
9+
10+
let pushAPI = try await PushAPI
11+
.initializePush(
12+
signer: signer,
13+
options: PushAPI.PushAPIInitializeOptions()
14+
)
15+
16+
let requests = try await pushAPI.chat.list(type: .REQUESTS)
17+
XCTAssertEqual(requests.count, 0)
18+
19+
20+
let chats = try await pushAPI.chat.list(type: .CHAT)
21+
XCTAssertEqual(chats.count, 0)
22+
}
23+
}
24+

Tests/PushAPI/PushAPITests.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Push
2+
import XCTest
3+
4+
class PushAPITests: XCTestCase {
5+
func testInitialize() async throws {
6+
let userPk = getRandomAccount()
7+
let signer = try SignerPrivateKey(privateKey: userPk)
8+
let address = try await signer.getAddress()
9+
10+
let pushAPI = try await PushAPI
11+
.initializePush(
12+
signer: signer,
13+
options: PushAPI.PushAPIInitializeOptions()
14+
)
15+
16+
let user = try await pushAPI.profile.info()
17+
18+
XCTAssertEqual(user?.did, "eip155:\(address)")
19+
}
20+
21+
func testProfileUpdate() async throws {
22+
let userPk = getRandomAccount()
23+
let signer = try SignerPrivateKey(privateKey: userPk)
24+
let address = try await signer.getAddress()
25+
26+
let pushAPI = try await PushAPI
27+
.initializePush(
28+
signer: signer,
29+
options: PushAPI.PushAPIInitializeOptions()
30+
)
31+
32+
let newName = "Push Swift"
33+
34+
try await pushAPI.profile.update(name: newName, desc: "Push Swift Tester")
35+
36+
let user = try await pushAPI.profile.info()
37+
38+
XCTAssertEqual(user?.profile.name, newName)
39+
}
40+
41+
42+
}

0 commit comments

Comments
 (0)