Skip to content

Commit

Permalink
Implement PushStream highlevel api in SDK (#95)
Browse files Browse the repository at this point in the history
* Scalability changes: modify update group methods (#67)

* feat: add getGroupInfo function

* chore: add deprecation notice to getGroupByName

* feat: add getGroupMemberCount function

* feat: add getGroupMembers functions

* feat: add getAllGroupMembers function

* feat: add getGroupMemberStatus function

* feat: ad getGroupMembersPublicKeys function

* feat: add getAllGroupMembersPublicKeys function

* feat: add updateGroupConfig, updateGroupMembers, updateGroupProfile

* feat: add  addMembers, removeMembers,  addAdmins, removeAdmins

* fix: only show group requests for members and not creator

* Revert "fix: only show group requests for members and not creator"

This reverts commit 0268196.

* chore: add ConnectedUser extends User

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>

* Scalability changes: add pgpv2 encryption in approve request (#68)

* feat: add getGroupInfo function

* chore: add deprecation notice to getGroupByName

* feat: add getGroupMemberCount function

* feat: add getGroupMembers functions

* feat: add getAllGroupMembers function

* feat: add getGroupMemberStatus function

* feat: ad getGroupMembersPublicKeys function

* feat: add getAllGroupMembersPublicKeys function

* feat: add updateGroupConfig, updateGroupMembers, updateGroupProfile

* feat: add  addMembers, removeMembers,  addAdmins, removeAdmins

* fix: only show group requests for members and not creator

* Revert "fix: only show group requests for members and not creator"

This reverts commit 0268196.

* chore: add ConnectedUser extends User

* feat: add scalability changes to approve request

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>

* Delete .DS_Store

* Implement scalability changes in encryption and decryption of messages (#70)

* feat: implement scalability changes for send message

* chore: remove unused code for update group profile

* chore: add scalability changes for decrypt

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>

* Remove the user property from the ConnectedUser class (#75)

* feat: add scalability changes fo updarte group

* chore: add fromUser constructor for ConnectedUser

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>
Co-authored-by: Madhur Gupta <[email protected]>

* Update example app to incorporate scalability changes (#80)

* feat: add update group info screen

* feat: implement group scalability changes in example app

* feat: disconnect socket when account logs out

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>
Co-authored-by: Madhur Gupta <[email protected]>

* feat: add PushApi class

* feat: add list,latest,history and send to Chat in PushApi clases

* feat: add Chat Api class functions

* feat: add all Chat class API functions

* chore: add demo_app and use_cases modules to example

* feat: add chat functions

* feat: add chat use cases

* chore: remove unneccesary log

* feat: add stream highlevel api in SDK

* chore: fix merge conflict

* chore: remove unusd code

* remove DS_Store file

---------

Co-authored-by: Gbogboade Ayomide <[email protected]>
Co-authored-by: Madhur Gupta <[email protected]>
  • Loading branch information
3 people authored Jan 16, 2024
1 parent 234a17c commit 722711e
Show file tree
Hide file tree
Showing 19 changed files with 999 additions and 135 deletions.
Binary file renamed test/.DS_Store → .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion example/use_cases/lib/models/signer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class EthersSigner extends push.Signer {
privateKey: ethersWallet.privateKey, message: Uint8List.fromList(m));
return signature;
} catch (e) {
print('override: getEip191Signature: error$e');
push.log('override: getEip191Signature: error$e');
return message;
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/push_restapi_dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export 'src/payloads/payloads.dart';
export 'src/video/video.dart';
export 'src/sockets/sockets.dart';

export 'src/pushapi/__pushapi.dart';
export 'src/pushapi/pushapi.dart';
export 'src/pushstream/push_stream.dart';

export 'src/main.dart';
2 changes: 1 addition & 1 deletion lib/src/helpers/src/crypto.dart
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ Future<String> decryptPGPKey({
}

//TODO: Implement verifyProfileKeys
verifyProfileKeys({
verifyProfileKeys({
required String encryptedPrivateKey,
required String publicKey,
required String did,
Expand Down
4 changes: 2 additions & 2 deletions lib/src/models/src/all.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,12 @@ class AdditionalMeta {
}
}

class NotificationPayload {
class NotificationApiPayload {
NotificationOptions notification;
PayloadData data;
dynamic recipients;

NotificationPayload({
NotificationApiPayload({
required this.notification,
required this.data,
required this.recipients,
Expand Down
8 changes: 4 additions & 4 deletions lib/src/payloads/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ String getUUID() {
return uuid.v4();
}

NotificationPayload? getPayloadForAPIInput(
NotificationApiPayload? getPayloadForAPIInput(
SendNotificationInputOptions inputOptions, dynamic recipients) {
if (inputOptions.notification != null && inputOptions.payload != null) {
return NotificationPayload(
return NotificationApiPayload(
notification: NotificationOptions(
title: inputOptions.notification?.title ?? '',
body: inputOptions.notification?.body ?? '',
Expand Down Expand Up @@ -117,7 +117,7 @@ Future<String> getVerificationProof({
required NOTIFICATION_TYPE notificationType,
required IDENTITY_TYPE identityType,
required String verifyingContract,
required NotificationPayload? payload,
required NotificationApiPayload? payload,
String? ipfsHash,
Map<String, dynamic> graph = const {},
required String uuid,
Expand Down Expand Up @@ -197,7 +197,7 @@ Future<String> getVerificationProof({

String getPayloadIdentity({
required IDENTITY_TYPE identityType,
required NotificationPayload? payload,
required NotificationApiPayload? payload,
NOTIFICATION_TYPE? notificationType,
String? ipfsHash,
Graph? graph,
Expand Down
4 changes: 0 additions & 4 deletions lib/src/pushapi/__pushapi.dart

This file was deleted.

121 changes: 4 additions & 117 deletions lib/src/pushapi/pushapi.dart
Original file line number Diff line number Diff line change
@@ -1,117 +1,4 @@
import '../../push_restapi_dart.dart';

class PushAPI {
late final Signer? _signer;
late final String _account;
late final String? _decryptedPgpPvtKey;
final String? pgpPublicKey;
final bool readMode;

void Function(ProgressHookType)? progressHook;

late Chat chat;
PushAPI({
Signer? signer,
required String account,
String? decryptedPgpPvtKey,
this.pgpPublicKey,
this.progressHook,
this.readMode = false,
ENV env = ENV.staging,
bool showHttpLog = false,
}) {
_signer = signer;
_account = account;
_decryptedPgpPvtKey = decryptedPgpPvtKey;

initPush(
env: env,
showHttpLog: showHttpLog,
);

chat = Chat(
signer: _signer,
account: _account,
decryptedPgpPvtKey: _decryptedPgpPvtKey,
pgpPublicKey: pgpPublicKey,
progressHook: progressHook,
);
}

static Future<PushAPI> initialize(
{Signer? signer, PushAPIInitializeOptions? options}) async {
if (signer == null && options?.account == null) {
throw Exception("Either 'signer' or 'account' must be provided.");
}

final readMode = signer != null;

// Get account
// Derives account from signer if not provided
String? derivedAccount;

if (signer != null) {
derivedAccount = getAccountAddress(
getWallet(address: options?.account, signer: signer));
} else {
derivedAccount = options?.account;
}

if (derivedAccount == null) {
throw Exception('Account could not be derived.');
}

String? decryptedPGPPrivateKey;
String? pgpPublicKey;

/**
* Decrypt PGP private key
* If user exists, decrypts the PGP private key
* If user does not exist, creates a new user and returns the decrypted PGP private key
*/
final user = await getUser(address: derivedAccount);

if (readMode) {
if (user != null && user.encryptedPrivateKey != null) {
decryptedPGPPrivateKey = await decryptPGPKey(
toUpgrade: options?.autoUpgrade,
progressHook: options?.progressHook,
additionalMeta: options?.versionMeta,
encryptedPGPPrivateKey: user.encryptedPrivateKey!,
wallet: getWallet(address: options?.account, signer: signer),
);
pgpPublicKey = user.publicKey;
} else {
final newUser = await createUser(
signer: signer,
progressHook: options?.progressHook ?? (_) {},
version: options?.version ?? ENCRYPTION_TYPE.PGP_V3,
);
decryptedPGPPrivateKey = newUser.decryptedPrivateKey;
pgpPublicKey = newUser.publicKey;
}
}

final api = PushAPI(
account: derivedAccount,
signer: signer,
decryptedPgpPvtKey: decryptedPGPPrivateKey,
pgpPublicKey: pgpPublicKey,
readMode: readMode,
showHttpLog: options?.showHttpLog ?? false,
);

return api;
}

//TODO initStream
Future initStream() async {}

Future<User?> info({String? overrideAccount}) async {
return getUser(address: overrideAccount ?? _account);
}

static String ensureSignerMessage() {
return 'Operation not allowed in read-only mode. Signer is required.';
}
}
export 'src/models.dart';
export 'src/pushapi.dart';
export 'src/chat.dart' hide GroupAPI, GroupParticipantsAPI;
export 'src/user.dart';
6 changes: 3 additions & 3 deletions lib/src/pushapi/chat.dart → lib/src/pushapi/src/chat.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// ignore_for_file: library_prefixes

import '../../push_restapi_dart.dart';
import '../chat/chat.dart' as PUSH_CHAT;
import '../user/user.dart' as PUSH_USER;
import '../../../push_restapi_dart.dart';
import '../../chat/chat.dart' as PUSH_CHAT;
import '../../user/user.dart' as PUSH_USER;

class Chat {
late final Signer? _signer;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// ignore_for_file: constant_identifier_names

import '../../push_restapi_dart.dart';
import '../../../push_restapi_dart.dart';

class PushAPIInitializeOptions {
void Function(ProgressHookType)? progressHook;
Expand Down
132 changes: 132 additions & 0 deletions lib/src/pushapi/src/pushapi.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import '../../../push_restapi_dart.dart';

class PushAPI {
late final Signer? _signer;
late final String _account;
late final String? _decryptedPgpPvtKey;
final String? pgpPublicKey;
final bool readMode;

void Function(ProgressHookType)? progressHook;

late Chat chat;
late PushStream stream;
PushAPI({
Signer? signer,
required String account,
String? decryptedPgpPvtKey,
this.pgpPublicKey,
this.progressHook,
this.readMode = false,
ENV env = ENV.staging,
bool showHttpLog = false,
}) {
_signer = signer;
_account = account;
_decryptedPgpPvtKey = decryptedPgpPvtKey;

initPush(
env: env,
showHttpLog: showHttpLog,
);

chat = Chat(
signer: _signer,
account: _account,
decryptedPgpPvtKey: _decryptedPgpPvtKey,
pgpPublicKey: pgpPublicKey,
progressHook: progressHook,
);
}

static Future<PushAPI> initialize({
Signer? signer,
PushAPIInitializeOptions? options,
}) async {
if (signer == null && options?.account == null) {
throw Exception("Either 'signer' or 'account' must be provided.");
}

final readMode = signer != null;

// Get account
// Derives account from signer if not provided
String? derivedAccount;

if (signer != null) {
derivedAccount = getAccountAddress(
getWallet(address: options?.account, signer: signer));
} else {
derivedAccount = options?.account;
}

if (derivedAccount == null) {
throw Exception('Account could not be derived.');
}

String? decryptedPGPPrivateKey;
String? pgpPublicKey;

/**
* Decrypt PGP private key
* If user exists, decrypts the PGP private key
* If user does not exist, creates a new user and returns the decrypted PGP private key
*/
final user = await getUser(address: derivedAccount);

if (readMode) {
if (user != null && user.encryptedPrivateKey != null) {
decryptedPGPPrivateKey = await decryptPGPKey(
toUpgrade: options?.autoUpgrade,
progressHook: options?.progressHook,
additionalMeta: options?.versionMeta,
encryptedPGPPrivateKey: user.encryptedPrivateKey!,
wallet: getWallet(address: options?.account, signer: signer),
);
pgpPublicKey = user.publicKey;
} else {
final newUser = await createUser(
signer: signer,
progressHook: options?.progressHook ?? (_) {},
version: options?.version ?? ENCRYPTION_TYPE.PGP_V3,
);
decryptedPGPPrivateKey = newUser.decryptedPrivateKey;
pgpPublicKey = newUser.publicKey;
}
}

final api = PushAPI(
account: derivedAccount,
signer: signer,
decryptedPgpPvtKey: decryptedPGPPrivateKey,
pgpPublicKey: pgpPublicKey,
readMode: readMode,
showHttpLog: options?.showHttpLog ?? false,
);

return api;
}

Future<PushStream> initStream(
{required List<STREAM> listen,
PushStreamInitializeOptions? options}) async {
stream = await PushStream.initialize(
account: _account,
listen: listen,
decryptedPgpPvtKey: _decryptedPgpPvtKey,
options: options,
progressHook: progressHook,
signer: _signer,
);

return stream;
}

Future<User?> info({String? overrideAccount}) async {
return getUser(address: overrideAccount ?? _account);
}

static String ensureSignerMessage() {
return 'Operation not allowed in read-only mode. Signer is required.';
}
}
14 changes: 14 additions & 0 deletions lib/src/pushapi/src/user.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import '../../../push_restapi_dart.dart';

class UserAPI {
late final String _account;
UserAPI({
required String account,
}) {
_account = account;
}

Future<User?> info({String? overrideAccount}) async {
return getUser(address: overrideAccount ?? _account);
}
}
4 changes: 4 additions & 0 deletions lib/src/pushstream/push_stream.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export 'src/push_stream.dart';
export 'src/models/models.dart';
export 'src/models/notification_model.dart';
export 'src/data_modifier.dart';
Loading

0 comments on commit 722711e

Please sign in to comment.