tootle-linux-client/src/InstanceAccount.vala

89 lines
2.4 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
2020-05-29 14:19:35 +02:00
public string instance { get; set; }
public string client_id { get; set; }
public string client_secret { get; set; }
2020-06-20 12:04:58 +02:00
public string access_token { get; set; }
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +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-06-29 23:43:45 +02:00
public new string handle {
2020-05-29 14:19:35 +02:00
owned get { return @"@$username@$short_instance"; }
2018-10-30 16:57:37 +01:00
}
2020-05-29 14:19:35 +02:00
public string short_instance {
owned get {
return instance
.replace ("https://", "")
.replace ("/","");
}
}
2020-06-20 12:04:58 +02:00
public static InstanceAccount from (Json.Node node) throws Error {
return Entity.from_json (typeof (InstanceAccount), node) as InstanceAccount;
}
2020-06-20 12:04:58 +02:00
public InstanceAccount () {
on_notification.connect (show_notification);
2018-05-27 18:25:16 +02:00
}
2020-05-29 14:19:35 +02:00
~InstanceAccount () {
unsubscribe ();
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public InstanceAccount.empty (string instance){
2020-06-20 12:04:58 +02:00
Object (
id: "",
instance: instance
);
2020-05-29 14:19:35 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public InstanceAccount.from_account (API.Account account) {
2020-06-20 12:04:58 +02:00
Object (
id: account.id
);
2020-05-29 14:19:35 +02:00
patch (account);
2018-05-27 18:25:16 +02:00
}
2019-03-11 15:14:37 +01:00
2018-10-29 16:43:34 +01:00
public bool is_current () {
2020-06-20 12:04:58 +02:00
return accounts.active.access_token == access_token;
2018-10-29 16:43:34 +01:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public string get_stream_url () {
2020-06-20 12:04:58 +02:00
return @"$instance/api/v1/streaming/?stream=user&access_token=$access_token";
2018-05-27 18:25:16 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public void subscribe () {
streams.subscribe (get_stream_url (), this, out stream);
2018-05-27 18:25:16 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public void unsubscribe () {
streams.unsubscribe (stream, this);
}
protected void show_notification (API.Notification obj) {
2020-05-29 14:19:35 +02:00
var title = Html.remove_tags (obj.kind.get_desc (obj.account));
2018-05-27 18:25:16 +02:00
var notification = new GLib.Notification (title);
if (obj.status != null) {
var body = "";
2020-05-29 14:19:35 +02:00
body += short_instance;
2018-05-27 18:25:16 +02:00
body += "\n";
body += Html.remove_tags (obj.status.content);
2018-05-27 18:25:16 +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);
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
if (obj.kind == API.NotificationType.WATCHLIST) {
2018-10-30 16:57:37 +01:00
cached_notifications.add (obj);
accounts.save ();
}
2018-05-27 18:25:16 +02:00
}
2019-03-11 15:14:37 +01:00
2018-05-27 18:25:16 +02:00
}