mastodon-api 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/lib/mastodon.rb +2 -0
  3. data/lib/mastodon/access_token.rb +21 -0
  4. data/lib/mastodon/account.rb +44 -2
  5. data/lib/mastodon/app.rb +8 -1
  6. data/lib/mastodon/card.rb +41 -0
  7. data/lib/mastodon/client.rb +8 -1
  8. data/lib/mastodon/conversation.rb +25 -0
  9. data/lib/mastodon/emoji.rb +21 -0
  10. data/lib/mastodon/entities/app.rb +12 -0
  11. data/lib/mastodon/entities/hashtag.rb +12 -0
  12. data/lib/mastodon/entities/media.rb +13 -1
  13. data/lib/mastodon/entities/mention.rb +1 -1
  14. data/lib/mastodon/field.rb +18 -0
  15. data/lib/mastodon/filter.rb +29 -0
  16. data/lib/mastodon/hashtag.rb +19 -0
  17. data/lib/mastodon/instance.rb +33 -0
  18. data/lib/mastodon/list.rb +15 -0
  19. data/lib/mastodon/media.rb +16 -2
  20. data/lib/mastodon/notification.rb +30 -0
  21. data/lib/mastodon/relationship.rb +23 -2
  22. data/lib/mastodon/rest/accounts.rb +52 -9
  23. data/lib/mastodon/rest/api.rb +24 -0
  24. data/lib/mastodon/rest/apps.rb +7 -0
  25. data/lib/mastodon/rest/conversations.rb +34 -0
  26. data/lib/mastodon/rest/custom_emojis.rb +16 -0
  27. data/lib/mastodon/rest/domain_blocks.rb +32 -0
  28. data/lib/mastodon/rest/endorsements.rb +36 -0
  29. data/lib/mastodon/rest/filters.rb +61 -0
  30. data/lib/mastodon/rest/instances.rb +28 -0
  31. data/lib/mastodon/rest/lists.rb +77 -0
  32. data/lib/mastodon/rest/media.rb +17 -3
  33. data/lib/mastodon/rest/notifications.rb +34 -0
  34. data/lib/mastodon/rest/relationships.rb +68 -1
  35. data/lib/mastodon/rest/reports.rb +20 -0
  36. data/lib/mastodon/rest/request.rb +7 -3
  37. data/lib/mastodon/rest/scheduled_statuses.rb +43 -0
  38. data/lib/mastodon/rest/search.rb +20 -0
  39. data/lib/mastodon/rest/statuses.rb +57 -5
  40. data/lib/mastodon/rest/suggestions.rb +13 -2
  41. data/lib/mastodon/rest/timelines.rb +19 -0
  42. data/lib/mastodon/rest/utils.rb +3 -3
  43. data/lib/mastodon/results.rb +18 -0
  44. data/lib/mastodon/scheduled_status.rb +25 -0
  45. data/lib/mastodon/status.rb +49 -4
  46. data/lib/mastodon/streaming/client.rb +96 -0
  47. data/lib/mastodon/streaming/connection.rb +44 -0
  48. data/lib/mastodon/streaming/events/filters_change.rb +7 -0
  49. data/lib/mastodon/streaming/events/status_delete.rb +16 -0
  50. data/lib/mastodon/streaming/message_parser.rb +23 -0
  51. data/lib/mastodon/streaming/response.rb +42 -0
  52. data/lib/mastodon/version.rb +2 -2
  53. data/mastodon.gemspec +5 -3
  54. metadata +68 -9
