Tooth/src/API/Account.vala

107 lines
2.7 KiB
Vala
Raw Normal View History

2022-11-13 20:57:43 +00:00
public class Tooth.API.Account : Entity, Widgetizable {
2020-05-29 12:19:35 +00:00
2020-06-20 10:04:58 +00:00
public string id { get; set; }
2020-05-29 12:19:35 +00:00
public string username { get; set; }
public string acct { get; set; }
/* internal display name representation */
private string _display_name = "";
/* User's display name: Specific display name, or falling back to the
nickname */
2020-05-29 12:19:35 +00:00
public string display_name {
set {
_display_name = value;
2020-05-29 12:19:35 +00:00
}
get {
return ( ( _display_name.length > 0 ) ? _display_name : username );
2020-05-29 12:19:35 +00:00
}
2018-04-14 17:18:42 +00:00
}
2020-05-29 12:19:35 +00:00
public string note { get; set; }
public bool locked { get; set; }
2020-05-29 12:19:35 +00:00
public string header { get; set; }
public string avatar { get; set; }
public string url { get; set; }
public string created_at { get; set; }
2022-11-17 18:32:26 +00:00
public Gee.ArrayList<API.Emoji>? emojis { get; set; }
2020-05-29 12:19:35 +00:00
public int64 followers_count { get; set; }
public int64 following_count { get; set; }
2020-06-20 10:04:58 +00:00
public int64 statuses_count { get; set; }
2020-06-29 21:43:45 +00:00
public Gee.ArrayList<API.AccountField>? fields { get; set; default = null; }
public string handle {
owned get {
return "@" + acct;
}
}
2020-09-05 08:02:42 +00:00
public string domain {
owned get {
var uri = new Soup.URI (url);
return uri.get_host ();
}
}
public string full_handle {
owned get {
return @"@$username@$domain";
}
}
2022-11-17 18:32:26 +00:00
public Gee.HashMap<string, string>? emojis_map {
owned get {
return gen_emojis_map();
}
}
private Gee.HashMap<string, string>? gen_emojis_map () {
var res = new Gee.HashMap<string, string>();
if (emojis != null && emojis.size > 0) {
emojis.@foreach (e => {
res.set(e.shortcode, e.url);
return true;
});
}
return res;
2022-11-17 18:32:26 +00:00
}
2020-05-29 12:19:35 +00:00
2020-06-20 10:04:58 +00:00
public static Account from (Json.Node node) throws Error {
return Entity.from_json (typeof (API.Account), node) as API.Account;
}
2019-03-11 14:14:37 +00:00
2020-05-29 12:19:35 +00:00
public bool is_self () {
return id == accounts.active.id;
}
2020-08-01 15:40:56 +00:00
public override bool is_local (InstanceAccount account) {
2020-09-05 08:02:42 +00:00
return account.domain in url;
2020-08-01 15:40:56 +00:00
}
2020-06-29 21:43:45 +00:00
public override Gtk.Widget to_widget () {
var status = new API.Status.from_account (this);
var status_widget = new Widgets.Status (status);
status_widget.actions.visible = false;
return status_widget;
2020-06-29 21:43:45 +00:00
}
2020-08-01 15:40:56 +00:00
public override void open () {
var view = new Views.Profile (this);
2021-07-23 11:41:03 +00:00
app.main_window.open_view (view);
2020-08-01 15:40:56 +00:00
}
public override void resolve_open (InstanceAccount account) {
if (is_local (account))
open ();
else {
account.resolve.begin (url, (obj, res) => {
2022-12-11 00:23:29 +00:00
try {
account.resolve.end (res).open ();
} catch (Error e) {
warning (@"Error opening account: $(account.handle) - $(e.message)");
}
2020-08-01 15:40:56 +00:00
});
}
}
2018-04-14 12:09:06 +00:00
}