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: network cleanup #616

Merged
merged 17 commits into from
Nov 14, 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
2 changes: 1 addition & 1 deletion src/API/Attachment.vala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class Tuba.API.Attachment : Entity, Widgetizable {
string? error = null;
InputStream? in_stream = null;
network.queue (msg, null,
(sess, mess, t_is) => {
(t_is) => {
in_stream = t_is;
upload.callback ();
},
Expand Down
2 changes: 1 addition & 1 deletion src/API/Poll.vala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ public class Tuba.API.Poll : Entity, Widgetizable {
public bool multiple { get; set; }
public int64 votes_count { get; set; }
public bool voted { get; set; default = true;}
public Gee.ArrayList<int> own_votes { get; set; }
public Gee.ArrayList<int> own_votes { get; set; default = null; }
public Gee.ArrayList<PollOption>? options { get; set; default = null; }

public Poll (string _id) {
Expand Down
6 changes: 3 additions & 3 deletions src/API/Relationship.vala
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ public class Tuba.API.Relationship : Entity {
new Request.GET ("/api/v1/accounts/relationships")
.with_account (accounts.active)
.with_param ("id", id)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
Network.parse_array (msg, parser, node => {
Network.parse_array (parser, node => {
invalidate (node);
});
})
Expand All @@ -58,7 +58,7 @@ public class Tuba.API.Relationship : Entity {
public void modify (string operation, string? param = null, string? val = null) {
var req = new Request.POST (@"/api/v1/accounts/$id/$operation")
.with_account (accounts.active)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
invalidate (node);
Expand Down
2 changes: 1 addition & 1 deletion src/API/Status/PreviewCard.vala
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ public class Tuba.API.PreviewCard : Entity, Widgetizable {


new Request.GET (special_api_url)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
bool failed = true;
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
Expand Down
25 changes: 14 additions & 11 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ namespace Tuba {
public static Network network;
public static Streams streams;

public static EntityCache entity_cache;
public static ImageCache image_cache;
public static BlurhashCache blurhash_cache;
// public static EntityCache entity_cache;
// public static ImageCache image_cache;
// public static BlurhashCache blurhash_cache;

public static GLib.Regex bookwyrm_regex;
public static GLib.Regex custom_emoji_regex;
public static GLib.Regex rtl_regex;
public static bool is_rtl;

public static bool start_hidden = false;

public static bool is_flatpak = false;
public static string cache_path;

public class Application : Adw.Application {

Expand Down Expand Up @@ -161,6 +161,8 @@ namespace Tuba {
warning (e.message);
}

cache_path = GLib.Path.build_path (GLib.Path.DIR_SEPARATOR_S, GLib.Environment.get_user_cache_dir (), Build.NAME.down ());

try {
bookwyrm_regex = new GLib.Regex ("/book/\\d+/s/[-_a-z0-9]*", GLib.RegexCompileFlags.OPTIMIZE);
} catch (GLib.RegexError e) {
Expand Down Expand Up @@ -207,13 +209,13 @@ namespace Tuba {
settings = new Settings ();
streams = new Streams ();
network = new Network ();
entity_cache = new EntityCache ();
image_cache = new ImageCache () {
maintenance_secs = 60 * 5
};
blurhash_cache = new BlurhashCache () {
maintenance_secs = 30
};
// entity_cache = new EntityCache ();
// image_cache = new ImageCache () {
// maintenance_secs = 60 * 5
// };
// blurhash_cache = new BlurhashCache () {
// maintenance_secs = 30
// };
accounts = new SecretAccountStore ();
accounts.init ();

Expand Down Expand Up @@ -265,6 +267,7 @@ namespace Tuba {
#if !DEV_MODE
settings.apply ();
#endif
network.flush_cache ();

base.shutdown ();
}
Expand Down
7 changes: 3 additions & 4 deletions src/Dialogs/Composer/AttachmentsPageAttachment.vala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class Tuba.AttachmentsPageAttachment : Widgets.Attachment.Item {
pic.file = file;
} else {
entity = t_entity;
image_cache.request_paintable (t_entity.preview_url, on_cache_response);
Tuba.Helper.Image.request_paintable (t_entity.preview_url, null, on_cache_response);
}
button.child = pic;

Expand Down Expand Up @@ -72,9 +72,8 @@ public class Tuba.AttachmentsPageAttachment : Widgets.Attachment.Item {
remove_from_model ();
}

protected virtual void on_cache_response (bool is_loaded, owned Gdk.Paintable? data) {
if (is_loaded)
pic.paintable = data;
protected virtual void on_cache_response (Gdk.Paintable? data) {
pic.paintable = data;
}

public virtual signal void remove_from_model () {}
Expand Down
5 changes: 2 additions & 3 deletions src/Dialogs/Composer/Completion/EmojiProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public class Tuba.EmojiProvider: Tuba.CompletionProvider {
switch (cell.get_column ()) {
case GtkSource.CompletionColumn.ICON:
var image = new Gtk.Image ();
image_cache.request_paintable (emoji.url, (is_loaded, paintable) => {
if (is_loaded)
image.paintable = paintable;
Tuba.Helper.Image.request_paintable (emoji.url, null, (paintable) => {
image.paintable = paintable;
});
cell.set_widget (image);
break;
Expand Down
9 changes: 4 additions & 5 deletions src/Dialogs/Composer/Completion/HandleProvider.vala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class Tuba.HandleProvider: Tuba.CompletionProvider {

var results = new GLib.ListStore (typeof (Object));
var parser = Network.get_parser_from_inputstream (req.response_body);
Network.parse_array (req.msg, parser, node => {
var entity = entity_cache.lookup_or_insert (node, typeof (API.Account));
Network.parse_array (parser, node => {
var entity = Tuba.Helper.Entity.from_json (node, typeof (API.Account));
if (entity is API.Account) {
var proposal = new Proposal (entity as API.Account);
results.append (proposal);
Expand All @@ -47,9 +47,8 @@ public class Tuba.HandleProvider: Tuba.CompletionProvider {
case GtkSource.CompletionColumn.ICON:
var avatar = new Adw.Avatar (32, null, true);
avatar.name = account.display_name;
image_cache.request_paintable (account.avatar, (is_loaded, paintable) => {
if (is_loaded)
avatar.custom_image = paintable;
Tuba.Helper.Image.request_paintable (account.avatar, null, (paintable) => {
avatar.custom_image = paintable;
});
cell.set_widget (avatar);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/Dialogs/ListEdit.vala
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ public class Tuba.Dialogs.ListEdit : Adw.PreferencesWindow {
private void update_members () {
new Request.GET (@"/api/v1/lists/$(list.id)/accounts")
.with_account (accounts.active)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
if (Network.get_array_size (parser) > 0) {
this.add (members_page);

Network.parse_array (msg, parser, node => {
Network.parse_array (parser, node => {
var member = API.Account.from (node);
var avi = new Widgets.Avatar () {
account = member,
Expand Down
2 changes: 1 addition & 1 deletion src/Dialogs/NewAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ public class Tuba.Dialogs.NewAccount: Adw.Window {
deck.push (done_step);

debug ("Switching to account");
accounts.activate (account);
accounts.activate (account, true);
}

public void redirect (Uri uri) {
Expand Down
2 changes: 1 addition & 1 deletion src/Dialogs/Preferences.vala
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public class Tuba.Dialogs.Preferences : Adw.PreferencesWindow {
new Request.PATCH ("/api/v1/accounts/update_credentials")
.with_account (accounts.active)
.with_form_data ("source[language]", new_lang)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
var updated = API.Account.from (node);
Expand Down
14 changes: 6 additions & 8 deletions src/Dialogs/ProfileEdit.vala
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,8 @@ public class Tuba.Dialogs.ProfileEdit : Adw.Window {
int64 max_value_length;
public ProfileEdit (API.Account acc) {
profile = acc;
image_cache.request_paintable (acc.header, on_background_cache_response);
image_cache.request_paintable (acc.avatar, on_avi_cache_response);
Tuba.Helper.Image.request_paintable (acc.header, null, on_background_cache_response);
Tuba.Helper.Image.request_paintable (acc.avatar, null, on_avi_cache_response);
avi.text = acc.display_name;
name_row.text = acc.display_name;
bio_text_view.buffer.text = acc.source?.note ?? "";
Expand Down Expand Up @@ -204,14 +204,12 @@ public class Tuba.Dialogs.ProfileEdit : Adw.Window {
fields_box.add (field);
}

void on_background_cache_response (bool is_loaded, owned Gdk.Paintable? data) {
if (is_loaded)
background.paintable = data;
void on_background_cache_response (Gdk.Paintable? data) {
background.paintable = data;
}

void on_avi_cache_response (bool is_loaded, owned Gdk.Paintable? data) {
if (is_loaded)
avi.custom_image = data;
void on_avi_cache_response (Gdk.Paintable? data) {
avi.custom_image = data;
}

File new_avi;
Expand Down
9 changes: 6 additions & 3 deletions src/Services/Accounts/AccountStore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ 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 clear_cache = false;

if (account == null && !saved.is_empty) {
account = saved[0];
clear_cache = true;
}

has_active = account != null;
activate (account);
activate (account, clear_cache);

if (!has_active)
app.present_window (true);
Expand Down Expand Up @@ -72,7 +74,7 @@ public abstract class Tuba.AccountStore : GLib.Object {
return iter.@get ();
}

public void activate (InstanceAccount? account) {
public void activate (InstanceAccount? account, bool clear_cache = false) {
if (active != null)
active.deactivated ();

Expand All @@ -81,7 +83,8 @@ public abstract class Tuba.AccountStore : GLib.Object {
return;
} else {
debug (@"Activating $(account.handle)…");
entity_cache.nuke ();
if (clear_cache)
network.clear_cache ();
account.verify_credentials.begin ((obj, res) => {
try {
account.verify_credentials.end (res);
Expand Down
8 changes: 4 additions & 4 deletions src/Services/Accounts/InstanceAccount.vala
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
public void gather_instance_info () {
new Request.GET ("/api/v1/instance")
.with_account (this)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
instance_info = API.Instance.from (node);
Expand All @@ -320,7 +320,7 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
public void gather_instance_custom_emojis () {
new Request.GET ("/api/v1/custom_emojis")
.with_account (this)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
Value res_emojis;
Expand All @@ -336,7 +336,7 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
new Request.GET ("/api/v1/notifications")
.with_account (this)
.with_param ("min_id", last_read_id.to_string ())
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var array = Network.get_array_mstd (parser);
if (array != null) {
Expand All @@ -353,7 +353,7 @@ public class Tuba.InstanceAccount : API.Account, Streamable {
public virtual void check_notifications () {
new Request.GET ("/api/v1/markers?timeline[]=notifications")
.with_account (this)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var root = network.parse (parser);
if (!root.has_member ("notifications")) return;
Expand Down
10 changes: 5 additions & 5 deletions src/Services/Accounts/Mastodon/Account.vala
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ public class Tuba.Mastodon.Account : InstanceAccount {
public override void reply_to_status_uri (string issuer_id, string uri) {
if (!check_issuer (issuer_id)) return;

if (entity_cache.contains (uri)) {
var status = entity_cache.lookup (uri) as API.Status;
new Dialogs.Compose.reply (status.formal);
} else {
// if (Tuba.EntityCache.contains (uri)) {
// var status = Tuba.EntityCache.lookup (uri) as API.Status;
// new Dialogs.Compose.reply (status.formal);
// } else {
resolve.begin (uri, (obj, res) => {
try {
var status = resolve.end (res) as API.Status;
Expand All @@ -303,6 +303,6 @@ public class Tuba.Mastodon.Account : InstanceAccount {
warning (e.message);
}
});
}
// }
}
}
2 changes: 1 addition & 1 deletion src/Services/Accounts/SecretAccountStore.vala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Tuba.SecretAccountStore : AccountStore {
if (account != null && account.id != "") {
new Request.GET (@"/api/v1/accounts/$(account.id)")
.with_account (account)
.then ((sess, msg, in_stream) => {
.then ((in_stream) => {
var parser = Network.get_parser_from_inputstream (in_stream);
var node = network.parse_node (parser);
var acc = API.Account.from (node);
Expand Down
Loading