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

236 lines
8.9 KiB
Vala
Raw Normal View History

2019-03-11 15:14:37 +01:00
public class Tootle.API.Status {
2018-04-14 14:09:06 +02:00
2018-10-23 12:05:24 +02:00
public signal void updated ();
2018-05-01 15:53:46 +02:00
2019-03-11 15:14:37 +01:00
public API.Account account;
2018-04-14 14:09:06 +02:00
public int64 id;
public string uri;
public string url;
2018-04-23 18:43:29 +02:00
public string? spoiler_text;
2018-04-14 14:09:06 +02:00
public string content;
2018-10-24 12:50:40 +02:00
public int64 replies_count;
2018-04-14 14:09:06 +02:00
public int64 reblogs_count;
public int64 favourites_count;
2018-10-24 14:01:32 +02:00
public string created_at;
2018-05-22 15:18:39 +02:00
public bool reblogged = false;
public bool favorited = false;
public bool sensitive = false;
public bool muted = false;
public bool pinned = false;
2019-03-11 15:14:37 +01:00
public API.StatusVisibility visibility;
public API.Status? reblog;
public API.Mention[]? mentions;
public API.Attachment[]? attachments;
2018-04-14 14:09:06 +02:00
2018-10-23 12:05:24 +02:00
public Status (int64 _id) {
id = _id;
2018-04-14 14:09:06 +02:00
}
2019-03-11 15:14:37 +01:00
2018-10-23 12:05:24 +02:00
public Status get_formal () {
return reblog != null ? reblog : this;
}
2018-04-14 14:09:06 +02:00
2018-10-24 14:01:32 +02:00
public static Status parse (Json.Object obj) {
2018-04-14 14:09:06 +02:00
var id = int64.parse (obj.get_string_member ("id"));
var status = new Status (id);
2019-03-11 15:14:37 +01:00
2018-05-03 10:11:31 +02:00
status.account = Account.parse (obj.get_object_member ("account"));
2018-04-14 14:09:06 +02:00
status.uri = obj.get_string_member ("uri");
status.created_at = obj.get_string_member ("created_at");
2018-10-24 12:50:40 +02:00
status.replies_count = obj.get_int_member ("replies_count");
2018-04-14 14:09:06 +02:00
status.reblogs_count = obj.get_int_member ("reblogs_count");
status.favourites_count = obj.get_int_member ("favourites_count");
status.content = Html.simplify ( obj.get_string_member ("content"));
2018-05-04 22:57:31 +02:00
status.sensitive = obj.get_boolean_member ("sensitive");
2018-05-11 13:28:49 +02:00
status.visibility = StatusVisibility.from_string (obj.get_string_member ("visibility"));
2019-03-11 15:14:37 +01:00
2018-07-08 02:47:35 +02:00
if (obj.has_member ("url"))
status.url = obj.get_string_member ("url");
else
status.url = obj.get_string_member ("uri").replace ("/activity", "");
2019-03-11 15:14:37 +01:00
2018-05-04 22:57:31 +02:00
var spoiler = obj.get_string_member ("spoiler_text");
if (spoiler != "")
status.spoiler_text = Html.simplify (spoiler);
2019-03-11 15:14:37 +01:00
2018-05-22 15:18:39 +02:00
if (obj.has_member ("reblogged"))
2018-05-04 22:57:31 +02:00
status.reblogged = obj.get_boolean_member ("reblogged");
2018-05-22 15:18:39 +02:00
if (obj.has_member ("favourited"))
2018-05-04 22:57:31 +02:00
status.favorited = obj.get_boolean_member ("favourited");
2018-05-22 15:18:39 +02:00
if (obj.has_member ("muted"))
status.muted = obj.get_boolean_member ("muted");
if (obj.has_member ("pinned"))
status.pinned = obj.get_boolean_member ("pinned");
2019-03-11 15:14:37 +01:00
2018-05-22 15:18:39 +02:00
if (obj.has_member ("reblog") && obj.get_null_member("reblog") != true)
2018-05-04 22:57:31 +02:00
status.reblog = Status.parse (obj.get_object_member ("reblog"));
2019-03-11 15:14:37 +01:00
API.Mention[]? _mentions = {};
2018-04-28 18:27:10 +02:00
obj.get_array_member ("mentions").foreach_element ((array, i, node) => {
var object = node.get_object ();
if (object != null)
2019-03-11 15:14:37 +01:00
_mentions += API.Mention.parse (object);
2018-04-28 18:27:10 +02:00
});
if (_mentions.length > 0)
status.mentions = _mentions;
2019-03-11 15:14:37 +01:00
API.Attachment[]? _attachments = {};
2018-05-04 22:57:31 +02:00
obj.get_array_member ("media_attachments").foreach_element ((array, i, node) => {
var object = node.get_object ();
if (object != null)
2019-03-11 15:14:37 +01:00
_attachments += API.Attachment.parse (object);
2018-05-04 22:57:31 +02:00
});
if (_attachments.length > 0)
status.attachments = _attachments;
2019-03-11 15:14:37 +01:00
2018-04-14 14:09:06 +02:00
return status;
}
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 ("uri");
builder.add_string_value (uri);
builder.set_member_name ("url");
builder.add_string_value (url);
builder.set_member_name ("content");
builder.add_string_value (content);
builder.set_member_name ("created_at");
builder.add_string_value (created_at);
builder.set_member_name ("visibility");
builder.add_string_value (visibility.to_string ());
builder.set_member_name ("sensitive");
builder.add_boolean_value (sensitive);
builder.set_member_name ("sensitive");
builder.add_boolean_value (sensitive);
builder.set_member_name ("replies_count");
builder.add_int_value (replies_count);
builder.set_member_name ("favourites_count");
builder.add_int_value (favourites_count);
builder.set_member_name ("reblogs_count");
builder.add_int_value (reblogs_count);
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
if (spoiler_text != null) {
builder.set_member_name ("spoiler_text");
builder.add_string_value (spoiler_text);
}
if (reblog != null) {
builder.set_member_name ("reblog");
builder.add_value (reblog.serialize ());
}
if (attachments != null) {
builder.set_member_name ("media_attachments");
builder.begin_array ();
2019-03-11 15:14:37 +01:00
foreach (API.Attachment attachment in attachments)
2018-10-30 16:57:37 +01:00
builder.add_value (attachment.serialize ());
builder.end_array ();
}
if (mentions != null) {
builder.set_member_name ("mentions");
builder.begin_array ();
2019-03-11 15:14:37 +01:00
foreach (API.Mention mention in mentions)
2018-10-30 16:57:37 +01:00
builder.add_value (mention.serialize ());
builder.end_array ();
}
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-05-21 17:23:31 +02:00
public bool is_owned (){
2018-10-23 12:05:24 +02:00
return get_formal ().account.id == accounts.current.id;
2018-05-21 17:23:31 +02:00
}
2019-03-11 15:14:37 +01:00
2019-03-12 09:12:53 +01:00
public bool has_spoiler () {
return get_formal ().spoiler_text != null || get_formal ().sensitive;
}
2018-06-06 16:49:13 +02:00
public string get_reply_mentions () {
var result = "";
if (account.acct != accounts.current.acct)
result = "@%s ".printf (account.acct);
2019-03-11 15:14:37 +01:00
2018-06-06 16:49:13 +02:00
if (mentions != null) {
2018-07-11 19:13:52 +02:00
foreach (var mention in mentions) {
var equals_current = mention.acct == accounts.current.acct;
var already_mentioned = mention.acct in result;
2019-03-11 15:14:37 +01:00
2018-07-11 19:13:52 +02:00
if (!equals_current && ! already_mentioned)
2018-06-06 16:49:13 +02:00
result += "@%s ".printf (mention.acct);
2018-07-11 19:13:52 +02:00
}
2018-06-06 16:49:13 +02:00
}
2019-03-11 15:14:37 +01:00
2018-06-06 16:49:13 +02:00
return result;
}
2019-03-11 15:14:37 +01:00
2019-03-14 12:55:27 +01:00
public void set_reblogged (bool rebl, Network.ErrorCallback? err = network.on_error) {
2018-05-01 15:53:46 +02:00
var action = rebl ? "reblog" : "unreblog";
2018-10-24 12:50:40 +02:00
var msg = new Soup.Message ("POST", "%s/api/v1/statuses/%lld/%s".printf (accounts.formal.instance, id, action));
2018-05-01 15:53:46 +02:00
msg.priority = Soup.MessagePriority.HIGH;
2019-03-14 12:55:27 +01:00
network.inject (msg, Network.INJECT_TOKEN);
network.queue (msg, (sess, message) => {
reblogged = rebl;
updated ();
}, (status, reason) => {
err (status, reason);
});
2018-05-01 15:53:46 +02:00
}
2019-03-11 15:14:37 +01:00
2019-03-14 12:55:27 +01:00
public void set_favorited (bool fav, Network.ErrorCallback? err = network.on_error) {
2018-05-01 15:53:46 +02:00
var action = fav ? "favourite" : "unfavourite";
2018-10-24 12:50:40 +02:00
var msg = new Soup.Message ("POST", "%s/api/v1/statuses/%lld/%s".printf (accounts.formal.instance, id, action));
2018-05-01 15:53:46 +02:00
msg.priority = Soup.MessagePriority.HIGH;
2019-03-14 12:55:27 +01:00
network.inject (msg, Network.INJECT_TOKEN);
network.queue (msg, (sess, message) => {
favorited = fav;
updated ();
}, (status, reason) => {
err (status, reason);
});
2018-05-01 15:53:46 +02:00
}
2019-03-11 15:14:37 +01:00
2019-03-14 12:55:27 +01:00
public void set_muted (bool mute, Network.ErrorCallback? err = network.on_error) {
2018-05-22 15:18:39 +02:00
var action = mute ? "mute" : "unmute";
2018-10-24 12:50:40 +02:00
var msg = new Soup.Message ("POST", "%s/api/v1/statuses/%lld/%s".printf (accounts.formal.instance, id, action));
2018-05-22 15:18:39 +02:00
msg.priority = Soup.MessagePriority.HIGH;
2019-03-14 12:55:27 +01:00
network.inject (msg, Network.INJECT_TOKEN);
network.queue (msg, (sess, message) => {
muted = mute;
updated ();
}, (status, reason) => {
err (status, reason);
});
2018-05-22 15:18:39 +02:00
}
2019-03-11 15:14:37 +01:00
2019-03-14 12:55:27 +01:00
public void set_pinned (bool pin, Network.ErrorCallback? err = network.on_error) {
var action = pin ? "pin" : "unpin";
2018-10-24 12:50:40 +02:00
var msg = new Soup.Message ("POST", "%s/api/v1/statuses/%lld/%s".printf (accounts.formal.instance, id, action));
msg.priority = Soup.MessagePriority.HIGH;
2019-03-14 12:55:27 +01:00
network.inject (msg, Network.INJECT_TOKEN);
network.queue (msg, (sess, message) => {
pinned = pin;
updated ();
}, (status, reason) => {
err (status, reason);
});
}
2019-03-14 12:55:27 +01:00
public void poof (Soup.SessionCallback? cb = null, Network.ErrorCallback? err = network.on_error) {
2018-10-24 12:50:40 +02:00
var msg = new Soup.Message ("DELETE", "%s/api/v1/statuses/%lld".printf (accounts.formal.instance, id));
2018-05-22 14:47:25 +02:00
msg.priority = Soup.MessagePriority.HIGH;
2019-03-14 12:55:27 +01:00
network.inject (msg, Network.INJECT_TOKEN);
network.queue (msg, (sess, message) => {
network.status_removed (id);
if (cb != null)
cb (sess, message);
}, (status, reason) => {
err (status, reason);
});
2018-05-22 14:47:25 +02:00
}
2018-04-14 14:09:06 +02:00
}