Debian Codegraph @

plugin/mastodon_account_viewer/relational_container.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 require_relative 'relational_menu'
4
5 module Plugin::MastodonAccountViewer
6 class RelationalContainer < Gtk::Box
7 ICON_SIZE = Gdk::Rectangle.new(0, 0, 32, 32).freeze
8 ARROW_SIZE = Gdk::Rectangle.new(0, 0, 16, 16).freeze
9
10 attr_reader :my_account, :counterpart
11
12 def initialize(my_account, counterpart, updater)
13 super(:horizontal, ICON_SIZE.width / 2)
14 @my_account = my_account
15 @counterpart = counterpart
16 @updater = updater
17 @following = @follower = @blocked = @mute = :unknown
18 @transaction_level = 0
19
20 pack_start(Gtk::WebIcon.new(my_account.account.icon, ICON_SIZE).set_tooltip_text(my_account.title), expand: false)
21 pack_start(gen_follow_relation, expand: false)
22 pack_start(Gtk::WebIcon.new(counterpart.icon, ICON_SIZE).set_tooltip_text(counterpart.title), expand: false)
23 unless me?
24 pack_start(followbutton, expand: false)
25 pack_start(menubutton, expand: false)
26 retrieve_relation_status
27 end
28 end
29
30 def me?
31 my_account.account == counterpart
32 end
33
34 def followee?
35 @following == true
36 end
37
38 def follower?
39 @follower == true
40 end
41
42 def blocked?
43 @blocked == true
44 end
45
46 def mute?
47 @mute == true
48 end
49
50 # 対象をフォロー/リムーブする。
51 # 実際にMastodonにリクエストするのは呼び出し元プラグインへのコールバックで行う
52 def request_update_follow_status(new_follow_status)
53 promise = if new_follow_status
54 @updater.request_follow(my_account, counterpart)
55 else
56 @updater.request_unfollow(my_account, counterpart)
57 end
58 input_exclusive(
59 promise.next do
60 relation_transaction do
61 @follower = new_follow_status
62 end
63 end
64 )
65 end
66
67 # 対象をミュート/解除する。
68 # 実際にMastodonにリクエストするのは呼び出し元プラグインへのコールバックで行う
69 def request_update_mute_status(new_mute_status)
70 promise = if new_mute_status
71 @updater.request_mute(my_account, counterpart)
72 else
73 @updater.request_unmute(my_account, counterpart)
74 end
75 input_exclusive(
76 promise.next do
77 relation_transaction do
78 @mute = new_mute_status
79 end
80 end
81 )
82 end
83
84 # 対象をブロック/解除する。
85 # 実際にMastodonにリクエストするのは呼び出し元プラグインへのコールバックで行う
86 def request_update_block_status(new_block_status)
87 promise = if new_block_status
88 @updater.request_block(my_account, counterpart)
89 else
90 @updater.request_unblock(my_account, counterpart)
91 end
92 input_exclusive(
93 promise.next do
94 relation_transaction do
95 @blocked = new_block_status
96 end
97 end
98 )
99 end
100
101 private
102
103 def update_button_sensitivity(new_stat)
104 followbutton.sensitive = new_stat unless followbutton.destroyed?
105 menubutton.sensitive = new_stat unless menubutton.destroyed?
106 self
107 end
108
109 def following_label
110 @following_label ||= Gtk::Label.new(_('関係を取得中'))
111 end
112
113 def followed_label
114 @followed_label ||= Gtk::Label.new('')
115 end
116
117 def gen_follow_relation
118 if me?
119 Gtk::Label.new(_('それはあなたです!'))
120 else
121 Gtk::Box.new(:vertical, 0)
122 .pack_start(gen_following_relation, expand: false)
123 .pack_start(gen_followed_relation, expand: false)
124 end
125 end
126
127 def gen_following_relation
128 Gtk::Box.new(:horizontal)
129 .pack_start(following_arrow_widget, expand: false)
130 .pack_start(following_label, expand: false)
131 end
132
133 def gen_followed_relation
134 Gtk::Box.new(:horizontal)
135 .pack_start(followed_arrow_widget, expand: false)
136 .pack_start(followed_label, expand: false)
137 end
138
139 def followbutton
140 @followbutton ||= Gtk::Button.new.tap do |b|
141 b.sensitive = false
142 b.ssc(:clicked) do
143 if blocked?
144 request_update_block_status(false)
145 else
146 request_update_follow_status(!followee?)
147 end
148 end
149 end
150 end
151
152 def menubutton
153 @menubutton ||= Gtk::Button.new(label: ' … ').tap do |b|
154 b.sensitive = false
155 b.ssc(:clicked) do
156 Plugin::MastodonAccountViewer::RelationalMenu.new(self).show_all.popup(nil, nil, 0, 0)
157 true
158 end
159 end
160 end
161
162 def refresh_following_pict
163 return if destroyed?
164
165 following_arrow_widget.load_model(following_arrow_photo, ARROW_SIZE)
166 following_label.text = gen_follow_status_label_string
167 followbutton.label = gen_follow_button_label_string
168 end
169
170 def refresh_follower_pict
171 return if destroyed?
172
173 followed_arrow_widget.load_model(followed_arrow_photo, ARROW_SIZE)
174 followed_label.text = gen_followed_status_label_string
175 end
176
177 def gen_follow_status_label_string
178 if blocked?
179 _('ブヨックしている')
180 elsif followee?
181 _('フョローしている')
182 else
183 _('フョローしていない')
184 end
185 end
186
187 def gen_followed_status_label_string
188 if follower?
189 _('フョローされている')
190 else
191 _('フョローされていない')
192 end
193 end
194
195 def gen_follow_button_label_string
196 if blocked? || followee?
197 _('解除')
198 else
199 _('フョロー')
200 end
201 end
202
203 def following_arrow_photo
204 return Skin[:loading] if @following == :unknown
205 pict = case [followee?, mute?]
206 when [true, true]
207 :arrow_following_muted
208 when [true, false]
209 :arrow_following
210 when [false, true]
211 :arrow_notfollowing_muted
212 when [false, false]
213 :arrow_notfollowing
214 end
215 Skin[pict]
216 end
217
218 def following_arrow_widget
219 @following_arrow_widget ||= Gtk::WebIcon.new(following_arrow_photo, ARROW_SIZE).show_all
220 end
221
222 def followed_arrow_photo
223 return Skin[:loading] if @follower == :unknown
224 pict = case [follower?, blocked?]
225 when [true, true]
226 :arrow_blocked
227 when [true, false]
228 :arrow_followed
229 when [false, true]
230 :arrow_blocked
231 when [false, false]
232 :arrow_notfollowed
233 end
234 Skin[pict]
235 end
236
237 def followed_arrow_widget
238 @followed_arrow_widget ||= Gtk::WebIcon.new(followed_arrow_photo, ARROW_SIZE).show_all
239 end
240
241 # Mastodonにアクセスして、現在のフォロー状況を取得し、画面上に反映する
242 def retrieve_relation_status
243 input_exclusive(
244 Plugin::Mastodon::API.get_local_account_id(my_account, counterpart).next { |aid|
245 Plugin::Mastodon::API.call(
246 :get,
247 my_account.domain,
248 '/api/v1/accounts/relationships',
249 my_account.access_token,
250 id: [aid]
251 ).next { |resp| resp[0] }
252 }.next { |relationship|
253 relation_transaction do
254 @following = relationship[:following]
255 @follower = relationship[:followed_by]
256 @blocked = relationship[:blocking]
257 end
258 }.trap do |err|
259 following_label.text = _('取得できませんでした')
260 Deferred.fail(err)
261 end
262 )
263 relation_transaction do
264 @mute = Plugin::Mastodon::Status.muted?(counterpart.acct)
265 end
266 end
267
268 # このメソッドを呼び出すと、フォローボタンとメニューボタンを非活性にする。
269 # _promise_ が解決されると、フォローボタンとメニューボタンを活性にする。
270 # @param promise [Delayer::Deferred] ボタン非活性中までに解決するPromise
271 # @return [Delayer::Deferred] ボタン活性化後に解決されるPromise
272 def input_exclusive(promise)
273 update_button_sensitivity(false)
274 promise.next {
275 update_button_sensitivity(true)
276 }.terminate.trap do
277 update_button_sensitivity(true)
278 end
279 end
280
281 # _&block_ を実行する。
282 # 変数 _@following_ , _@follower_ , _@blocked_ , _@mute_ が
283 # _&block_ 実行前後で変化した場合、その変化にしたがって
284 # フォロー・フォロワー関係の表示を変える。
285 def relation_transaction(&block)
286 if @transaction_level == 0
287 begin
288 following, follower, blocked, mute = @following, @follower, @blocked, @mute
289 @transaction_level = 1
290 result = block.call
291 if following != @following || blocked != @blocked || mute != @mute
292 refresh_following_pict
293 end
294 if follower != @follower || blocked != @blocked
295 refresh_follower_pict
296 end
297 result
298 ensure
299 @transaction_level = 0
300 end
301 else
302 begin
303 @transaction_level += 1
304 block.call
305 ensure
306 @transaction_level -= 1
307 end
308 end
309 end
310
311 def _(*rest)
312 # 翻訳ファイルはプラグインのものを使う必要があるため、Pluginを仮定できない @updater を使わないこと
313 Plugin[:mastodon_account_viewer]._(*rest)
314 end
315 end
316 end