diff --git a/meson.build b/meson.build index 65997b7..1a15c13 100644 --- a/meson.build +++ b/meson.build @@ -25,6 +25,7 @@ executable( 'src/NetManager.vala', 'src/Utils.vala', 'src/CmdRunner.vala', + 'src/Notificator.vala', 'src/API/Account.vala', 'src/API/Relationship.vala', 'src/API/Mention.vala', diff --git a/src/API/Account.vala b/src/API/Account.vala index dc79075..6a03c71 100644 --- a/src/API/Account.vala +++ b/src/API/Account.vala @@ -49,7 +49,7 @@ public class Tootle.Account{ var url = "%s/api/v1/accounts/relationships?id=%lld".printf (Tootle.settings.instance_url, id); var msg = new Soup.Message("GET", url); msg.priority = Soup.MessagePriority.HIGH; - Tootle.network.queue(msg, (sess, mess) => { + Tootle.network.queue (msg, (sess, mess) => { try{ var root = Tootle.network.parse_array (mess).get_object_element (0); rs = Relationship.parse (root); @@ -68,7 +68,7 @@ public class Tootle.Account{ var url = "%s/api/v1/accounts/%lld/%s".printf (Tootle.settings.instance_url, id, action); var msg = new Soup.Message("POST", url); msg.priority = Soup.MessagePriority.HIGH; - Tootle.network.queue(msg, (sess, mess) => { + Tootle.network.queue (msg, (sess, mess) => { try{ var root = Tootle.network.parse (mess); rs = Relationship.parse (root); @@ -87,7 +87,7 @@ public class Tootle.Account{ var url = "%s/api/v1/accounts/%lld/%s".printf (Tootle.settings.instance_url, id, action); var msg = new Soup.Message("POST", url); msg.priority = Soup.MessagePriority.HIGH; - Tootle.network.queue(msg, (sess, mess) => { + Tootle.network.queue (msg, (sess, mess) => { try{ var root = Tootle.network.parse (mess); rs = Relationship.parse (root); @@ -106,7 +106,7 @@ public class Tootle.Account{ var url = "%s/api/v1/accounts/%lld/%s".printf (Tootle.settings.instance_url, id, action); var msg = new Soup.Message("POST", url); msg.priority = Soup.MessagePriority.HIGH; - Tootle.network.queue(msg, (sess, mess) => { + Tootle.network.queue (msg, (sess, mess) => { try{ var root = Tootle.network.parse (mess); rs = Relationship.parse (root); @@ -119,5 +119,12 @@ public class Tootle.Account{ }); return msg; } + + public Soup.Message get_stream () { + var url = "%sapi/v1/streaming/?stream=user&access_token=%s".printf (Tootle.settings.instance_url, Tootle.settings.access_token); + var msg = new Soup.Message("GET", url); + msg.priority = Soup.MessagePriority.VERY_HIGH; + return msg; + } } diff --git a/src/NetManager.vala b/src/NetManager.vala index 9bb7c07..1dcbde9 100644 --- a/src/NetManager.vala +++ b/src/NetManager.vala @@ -13,6 +13,8 @@ public class Tootle.NetManager : GLib.Object { private Soup.Cache cache; public string cache_path; + private Notificator notificator; + construct { cache_path = "%s/%s".printf (GLib.Environment.get_user_cache_dir (), "tootle"); cache = new Soup.Cache (cache_path, Soup.CacheType.SINGLE_USER); @@ -39,9 +41,27 @@ public class Tootle.NetManager : GLib.Object { session.add_feature (cache); Tootle.app.shutdown.connect (() => cache.flush ()); } + + Tootle.accounts.switched.connect (acc => { + if (acc == null) { + if (notificator != null) + notificator.close (); + return; + } + notificator = new Notificator (acc); + //notificator.start (); + }); } - public Soup.Message queue(Soup.Message msg, Soup.SessionCallback? cb = null) { + public void abort (Soup.Message msg) { + session.cancel_message (msg, 0); + } + + public async WebsocketConnection stream (Soup.Message msg) { + return yield session.websocket_connect_async (msg, null, null, null); + } + + public Soup.Message queue (Soup.Message msg, Soup.SessionCallback? cb = null) { requests_processing++; started (); @@ -126,5 +146,7 @@ public class Tootle.NetManager : GLib.Object { }); Tootle.network.queue (msg); } + + } diff --git a/src/Notificator.vala b/src/Notificator.vala new file mode 100644 index 0000000..cda4e4a --- /dev/null +++ b/src/Notificator.vala @@ -0,0 +1,29 @@ +using GLib; +using Soup; + +public class Tootle.Notificator : GLib.Object { + + weak Account account; + WebsocketConnection? connection; + + public Notificator (Account acc){ + Object (); + account = acc; + } + + public async void start () { + var msg = account.get_stream (); + connection = yield Tootle.network.stream (msg); + connection.error.connect (e => error (e.message)); + connection.message.connect ((i, bytes) => { + warning ((string)bytes.get_data ()); + }); + debug ("Receiving notifications for %lld", account.id); + } + + public void close () { + debug ("Closing notifications for %lld", account.id); + connection.close (0, null); + } + +} diff --git a/src/Views/HomeView.vala b/src/Views/HomeView.vala index 776ac47..d1e678a 100644 --- a/src/Views/HomeView.vala +++ b/src/Views/HomeView.vala @@ -16,6 +16,7 @@ public class Tootle.HomeView : Tootle.AbstractView { // var s = new Status(1); // s.content = "Test content, wow!"; // prepend (s); + request (); }