tootle-linux-client/src/Services/Accounts/InstanceAccount.vala

86 lines
2.3 KiB
Vala
Raw Normal View History

2018-10-28 13:54:09 +01:00
using GLib;
2018-10-30 16:57:37 +01:00
using Gee;
2018-10-28 13:54:09 +01:00
2020-05-29 14:19:35 +02:00
public class Tootle.InstanceAccount : API.Account, IStreamListener {
2018-05-27 18:25:16 +02:00
2021-02-12 16:26:37 +01:00
public string? backend { set; get; }
2020-09-10 19:10:24 +02:00
public string? instance { get; set; }
public string? client_id { get; set; }
public string? client_secret { get; set; }
public string? access_token { get; set; }
2021-02-12 16:26:37 +01:00
public Error? error { get; set; }
2019-03-11 15:14:37 +01:00
2020-08-01 17:40:56 +02:00
public int64 last_seen_notification { get; set; default = 0; }
public bool has_unread_notifications { get; set; default = false; }
public ArrayList<API.Notification> cached_notifications { get; set; default = new ArrayList<API.Notification> (); }
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
protected string? stream;
2018-05-27 18:25:16 +02:00
2020-08-01 17:40:56 +02:00
public new string handle {
2020-09-05 10:02:42 +02:00
owned get { return @"@$username@$domain"; }
2020-08-01 17:40:56 +02:00
}
2020-05-29 14:19:35 +02:00
2021-02-12 16:26:37 +01:00
construct {
2020-08-01 17:40:56 +02:00
on_notification.connect (show_notification);
}
2019-03-11 15:14:37 +01:00
2020-08-01 17:40:56 +02:00
public InstanceAccount.empty (string instance){
Object (id: "", instance: instance);
}
2021-02-12 16:26:37 +01:00
~InstanceAccount () {
unsubscribe ();
2020-08-01 17:40:56 +02:00
}
public bool is_current () {
return accounts.active.access_token == access_token;
}
2021-02-12 16:26:37 +01:00
// TODO: This should be IStreamable
2020-08-01 17:40:56 +02:00
public string get_stream_url () {
return @"$instance/api/v1/streaming/?stream=user&access_token=$access_token";
}
public void subscribe () {
streams.subscribe (get_stream_url (), this, out stream);
}
public void unsubscribe () {
streams.unsubscribe (stream, this);
}
2021-02-12 16:26:37 +01:00
public async void verify_credentials () throws Error {
var req = new Request.GET ("/api/v1/accounts/verify_credentials").with_account (this);
yield req.await ();
var node = network.parse_node (req);
var updated = API.Account.from (node);
patch (updated);
message (@"$handle: profile updated");
}
2020-08-01 17:40:56 +02:00
public async Entity resolve (string url) throws Error {
message (@"Resolving URL: \"$url\"...");
var results = yield API.SearchResults.request (url, this);
var entity = results.first ();
message (@"Found $(entity.get_class ().get_name ())");
return entity;
}
2021-02-12 16:26:37 +01:00
// TODO: notification actions
2020-08-01 17:40:56 +02:00
void show_notification (API.Notification obj) {
var title = HtmlUtils.remove_tags (obj.kind.get_desc (obj.account));
2020-08-01 17:40:56 +02:00
var notification = new GLib.Notification (title);
if (obj.status != null) {
var body = "";
2020-09-05 10:02:42 +02:00
body += domain;
2020-08-01 17:40:56 +02:00
body += "\n";
body += HtmlUtils.remove_tags (obj.status.content);
2020-08-01 17:40:56 +02:00
notification.set_body (body);
}
2019-03-11 15:14:37 +01:00
2020-05-30 18:37:35 +02:00
app.send_notification (app.application_id + ":" + obj.id.to_string (), notification);
2020-08-01 17:40:56 +02:00
}
2019-03-11 15:14:37 +01:00
2018-05-27 18:25:16 +02:00
}