Tooth/src/API/Status.vala

133 lines
4.0 KiB
Vala
Raw Normal View History

2020-05-29 12:19:35 +00:00
using Gee;
2022-11-13 20:57:43 +00:00
public class Tooth.API.Status : Entity, Widgetizable {
2020-05-29 12:19:35 +00:00
2021-07-23 11:41:03 +00:00
~Status () {
message ("[OBJ] Destroyed "+uri);
}
2020-06-20 10:04:58 +00:00
public string id { get; set; }
public API.Account account { get; set; }
2020-05-29 12:19:35 +00: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 10:04:58 +00:00
public bool favourited { get; set; default = false; }
public bool bookmarked { get; set; default = false; }
2020-05-29 12:19:35 +00:00
public bool sensitive { get; set; default = false; }
public bool muted { get; set; default = false; }
public bool pinned { get; set; default = false; }
2021-07-23 11:41:03 +00:00
public string visibility { get; set; default = "public"; } // TODO: Bring back default post visibility preference
2020-05-29 12:19:35 +00:00
public API.Status? reblog { get; set; default = null; }
public ArrayList<API.Mention>? mentions { get; set; default = null; }
2020-06-20 10:04:58 +00:00
public ArrayList<API.Attachment>? media_attachments { get; set; default = null; }
public API.Poll? poll { get; set; default = null; }
2020-06-20 10:04:58 +00:00
public string? t_url { get; set; }
2020-06-20 10:04:58 +00:00
public string url {
owned get { return this.get_modified_url (); }
set { this.t_url = value; }
2020-06-20 10:04:58 +00:00
}
string get_modified_url () {
if (this.t_url == null) {
2020-06-20 10:04:58 +00:00
return this.uri.replace ("/activity", "");
}
return this.t_url;
2020-06-20 10:04:58 +00:00
}
2020-05-29 12:19:35 +00:00
public Status formal {
get { return reblog ?? this; }
2018-04-14 12:09:06 +00:00
}
2019-03-11 14:14:37 +00:00
2020-06-20 10:04:58 +00:00
public bool has_spoiler {
2020-05-29 12:19:35 +00:00
get {
2020-06-20 10:04:58 +00:00
return formal.sensitive ||
!(formal.spoiler_text == null || formal.spoiler_text == "");
2020-05-29 12:19:35 +00:00
}
}
2019-03-11 14:14:37 +00:00
2021-07-23 11:41:03 +00:00
public bool can_be_boosted {
get {
return this.formal.visibility != "direct";
}
}
2020-06-20 10:04:58 +00:00
public static Status from (Json.Node node) throws Error {
return Entity.from_json (typeof (API.Status), node) as API.Status;
}
2020-05-29 12:19:35 +00:00
public Status.empty () {
2020-06-01 00:54:20 +00:00
Object (
2021-07-23 11:41:03 +00:00
id: ""
2020-06-01 00:54:20 +00:00
);
2018-04-14 12:09:06 +00:00
}
2019-03-11 14:14:37 +00:00
2020-05-29 12:19:35 +00:00
public Status.from_account (API.Account account) {
Object (
2020-06-20 10:04:58 +00:00
id: "",
2020-05-29 12:19:35 +00:00
account: account,
created_at: account.created_at
);
if (account.note == "")
content = "";
else if ("\n" in account.note)
content = account.note.split ("\n")[0];
2020-05-29 12:19:35 +00:00
else
content = account.note;
2020-05-29 12:19:35 +00:00
}
2020-06-03 15:06:11 +00:00
public override Gtk.Widget to_widget () {
return new Widgets.Status (this);
2020-06-03 15:06:11 +00:00
}
2020-08-01 15:40:56 +00:00
public override void open () {
var view = new Views.Thread (formal);
2021-07-23 11:41:03 +00:00
app.main_window.open_view (view);
2020-08-01 15:40:56 +00:00
}
2018-05-21 15:23:31 +00:00
public bool is_owned (){
2020-05-29 12:19:35 +00:00
return formal.account.id == accounts.active.id;
2019-03-12 08:12:53 +00:00
}
2020-07-30 19:02:03 +00:00
public bool has_media () {
2021-07-23 11:41:03 +00:00
return media_attachments != null && !media_attachments.is_empty;
2020-07-30 19:02:03 +00:00
}
2021-07-23 11:41:03 +00:00
public virtual string get_reply_mentions () {
2018-06-06 14:49:13 +00:00
var result = "";
2020-05-29 12:19:35 +00:00
if (account.acct != accounts.active.acct)
2020-09-05 08:02:42 +00:00
result = @"$(account.handle) ";
2019-03-11 14:14:37 +00:00
2018-06-06 14:49:13 +00:00
if (mentions != null) {
2018-07-11 17:13:52 +00:00
foreach (var mention in mentions) {
2020-05-29 12:19:35 +00:00
var equals_current = mention.acct == accounts.active.acct;
2018-07-11 17:13:52 +00:00
var already_mentioned = mention.acct in result;
2019-03-11 14:14:37 +00:00
2021-07-23 11:41:03 +00:00
if (!equals_current && !already_mentioned)
2020-09-05 08:02:42 +00:00
result += @"$(mention.handle) ";
2018-07-11 17:13:52 +00:00
}
2018-06-06 14:49:13 +00:00
}
2019-03-11 14:14:37 +00:00
2018-06-06 14:49:13 +00:00
return result;
}
2019-03-11 14:14:37 +00:00
2021-02-02 13:34:36 +00:00
public Request action (string action) {
2021-07-23 11:41:03 +00:00
var req = new Request.POST (@"/api/v1/statuses/$(formal.id)/$action").with_account (accounts.active);
req.priority = Soup.MessagePriority.HIGH;
return req;
}
2020-07-30 19:02:03 +00:00
public Request annihilate () {
return new Request.DELETE (@"/api/v1/statuses/$id")
.with_account (accounts.active);
2018-05-22 12:47:25 +00:00
}
2018-04-14 12:09:06 +00:00
}