tootle-linux-client/src/InstanceAccount.vala

161 lines
5.2 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; }
public string 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-05-29 14:19:35 +02:00
public string handle {
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 ("/","");
}
}
public InstanceAccount (Json.Object obj) {
Object (
username: obj.get_string_member ("username"),
instance: obj.get_string_member ("instance"),
client_id: obj.get_string_member ("id"),
client_secret: obj.get_string_member ("secret"),
token: obj.get_string_member ("access_token"),
last_seen_notification: obj.get_int_member ("last_seen_notification"),
has_unread_notifications: obj.get_boolean_member ("has_unread_notifications")
);
var cached = obj.get_object_member ("cached_profile");
var account = new API.Account (cached);
patch (account);
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
var notifications = obj.get_array_member ("cached_notifications");
notifications.foreach_element ((arr, i, node) => {
var notification = new API.Notification (node.get_object ());
cached_notifications.add (notification);
});
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){
Object (id: 0, instance: instance);
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public InstanceAccount.from_account (API.Account account) {
Object (id: account.id);
patch (account);
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 InstanceAccount patch (API.Account account) {
Utils.merge (this, account);
return this;
}
2018-10-29 16:43:34 +01:00
public bool is_current () {
2020-05-29 14:19:35 +02:00
return accounts.active.token == 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 () {
return @"$instance/api/v1/streaming/?stream=user&access_token=$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);
}
public override Json.Node? serialize () {
2018-05-27 18:25:16 +02:00
var builder = new Json.Builder ();
builder.begin_object ();
2020-05-29 14:19:35 +02:00
2018-05-27 18:25:16 +02:00
builder.set_member_name ("hash");
builder.add_string_value ("test");
builder.set_member_name ("username");
2018-10-27 10:55:40 +02:00
builder.add_string_value (username);
2018-05-27 18:25:16 +02:00
builder.set_member_name ("instance");
2018-10-27 10:55:40 +02:00
builder.add_string_value (instance);
2018-05-27 18:25:16 +02:00
builder.set_member_name ("id");
2018-10-27 10:55:40 +02:00
builder.add_string_value (client_id);
2018-05-27 18:25:16 +02:00
builder.set_member_name ("secret");
2018-10-27 10:55:40 +02:00
builder.add_string_value (client_secret);
2020-05-29 14:19:35 +02:00
builder.set_member_name ("access_token");
2018-10-27 10:55:40 +02:00
builder.add_string_value (token);
2018-10-29 16:43:34 +01:00
builder.set_member_name ("last_seen_notification");
builder.add_int_value (last_seen_notification);
builder.set_member_name ("has_unread_notifications");
builder.add_boolean_value (has_unread_notifications);
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
var cached_profile = base.serialize ();
builder.set_member_name ("cached_profile");
builder.add_value (cached_profile);
2018-10-30 16:57:37 +01:00
builder.set_member_name ("cached_notifications");
builder.begin_array ();
cached_notifications.@foreach (notification => {
var node = notification.serialize ();
if (node != null)
builder.add_value (node);
return true;
});
builder.end_array ();
2019-03-11 15:14:37 +01:00
2018-05-27 18:25:16 +02:00
builder.end_object ();
return builder.get_root ();
}
2019-03-11 15:14:37 +01:00
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
// protected void on_status_added (API.Status status) { //TODO: Watchlist
2020-05-30 12:31:02 +02:00
// if (!is_current ())
// return;
// watchlist.users.@foreach (item => {
// var acct = status.account.acct;
// if (item == acct || item == "@" + acct) {
// var obj = new API.Notification (-1);
// obj.kind = API.NotificationType.WATCHLIST;
// obj.account = status.account;
// obj.status = status;
// on_notification (obj);
// }
// return true;
// });
// }
2018-05-27 18:25:16 +02:00
}