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

128 lines
4.1 KiB
Vala
Raw Normal View History

2020-05-29 14:19:35 +02:00
using Gee;
2020-06-20 12:04:58 +02:00
public class Tootle.API.Status : Entity, Widgetizable {
2020-05-29 14:19:35 +02:00
2020-06-20 12:04:58 +02:00
public string id { get; set; }
public API.Account account { get; set; }
2020-05-29 14:19:35 +02:00
public string uri { get; set; }
public string? spoiler_text { get; set; default = null; }
public string? in_reply_to_id { get; set; default = null; }
public string? in_reply_to_account_id { get; set; default = null; }
public string content { get; set; default = ""; }
public int64 replies_count { get; set; default = 0; }
public int64 reblogs_count { get; set; default = 0; }
public int64 favourites_count { get; set; default = 0; }
public string created_at { get; set; default = "0"; }
public bool reblogged { get; set; default = false; }
2020-06-20 12:04:58 +02:00
public bool favourited { get; set; default = false; }
2020-05-29 14:19:35 +02:00
public bool sensitive { get; set; default = false; }
public bool muted { get; set; default = false; }
public bool pinned { get; set; default = false; }
2020-06-20 12:04:58 +02:00
public API.Visibility visibility { get; set; default = settings.default_post_visibility; }
2020-05-29 14:19:35 +02:00
public API.Status? reblog { get; set; default = null; }
public ArrayList<API.Mention>? mentions { get; set; default = null; }
2020-06-20 12:04:58 +02:00
public ArrayList<API.Attachment>? media_attachments { get; set; default = null; }
public string? _url { get; set; }
public string url {
owned get { return this.get_modified_url (); }
set { this._url = value; }
}
string get_modified_url () {
if (this._url == null) {
return this.uri.replace ("/activity", "");
}
return this._url;
}
2020-05-29 14:19:35 +02:00
public Status formal {
get { return reblog ?? this; }
2018-04-14 14:09:06 +02:00
}
2019-03-11 15:14:37 +01:00
2020-06-20 12:04:58 +02:00
public bool has_spoiler {
2020-05-29 14:19:35 +02:00
get {
2020-06-20 12:04:58 +02:00
return formal.sensitive ||
!(formal.spoiler_text == null || formal.spoiler_text == "");
2020-05-29 14:19:35 +02:00
}
}
2019-03-11 15:14:37 +01:00
2020-06-20 12:04:58 +02:00
public static Status from (Json.Node node) throws Error {
return Entity.from_json (typeof (API.Status), node) as API.Status;
}
2020-05-29 14:19:35 +02:00
public Status.empty () {
2020-06-01 02:54:20 +02:00
Object (
2020-06-20 12:04:58 +02:00
id: "",
2020-06-01 02:54:20 +02:00
visibility: settings.default_post_visibility
);
2018-04-14 14:09:06 +02:00
}
2019-03-11 15:14:37 +01:00
2020-05-29 14:19:35 +02:00
public Status.from_account (API.Account account) {
Object (
2020-06-20 12:04:58 +02:00
id: "",
2020-05-29 14:19:35 +02:00
account: account,
created_at: account.created_at
);
if (account.note == "")
content = "";
else if ("\n" in account.note)
content = Html.remove_tags (account.note.split ("\n")[0]);
else
content = Html.remove_tags (account.note);
}
2020-06-03 17:06:11 +02:00
public override Gtk.Widget to_widget () {
var w = new Widgets.Status (this);
w.button_press_event.connect (w.open);
return w;
}
2018-05-21 17:23:31 +02:00
public bool is_owned (){
2020-05-29 14:19:35 +02:00
return formal.account.id == accounts.active.id;
2019-03-12 09:12:53 +01:00
}
2018-06-06 16:49:13 +02:00
public string get_reply_mentions () {
var result = "";
2020-05-29 14:19:35 +02:00
if (account.acct != accounts.active.acct)
2018-06-06 16:49:13 +02:00
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) {
2020-05-29 14:19:35 +02:00
var equals_current = mention.acct == accounts.active.acct;
2018-07-11 19:13:52 +02:00
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
2020-05-29 14:19:35 +02:00
public void action (string action, owned Network.ErrorCallback? err = network.on_error) {
new Request.POST (@"/api/v1/statuses/$(formal.id)/$action")
.with_account (accounts.active)
2020-06-20 12:04:58 +02:00
.then ((sess, msg) => {
var node = network.parse_node (msg);
var upd = API.Status.from (node).formal;
patch (upd);
2020-05-29 14:19:35 +02:00
})
.on_error ((status, reason) => err (status, reason))
.exec ();
}
2020-05-29 14:19:35 +02:00
public void poof (owned Soup.SessionCallback? cb = null, owned Network.ErrorCallback? err = network.on_error) {
new Request.DELETE (@"/api/v1/statuses/$id")
.with_account (accounts.active)
.then ((sess, msg) => {
2020-05-30 12:31:02 +02:00
streams.force_delete (id);
2020-05-29 14:19:35 +02:00
cb (sess, msg);
})
.on_error ((status, reason) => err (status, reason))
.exec ();
2018-05-22 14:47:25 +02:00
}
2018-04-14 14:09:06 +02:00
}