Skip to content

Commit

Permalink
feat(profile): refresh user when appropriate (#1174)
Browse files Browse the repository at this point in the history
  • Loading branch information
GeopJr authored Nov 24, 2024
1 parent 857d749 commit dc9981f
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 162 deletions.
41 changes: 37 additions & 4 deletions src/Views/Profile.vala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,25 @@ public class Tuba.Views.Profile : Views.Accounts {
Object (account: t_acc, rs: new API.Relationship.for_account (t_acc));
}

public async bool update_profile () {
Request req = new Request.GET (@"/api/v1/accounts/$(account.id)").with_account (accounts.active);

try {
yield req.await ();
var parser = Network.get_parser_from_inputstream (req.response_body);
var node = network.parse_node (parser);
var updated = API.Account.from (node);
account.patch (updated);

return true;
} catch (Error e) {
warning (@"Couldn't update account $(account.id): $(e.message)");
app.toast (e.message);
}

return false;
}

public override Gtk.Widget to_widget () {
return new Widgets.Cover (this);
}
Expand All @@ -29,6 +48,7 @@ public class Tuba.Views.Profile : Views.Accounts {
public ProfileAccount profile { get; construct set; }
public Widgets.ProfileFilterGroup.Filter filter { get; set; default = Widgets.ProfileFilterGroup.Filter.POSTS; }
public string source { get; set; default = "statuses"; }
private signal void cover_profile_update (API.Account acc);

protected Gtk.MenuButton menu_button;
protected SimpleAction muting_action;
Expand All @@ -52,6 +72,8 @@ public class Tuba.Views.Profile : Views.Accounts {
model.insert (0, profile);
model.insert (1, filter_group);
profile.rs.invalidated.connect (on_rs_updated);

if (acc.is_self ()) update_profile_cover ();
}
~Profile () {
debug ("Destroying Profile view");
Expand Down Expand Up @@ -100,6 +122,7 @@ public class Tuba.Views.Profile : Views.Accounts {
widget_cover.aria_updated.connect (on_cover_aria_update);
widget_cover.remove_css_class ("card");
widget_cover.remove_css_class ("card-spacing");
this.cover_profile_update.connect (widget_cover.update_cover_from_profile);

var row = new Gtk.ListBoxRow () {
focusable = true,
Expand Down Expand Up @@ -142,6 +165,11 @@ public class Tuba.Views.Profile : Views.Accounts {
GLib.Idle.add (append_pinned);
}

public override void on_manual_refresh () {
update_profile_cover ();
base.on_manual_refresh ();
}

protected void change_timeline_source (string t_source) {
source = t_source;

Expand Down Expand Up @@ -204,20 +232,25 @@ public class Tuba.Views.Profile : Views.Accounts {

private void on_edit_save () {
if (profile.account.is_self ()) {
model.remove (0);

// for (uint i = 0; i < model.get_n_items (); i++) {
// var status_obj = (API.Status)model.get_item (i);
// if (status_obj.formal.account.id == profile.account.id) {
// Tuba.EntityCache.remove (status_obj.formal.uri);
// }
// }

model.insert (0, new ProfileAccount (accounts.active));
on_refresh ();
this.cover_profile_update (accounts.active);
}
}

private void update_profile_cover () {
profile.update_profile.begin ((obj, res) => {
if (profile.update_profile.end (res)) {
this.cover_profile_update (profile.account);
}
});
}

protected override void clear () {
base.clear_all_but_first (2);
}
Expand Down
9 changes: 6 additions & 3 deletions src/Views/Timeline.vala
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class Tuba.Views.Timeline : AccountHolder, Streamable, Views.ContentBase

private void on_drag_end (double x, double y) {
if (scrolled.vadjustment.value == 0.0 && pull_to_refresh_spinner.margin_top >= 125) {
on_refresh ();
on_manual_refresh ();
}

is_pulling = false;
Expand All @@ -86,8 +86,8 @@ public class Tuba.Views.Timeline : AccountHolder, Streamable, Views.ContentBase
reached_close_to_top.connect (finish_queue);
#endif

app.refresh.connect (on_refresh);
status_button.clicked.connect (on_refresh);
app.refresh.connect (on_manual_refresh);
status_button.clicked.connect (on_manual_refresh);

construct_account_holder ();

Expand Down Expand Up @@ -249,6 +249,9 @@ public class Tuba.Views.Timeline : AccountHolder, Streamable, Views.ContentBase
GLib.Idle.add (request);
}

public virtual void on_manual_refresh () {
on_refresh ();
}

protected virtual void on_account_changed (InstanceAccount? acc) {
account = acc;
Expand Down
2 changes: 1 addition & 1 deletion src/Widgets/Account.vala
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public class Tuba.Widgets.Account : Gtk.ListBoxRow {
}

private void on_tuba_rs () {
if (api_account != null)
if (api_account != null && api_account.tuba_rs != null)
rs = api_account.tuba_rs;
}

Expand Down
Loading

0 comments on commit dc9981f

Please sign in to comment.