That fuck shit the fascists are using
1package org.tm.archive.linkpreview;
2
3import android.os.Parcel;
4import android.os.Parcelable;
5
6import androidx.annotation.NonNull;
7import androidx.annotation.Nullable;
8import androidx.core.os.ParcelCompat;
9import androidx.core.text.HtmlCompat;
10
11import com.fasterxml.jackson.annotation.JsonIgnore;
12import com.fasterxml.jackson.annotation.JsonProperty;
13
14import org.tm.archive.attachments.Attachment;
15import org.tm.archive.attachments.AttachmentId;
16import org.tm.archive.attachments.DatabaseAttachment;
17import org.tm.archive.util.JsonUtils;
18
19import java.io.IOException;
20import java.util.Optional;
21
22public class LinkPreview implements Parcelable {
23
24 @JsonProperty
25 private final String url;
26
27 @JsonProperty
28 private final String title;
29
30 @JsonProperty
31 private final String description;
32
33 @JsonProperty
34 private final long date;
35
36 @JsonProperty
37 private final AttachmentId attachmentId;
38
39 @JsonIgnore
40 private final Optional<Attachment> thumbnail;
41
42 public LinkPreview(@NonNull String url, @NonNull String title, @NonNull String description, long date, @NonNull DatabaseAttachment thumbnail) {
43 this.url = url;
44 this.title = title;
45 this.description = description;
46 this.date = date;
47 this.thumbnail = Optional.of(thumbnail);
48 this.attachmentId = thumbnail.attachmentId;
49 }
50
51 public LinkPreview(@NonNull String url, @NonNull String title, @NonNull String description, long date, @NonNull Optional<Attachment> thumbnail) {
52 this.url = url;
53 this.title = title;
54 this.description = description;
55 this.date = date;
56 this.thumbnail = thumbnail;
57 this.attachmentId = null;
58 }
59
60 public LinkPreview(@JsonProperty("url") @NonNull String url,
61 @JsonProperty("title") @NonNull String title,
62 @JsonProperty("description") @Nullable String description,
63 @JsonProperty("date") long date,
64 @JsonProperty("attachmentId") @Nullable AttachmentId attachmentId)
65 {
66 this.url = url;
67 this.title = title;
68 this.description = Optional.ofNullable(description).orElse("");
69 this.date = date;
70 this.attachmentId = attachmentId;
71 this.thumbnail = Optional.empty();
72 }
73
74 protected LinkPreview(Parcel in) {
75 url = in.readString();
76 title = in.readString();
77 description = in.readString();
78 date = in.readLong();
79 attachmentId = ParcelCompat.readParcelable(in, AttachmentId.class.getClassLoader(), AttachmentId.class);
80 thumbnail = Optional.ofNullable(ParcelCompat.readParcelable(in, Attachment.class.getClassLoader(), Attachment.class));
81 }
82
83 @Override
84 public void writeToParcel(Parcel dest, int flags) {
85 dest.writeString(url);
86 dest.writeString(title);
87 dest.writeString(description);
88 dest.writeLong(date);
89 dest.writeParcelable(attachmentId, flags);
90 dest.writeParcelable(thumbnail.orElse(null), 0);
91 }
92
93 @Override
94 public int describeContents() {
95 return 0;
96 }
97
98 public static final Creator<LinkPreview> CREATOR = new Creator<LinkPreview>() {
99 @Override
100 public LinkPreview createFromParcel(Parcel in) {
101 return new LinkPreview(in);
102 }
103
104 @Override
105 public LinkPreview[] newArray(int size) {
106 return new LinkPreview[size];
107 }
108 };
109
110 public @NonNull String getUrl() {
111 return url;
112 }
113
114 public @NonNull String getTitle() {
115 return HtmlCompat.fromHtml(title, 0).toString();
116 }
117
118 public @NonNull String getDescription() {
119 if (description.equals(title)) {
120 return "";
121 } else {
122 return HtmlCompat.fromHtml(description, 0).toString();
123 }
124 }
125
126 public long getDate() {
127 return date;
128 }
129
130 public @NonNull Optional<Attachment> getThumbnail() {
131 return thumbnail;
132 }
133
134 public @Nullable AttachmentId getAttachmentId() {
135 return attachmentId;
136 }
137
138 public @NonNull String serialize() throws IOException {
139 return JsonUtils.toJson(this);
140 }
141
142 public static @NonNull LinkPreview deserialize(@NonNull String serialized) throws IOException {
143 return JsonUtils.fromJson(serialized, LinkPreview.class);
144 }
145}