tootle-linux-client/src/InstanceAccount.vala

115 lines
3.7 KiB
Vala
Raw Normal View History

2018-10-28 13:54:09 +01:00
using GLib;
public class Tootle.InstanceAccount : Object {
2018-05-27 18:25:16 +02:00
public string username {get; set;}
public string instance {get; set;}
public string client_id {get; set;}
public string client_secret {get; set;}
public string token {get; set;}
2018-10-28 13:54:09 +01:00
public bool has_unread {get; set;}
2018-05-27 18:25:16 +02:00
private Notificator? notificator;
2018-10-23 12:05:24 +02:00
public InstanceAccount () {}
2018-05-27 18:25:16 +02:00
public string get_pretty_instance () {
return instance
.replace ("https://", "")
.replace ("/","");
}
public void start_notificator () {
if (notificator != null)
notificator.close ();
notificator = new Notificator (get_stream ());
2018-07-14 10:37:41 +02:00
notificator.status_added.connect (status_added);
2018-05-27 18:25:16 +02:00
notificator.status_removed.connect (status_removed);
notificator.notification.connect (notification);
notificator.start ();
}
2018-06-13 15:13:41 +02:00
public Soup.Message get_stream () {
2018-05-27 18:25:16 +02:00
var url = "%s/api/v1/streaming/?stream=user&access_token=%s".printf (instance, token);
2018-06-13 15:13:41 +02:00
return new Soup.Message ("GET", url);
2018-05-27 18:25:16 +02:00
}
public void close_notificator () {
if (notificator != null)
notificator.close ();
}
public Json.Node serialize () {
var builder = new Json.Builder ();
builder.begin_object ();
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);
2018-05-27 18:25:16 +02:00
builder.set_member_name ("token");
2018-10-27 10:55:40 +02:00
builder.add_string_value (token);
2018-10-28 13:54:09 +01:00
builder.set_member_name ("has_unread");
builder.add_boolean_value (has_unread);
2018-05-27 18:25:16 +02:00
builder.end_object ();
return builder.get_root ();
}
public static InstanceAccount parse (Json.Object obj) {
var acc = new InstanceAccount ();
acc.username = obj.get_string_member ("username");
acc.instance = obj.get_string_member ("instance");
acc.client_id = obj.get_string_member ("id");
acc.client_secret = obj.get_string_member ("secret");
acc.token = obj.get_string_member ("token");
2018-10-28 13:54:09 +01:00
acc.has_unread = obj.get_boolean_member ("has_unread");
2018-05-27 18:25:16 +02:00
return acc;
}
2018-10-23 12:05:24 +02:00
public void notification (Notification obj) {
var title = Html.remove_tags (obj.type.get_desc (obj.account));
2018-05-27 18:25:16 +02:00
var notification = new GLib.Notification (title);
if (obj.status != null) {
var body = "";
body += get_pretty_instance ();
body += "\n";
body += Html.remove_tags (obj.status.content);
2018-05-27 18:25:16 +02:00
notification.set_body (body);
}
2018-05-30 14:58:27 +02:00
if (settings.notifications)
app.send_notification (app.application_id + ":" + obj.id.to_string (), notification);
2018-05-28 00:58:38 +02:00
2018-05-31 14:52:06 +02:00
if (accounts.formal.token == this.token)
2018-10-23 12:05:24 +02:00
network.notification (obj);
2018-05-27 18:25:16 +02:00
}
private void status_removed (int64 id) {
2018-06-13 15:13:41 +02:00
if (accounts.formal.token == this.token)
2018-05-27 18:25:16 +02:00
network.status_removed (id);
}
2018-07-14 10:37:41 +02:00
2018-10-23 12:05:24 +02:00
private void status_added (Status status) {
2018-07-14 10:37:41 +02:00
if (accounts.formal.token != this.token)
return;
var acct = status.account.acct;
var obj = new Notification (-1);
obj.type = NotificationType.WATCHLIST;
obj.account = status.account;
obj.status = status;
watchlist.users.@foreach (item => {
if (item == acct || item == "@" + acct)
2018-10-23 12:05:24 +02:00
notification (obj);
2018-10-27 10:29:25 +02:00
return true;
2018-07-14 10:37:41 +02:00
});
}
2018-05-27 18:25:16 +02:00
}