1
0
mirror of https://gitlab.gnome.org/World/tootle synced 2025-02-17 03:51:11 +01:00

Rewrite notificator handling

This commit is contained in:
bleakgrey 2018-06-20 18:50:42 +03:00
parent 8498548584
commit c7e7deea93
10 changed files with 90 additions and 59 deletions

View File

@ -62,6 +62,7 @@ executable(
'src/Views/FollowingView.vala',
'src/Views/FavoritesView.vala',
'src/Views/SearchView.vala',
'src/Views/HashtagView.vala',
dependencies: [
dependency('gtk+-3.0'),
dependency('glib-2.0', version: '>=2.30.0'),

View File

@ -43,7 +43,7 @@ public class Tootle.Desktop {
FileOutputStream stream = file.create (FileCreateFlags.PRIVATE);
stream.write (data);
}
app.toast ("Media downloaded");
app.toast (_("Media downloaded"));
} catch (Error e) {
app.toast (e.message);
warning ("Error: %s\n", e.message);

View File

@ -18,6 +18,10 @@ public class Tootle.Notificator : GLib.Object {
this.msg.priority = Soup.MessagePriority.VERY_HIGH;
}
public string get_url () {
return msg.get_uri ().to_string (false);
}
public string get_name () {
var name = msg.get_uri ().to_string (true);
if ("&access_token" in name) {
@ -29,9 +33,12 @@ public class Tootle.Notificator : GLib.Object {
}
public async void start () {
if (connection != null)
return;
try {
info ("Starting notificator: %s", get_name ());
connection = yield Tootle.network.stream (msg);
connection = yield network.stream (msg);
connection.error.connect (on_error);
connection.message.connect (on_message);
connection.closed.connect (on_closed);

View File

@ -2,11 +2,6 @@ public class Tootle.DirectView : TimelineView {
public DirectView () {
base ("direct");
notificator = new Notificator (get_stream ());
notificator.status_added.connect ((ref status) => {
if (settings.live_updates)
on_status_added (ref status);
});
}
public override string get_icon () {
@ -17,7 +12,7 @@ public class Tootle.DirectView : TimelineView {
return _("Direct Messages");
}
protected Soup.Message get_stream () {
public override Soup.Message? get_stream () {
var url = "%s/api/v1/streaming/?stream=direct&access_token=%s".printf (accounts.formal.instance, accounts.formal.token);
return new Soup.Message("GET", url);
}

View File

@ -2,11 +2,6 @@ public class Tootle.FederatedView : TimelineView {
public FederatedView () {
base ("public");
notificator = new Notificator (get_stream ());
notificator.status_added.connect ((ref status) => {
if (settings.live_updates_public)
on_status_added (ref status);
});
}
public override string get_icon () {
@ -17,7 +12,11 @@ public class Tootle.FederatedView : TimelineView {
return _("Federated Timeline");
}
protected Soup.Message get_stream () {
protected override bool is_public () {
return true;
}
public override Soup.Message? get_stream () {
var url = "%s/api/v1/streaming/?stream=public&access_token=%s".printf (accounts.formal.instance, accounts.formal.token);
return new Soup.Message("GET", url);
}

View File

@ -0,0 +1,20 @@
public class Tootle.HashtagView : TimelineView {
public HashtagView (string hashtag) {
base ("tag/" + hashtag);
}
public string get_hashtag () {
return this.timeline.substring (4);
}
public override string get_name () {
return get_hashtag ();
}
public override Soup.Message? get_stream () {
var url = "%s/api/v1/streaming/?stream=hashtag&tag=%s&access_token=%s".printf (accounts.formal.instance, get_hashtag (), accounts.formal.token);
return new Soup.Message("GET", url);
}
}

View File

@ -2,11 +2,6 @@ public class Tootle.HomeView : TimelineView {
public HomeView () {
base ("home");
notificator = new Notificator (accounts.formal.get_stream ());
notificator.status_added.connect ((ref status) => {
if (settings.live_updates)
on_status_added (ref status);
});
}
public override string get_icon () {
@ -16,5 +11,9 @@ public class Tootle.HomeView : TimelineView {
public override string get_name () {
return _("Home");
}
public override Soup.Message? get_stream () {
return accounts.formal.get_stream ();
}
}

View File

@ -2,11 +2,6 @@ public class Tootle.LocalView : TimelineView {
public LocalView () {
base ("public");
notificator = new Notificator (get_stream ());
notificator.status_added.connect ((ref status) => {
if (settings.live_updates_public)
on_status_added (ref status);
});
}
public override string get_icon () {
@ -23,7 +18,11 @@ public class Tootle.LocalView : TimelineView {
return url;
}
protected Soup.Message get_stream () {
protected override bool is_public () {
return true;
}
public override Soup.Message? get_stream () {
var url = "%s/api/v1/streaming/?stream=public:local&access_token=%s".printf (accounts.formal.instance, accounts.formal.token);
return new Soup.Message("GET", url);
}

View File

@ -10,22 +10,7 @@ public class Tootle.TimelineView : AbstractView {
protected string? page_next;
protected string? page_prev;
private Notificator? _notificator;
public Notificator? notificator {
get {
return _notificator;
}
set {
if (_notificator != null)
_notificator.close ();
_notificator = value;
if (_notificator != null)
_notificator.start ();
}
}
protected Notificator? notificator;
public TimelineView (string timeline, string pars = "") {
base ();
@ -39,6 +24,7 @@ public class Tootle.TimelineView : AbstractView {
notificator.close ();
});
setup_notificator ();
request ();
}
@ -128,7 +114,7 @@ public class Tootle.TimelineView : AbstractView {
var msg = new Soup.Message("GET", get_url ());
msg.finished.connect (() => empty_state ());
Tootle.network.queue(msg, (sess, mess) => {
network.queue(msg, (sess, mess) => {
try{
Tootle.network.parse_array (mess).foreach_element ((array, i, node) => {
var object = node.get_object ();
@ -151,19 +137,56 @@ public class Tootle.TimelineView : AbstractView {
request ();
}
public virtual Soup.Message? get_stream (){
return null;
}
public virtual void on_account_changed (Account? account){
if(account == null)
return;
if (notificator != null) {
notificator.close ();
notificator.start ();
var stream = get_stream ();
if (notificator != null && stream != null) {
var old_url = notificator.get_url ();
var new_url = stream.get_uri ().to_string (false);
if (old_url != new_url) {
info ("Updating notificator %s", notificator.get_name ());
setup_notificator ();
}
}
on_refresh ();
}
public override void bottom_reached (){
protected void setup_notificator () {
if (notificator != null)
notificator.close ();
var stream = get_stream ();
if (stream == null)
return;
notificator = new Notificator (stream);
notificator.status_added.connect ((ref status) => {
if (can_stream ())
on_status_added (ref status);
});
notificator.start ();
}
protected virtual bool is_public () {
return false;
}
protected virtual bool can_stream () {
var allowed_public = true;
if (is_public ())
allowed_public = settings.live_updates_public;
return settings.live_updates && allowed_public;
}
protected override void bottom_reached (){
if (is_last_page) {
debug ("Last page reached");
return;

View File

@ -48,19 +48,7 @@ public class Tootle.RichLabel : Gtk.Label {
if ("/tags/" in url){
var encoded = url.split("/tags/")[1];
var hashtag = Soup.URI.decode (encoded);
var msg_url = "%s/api/v1/streaming/?stream=hashtag&access_token=%s&tag=%s"
.printf (accounts.formal.instance, accounts.formal.token, encoded);
var msg = new Soup.Message("GET", msg_url);
var timeline = new TimelineView ("tag/" + hashtag);
timeline.notificator = new Notificator (msg);
timeline.notificator.status_added.connect ((ref status) => {
if (settings.live_updates)
timeline.on_status_added (ref status);
});
window.open_view (timeline);
window.open_view (new HashtagView (hashtag));
return true;
}