Add Notificator class

This commit is contained in:
bleakgrey 2018-05-11 18:19:48 +03:00
parent dac0a73bcc
commit c7eb96b151
5 changed files with 65 additions and 5 deletions

View File

@ -25,6 +25,7 @@ executable(
'src/NetManager.vala', 'src/NetManager.vala',
'src/Utils.vala', 'src/Utils.vala',
'src/CmdRunner.vala', 'src/CmdRunner.vala',
'src/Notificator.vala',
'src/API/Account.vala', 'src/API/Account.vala',
'src/API/Relationship.vala', 'src/API/Relationship.vala',
'src/API/Mention.vala', 'src/API/Mention.vala',

View File

@ -49,7 +49,7 @@ public class Tootle.Account{
var url = "%s/api/v1/accounts/relationships?id=%lld".printf (Tootle.settings.instance_url, id); var url = "%s/api/v1/accounts/relationships?id=%lld".printf (Tootle.settings.instance_url, id);
var msg = new Soup.Message("GET", url); var msg = new Soup.Message("GET", url);
msg.priority = Soup.MessagePriority.HIGH; msg.priority = Soup.MessagePriority.HIGH;
Tootle.network.queue(msg, (sess, mess) => { Tootle.network.queue (msg, (sess, mess) => {
try{ try{
var root = Tootle.network.parse_array (mess).get_object_element (0); var root = Tootle.network.parse_array (mess).get_object_element (0);
rs = Relationship.parse (root); 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 url = "%s/api/v1/accounts/%lld/%s".printf (Tootle.settings.instance_url, id, action);
var msg = new Soup.Message("POST", url); var msg = new Soup.Message("POST", url);
msg.priority = Soup.MessagePriority.HIGH; msg.priority = Soup.MessagePriority.HIGH;
Tootle.network.queue(msg, (sess, mess) => { Tootle.network.queue (msg, (sess, mess) => {
try{ try{
var root = Tootle.network.parse (mess); var root = Tootle.network.parse (mess);
rs = Relationship.parse (root); 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 url = "%s/api/v1/accounts/%lld/%s".printf (Tootle.settings.instance_url, id, action);
var msg = new Soup.Message("POST", url); var msg = new Soup.Message("POST", url);
msg.priority = Soup.MessagePriority.HIGH; msg.priority = Soup.MessagePriority.HIGH;
Tootle.network.queue(msg, (sess, mess) => { Tootle.network.queue (msg, (sess, mess) => {
try{ try{
var root = Tootle.network.parse (mess); var root = Tootle.network.parse (mess);
rs = Relationship.parse (root); 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 url = "%s/api/v1/accounts/%lld/%s".printf (Tootle.settings.instance_url, id, action);
var msg = new Soup.Message("POST", url); var msg = new Soup.Message("POST", url);
msg.priority = Soup.MessagePriority.HIGH; msg.priority = Soup.MessagePriority.HIGH;
Tootle.network.queue(msg, (sess, mess) => { Tootle.network.queue (msg, (sess, mess) => {
try{ try{
var root = Tootle.network.parse (mess); var root = Tootle.network.parse (mess);
rs = Relationship.parse (root); rs = Relationship.parse (root);
@ -119,5 +119,12 @@ public class Tootle.Account{
}); });
return msg; 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;
}
} }

View File

@ -13,6 +13,8 @@ public class Tootle.NetManager : GLib.Object {
private Soup.Cache cache; private Soup.Cache cache;
public string cache_path; public string cache_path;
private Notificator notificator;
construct { construct {
cache_path = "%s/%s".printf (GLib.Environment.get_user_cache_dir (), "tootle"); cache_path = "%s/%s".printf (GLib.Environment.get_user_cache_dir (), "tootle");
cache = new Soup.Cache (cache_path, Soup.CacheType.SINGLE_USER); cache = new Soup.Cache (cache_path, Soup.CacheType.SINGLE_USER);
@ -39,9 +41,27 @@ public class Tootle.NetManager : GLib.Object {
session.add_feature (cache); session.add_feature (cache);
Tootle.app.shutdown.connect (() => cache.flush ()); 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++; requests_processing++;
started (); started ();
@ -126,5 +146,7 @@ public class Tootle.NetManager : GLib.Object {
}); });
Tootle.network.queue (msg); Tootle.network.queue (msg);
} }
} }

29
src/Notificator.vala Normal file
View File

@ -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);
}
}

View File

@ -16,6 +16,7 @@ public class Tootle.HomeView : Tootle.AbstractView {
// var s = new Status(1); // var s = new Status(1);
// s.content = "Test content, wow!"; // s.content = "Test content, wow!";
// prepend (s); // prepend (s);
request (); request ();
} }