@@ -0,0 +1,30 @@
1
+ module Mastodon
2
+ class Notification < Mastodon::Base
3
+ # @!attribute [r] id
4
+ # @return [String]
5
+ # @!attribute [r] type
6
+ # @return [String]
7
+ # @!attribute [r] created_at
8
+ # @return [String]
9
+ # @!attribute [r] account
10
+ # @return [Mastodon::Account]
11
+ # @!attribute [r] status
12
+ # @return [Mastodon::Status]
13
+
14
+ normal_attr_reader :id, :type, :created_at
15
+
16
+ object_attr_reader :account, Mastodon::Account
17
+ object_attr_reader :status, Mastodon::Status
18
+
19
+ def initialize(attributes = {})
20
+ attributes.fetch('id')
21
+ super
22
+ end
23
+
24
+ # Does this notification include a status?
25
+ # @return [Boolean] true if a status is included, false otherwise
26
+ def status?
27
+ attributes.key?('status')
28
+ end
29
+ end
30
+ end
@@ -1,16 +1,37 @@
1
1
  module Mastodon
2
2
  class Relationship < Mastodon::Base
3
3
  # @!attribute [r] id
4
- # @return [Integer] Account ID
4
+ # @return [String] Account ID
5
5
  # @!attribute [r] following?
6
6
  # @return [Boolean]
7
7
  # @!attribute [r] followed_by?
8
8
  # @return [Boolean]
9
9
  # @!attribute [r] blocking?
10
10
  # @return [Boolean]
11
+ # @!attribute [r] muting?
12
+ # @return [Boolean]
13
+ # @!attribute [r] muting_notifications?
14
+ # @return [Boolean]
15
+ # @!attribute [r] requested?
16
+ # @return [Boolean]
17
+ # @!attribute [r] domain_blocking?
18
+ # @return [Boolean]
19
+ # @!attribute [r] showing_reblogs?
20
+ # @return [Boolean]
21
+ # @!attribute [r] endorsed?
22
+ # @return [Boolean]
11
23
 
12
24
  normal_attr_reader :id
13
- predicate_attr_reader :following, :followed_by, :blocking
25
+
26
+ predicate_attr_reader :following,
27
+ :followed_by,
28
+ :blocking,
29
+ :muting,
30
+ :muting_notifications,
31
+ :requested,
32
+ :domain_blocking,
33
+ :showing_reblogs,
34
+ :endorsed
14
35
 
15
36
  def initialize(attributes = {})
16
37
  attributes.fetch('id')
@@ -1,5 +1,6 @@
1
1
  require 'mastodon/rest/utils'
2
2
  require 'mastodon/account'
3
+ require 'mastodon/access_token'
3
4
 
4
5
  module Mastodon
5
6
  module REST
@@ -12,6 +13,22 @@ module Mastodon
12
13
  perform_request_with_object(:get, '/api/v1/accounts/verify_credentials', {}, Mastodon::Account)
13
14
  end
14
15
 
16
+ # Update authenticated account attributes
17
+ # @param params [Hash]
18
+ # @option params :display_name [String] The name to display in the user's profile
19
+ # @option params :note [String] A new biography for the user
20
+ # @option params :avatar [File, StringIO, HTTP::FormData::File]
21
+ # @option params :header [File, StringIO, HTTP::FormData::File]
22
+ # @return [Mastodon::Account]
23
+ def update_credentials(params = {})
24
+ %i(avatar header).each do |key|
25
+ next unless params.key?(key)
26
+ params[key] = params[key].is_a?(HTTP::FormData::File) ? params[key] : HTTP::FormData::File.new(params[key])
27
+ end
28
+
29
+ perform_request_with_object(:patch, '/api/v1/accounts/update_credentials', params, Mastodon::Account)
30
+ end
31
+
15
32
  # Retrieve account
16
33
  # @param id [Integer]
17
34
  # @return [Mastodon::Account]
@@ -21,23 +38,49 @@ module Mastodon
21
38
 
22
39
  # Get a list of followers
23
40
  # @param id [Integer]
41
+ # @param options [Hash]
42
+ # @option options :max_id [Integer]
43
+ # @option options :since_id [Integer]
44
+ # @option options :min_id [Integer]
45
+ # @option options :limit [Integer]
24
46
  # @return [Mastodon::Collection<Mastodon::Account>]
25
- def followers(id)
26
- perform_request_with_collection(:get, "/api/v1/accounts/#{id}/followers", {}, Mastodon::Account)
47
+ def followers(id, options = {})
48
+ perform_request_with_collection(:get, "/api/v1/accounts/#{id}/followers", options, Mastodon::Account)
27
49
  end
28
50
 
29
51
  # Get a list of followed accounts
30
52
  # @param id [Integer]
53
+ # @param options [Hash]
54
+ # @option options :max_id [Integer]
55
+ # @option options :since_id [Integer]
56
+ # @option options :min_id [Integer]
57
+ # @option options :limit [Integer]
31
58
  # @return [Mastodon::Collection<Mastodon::Account>]
32
- def following(id)
33
- perform_request_with_collection(:get, "/api/v1/accounts/#{id}/following", {}, Mastodon::Account)
59
+ def following(id, options = {})
60
+ perform_request_with_collection(:get, "/api/v1/accounts/#{id}/following", options, Mastodon::Account)
34
61
  end
35
62
 
36
- # Follow a remote user
37
- # @param uri [String] The URI of the remote user, in the format of username@domain
38
- # @return [Mastodon::Account]
39
- def follow_by_uri(uri)
40
- perform_request_with_object(:post, '/api/v1/follows', { uri: uri }, Mastodon::Account)
63
+ # Sign up (requires authentication with client credentials)
64
+ # @param params [Hash]
65
+ # @option params :username [String]
66
+ # @option params :email [String]
67
+ # @option params :password [String]
68
+ # @option params :agreement [Boolean]
69
+ # @option params :locale [String]
70
+ # @return [Mastodon::AccessToken]
71
+ def create_account(params = {})
72
+ perform_request_with_object(:post, '/api/v1/accounts', params, Mastodon::AccessToken)
73
+ end
74
+
75
+ # Search accounts
76
+ # @param query [String]
77
+ # @param params [Hash]
78
+ # @option params :limit [Integer]
79
+ # @option params :resolve [Boolean] Whether to attempt resolving unknown remote accounts
80
+ # @option params :following [Boolean] Only return matches the authenticated user follows
81
+ # @return [Mastodon::Collection<Mastodon::Account>]
82
+ def search_accounts(query, params = {})
83
+ perform_request_with_collection(:get, '/api/v1/accounts/search', { q: query }.merge(params), Mastodon::Account)
41
84
  end
42
85
  end
43
86
  end
@@ -1,10 +1,22 @@
1
1
  require 'mastodon/rest/statuses'
2
2
  require 'mastodon/rest/accounts'
3
3
  require 'mastodon/rest/timelines'
4
+ require 'mastodon/rest/notifications'
5
+ require 'mastodon/rest/search'
4
6
  require 'mastodon/rest/relationships'
5
7
  require 'mastodon/rest/media'
6
8
  require 'mastodon/rest/suggestions'
7
9
  require 'mastodon/rest/apps'
10
+ require 'mastodon/rest/instances'
11
+ require 'mastodon/rest/custom_emojis'
12
+ require 'mastodon/rest/domain_blocks'
13
+ require 'mastodon/rest/filters'
14
+ require 'mastodon/rest/endorsements'
15
+ require 'mastodon/rest/suggestions'
16
+ require 'mastodon/rest/reports'
17
+ require 'mastodon/rest/lists'
18
+ require 'mastodon/rest/scheduled_statuses'
19
+ require 'mastodon/rest/conversations'
8
20
 
9
21
  module Mastodon
10
22
  module REST
@@ -12,10 +24,22 @@ module Mastodon
12
24
  include Mastodon::REST::Statuses
13
25
  include Mastodon::REST::Accounts
14
26
  include Mastodon::REST::Timelines
27
+ include Mastodon::REST::Notifications
28
+ include Mastodon::REST::Search
15
29
  include Mastodon::REST::Relationships
16
30
  include Mastodon::REST::Media
17
31
  include Mastodon::REST::Suggestions
18
32
  include Mastodon::REST::Apps
33
+ include Mastodon::REST::Instances
34
+ include Mastodon::REST::CustomEmojis
35
+ include Mastodon::REST::DomainBlocks
36
+ include Mastodon::REST::Filters
37
+ include Mastodon::REST::Endorsements
38
+ include Mastodon::REST::Suggestions
39
+ include Mastodon::REST::Reports
40
+ include Mastodon::REST::Lists
41
+ include Mastodon::REST::ScheduledStatuses
42
+ include Mastodon::REST::Conversations
19
43
  end
20
44
  end
21
45
  end
@@ -1,5 +1,6 @@
1
1
  require 'mastodon/rest/utils'
2
2
  require 'mastodon/app'
3
+ require 'mastodon/entities/app'
3
4
 
4
5
  module Mastodon
5
6
  module REST
@@ -15,6 +16,12 @@ module Mastodon
15
16
  def create_app(name, redirect_uri, scopes = 'read', website = nil)
16
17
  perform_request_with_object(:post, '/api/v1/apps', { client_name: name, redirect_uris: redirect_uri, scopes: scopes, website: website }, Mastodon::App)
17
18
  end
19
+
20
+ # Return currently used app and confirm that client credentials are valid
21
+ # @return [Mastodon::Entities::App]
22
+ def verify_app_credentials
23
+ perform_request_with_object(:get, '/api/v1/apps/verify_credentials', {}, Mastodon::Entities::App)
24
+ end
18
25
  end
19
26
  end
20
27
  end
@@ -0,0 +1,34 @@
1
+ require 'mastodon/rest/utils'
2
+ require 'mastodon/conversation'
3
+
4
+ module Mastodon
5
+ module REST
6
+ module Conversations
7
+ include Mastodon::REST::Utils
8
+
9
+ # Get a list of conversations
10
+ # @param options [Hash]
11
+ # @option options :max_id [Integer]
12
+ # @option options :since_id [Integer]
13
+ # @option options :min_id [Integer]
14
+ # @option options :limit [Integer]
15
+ # @return [Mastodon::Collection<Mastodon::Conversation>]
16
+ def conversations(options = {})
17
+ perform_request_with_collection(:get, '/api/v1/conversations', options, Mastodon::Conversation)
18
+ end
19
+
20
+ # Mark a conversation as read
21
+ # @param id [Integer]
22
+ # @return [Mastodon::Conversation]
23
+ def mark_conversation_as_read(id)
24
+ perform_request_with_object(:post, "/api/v1/conversations/#{id}/read", {}, Mastodon::Conversation)
25
+ end
26
+
27
+ # Delete a conversation. Does not delete statuses in the conversation
28
+ # @param id [Integer]
29
+ def delete_conversation(id)
30
+ perform_request(:delete, "/api/v1/conversations/#{id}")
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,16 @@
1
+ require 'mastodon/rest/utils'
2
+ require 'mastodon/emoji'
3
+
4
+ module Mastodon
5
+ module REST
6
+ module CustomEmojis
7
+ include Mastodon::REST::Utils
8
+
9
+ # Get a list of custom emojis on the server
10
+ # @return [Mastodon::Collection<Mastodon::Emoji>]
11
+ def custom_emojis
12
+ perform_request_with_collection('/api/v1/custom_emojis', {}, Mastodon::Emoji)
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,32 @@
1
+ require 'mastodon/rest/utils'
2
+
3
+ module Mastodon
4
+ module REST
5
+ module DomainBlocks
6
+ include Mastodon::REST::Utils
7
+
8
+ # Get a list of blocked domains
9
+ # @param options [Hash]
10
+ # @option options :max_id [Integer]
11
+ # @option options :since_id [Integer]
12
+ # @option options :min_id [Integer]
13
+ # @option options :limit [Integer]
14
+ # @return [Mastodon::Collection<String>]
15
+ def domain_blocks(options = {})
16
+ perform_request_with_collection('/api/v1/domain_blocks', options, String)
17
+ end
18
+
19
+ # Block a domain
20
+ # @param domain [String]
21
+ def block_domain(domain)
22
+ perform_request(:post, '/api/v1/domain_blocks', domain: domain)
23
+ end
24
+
25
+ # Unblock a domain
26
+ # @param domain [String]
27
+ def unblock_domain(domain)
28
+ perform_request(:delete, '/api/v1/domain_blocks', domain: domain)
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,36 @@
1
+ require 'mastodon/rest/utils'
2
+ require 'mastodon/account'
3
+ require 'mastodon/relationship'
4
+
5
+ module Mastodon
6
+ module REST
7
+ module Endorsements
8
+ include Mastodon::REST::Utils
9
+
10
+ # Get a list of endorsed accounts
11
+ # @param options [Hash]
12
+ # @option options :max_id [Integer]
13
+ # @option options :since_id [Integer]
14
+ # @option options :min_id [Integer]
15
+ # @option options :limit [Integer]
16
+ # @return [Mastodon::Collection<Mastodon::Account>]
17
+ def endorsements(options = {})
18
+ perform_request_with_collection('/api/v1/endorsements', options, Mastodon::Account)
19
+ end
20
+
21
+ # Endorse an account (feature on own profile)
22
+ # @param account_id [Integer]
23
+ # @return [Mastodon::Relationship]
24
+ def endorse(account_id)
25
+ perform_request_with_object(:post, "/api/v1/accounts/#{account_id}/pin", {}, Mastodon::Relationship)
26
+ end
27
+
28
+ # Unendorse an account (no longer feature it on own profile)
29
+ # @param account_id [Integer]
30
+ # @return [Mastodon::Relationship]
31
+ def unendorse(account_id)
32
+ perform_request_with_object(:post, "/api/v1/accounts/#{account_id}/unpin", {}, Mastodon::Relationship)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,61 @@
1
+ require 'mastodon/rest/utils'
2
+ require 'mastodon/filter'
3
+
4
+ module Mastodon
5
+ module REST
6
+ module Filters
7
+ include Mastodon::REST::Utils
8
+
9
+ # Get a list of filters
10
+ # @param options [Hash]
11
+ # @option options :max_id [Integer]
12
+ # @option options :since_id [Integer]
13
+ # @option options :min_id [Integer]
14
+ # @option options :limit [Integer]
15
+ # @return [Mastodon::Collection<Mastodon::Filter>]
16
+ def filters(options = {})
17
+ perform_request_with_collection('/api/v1/filters', options, Mastodon::Filter)
18
+ end
19
+
20
+ # Retrieve a filter
21
+ # @param id [Integer]
22
+ # @return [Mastodon::Filter]
23
+ def filter(id)
24
+ perform_request_with_object(:put, "/api/v1/filters/#{id}", {}, Mastodon::Filter)
25
+ end
26
+
27
+ # Create a filter
28
+ # @param params [Hash]
29
+ # @option params :phrase [String]
30
+ # @option params :context [Array<String>]
31
+ # @option params :irreversible [Boolean]
32
+ # @option params :whole_word [Boolean]
33
+ # @option params :expires_in [Integer]
34
+ # @return [Mastodon::Filter]
35
+ def create_filter(params = {})
36
+ params[:'context[]'] = params.delete(:context) if params.key?(:context)
37
+ perform_request_with_object(:post, '/api/v1/filters', params, Mastodon::Filter)
38
+ end
39
+
40
+ # Update a filter
41
+ # @param id [Integer]
42
+ # @param params [Hash]
43
+ # @option params :phrase [String]
44
+ # @option params :context [Array<String>]
45
+ # @option params :irreversible [Boolean]
46
+ # @option params :whole_word [Boolean]
47
+ # @option params :expires_in [Integer]
48
+ # @return [Mastodon::Filter]
49
+ def update_filter(id, params = {})
50
+ params[:'context[]'] = params.delete(:context) if params.key?(:context)
51
+ perform_request_with_object(:put, "/api/v1/filters/#{id}", params, Mastodon::Filter)
52
+ end
53
+
54
+ # Delete a filter
55
+ # @param id [Integer]
56
+ def delete_filter(id)
57
+ perform_request(:delete, "/api/v1/filters/#{id}")
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,28 @@
1
+ require 'mastodon/rest/utils'
2
+ require 'mastodon/instance'
3
+
4
+ module Mastodon
5
+ module REST
6
+ module Instances
7
+ include Mastodon::REST::Utils
8
+
9
+ # Retrieve the current instance. Does not require authentication
10
+ # @return [Mastodon::Instance]
11
+ def instance
12
+ perform_request_with_object(:get, '/api/v1/instance', {}, Mastodon::Instance)
13
+ end
14
+
15
+ # Retrieve activity statistics for the current instance. Does not require authentication
16
+ # @return [Mastodon::Collection<Hash>]
17
+ def activity
18
+ perform_request_with_collection(:get, '/api/v1/instance/activity', {}, Hash)
19
+ end
20
+
21
+ # Retrieve domains of instances known to the current instance. Does not require authentication
22
+ # @return [Mastodon::Collection<String>]
23
+ def peers
24
+ perform_request_with_collection(:get, '/api/v1/instance/peers', {}, String)
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,77 @@
1
+ require 'mastodon/rest/utils'
2
+ require 'mastodon/list'
3
+ require 'mastodon/account'
4
+
5
+ module Mastodon
6
+ module REST
7
+ module Lists
8
+ include Mastodon::REST::Utils
9
+
10
+ # Get a list of your lists
11
+ # @param options [Hash]
12
+ # @option options :max_id [Integer]
13
+ # @option options :since_id [Integer]
14
+ # @option options :limit [Integer]
15
+ # @return [Mastodon::Collection<Mastodon::List>]
16
+ def lists(options = {})
17
+ perform_request_with_collection(:get, '/api/v1/lists', options, Mastodon::List)
18
+ end
19
+
20
+ # Retrieve a list
21
+ # @param id [Integer]
22
+ # @return [Mastodon::List]
23
+ def list(id)
24
+ perform_request_with_object(:get, "/api/v1/lists/#{id}", {}, Mastodon::List)
25
+ end
26
+
27
+ # Create a list
28
+ # @param params [Hash]
29
+ # @option params :title
30
+ # @return [Mastodon::List]
31
+ def create_list(params = {})
32
+ perform_request_with_object(:post, '/api/v1/lists/', params, Mastodon::List)
33
+ end
34
+
35
+ # Update a list
36
+ # @param id [Integer]
37
+ # @param params [Hash]
38
+ # @option params :title
39
+ # @return [Mastodon::List]
40
+ def update_list(id, params = {})
41
+ perform_request_with_object(:put, "/api/v1/lists/#{id}", params, Mastodon::List)
42
+ end
43
+
44
+ # Delete a list
45
+ # @param id [Integer]
46
+ def delete_list(id)
47
+ perform_request(:delete, "/api/v1/lists/#{id}")
48
+ end
49
+
50
+ # Get a list of accounts on the list
51
+ # @param id [Integer]
52
+ # @param options [Hash]
53
+ # @option options :max_id [Integer]
54
+ # @option options :since_id [Integer]
55
+ # @option options :min_id [Integer]
56
+ # @option options :limit [Integer]
57
+ # @return [Mastodon::Collection<Mastodon::Account>]
58
+ def list_accounts(id, options = {})
59
+ perform_request_with_collection(:get, "/api/v1/lists/#{id}/accounts", options, Mastodon::Account)
60
+ end
61
+
62
+ # Add accounts to a list
63
+ # @param id [Integer]
64
+ # @param account_ids [Array<Integer>]
65
+ def add_accounts_to_list(id, account_ids = [])
66
+ perform_request(:post, "/api/v1/lists/#{id}/accounts", { 'account_ids[]': account_ids })
67
+ end
68
+
69
+ # Remove accounts from list
70
+ # @param id [Integer]
71
+ # @param account_ids [Array<Integer>]
72
+ def remove_accounts_from_list(id, account_ids = [])
73
+ perform_request(:delete, "/api/v1/lists/#{id}/accounts", { 'account_ids[]': account_ids })
74
+ end
75
+ end
76
+ end
77
+ end