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

85 lines
2.6 KiB
Vala
Raw Normal View History

2020-05-29 14:19:35 +02:00
public class Tootle.API.Notification : GLib.Object {
2018-04-17 14:01:55 +02:00
2020-05-29 14:19:35 +02:00
public int64 id { get; construct set; }
public Account account { get; construct set; }
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public NotificationType kind { get; set; }
public string created_at { get; set; }
public Status? status { get; set; default = null; }
2018-04-17 14:01:55 +02:00
2020-05-29 14:19:35 +02:00
public Notification (Json.Object obj) throws Oopsie {
Object (
id: int64.parse (obj.get_string_member ("id")),
kind: NotificationType.from_string (obj.get_string_member ("type")),
created_at: obj.get_string_member ("created_at"),
account: new Account (obj.get_object_member ("account"))
);
2019-03-11 15:14:37 +01:00
2018-04-17 14:01:55 +02:00
if (obj.has_member ("status"))
2020-05-29 14:19:35 +02:00
status = new Status (obj.get_object_member ("status"));
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public Notification.follow_request (Json.Object obj) {
Object (
id: 0,
kind: NotificationType.FOLLOW_REQUEST,
account: new Account (obj)
);
2018-04-17 14:01:55 +02:00
}
2019-03-11 15:14:37 +01: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");
2020-05-29 14:19:35 +02:00
builder.add_string_value (kind.to_string ());
2018-10-30 16:57:37 +01:00
builder.set_member_name ("created_at");
builder.add_string_value (created_at);
2019-03-11 15:14:37 +01:00
2018-10-30 16:57:37 +01:00
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 ());
}
2019-03-11 15:14:37 +01:00
2018-10-30 16:57:37 +01:00
builder.end_object ();
return builder.get_root ();
}
2019-03-11 15:14:37 +01:00
2018-07-14 10:37:41 +02:00
public Soup.Message? dismiss () {
2020-05-29 14:19:35 +02:00
if (kind == NotificationType.WATCHLIST) {
if (accounts.active.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
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
if (kind == NotificationType.FOLLOW_REQUEST)
2018-05-09 17:32:53 +02:00
return reject_follow_request ();
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
var req = new Request.POST ("/api/v1/notifications/dismiss")
.with_account (accounts.active)
.with_param ("id", id.to_string ())
.exec ();
return req;
2018-05-09 17:32:53 +02:00
}
2019-03-11 15:14:37 +01:00
2018-05-09 17:32:53 +02:00
public Soup.Message accept_follow_request () {
2020-05-29 14:19:35 +02:00
var req = new Request.POST (@"/api/v1/follow_requests/$(account.id)/authorize")
.with_account (accounts.active)
.exec ();
return req;
2018-05-09 17:32:53 +02:00
}
2019-03-11 15:14:37 +01:00
2018-05-09 17:32:53 +02:00
public Soup.Message reject_follow_request () {
2020-05-29 14:19:35 +02:00
var req = new Request.POST (@"/api/v1/follow_requests/$(account.id)/reject")
.with_account (accounts.active)
.exec ();
return req;
2018-05-09 17:32:53 +02:00
}
2018-04-17 14:01:55 +02:00
}