Debian Codegraph @

plugin/mastodon/model/instance.rb

raw symbols SCIP
1 module Plugin::Mastodon
2 class Instance < Diva::Model
3 register :mastodon_instance, name: Plugin[:mastodon]._('Mastodonサーバー')
4
5 field.string :domain, required: true
6 field.string :client_key, required: true
7 field.string :client_secret, required: true
8 field.bool :retrieve, required: true
9
10 field.string :title, required: false
11 field.string :short_description, required: false
12 field.string :description, required: false
13 field.string :version, required: false
14 field.has :configuration, ServerConfiguration, required: false
15
16 class << self
17 def add(domain, retrieve = true)
18 Delayer::Deferred.new.next {
19 return nil if UserConfig[:mastodon_instances].has_key?(domain)
20
21 Plugin::Mastodon::API.call(
22 :post, domain, '/api/v1/apps',
23 client_name: Plugin::Mastodon::CLIENT_NAME,
24 redirect_uris: 'urn:ietf:wg:oauth:2.0:oob',
25 scopes: 'read write follow',
26 website: Plugin::Mastodon::WEB_SITE
27 )
28 }.next{ |resp|
29 self.new(
30 domain: domain,
31 client_key: resp[:client_id],
32 client_secret: resp[:client_secret],
33 retrieve: retrieve,
34 ).store
35 }
36 end
37
38 def add_ifn(domain, retrieve = true)
39 Delayer::Deferred.new.next do
40 self.load(domain) || +self.add(domain, retrieve)
41 end
42 end
43
44 def load(domain)
45 if UserConfig[:mastodon_instances][domain]
46 self.new(
47 domain: domain,
48 client_key: UserConfig[:mastodon_instances][domain][:client_key],
49 client_secret: UserConfig[:mastodon_instances][domain][:client_secret],
50 retrieve: UserConfig[:mastodon_instances][domain][:retrieve],
51 )
52 end
53 end
54
55 def remove(domain)
56 UserConfig[:mastodon_instances].delete(domain)
57 end
58
59 def domains
60 UserConfig[:mastodon_instances].keys.dup
61 end
62
63 def settings
64 UserConfig[:mastodon_instances].map do |domain, value|
65 { domain: domain, retrieve: value[:retrieve] }
66 end
67 end
68 end # class instance
69
70 def initialize(*)
71 super
72 Plugin.call(:mastodon_server_created, self)
73 end
74
75 def sse
76 Plugin::Mastodon::SSEPublicType.new(server: self)
77 end
78
79 def rest
80 Plugin::Mastodon::RestPublicType.new(server: self)
81 end
82
83 def store
84 configs = UserConfig[:mastodon_instances].dup
85 configs[domain] = { client_key: client_key, client_secret: client_secret, retrieve: retrieve }
86 UserConfig[:mastodon_instances] = configs
87 self
88 end
89
90 def authorize_url
91 params = URI.encode_www_form({
92 scope: 'read write follow',
93 response_type: 'code',
94 redirect_uri: 'urn:ietf:wg:oauth:2.0:oob',
95 client_id: client_key
96 })
97 'https://' + domain + '/oauth/authorize?' + params
98 end
99
100 def ==(other)
101 case other
102 when Plugin::Mastodon::Instance
103 domain == other.domain
104 end
105 end
106
107 def inspect
108 "#<#{self.class.name}: #{domain}>"
109 end
110 end
111 end