| 1 |
|
| 2 |
module Plugin::Mastodon |
| 3 |
class World < Diva::Model |
| 4 |
extend Memoist |
| 5 |
|
| 6 |
register :mastodon, name: Plugin[:mastodon]._('Mastodon') |
| 7 |
|
| 8 |
field.string :id, required: true |
| 9 |
field.string :slug, required: true |
| 10 |
alias :name :slug |
| 11 |
field.string :domain, required: true |
| 12 |
field.string :access_token, required: true |
| 13 |
field.has :account, Account, required: true |
| 14 |
|
| 15 |
alias :user_obj :account |
| 16 |
|
| 17 |
@@lists = Hash.new |
| 18 |
@@followings = Hash.new |
| 19 |
@@followers = Hash.new |
| 20 |
@@blocks = Hash.new |
| 21 |
|
| 22 |
memoize def path |
| 23 |
"/#{account.acct.split('@').reverse.join('/')}" |
| 24 |
end |
| 25 |
|
| 26 |
def inspect |
| 27 |
"mastodon-world(#{account.acct})" |
| 28 |
end |
| 29 |
|
| 30 |
def icon |
| 31 |
account.icon |
| 32 |
end |
| 33 |
|
| 34 |
def title |
| 35 |
account.title |
| 36 |
end |
| 37 |
|
| 38 |
def server |
| 39 |
@server ||= Plugin::Mastodon::Instance.load(domain) |
| 40 |
end |
| 41 |
|
| 42 |
def sse |
| 43 |
Plugin::Mastodon::SSEAuthorizedType.new(world: self) |
| 44 |
end |
| 45 |
|
| 46 |
def rest |
| 47 |
Plugin::Mastodon::RestAuthorizedType.new(world: self) |
| 48 |
end |
| 49 |
|
| 50 |
def get_lists |
| 51 |
Delayer::Deferred.new do |
| 52 |
if @@lists[uri.to_s] |
| 53 |
@@lists[uri.to_s] はAPIクラスで行いたい |
| 54 |
else |
| 55 |
API.call(:get, domain, '/api/v1/lists', access_token).next do |lists| |
| 56 |
@@lists[uri.to_s] = lists.value |
| 57 |
end |
| 58 |
end |
| 59 |
end |
| 60 |
end |
| 61 |
|
| 62 |
def update_mutes! |
| 63 |
params = { limit: 80 } |
| 64 |
since_id = nil |
| 65 |
Status.clear_mutes |
| 66 |
while mutes = Plugin::Mastodon::API.call!(:get, domain, '/api/v1/mutes', access_token, **params) |
| 67 |
Status.add_mutes(mutes.value) |
| 68 |
return unless mutes.header && mutes.header[:prev] |
| 69 |
url = mutes.header[:prev] |
| 70 |
params = URI.decode_www_form(url.query).to_h.map{|k,v| [k.to_sym, v] }.to_h |
| 71 |
return if params[:since_id].to_i == since_id |
| 72 |
since_id = params[:since_id].to_i |
| 73 |
|
| 74 |
sleep 1 |
| 75 |
end |
| 76 |
end |
| 77 |
|
| 78 |
稿する |
| 79 |
(ローカル)ID |
| 80 |
の配列(最大4) |
| 81 |
の明示的な指定 |
| 82 |
コメント |
| 83 |
ic" のいずれか。 |
| 84 |
def post(to: nil, message:, **params) |
| 85 |
params[:status] = message |
| 86 |
if to ||= params[:replyto] |
| 87 |
API.get_local_status_id(self, to).next{ |status_id| |
| 88 |
API.call(:post, domain, '/api/v1/statuses', access_token, in_reply_to_id: status_id, **params) |
| 89 |
}.terminate(Plugin[:mastodon]._('返信先Statusが%{domain}内に見つかりませんでした:%{url}') % {domain: domain, url: to.url}) |
| 90 |
else |
| 91 |
API.call(:post, domain, '/api/v1/statuses', access_token, **params) |
| 92 |
end |
| 93 |
end |
| 94 |
|
| 95 |
する。 |
| 96 |
|
| 97 |
toot |
| 98 |
|
| 99 |
られたstatusを返すDeferred |
| 100 |
def reblog(status) |
| 101 |
Plugin::Mastodon::API.get_local_status_id(self, status.actual_status).next{ |status_id| |
| 102 |
new_status_hash = +Plugin::Mastodon::API.call(:post, domain, '/api/v1/statuses/' + status_id.to_s + '/reblog', access_token) |
| 103 |
new_status = Plugin::Mastodon::Status.build(server, new_status_hash.value) |
| 104 |
Plugin.call(:share, new_status.user, status) |
| 105 |
new_status |
| 106 |
} |
| 107 |
end |
| 108 |
|
| 109 |
def get_accounts!(type) |
| 110 |
promise = Delayer::Deferred.new(true) |
| 111 |
Thread.new do |
| 112 |
accounts = [] |
| 113 |
params = { |
| 114 |
limit: 80 |
| 115 |
} |
| 116 |
API.all_with_world!(self, :get, "/api/v1/accounts/#{account.id}/#{type}", **params) do |hash| |
| 117 |
accounts << hash |
| 118 |
end |
| 119 |
promise.call(accounts.map {|hash| Account.new hash }) |
| 120 |
rescue Exception => e |
| 121 |
Plugin::Mastodon::Util.ppf e if Mopt.error_level >= 2 |
| 122 |
promise.fail("failed to get #{type}") |
| 123 |
end |
| 124 |
promise |
| 125 |
end |
| 126 |
|
| 127 |
def following?(acct) |
| 128 |
acct = acct.acct if acct.is_a?(Account) |
| 129 |
@@followings[uri.to_s].any? { |account| account.acct == acct } |
| 130 |
end |
| 131 |
|
| 132 |
def followings(cache: true, **opts) |
| 133 |
promise = Delayer::Deferred.new(true) |
| 134 |
Thread.new do |
| 135 |
next promise.call(@@followings[uri.to_s]) if cache && @@followings[uri.to_s] |
| 136 |
get_accounts!('following').next do |accounts| |
| 137 |
@@followings[uri.to_s] = accounts |
| 138 |
promise.call(accounts) |
| 139 |
end |
| 140 |
end |
| 141 |
promise |
| 142 |
end |
| 143 |
|
| 144 |
def followers(cache: true, **opts) |
| 145 |
promise = Delayer::Deferred.new(true) |
| 146 |
Thread.new do |
| 147 |
next promise.call(@@followers[uri.to_s]) if cache && @@followers[uri.to_s] |
| 148 |
get_accounts!('followers').next do |accounts| |
| 149 |
@@followers[uri.to_s] = accounts |
| 150 |
promise.call(accounts) |
| 151 |
end |
| 152 |
end |
| 153 |
promise |
| 154 |
end |
| 155 |
|
| 156 |
def blocks |
| 157 |
promise = Delayer::Deferred.new(true) |
| 158 |
Thread.new do |
| 159 |
accounts = [] |
| 160 |
params = { |
| 161 |
limit: 80 |
| 162 |
} |
| 163 |
API.all_with_world!(self, :get, "/api/v1/blocks", **params) do |hash| |
| 164 |
accounts << hash |
| 165 |
end |
| 166 |
@@blocks[uri.to_s] = accounts.map { |hash| Account.new hash } |
| 167 |
promise.call(@@blocks[uri.to_s]) |
| 168 |
rescue Exception => e |
| 169 |
Plugin::Mastodon::Util.ppf e if Mopt.error_level >= 2 |
| 170 |
promise.fail('failed to get blocks') |
| 171 |
end |
| 172 |
promise |
| 173 |
end |
| 174 |
|
| 175 |
def block?(acct) |
| 176 |
@@blocks[uri.to_s].any? { |acc| acc.acct == acct } |
| 177 |
end |
| 178 |
|
| 179 |
def account_action(account, type) |
| 180 |
Plugin::Mastodon::API.get_local_account_id(self, account).next{ |account_id| |
| 181 |
Plugin::Mastodon::API.call(:post, domain, "/api/v1/accounts/#{account_id}/#{type}", access_token) |
| 182 |
} |
| 183 |
end |
| 184 |
|
| 185 |
def follow(account) |
| 186 |
account_action(account, "follow").next{ |ret| |
| 187 |
@@followings[uri.to_s] = [*@@followings[uri.to_s], account] |
| 188 |
followings(cache: false) |
| 189 |
ret |
| 190 |
} |
| 191 |
end |
| 192 |
|
| 193 |
def unfollow(account) |
| 194 |
account_action(account, "unfollow").next{ |ret| |
| 195 |
if @@followings[uri.to_s] |
| 196 |
@@followings[uri.to_s].delete_if do |acc| |
| 197 |
acc.acct == account.acct |
| 198 |
end |
| 199 |
followings(cache: false) |
| 200 |
end |
| 201 |
ret |
| 202 |
} |
| 203 |
end |
| 204 |
|
| 205 |
def mute(account) |
| 206 |
account_action(account, "mute").next{ update_mutes! } |
| 207 |
end |
| 208 |
|
| 209 |
def unmute(account) |
| 210 |
account_action(account, "unmute").next{ update_mutes! } |
| 211 |
end |
| 212 |
|
| 213 |
def block(account) |
| 214 |
account_action(account, "block").next{ |
| 215 |
if @@followings[uri.to_s] |
| 216 |
@@followings[uri.to_s].delete_if do |acc| |
| 217 |
acc.acct == account.acct |
| 218 |
end |
| 219 |
end |
| 220 |
blocks |
| 221 |
} |
| 222 |
end |
| 223 |
|
| 224 |
def unblock(account) |
| 225 |
account_action(account, "unblock").next{ blocks } |
| 226 |
end |
| 227 |
|
| 228 |
def pin(status) |
| 229 |
Plugin::Mastodon::API.get_local_status_id(self, status).next{ |status_id| |
| 230 |
Plugin::Mastodon::API.call(:post, domain, "/api/v1/statuses/#{status_id}/pin", access_token) |
| 231 |
}.next{ |
| 232 |
status.pinned = true |
| 233 |
} |
| 234 |
end |
| 235 |
|
| 236 |
def unpin(status) |
| 237 |
Plugin::Mastodon::API.get_local_status_id(self, status).next{ |status_id| |
| 238 |
Plugin::Mastodon::API.call(:post, domain, "/api/v1/statuses/#{status_id}/unpin", access_token) |
| 239 |
}.next{ |
| 240 |
status.pinned = false |
| 241 |
} |
| 242 |
end |
| 243 |
|
| 244 |
def bookmark(status) |
| 245 |
Plugin::Mastodon::API.get_local_status_id(self, status).next{ |status_id| |
| 246 |
Plugin::Mastodon::API.call(:post, domain, "/api/v1/statuses/#{status_id}/bookmark", access_token) |
| 247 |
}.next{ |
| 248 |
status.bookmarked = true |
| 249 |
} |
| 250 |
end |
| 251 |
|
| 252 |
def unbookmark(status) |
| 253 |
Plugin::Mastodon::API.get_local_status_id(self, status).next{ |status_id| |
| 254 |
Plugin::Mastodon::API.call(:post, domain, "/api/v1/statuses/#{status_id}/unbookmark", access_token) |
| 255 |
}.next{ |
| 256 |
status.bookmarked = false |
| 257 |
} |
| 258 |
end |
| 259 |
|
| 260 |
def report_for_spam(statuses, comment) |
| 261 |
Deferred.when( |
| 262 |
Plugin::Mastodon::API.get_local_account_id(self, statuses.first.account), |
| 263 |
Deferred.when(statuses.map { |status| Plugin::Mastodon::API.get_local_status_id(self, status) }) |
| 264 |
).next{ |account_id, spam_ids| |
| 265 |
Plugin::Mastodon::API.call(:post, domain, "/api/v1/reports", access_token, |
| 266 |
account_id: account_id, |
| 267 |
status_ids: spam_ids, |
| 268 |
comment: comment) |
| 269 |
} |
| 270 |
end |
| 271 |
|
| 272 |
def update_account |
| 273 |
Plugin::Mastodon::API.call(:get, domain, '/api/v1/accounts/verify_credentials', access_token).next{ |resp| |
| 274 |
resp[:acct] = resp[:acct] + '@' + domain |
| 275 |
self.account = Plugin::Mastodon::Account.new(resp.value) |
| 276 |
Plugin.call(:world_modify, self) |
| 277 |
} |
| 278 |
end |
| 279 |
|
| 280 |
def update_profile(**opts) |
| 281 |
params = {} |
| 282 |
|
| 283 |
ータ名とMastodon APIのパラメータ名に違いがある |
| 284 |
|
| 285 |
示名 |
| 286 |
params[:display_name] = opts[:name] if opts[:name] |
| 287 |
|
| 288 |
params[:note] = opts[:biography] if opts[:biography] |
| 289 |
|
| 290 |
ロー承認制 |
| 291 |
params[:locked] = opts[:locked] if opts[:locked] |
| 292 |
トであることの表明 |
| 293 |
params[:bot] = opts[:bot] if opts[:bot] |
| 294 |
if [:privacy, :sensitive, :language].any?{|key| opts[:source] && opts[:source][key] } |
| 295 |
params[:source] = Hash.new |
| 296 |
ルト公開範囲 |
| 297 |
params[:source][:privacy] = opts[:source_privacy] if opts[:source_privacy] |
| 298 |
ルトでNSFW |
| 299 |
params[:source][:sensitive] = opts[:source_sensitive] if opts[:source_sensitive] |
| 300 |
(ex: "ja") or nil(自動検出)) |
| 301 |
params[:source][:language] = opts[:source_language] if opts[:source_language] |
| 302 |
end |
| 303 |
ィール補足情報 |
| 304 |
if (1..4).any?{|i| opts[:"field_name#{i}"] && opts[:"field_value#{i}"] } |
| 305 |
params[:fields_attributes] = Array.new |
| 306 |
(1..4).each do |i| |
| 307 |
name = opts[:"field_name#{i}"] |
| 308 |
next unless name |
| 309 |
value = opts[:"field_value#{i}"] |
| 310 |
next unless value |
| 311 |
params[:fields_attributes] << { name: name, value: value } |
| 312 |
end |
| 313 |
end |
| 314 |
ds = [] |
| 315 |
if opts[:icon] |
| 316 |
if opts[:icon].is_a?(Plugin::Photo::Photo) |
| 317 |
ds << opts[:icon].download.next{|photo| [:avatar, photo] } |
| 318 |
else |
| 319 |
params[:avatar] = opts[:icon] |
| 320 |
end |
| 321 |
end |
| 322 |
if opts[:header] |
| 323 |
if opts[:header].is_a?(Plugin::Photo::Photo) |
| 324 |
ds << opts[:header].download.next{|photo| [:header, photo] } |
| 325 |
else |
| 326 |
params[:header] = opts[:header] |
| 327 |
end |
| 328 |
end |
| 329 |
if ds.empty? |
| 330 |
ds << Delayer::Deferred.new.next{ [:none, nil] } |
| 331 |
end |
| 332 |
Delayer::Deferred.when(ds).next{|vs| |
| 333 |
vs.each do |key, val| |
| 334 |
params[key] = val |
| 335 |
end |
| 336 |
new_account = +Plugin::Mastodon::API.call(:patch, domain, '/api/v1/accounts/update_credentials', access_token, **params) |
| 337 |
self.account = Plugin::Mastodon::Account.new(new_account.value) |
| 338 |
Plugin.call(:world_modify, self) |
| 339 |
} |
| 340 |
end |
| 341 |
end |
| 342 |
end |