1
+ import 'package:drift/drift.dart' ;
2
+
3
+ class LegacyAccount {
4
+ final Uri ? realm;
5
+ final String ? apiKey;
6
+ final String ? email;
7
+ final int ? userId;
8
+ final String ? zulipVersion;
9
+ final int ? zulipFeatureLevel;
10
+ final String ? ackedPushToken;
11
+ final DateTime ? lastDismissedServerPushSetupNotice;
12
+ final DateTime ? lastDismissedServerNotifsExpiringBanner;
13
+ final bool ? silenceServerPushSetupWarnings;
14
+
15
+ LegacyAccount ({
16
+ this .realm,
17
+ this .apiKey,
18
+ this .email,
19
+ this .userId,
20
+ this .zulipVersion,
21
+ this .zulipFeatureLevel,
22
+ this .ackedPushToken,
23
+ this .lastDismissedServerPushSetupNotice,
24
+ this .lastDismissedServerNotifsExpiringBanner,
25
+ this .silenceServerPushSetupWarnings,
26
+ });
27
+
28
+ factory LegacyAccount .fromJson (
29
+ Map <String , dynamic > json, {
30
+ ValueSerializer ? serializer,
31
+ }) {
32
+ serializer ?? = driftRuntimeOptions.defaultSerializer;
33
+ return LegacyAccount (
34
+ realm: serializer.fromJson <Uri ?>(json['realm' ]),
35
+ apiKey: serializer.fromJson <String ?>(json['apiKey' ]),
36
+ email: serializer.fromJson <String ?>(json['email' ]),
37
+ userId: serializer.fromJson <int ?>(json['userId' ]),
38
+ zulipVersion: serializer.fromJson <String ?>(json['zulipVersion' ]),
39
+ zulipFeatureLevel: serializer.fromJson <int ?>(json['zulipFeatureLevel' ]),
40
+ ackedPushToken: serializer.fromJson <String ?>(json['ackedPushToken' ]),
41
+ lastDismissedServerPushSetupNotice: serializer.fromJson <DateTime ?>(
42
+ json['lastDismissedServerPushSetupNotice' ]),
43
+ lastDismissedServerNotifsExpiringBanner: serializer.fromJson <DateTime ?>(
44
+ json['lastDismissedServerNotifsExpiringBanner' ]),
45
+ silenceServerPushSetupWarnings: serializer.fromJson <bool ?>(
46
+ json['silenceServerPushSetupWarnings' ]),
47
+ );
48
+ }
49
+
50
+ @override
51
+ String toString () {
52
+ return 'LegacyAccount{realm: $realm , apiKey: $apiKey ,'
53
+ ' email: $email , userId: $userId , zulipVersion: $zulipVersion ,'
54
+ ' zulipFeatureLevel: $zulipFeatureLevel , ackedPushToken: $ackedPushToken ,'
55
+ ' lastDismissedServerPushSetupNotice: $lastDismissedServerPushSetupNotice ,'
56
+ ' lastDismissedServerNotifsExpiringBanner: $lastDismissedServerNotifsExpiringBanner ,'
57
+ ' silenceServerPushSetupWarnings: $silenceServerPushSetupWarnings }' ;
58
+ }
59
+
60
+
61
+ /// This method should return the json data of the account in the latest version
62
+ /// of migrations or null if the data can't be migrated.
63
+ static Map <String , dynamic >? applyMigrations (Map <String , dynamic > json, int version) {
64
+ if (version < 9 ) {
65
+ // json['ackedPushToken'] should be set to null
66
+ json['ackedPushToken' ] = null ;
67
+ }
68
+
69
+ if (version < 11 ) {
70
+ // removes multiple trailing slashes from json['realm'].
71
+ json['realm' ] = json['realm' ].replaceAll (RegExp (r'/+$' ), '' );
72
+ }
73
+
74
+ if (version < 12 ) {
75
+ // Add zulipVersion to accounts.
76
+ json['zulipVersion' ] = null ;
77
+ }
78
+
79
+ // if (version < 13) {
80
+ // this should convert json['zulipVersion'] from `string | null` to `ZulipVersion | null`
81
+ // but we already have it as `string | null` in this app so no point of
82
+ // doing this then making it string back
83
+ // }
84
+
85
+ if (version < 14 ) {
86
+ // Add zulipFeatureLevel to accounts.
87
+ json['zulipFeatureLevel' ] = null ;
88
+ }
89
+
90
+ if (version < 15 ) {
91
+ // json['realm'] is a string not uri
92
+ json['realm' ] = Uri .parse (json['realm' ] as String );
93
+ }
94
+
95
+ if (version < 27 ) {
96
+ // Remove accounts with "in-progress" login state (empty json['email'])
97
+ // make all fields null
98
+ if (json['email' ] == null || json['email' ] == '' ) {
99
+ return null ;
100
+ }
101
+ }
102
+
103
+ if (version < 33 ) {
104
+ // Add userId to accounts.
105
+ json['userId' ] = null ;
106
+ }
107
+
108
+ if (version < 36 ) {
109
+ // Add lastDismissedServerPushSetupNotice to accounts.
110
+ json['lastDismissedServerPushSetupNotice' ] = null ;
111
+
112
+ }
113
+
114
+ if (version < 58 ) {
115
+ const requiredKeys = [
116
+ 'realm' ,
117
+ 'apiKey' ,
118
+ 'email' ,
119
+ 'userId' ,
120
+ 'zulipVersion' ,
121
+ 'zulipFeatureLevel' ,
122
+ 'ackedPushToken' ,
123
+ 'lastDismissedServerPushSetupNotice' ,
124
+ ];
125
+ bool hasAllRequiredKeys = requiredKeys.every ((key) => json.containsKey (key));
126
+ if (! hasAllRequiredKeys) {
127
+ return null ;
128
+ }
129
+ }
130
+
131
+ if (version < 62 ) {
132
+ // Add silenceServerPushSetupWarnings to accounts.
133
+ json['silenceServerPushSetupWarnings' ] = false ;
134
+ }
135
+
136
+ if (version < 66 ) {
137
+ // Add lastDismissedServerNotifsExpiringBanner to accounts.
138
+ json['lastDismissedServerNotifsExpiringBanner' ] = null ;
139
+ }
140
+ return json;
141
+ }
142
+ }
0 commit comments