Add Notificator class
This commit is contained in:
parent
dac0a73bcc
commit
c7eb96b151
|
@ -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',
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -16,6 +16,7 @@ public class Tootle.HomeView : Tootle.AbstractView {
|
|||
// var s = new Status(1);
|
||||
// s.content = "Test content, wow!";
|
||||
// prepend (s);
|
||||
|
||||
request ();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue