Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support old and new reputation type #1664

Merged
merged 14 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 3 additions & 46 deletions app/lib/models/ceremonies/ceremonies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import 'dart:convert';

import 'package:json_annotation/json_annotation.dart';

import 'package:encointer_wallet/models/communities/community_identifier.dart';
import 'package:ew_polkadart/encointer_types.dart' as et;
import 'package:encointer_wallet/utils/enum.dart';

export 'reputation/v1.dart';
export 'reputation/v2.dart';

part 'ceremonies.g.dart';

@JsonSerializable(explicitToJson: true)
Expand Down Expand Up @@ -72,23 +74,6 @@ class AggregatedAccountDataGlobal {
// ignore: constant_identifier_names
enum ParticipantType { Bootstrapper, Reputable, Endorsee, Newbie }

@JsonSerializable()
class CommunityReputation {
CommunityReputation(this.communityIdentifier, this.reputation);

factory CommunityReputation.fromJson(Map<String, dynamic> json) => _$CommunityReputationFromJson(json);

Map<String, dynamic> toJson() => _$CommunityReputationToJson(this);

CommunityIdentifier communityIdentifier;
Reputation reputation;

@override
String toString() {
return jsonEncode(this);
}
}

@JsonSerializable()
class Meetup {
Meetup(this.index, this.locationIndex, this.time, this.registry);
Expand Down Expand Up @@ -116,10 +101,6 @@ class Meetup {
// ignore: constant_identifier_names
enum CeremonyPhase { Registering, Assigning, Attesting }

// For compatibility with substrate's naming convention.
// ignore: constant_identifier_names
enum Reputation { Unverified, UnverifiedReputable, VerifiedUnlinked, VerifiedLinked }

// -- Helper functions for above types

CeremonyPhase ceremonyPhaseTypeFromPolkadart(et.CeremonyPhaseType phase) {
Expand All @@ -137,20 +118,6 @@ CeremonyPhase? ceremonyPhaseFromString(String value) {
return getEnumFromString(CeremonyPhase.values, value);
}

Reputation? reputationFromString(String value) {
return getEnumFromString(Reputation.values, value);
}

extension ReputationExtension on Reputation {
String toValue() {
return toEnumValue(this);
}

bool isVerified() {
return this == Reputation.VerifiedUnlinked || this == Reputation.VerifiedLinked;
}
}

extension ParticipantTypeExtension on ParticipantType {
String toValue() {
return toEnumValue(this);
Expand All @@ -164,13 +131,3 @@ extension CeremonyPhaseExtension on CeremonyPhase {
return toEnumValue(this);
}
}

/// Takes a `List<dynamic>` which contains a `List<List<[int, Map<String, dynamic>]>` and
/// transforms it into a `Map<int, CommunityReputation>`.
Map<int, CommunityReputation> reputationsFromList(List<dynamic> reputationsList) {
final reputations = reputationsList.cast<List<dynamic>>();

return {
for (final cr in reputations) cr[0] as int: CommunityReputation.fromJson(cr[1] as Map<String, dynamic>),
};
}
17 changes: 0 additions & 17 deletions app/lib/models/ceremonies/ceremonies.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

62 changes: 62 additions & 0 deletions app/lib/models/ceremonies/reputation/v1.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import 'dart:convert';

import 'package:encointer_wallet/models/ceremonies/reputation/v2.dart';
import 'package:encointer_wallet/utils/enum.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:encointer_wallet/models/communities/community_identifier.dart';

part 'v1.g.dart';

@JsonSerializable()
class CommunityReputationV1 {
CommunityReputationV1(this.communityIdentifier, this.reputation);

factory CommunityReputationV1.fromJson(Map<String, dynamic> json) => _$CommunityReputationV1FromJson(json);

Map<String, dynamic> toJson() => _$CommunityReputationV1ToJson(this);

CommunityIdentifier communityIdentifier;
ReputationV1 reputation;

CommunityReputation toV2(int currentCeremonyIndex) {
return CommunityReputation(communityIdentifier, reputation.toV2(currentCeremonyIndex));
}

@override
String toString() {
return jsonEncode(this);
}
}

// For compatibility with substrate's naming convention.
// ignore: constant_identifier_names
enum ReputationV1 { Unverified, UnverifiedReputable, VerifiedUnlinked, VerifiedLinked }

extension ReputationV1Extension on ReputationV1 {
String toValue() {
return toEnumValue(this);
}

bool isVerified() {
return this == ReputationV1.VerifiedUnlinked || this == ReputationV1.VerifiedLinked;
}

Reputation toV2(int currentCeremonyIndex) {
return switch (this) {
ReputationV1.Unverified => Reputation.values.unverified(),
ReputationV1.UnverifiedReputable => Reputation.values.unverifiedReputable(),
ReputationV1.VerifiedUnlinked => Reputation.values.verifiedUnlinked(),
ReputationV1.VerifiedLinked => Reputation.values.verifiedLinked(currentCeremonyIndex),
};
}
}

/// Takes a `List<dynamic>` which contains a `List<List<[int, Map<String, dynamic>]>` and
/// transforms it into a `Map<int, CommunityReputation>`.
Map<int, CommunityReputationV1> reputationsV1FromList(List<dynamic> reputationsList) {
final reputations = reputationsList.cast<List<dynamic>>();

return {
for (final cr in reputations) cr[0] as int: CommunityReputationV1.fromJson(cr[1] as Map<String, dynamic>),
};
}
24 changes: 24 additions & 0 deletions app/lib/models/ceremonies/reputation/v1.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

156 changes: 156 additions & 0 deletions app/lib/models/ceremonies/reputation/v2.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import 'dart:convert';

import 'package:encointer_wallet/utils/enum.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:encointer_wallet/models/communities/community_identifier.dart';

part 'v2.g.dart';

@JsonSerializable()
class CommunityReputation {
CommunityReputation(this.communityIdentifier, this.reputation);

factory CommunityReputation.fromJson(Map<String, dynamic> json) => _$CommunityReputationFromJson(json);

Map<String, dynamic> toJson() => _$CommunityReputationToJson(this);

CommunityIdentifier communityIdentifier;
Reputation reputation;

@override
String toString() {
return jsonEncode(this);
}
}

abstract class Reputation {
const Reputation();

factory Reputation.fromJson(dynamic json) {
if (json.runtimeType == String) {
final variant = json as String;
switch (variant) {
case 'Unverified':
return const Unverified();
case 'UnverifiedReputable':
return const UnverifiedReputable();
case 'VerifiedUnlinked':
return const VerifiedUnlinked();
default:
throw Exception('Reputation: Invalid variant: "$variant"');
}
} else {
final variant = (json as Map<String, dynamic>).keys.first;
switch (variant) {
case 'VerifiedLinked':
return VerifiedLinked(json.values.first as int);
default:
throw Exception('Reputation: Invalid variant: "$variant"');
}
}
}

static const $Reputation values = $Reputation();

Map<String, dynamic> toJson();
}

class $Reputation {
const $Reputation();

Unverified unverified() {
return const Unverified();
}

UnverifiedReputable unverifiedReputable() {
return const UnverifiedReputable();
}

VerifiedUnlinked verifiedUnlinked() {
return const VerifiedUnlinked();
}

VerifiedLinked verifiedLinked(int value0) {
return VerifiedLinked(value0);
}
}

class Unverified extends Reputation {
const Unverified();

@override
Map<String, dynamic> toJson() => {'Unverified': null};

@override
bool operator ==(Object other) => other is Unverified;

@override
int get hashCode => runtimeType.hashCode;
}

class UnverifiedReputable extends Reputation {
const UnverifiedReputable();

@override
Map<String, dynamic> toJson() => {'UnverifiedReputable': null};

@override
bool operator ==(Object other) => other is UnverifiedReputable;

@override
int get hashCode => runtimeType.hashCode;
}

class VerifiedUnlinked extends Reputation {
const VerifiedUnlinked();

@override
Map<String, dynamic> toJson() => {'VerifiedUnlinked': null};

@override
bool operator ==(Object other) => other is VerifiedUnlinked;

@override
int get hashCode => runtimeType.hashCode;
}

class VerifiedLinked extends Reputation {
const VerifiedLinked(this.value0);

/// CeremonyIndexType
final int value0;

@override
Map<String, int> toJson() => {'VerifiedLinked': value0};

@override
bool operator ==(Object other) =>
identical(
this,
other,
) ||
other is VerifiedLinked && other.value0 == value0;

@override
int get hashCode => value0.hashCode;
}

extension ReputationExtension on Reputation {
String toValue() {
return toEnumValue(this);
}

bool isVerified() {
return runtimeType == VerifiedUnlinked || runtimeType == VerifiedLinked;
}
}

/// Takes a `List<dynamic>` which contains a `List<List<[int, Map<String, dynamic>]>` and
/// transforms it into a `Map<int, CommunityReputation>`.
Map<int, CommunityReputation> reputationsFromList(List<dynamic> reputationsList) {
final reputations = reputationsList.cast<List<dynamic>>();

return {
for (final cr in reputations) cr[0] as int: CommunityReputation.fromJson(cr[1] as Map<String, dynamic>),
};
}
17 changes: 17 additions & 0 deletions app/lib/models/ceremonies/reputation/v2.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading