-
-
Notifications
You must be signed in to change notification settings - Fork 51
/
Notification.vala
228 lines (211 loc) · 7.62 KB
/
Notification.vala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
public class Tuba.API.Notification : Entity, Widgetizable {
public class RelationshipSeveranceEvent : Entity {
public string kind { get; set; }
public string target_name { get; set; default = ""; }
public int64 relationships_count { get; set; default = 0; }
public int64 followers_count { get; set; default = 0; }
public int64 following_count { get; set; default = 0; }
public string to_string () {
switch (kind) {
case "user_domain_block":
return GLib.ngettext (
// translators: the first variable is an instance (e.g. mastodon.social), the others are numbers,
// this is the singular version, '1 account you follow'
_("You have blocked %s, removing %s of your followers and %s account you follow.").printf (
"<b>%s</b>".printf (target_name),
"<b>%lld</b>".printf (followers_count),
"<b>1</b>"
),
// translators: the first variable is an instance (e.g. mastodon.social), the other two are numbers,
// this is the plural version, '4 accounts you follow'
_("You have blocked %s, removing %s of your followers and %s accounts you follow.").printf (
"<b>%s</b>".printf (target_name),
"<b>%lld</b>".printf (followers_count),
"<b>%lld</b>".printf (following_count)
),
(ulong) following_count
);
case "domain_block":
return GLib.ngettext (
// translators: the first variable is an instance (e.g. mastodon.social), the others are numbers,
// this is the singular version, '1 account you follow'
_("An admin has blocked %s, including %s of your followers and %s account you follow.").printf (
"<b>%s</b>".printf (target_name),
"<b>%lld</b>".printf (followers_count),
"<b>1</b>"
),
// translators: the first variable is an instance (e.g. mastodon.social), the other two are numbers,
// this is the plural version, '4 accounts you follow'
_("An admin has blocked %s, including %s of your followers and %s accounts you follow.").printf (
"<b>%s</b>".printf (target_name),
"<b>%lld</b>".printf (followers_count),
"<b>%lld</b>".printf (following_count)
),
(ulong) following_count
);
case "account_suspension":
// translators: the first variable is a user handle so 'them' refers to that user
return _("An admin has suspended %s, which means you can no longer receive updates from them or interact with them.").printf (
"<b>%s</b>".printf (target_name)
);
default:
assert_not_reached ();
}
}
}
public string id { get; set; }
public API.Account account { get; set; }
public string? kind { get; set; default = null; }
public string? created_at { get; set; default = null; }
public API.Status? status { get; set; default = null; }
public API.Admin.Report? report { get; set; default = null; }
// the docs claim that 'relationship_severance_event'
// is the one used but that is not true
public RelationshipSeveranceEvent? event { get; set; default = null; }
public RelationshipSeveranceEvent? relationship_severance_event { get; set; default = null; }
public override void open () {
switch (kind) {
case InstanceAccount.KIND_SEVERED_RELATIONSHIPS:
Host.open_url (@"$(accounts.active.instance)/severed_relationships");
break;
case InstanceAccount.KIND_ADMIN_REPORT:
if (report != null) {
if (accounts.active.admin_mode) {
var admin_window = new Dialogs.Admin.Window ();
admin_window.present ();
admin_window.open_reports ();
} else {
Host.open_url (@"$(accounts.active.instance)/admin/reports/$(report.id)");
}
}
break;
default:
if (status != null) {
status.open ();
} else {
account.open ();
}
break;
}
}
public override Gtk.Widget to_widget () {
switch (kind) {
case InstanceAccount.KIND_FOLLOW:
case InstanceAccount.KIND_FOLLOW_REQUEST:
this.account.tuba_rs = new API.Relationship.for_account (this.account);
return new Widgets.Account (this.account);
case InstanceAccount.KIND_ADMIN_SIGNUP:
this.account.tuba_rs = new API.Relationship.for_account (this.account);
return new Widgets.Account (this.account) {
// translators: as in just registered in the instance,
// this is a notification type only visible
// to admins
additional_label = _("Signed Up")
};
case InstanceAccount.KIND_SEVERED_RELATIONSHIPS:
RelationshipSeveranceEvent? t_event = event == null ? relationship_severance_event : event;
return create_basic_card ("tuba-heart-broken-symbolic", t_event.to_string ());
case InstanceAccount.KIND_ADMIN_REPORT:
return create_basic_card ("tuba-police-badge2-symbolic", report.to_string (this.created_at));
default:
return new Widgets.Notification (this);
}
}
private Gtk.Widget create_basic_card (string icon_name, string label) {
var box = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 16) {
margin_top = 8,
margin_bottom = 8,
margin_start = 16,
margin_end = 16
};
box.append (new Gtk.Image.from_icon_name (icon_name) {
icon_size = Gtk.IconSize.LARGE
});
box.append (new Gtk.Label (label) {
vexpand = true,
xalign = 0.0f,
use_markup = true,
css_classes = {"title"},
wrap = true,
wrap_mode = Pango.WrapMode.WORD_CHAR,
hexpand = true
});
var row = new Widgets.ListBoxRowWrapper () {
child = box,
};
row.open.connect (open);
return row;
}
public virtual async GLib.Notification to_toast (InstanceAccount issuer, int others = 0) {
Tuba.InstanceAccount.Kind res_kind;
bool should_show_buttons = issuer == accounts.active;
var kind_actor_name = account.display_name;
if (others > 0) {
// translators: <user> (& <amount> others) <actions>
// for example: GeopJr (& 10 others) mentioned you
kind_actor_name = _("%s (& %d others)").printf (account.display_name, others);
}
issuer.describe_kind (kind, out res_kind, kind_actor_name);
var toast = new GLib.Notification (res_kind.description);
if (status != null) {
var body = "";
body += HtmlUtils.remove_tags (status.content);
toast.set_body (body);
}
if (should_show_buttons) {
toast.set_default_action_and_target_value (
"app.open-status-url",
new Variant.string (
status?.url ?? account.url
)
);
switch (kind) {
case InstanceAccount.KIND_MENTION:
if (status != null) {
toast.add_button_with_target_value (
_("Reply…"),
"app.reply-to-status-uri",
new Variant.tuple ({accounts.active.id, status.uri})
);
}
break;
case InstanceAccount.KIND_FOLLOW:
toast.add_button_with_target_value (
_("Remove from Followers"),
"app.remove-from-followers",
new Variant.tuple ({accounts.active.id, account.id})
);
toast.add_button_with_target_value (
_("Follow Back"),
"app.follow-back",
new Variant.tuple ({accounts.active.id, account.id})
);
break;
case InstanceAccount.KIND_FOLLOW_REQUEST:
toast.add_button_with_target_value (
_("Decline"),
"app.answer-follow-request",
new Variant.tuple ({accounts.active.id, account.id, false})
);
toast.add_button_with_target_value (
_("Accept"),
"app.answer-follow-request",
new Variant.tuple ({accounts.active.id, account.id, true})
);
break;
}
}
Icon? icon = null;
if (Tuba.is_flatpak) {
Bytes avatar_bytes = yield Tuba.Helper.Image.request_bytes (account.avatar);
if (avatar_bytes != null)
icon = new BytesIcon (avatar_bytes);
} else {
var icon_file = GLib.File.new_for_uri (account.avatar);
icon = new FileIcon (icon_file);
}
if (icon != null)
toast.set_icon (icon);
return toast;
}
}