tootle-linux-client/src/API/Notification.vala

93 lines
3.0 KiB
Vala
Raw Normal View History

2018-10-30 16:57:37 +01:00
public class Tootle.Notification {
2018-04-17 14:01:55 +02:00
public int64 id;
public NotificationType type;
public string created_at;
public Status? status;
public Account? account;
2018-10-23 12:05:24 +02:00
public Notification (int64 _id) {
id = _id;
2018-04-17 14:01:55 +02:00
}
2018-10-30 16:57:37 +01:00
public static Notification parse (Json.Object obj) {
2018-04-17 14:01:55 +02:00
var id = int64.parse (obj.get_string_member ("id"));
var notification = new Notification (id);
notification.type = NotificationType.from_string (obj.get_string_member ("type"));
notification.created_at = obj.get_string_member ("created_at");
if (obj.has_member ("status"))
2018-10-29 16:43:34 +01:00
notification.status = Status.parse (obj.get_object_member ("status"));
2018-04-17 14:01:55 +02:00
if (obj.has_member ("account"))
2018-10-29 16:43:34 +01:00
notification.account = Account.parse (obj.get_object_member ("account"));
2018-04-17 14:01:55 +02:00
return notification;
}
2018-05-09 17:32:53 +02:00
2018-10-30 16:57:37 +01:00
public Json.Node? serialize () {
var builder = new Json.Builder ();
builder.begin_object ();
builder.set_member_name ("id");
builder.add_string_value (id.to_string ());
builder.set_member_name ("type");
builder.add_string_value (type.to_string ());
builder.set_member_name ("created_at");
builder.add_string_value (created_at);
if (status != null) {
builder.set_member_name ("status");
builder.add_value (status.serialize ());
}
if (account != null) {
builder.set_member_name ("account");
builder.add_value (account.serialize ());
}
builder.end_object ();
return builder.get_root ();
}
2018-05-09 17:32:53 +02:00
public static Notification parse_follow_request (Json.Object obj) {
var notification = new Notification (-1);
var account = Account.parse (obj);
notification.type = NotificationType.FOLLOW_REQUEST;
notification.account = account;
return notification;
}
2018-07-14 10:37:41 +02:00
public Soup.Message? dismiss () {
2018-10-30 16:57:37 +01:00
if (type == NotificationType.WATCHLIST) {
2018-10-31 13:05:36 +01:00
if (accounts.formal.cached_notifications.remove (this))
2018-10-30 16:57:37 +01:00
accounts.save ();
2018-07-14 10:37:41 +02:00
return null;
2018-10-30 16:57:37 +01:00
}
2018-07-14 10:37:41 +02:00
2018-05-09 17:32:53 +02:00
if (type == NotificationType.FOLLOW_REQUEST)
return reject_follow_request ();
2018-07-14 10:37:41 +02:00
var url = "%s/api/v1/notifications/dismiss?id=%lld".printf (accounts.formal.instance, id);
2018-10-30 16:57:37 +01:00
var msg = new Soup.Message ("POST", url);
network.queue (msg);
2018-05-09 17:32:53 +02:00
return msg;
}
public Soup.Message accept_follow_request () {
2018-07-14 10:37:41 +02:00
var url = "%s/api/v1/follow_requests/%lld/authorize".printf (accounts.formal.instance, account.id);
2018-10-30 16:57:37 +01:00
var msg = new Soup.Message ("POST", url);
network.queue (msg);
2018-05-09 17:32:53 +02:00
return msg;
}
public Soup.Message reject_follow_request () {
2018-07-14 10:37:41 +02:00
var url = "%s/api/v1/follow_requests/%lld/reject".printf (accounts.formal.instance, account.id);
2018-10-30 16:57:37 +01:00
var msg = new Soup.Message ("POST", url);
network.queue (msg);
2018-05-09 17:32:53 +02:00
return msg;
}
2018-04-17 14:01:55 +02:00
}