Debian Codegraph @

plugin/mastodon_account_viewer/mastodon_account_viewer.rb

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # frozen_string_literal: true
3
4 require_relative 'relational_container'
5
6 Plugin.create(:mastodon_account_viewer) do
7 defmodelviewer(Plugin::Mastodon::Account) do |user|
8 since_day = (Time.now - user.created_at).to_i / (60 * 60 * 24)
9 param_mastodon_start_date = {
10 year: user.created_at.strftime('%Y'),
11 month: user.created_at.strftime('%m'),
12 day: user.created_at.strftime('%d'),
13 hour: user.created_at.strftime('%H'),
14 minute: user.created_at.strftime('%M'),
15 second: user.created_at.strftime('%S'),
16 since_day: since_day
17 }
18 param_toot_count = {
19 count: user.statuses_count,
20 toots_per_day: since_day == 0 ? user.statuses_count : Rational(user.statuses_count, since_day).to_f.round(2)
21 }
22 [
23 [_('名前'), user.display_name],
24 [_('acct'), user.acct],
25 [_('フォロー'), user.following_count],
26 [_('フォロワー'), user.followers_count],
27 [_('Mastodon開始'), _('%{year}/%{month}/%{day} %{hour}:%{minute}:%{second} (%{since_day}日)') % param_mastodon_start_date],
28 [_('Toot'), _('%{count} (%{toots_per_day}toots/day)') % param_toot_count]
29 ].freeze
30 end
31
32 deffragment(Plugin::Mastodon::Account, :bio, _('ユーザについて')) do |user|
33 set_icon user.icon
34 score = score_of(user.profile)
35 bio = ::Gtk::IntelligentTextview.new(score)
36 bio.hexpand = true
37
38 grid = Gtk::Grid.new
39 grid.orientation = :vertical
40 grid.row_spacing = 8
41 grid.margin = 4
42 grid <<
43 user_field_table(
44 user.fields&.map do |f|
45 f.emojis ||= user.emojis
46 [f.name, f]
47 end) <<
48 bio <<
49 relation_bar(user)
50
51 nativewidget grid
52 end
53
54 def user_field_table(header_columns)
55 ::Gtk::Table.new(2, header_columns.size).tap { |table|
56 header_columns.each_with_index do |(key, value), index|
57 table.
58 attach(::Gtk::Label.new(key.to_s).set_halign(:end), 0, 1, index, index + 1).
59 attach(cell_widget(value), 1, 2, index, index + 1)
60 end
61 }.set_row_spacing(0, 4).
62 set_row_spacing(1, 4).
63 set_col_spacing(0, 16)
64 end
65
66 def cell_widget(model_or_str)
67 case model_or_str
68 when Diva::Model
69 ::Gtk::IntelligentTextview.new(
70 Plugin[:modelviewer].score_of(model_or_str)
71 )
72 else
73 ::Gtk::IntelligentTextview.new(model_or_str.to_s)
74 end
75 end
76
77 # フォロー関係の表示・操作用ウィジェット
78 def relation_bar(user)
79 container = ::Gtk::Box.new(:vertical, 4)
80 Plugin.collect(:mastodon_worlds).each do |me|
81 container.pack_start(Plugin::MastodonAccountViewer::RelationalContainer.new(me, user, self), expand: false)
82 end
83 container
84 end
85
86 def request_unmute(world, user)
87 unmute_user(world, user)
88 end
89
90 def request_mute(world, user)
91 dialog(_('ミュートする')) {
92 label _('以下のユーザーをミュートしますか?')
93 link user
94 }.next do
95 mute_user(world, user)
96 end
97 end
98
99 def request_unblock(world, user)
100 unblock_user(world, user)
101 end
102
103 def request_block(world, user)
104 dialog(_('ブロックする')) {
105 label _('以下のユーザーをブロックしますか?')
106 link user
107 }.next do
108 block_user(world, user)
109 end
110 end
111
112 def request_unfollow(world, user)
113 unfollow_user(world, user)
114 end
115
116 def request_follow(world, user)
117 follow_user(world, user)
118 end
119
120 deffragment(Plugin::Mastodon::Account, :user_timeline, _('ユーザタイムライン')) do |user|
121 set_icon Skin[:timeline]
122 tl = timeline(nil) do
123 order do |message|
124 retweet = message.retweeted_statuses.find { |r| user.id == r.user.id }
125 (retweet || message).created.to_i
126 end
127 end
128 world, = Plugin.filtering(:mastodon_current, nil)
129 Plugin::Mastodon::API.get_local_account_id(world, user).next { |account_id|
130 Plugin::Mastodon::API.call(:get, world.domain, "/api/v1/accounts/#{account_id}/statuses", world.access_token).next do |res|
131 tl << Plugin::Mastodon::Status.bulk_build(world.server, res.value)
132 end
133 }.terminate
134 acct, domain = user.acct.split('@', 2)
135 if domain != world.domain
136 Plugin::Mastodon::API.call(
137 :get,
138 domain,
139 "/users/#{acct}/outbox?page=true",
140 nil,
141 {},
142 { 'Accept' => 'application/activity+json' }
143 ).next { |res|
144 res[:orderedItems].map { |record|
145 case record[:type]
146 when 'Create'
147 # トゥート
148 record.dig(:object, :url)
149 when 'Announce'
150 # ブースト
151 Plugin::Mastodon::Status::TOOT_ACTIVITY_URI_RE.match(record[:atomUri]) do |m|
152 "https://#{m[:domain]}/@#{m[:acct]}/#{m[:status_id]}"
153 end
154 end
155 }.compact.each do |url|
156 status = Plugin::Mastodon::Status.findbyuri(url) || +Plugin::Mastodon::Status.fetch(url)
157 tl << status if status
158 end
159 }.terminate
160 end
161 end
162 end