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

feat(secretaccount): use uuids #699

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 6 additions & 4 deletions src/Services/Accounts/AccountStore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public abstract class Tuba.AccountStore : GLib.Object {

public bool ensure_active_account () {
var has_active = false;
var account = find_by_handle (settings.active_account);
var account = find_by_uuid (settings.active_account);
var clear_cache = false;

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

public InstanceAccount? find_by_handle (string handle) {
public InstanceAccount? find_by_uuid (string uuid) {
if (!GLib.Uuid.string_is_valid (uuid)) return null;
var iter = saved.filter (acc => {
return acc.handle == handle;
return acc.uuid == uuid;
});
iter.next ();

Expand All @@ -89,7 +90,7 @@ public abstract class Tuba.AccountStore : GLib.Object {
try {
account.verify_credentials.end (res);
account.error = null;
settings.active_account = account.handle;
settings.active_account = account.uuid;
if (account.source != null && account.source.language != null && account.source.language != "")
settings.default_language = account.source.language;
}
Expand Down Expand Up @@ -117,6 +118,7 @@ public abstract class Tuba.AccountStore : GLib.Object {
if (account == null)
throw new Oopsie.INTERNAL (@"Account $handle has unknown backend: $backend");

if (account.uuid == null || !GLib.Uuid.string_is_valid (account.uuid)) account.uuid = GLib.Uuid.string_random ();
return account;
}

Expand Down
1 change: 1 addition & 0 deletions src/Services/Accounts/InstanceAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
public const string KIND_REMOTE_REBLOG = "__remote-reblog";
public const string KIND_EDITED = "update";

public string uuid { get; set; }
public string? backend { set; get; }
public API.Instance? instance_info { get; set; }
public Gee.ArrayList<API.Emoji>? instance_emojis { get; set; }
Expand Down
27 changes: 22 additions & 5 deletions src/Services/Accounts/SecretAccountStore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,9 @@ public class Tuba.SecretAccountStore : AccountStore {
}

secrets.foreach (item => {
var account = secret_to_account (item);
// TODO: remove uuid fallback
bool force_save = false;
var account = secret_to_account (item, out force_save);
if (account != null && account.id != "") {
new Request.GET (@"/api/v1/accounts/$(account.id)")
.with_account (account)
Expand All @@ -80,6 +82,9 @@ public class Tuba.SecretAccountStore : AccountStore {
.exec ();
saved.add (account);
account.added ();

// TODO: remove uuid fallback
if (force_save) save ();
}
});
changed (saved);
Expand Down Expand Up @@ -169,6 +174,9 @@ public class Tuba.SecretAccountStore : AccountStore {
builder.set_member_name ("backend");
builder.add_string_value (account.backend);

builder.set_member_name ("uuid");
builder.add_string_value (account.uuid);

// If display name has emojis it's
// better to save and load them
// so users don't see their shortcode
Expand Down Expand Up @@ -217,16 +225,25 @@ public class Tuba.SecretAccountStore : AccountStore {
);
}

InstanceAccount? secret_to_account (Secret.Retrievable item) {
InstanceAccount? secret_to_account (Secret.Retrievable item, out bool force_save) {
InstanceAccount? account = null;

// TODO: remove uuid fallback
force_save = false;
try {
var secret = item.retrieve_secret_sync ();
var contents = secret.get_text ();
var parser = new Json.Parser ();
parser.load_from_data (contents, -1);
account = accounts.create_account (parser.get_root ());
}
catch (GLib.Error e) {

// TODO: remove uuid fallback
var root = parser.get_root ();
bool had_uuid = root.get_object ().has_member ("uuid");
account = accounts.create_account (root);

// TODO: remove uuid fallback
force_save = !had_uuid && account.uuid != null;
} catch (GLib.Error e) {
warning (e.message);
}
return account;
Expand Down
1 change: 0 additions & 1 deletion src/Services/Settings.vala
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
public class Tuba.Settings : GLib.Settings {

public string active_account { get; set; }
public string default_language { get; set; default = "en"; }
public ColorScheme color_scheme { get; set; }
Expand Down