Skip to content

Commit 99ccf45

Browse files
authored
feat(secretaccount): use uuids (#699)
1 parent c627506 commit 99ccf45

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

src/Services/Accounts/AccountStore.vala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public abstract class Tuba.AccountStore : GLib.Object {
88

99
public bool ensure_active_account () {
1010
var has_active = false;
11-
var account = find_by_handle (settings.active_account);
11+
var account = find_by_uuid (settings.active_account);
1212
var clear_cache = false;
1313

1414
if (account == null && !saved.is_empty) {
@@ -62,9 +62,10 @@ public abstract class Tuba.AccountStore : GLib.Object {
6262
ensure_active_account ();
6363
}
6464

65-
public InstanceAccount? find_by_handle (string handle) {
65+
public InstanceAccount? find_by_uuid (string uuid) {
66+
if (!GLib.Uuid.string_is_valid (uuid)) return null;
6667
var iter = saved.filter (acc => {
67-
return acc.handle == handle;
68+
return acc.uuid == uuid;
6869
});
6970
iter.next ();
7071

@@ -89,7 +90,7 @@ public abstract class Tuba.AccountStore : GLib.Object {
8990
try {
9091
account.verify_credentials.end (res);
9192
account.error = null;
92-
settings.active_account = account.handle;
93+
settings.active_account = account.uuid;
9394
if (account.source != null && account.source.language != null && account.source.language != "")
9495
settings.default_language = account.source.language;
9596
}
@@ -117,6 +118,7 @@ public abstract class Tuba.AccountStore : GLib.Object {
117118
if (account == null)
118119
throw new Oopsie.INTERNAL (@"Account $handle has unknown backend: $backend");
119120

121+
if (account.uuid == null || !GLib.Uuid.string_is_valid (account.uuid)) account.uuid = GLib.Uuid.string_random ();
120122
return account;
121123
}
122124

src/Services/Accounts/InstanceAccount.vala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
1515
public const string KIND_REMOTE_REBLOG = "__remote-reblog";
1616
public const string KIND_EDITED = "update";
1717

18+
public string uuid { get; set; }
1819
public string? backend { set; get; }
1920
public API.Instance? instance_info { get; set; }
2021
public Gee.ArrayList<API.Emoji>? instance_emojis { get; set; }

src/Services/Accounts/SecretAccountStore.vala

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ public class Tuba.SecretAccountStore : AccountStore {
6161
}
6262

6363
secrets.foreach (item => {
64-
var account = secret_to_account (item);
64+
// TODO: remove uuid fallback
65+
bool force_save = false;
66+
var account = secret_to_account (item, out force_save);
6567
if (account != null && account.id != "") {
6668
new Request.GET (@"/api/v1/accounts/$(account.id)")
6769
.with_account (account)
@@ -80,6 +82,9 @@ public class Tuba.SecretAccountStore : AccountStore {
8082
.exec ();
8183
saved.add (account);
8284
account.added ();
85+
86+
// TODO: remove uuid fallback
87+
if (force_save) save ();
8388
}
8489
});
8590
changed (saved);
@@ -169,6 +174,9 @@ public class Tuba.SecretAccountStore : AccountStore {
169174
builder.set_member_name ("backend");
170175
builder.add_string_value (account.backend);
171176

177+
builder.set_member_name ("uuid");
178+
builder.add_string_value (account.uuid);
179+
172180
// If display name has emojis it's
173181
// better to save and load them
174182
// so users don't see their shortcode
@@ -217,16 +225,25 @@ public class Tuba.SecretAccountStore : AccountStore {
217225
);
218226
}
219227

220-
InstanceAccount? secret_to_account (Secret.Retrievable item) {
228+
InstanceAccount? secret_to_account (Secret.Retrievable item, out bool force_save) {
221229
InstanceAccount? account = null;
230+
231+
// TODO: remove uuid fallback
232+
force_save = false;
222233
try {
223234
var secret = item.retrieve_secret_sync ();
224235
var contents = secret.get_text ();
225236
var parser = new Json.Parser ();
226237
parser.load_from_data (contents, -1);
227-
account = accounts.create_account (parser.get_root ());
228-
}
229-
catch (GLib.Error e) {
238+
239+
// TODO: remove uuid fallback
240+
var root = parser.get_root ();
241+
bool had_uuid = root.get_object ().has_member ("uuid");
242+
account = accounts.create_account (root);
243+
244+
// TODO: remove uuid fallback
245+
force_save = !had_uuid && account.uuid != null;
246+
} catch (GLib.Error e) {
230247
warning (e.message);
231248
}
232249
return account;

src/Services/Settings.vala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
public class Tuba.Settings : GLib.Settings {
2-
32
public string active_account { get; set; }
43
public string default_language { get; set; default = "en"; }
54
public ColorScheme color_scheme { get; set; }

0 commit comments

Comments
 (0)