Skip to content

Commit

Permalink
Added resync existing user
Browse files Browse the repository at this point in the history
  • Loading branch information
i5hi committed Jan 5, 2023
1 parent 04e4c6a commit 1f8cc58
Show file tree
Hide file tree
Showing 18 changed files with 598 additions and 142 deletions.
6 changes: 6 additions & 0 deletions lib/api/interface/libcp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ abstract class ICypherpost {
required String inviteCode,
});

R<InvitationDetail> selfInviteCode({
required String hostname,
required int socks5,
required String socialRoot,
});

R<ServerStatus> leaveServer({
required String hostname,
required int socks5,
Expand Down
18 changes: 18 additions & 0 deletions lib/api/libcp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,24 @@ class LibCypherpost implements ICypherpost {
return R(result: InvitationDetail.fromJson(resp));
}

@override
R<InvitationDetail> selfInviteCode({
required String hostname,
required int socks5,
required String socialRoot,
}) {
final resp = _libcp.selfInvitation(
hostname: hostname,
socks5: socks5,
socialRoot: socialRoot,
);

if (resp.contains('error')) {
return R(error: SMError.fromJson(resp).error);
} else
return R(result: InvitationDetail.fromJson(resp));
}

@override
R<ServerStatus> leaveServer({
required String hostname,
Expand Down
2 changes: 1 addition & 1 deletion lib/cubit/network/discover.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class DiscoverCubit extends Cubit<DiscoverState> {
}

final networkMembers = NetworkMembers(
id: state.network.id!,
id: state.network.id,
members: members.result!,
);
await saveToStorage(networkMembers);
Expand Down
129 changes: 121 additions & 8 deletions lib/cubit/network/overview.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import 'package:sats/api/libcp.dart';
import 'package:sats/cubit/logger.dart';
import 'package:sats/cubit/social-root.dart';
import 'package:sats/cubit/tor.dart';
import 'package:sats/model/network-chat-history.dart';
import 'package:sats/model/network-identity.dart';
import 'package:sats/model/network-members.dart';
import 'package:sats/model/result.dart';
import 'package:sats/pkg/interface/clipboard.dart';
import 'package:sats/pkg/interface/storage.dart';
Expand All @@ -25,6 +27,10 @@ class OverviewState with _$OverviewState {
@Default('') String error,
@Default('') String loading,
@Default('') String displayedInviteSecret,
@Default(0) int latestGenesis,
@Default([]) List<Verified> verifiedPosts,
@Default([]) List<String> corruptedPostIds,
@Default([]) List<MemberIdentity> members,
}) = _OverviewState;

const OverviewState._();
Expand All @@ -50,21 +56,50 @@ class OverviewCubit extends Cubit<OverviewState> {

Future<void> load() async {
try {
// load from storage and update from network - all posts
emit(
state.copyWith(
displayedInviteSecret: (state.network.lastInviteSecret == null)
? ''
: state.network.lastInviteSecret!,
pubkey: _socialRoot.state.key!.pubkey,
loading: 'fromStorage',
),
);
final storedHistory = _storage.getItem<NetworkChatHistory>(
StoreKeys.ChatHistory.name,
state.network.id!,
);
if (storedHistory.hasError) return;

final storedMembers = _storage.getItem<NetworkMembers>(
StoreKeys.Members.name,
state.network.id!,
);
if (storedMembers.hasError) return;
await Future.delayed(const Duration(milliseconds: 300));

emit(
state.copyWith(
verifiedPosts: storedHistory.result!.verifiedPosts
.map((e) => Verified.fromJson(e))
.toList(),
corruptedPostIds: storedHistory.result!.corruptedPostIds,
latestGenesis: storedHistory.result!.latestGenesis,
members: storedMembers.result!.members,
loading: '',
),
);
} catch (e, s) {
_logger.logException(e, 'NetworkOverview.load', s);
_logger.logException(e, 'OverviewCubit.load', s);
}
}

MemberIdentity? usernameByPubkey(String pubkey) {
return state.members.firstWhere(
(m) => m.pubkey == pubkey,
orElse: () => MemberIdentity(
username: '*notfound*',
pubkey: pubkey,
),
);
}

Future<void> generateInvite() async {
try {
emit(
Expand Down Expand Up @@ -116,7 +151,7 @@ class OverviewCubit extends Cubit<OverviewState> {
),
);
} catch (e, s) {
_logger.logException(e, 'NetworkOverview.genInvite', s);
_logger.logException(e, 'OverviewCubit.genInvite', s);
}
}

Expand Down Expand Up @@ -155,7 +190,7 @@ class OverviewCubit extends Cubit<OverviewState> {
),
);
} catch (e, s) {
_logger.logException(e, 'NetworkOverview.leave', s);
_logger.logException(e, 'OverviewCubit.leave', s);
}
}

Expand All @@ -180,6 +215,73 @@ class OverviewCubit extends Cubit<OverviewState> {
}
}

Future<void> fetchAllPosts() async {
emit(
state.copyWith(
loading: 'fetching',
error: '',
),
);
final socks5 = _torCubit.state.socks5Port;
final socialRoot = _socialRoot.state.key!.xprv;

final sorted = await compute(getPosts, {
'hostname': 'https://' + state.network.hostname,
'socks5': socks5.toString(),
'socialRoot': socialRoot,
'genesisFilter': state.latestGenesis.toString(),
});

if (sorted.hasError) {
emit(
state.copyWith(
error: sorted.error!,
loading: '',
),
);
return;
}

final allVerifiedPosts = state.verifiedPosts + sorted.result!.verified!;

final history = NetworkChatHistory(
verifiedPosts: allVerifiedPosts.map((e) => e.toJson()).toList(),
latestGenesis: sorted.result!.latestGenesis! + 1,
corruptedPostIds: state.corruptedPostIds + sorted.result!.corrupted!,
);

await updateNetworkPosts(history);
await load();
emit(
state.copyWith(
loading: '',
),
);
}

void removeDuplicatePosts() {}

Future<void> updateNetworkPosts(NetworkChatHistory history) async {
try {
final status = await _storage.saveItemAt<NetworkChatHistory>(
StoreKeys.ChatHistory.name,
state.network.id!,
history,
);
if (status.hasError) {
emit(
state.copyWith(
error: couldNotSaveError,
loading: '',
),
);
return;
}
} catch (e, s) {
_logger.logException(e, 'OverviewCubit.load', s);
}
}

void purgeServerModels() {
try {
final status = _storage.deleteItemAt<NetworkIdentity>(
Expand Down Expand Up @@ -233,3 +335,14 @@ R<ServerStatus> leave(dynamic data) {
);
return resp;
}

R<SortedPosts> getPosts(dynamic data) {
final obj = data as Map<String, String?>;
final resp = LibCypherpost().getAllPosts(
hostname: obj['hostname']!,
socks5: int.parse(obj['socks5']!),
socialRoot: obj['socialRoot']!,
genesisFilter: int.parse(obj['genesisFilter']!),
);
return resp;
}
Loading

0 comments on commit 1f8cc58

Please sign in to comment.