Debian Codegraph @

plugin/mastodon/model/account.rb

raw symbols SCIP
1 # coding: utf-8
2 module Plugin::Mastodon
3 class AccountField < Diva::Model
4 field.string :name
5 field.string :value
6 field.has :emojis, [Emoji]
7
8 def description
9 d, _ = description_score
10 d
11 end
12
13 def score
14 _, s = description_score
15 s
16 end
17
18 def inspect
19 "#{name}: #{value}"
20 end
21
22 private
23
24 # TODO: modelがScoreをキャッシュするべきではない
25 def description_score
26 @description_score ||= Plugin::Mastodon::Parser.dictate_score(value, emojis: emojis)
27 end
28 end
29
30 class AccountSource < Diva::Model
31 #register :mastodon_account_source, name: "Mastodonアカウント追加情報(Mastodon)"
32
33 field.string :privacy
34 field.bool :sensitive
35 field.string :language
36 field.string :note
37 field.has :fields, [AccountField]
38 end
39
40 # https://github.com/tootsuite/documentation/blob/master/Using-the-API/API.md#status
41 class Account < Diva::Model
42 include Diva::Model::UserMixin
43
44 register :mastodon_account, name: Plugin[:mastodon]._('Mastodonアカウント')
45
46 field.string :id, required: true
47 field.string :username, required: true
48 field.string :acct, required: true
49 field.string :display_name, required: true
50 field.bool :locked, required: true
51 field.time :created_at, required: true
52 field.int :followers_count, required: true
53 field.int :following_count, required: true
54 field.int :statuses_count, required: true
55 field.string :note, required: true
56 field.uri :url, required: true
57 field.uri :avatar, required: true
58 field.uri :avatar_static, required: true
59 field.uri :header, required: true
60 field.uri :header_static, required: true
61 field.has :emojis, [Emoji]
62 field.has :moved, Account
63 field.has :fields, [AccountField]
64 field.bool :bot
65 field.has :source, AccountSource
66
67 alias :perma_link :url
68 alias :uri :url
69 alias :idname :acct
70 alias :name :display_name
71
72 @@account_storage = WeakStorage.new(String, Account, name: 'mastodon-account')
73
74 ACCOUNT_URI_RE = %r!\Ahttps://(?<domain>[^/]+)/@(?<acct>\w{1,30})\z!
75
76 handle ACCOUNT_URI_RE do |uri|
77 m = ACCOUNT_URI_RE.match(uri.to_s)
78 acct = "#{m["acct"]}@#{m["domain"]}"
79 account = Account.findbyacct(acct)
80 next account if account
81
82 Account.fetch(acct)
83 end
84
85 def self.regularize_acct_by_domain(domain, acct)
86 if acct.include?('@')
87 acct
88 else
89 "#{acct}@#{domain}"
90 end
91 end
92
93 def self.regularize_acct(hash)
94 domain = Diva::URI.new(hash[:url]).host
95 acct = hash[:acct]
96 hash[:acct] = self.regularize_acct_by_domain(domain, acct)
97 hash
98 end
99
100 def self.domain(url)
101 Diva::URI.new(url.to_s).host
102 end
103
104 def self.findbyacct(acct)
105 @@account_storage[acct]
106 end
107
108 def self.fetch(acct)
109 world, = Plugin.filtering(:mastodon_current, nil)
110 Plugin::Mastodon::API.call(:get, world.domain, '/api/v2/search', world.access_token, q: acct, resolve: true).next{ |resp|
111 resp[:accounts].select{|account| account[:acct] === acct }.first&.yield_self(&Account.method(:new))
112 }
113 end
114
115 def domain
116 self.class.domain(url)
117 end
118
119 def initialize(hash)
120 if hash[:created_at].is_a? String
121 hash[:created_at] = Time.parse(hash[:created_at]).localtime
122 end
123 hash = self.class.regularize_acct(hash)
124
125 # activity対策
126 hash[:idname] = hash[:acct]
127
128 hash[:name] = hash[:display_name]
129
130 super hash
131
132 @@account_storage[hash[:acct]] = self
133 end
134
135 def inspect
136 "mastodon-account(#{acct})"
137 end
138
139 def to_s
140 "mastodon-account(#{acct})"
141 end
142
143 def title
144 "#{acct}(#{display_name})"
145 end
146
147 def description
148 "@#{acct}"
149 end
150
151 def icon
152 Plugin.collect(:photo_filter, avatar_static, Pluggaloid::COLLECT).lazy.map { |photo|
153 Plugin.filtering(:miracle_icon_filter, photo)[0]
154 }.first
155 end
156
157 def me?(world = Plugin.collect(:worlds))
158 case world
159 when Enumerable
160 world.any?(&method(:me?))
161 when Diva::Model
162 world.class.slug == :mastodon && self == world.account
163 end
164 end
165
166 def profile
167 @profile ||= AccountProfile.new(account: self)
168 end
169 end
170 end