Initial commit
This commit is contained in:
commit
9c4856bdb1
73 changed files with 1393 additions and 0 deletions
8
app/api/mastodon/api.rb
Normal file
8
app/api/mastodon/api.rb
Normal file
|
@ -0,0 +1,8 @@
|
|||
module Mastodon
|
||||
class API < Grape::API
|
||||
rescue_from :all
|
||||
|
||||
mount Mastodon::Ostatus
|
||||
mount Mastodon::Rest
|
||||
end
|
||||
end
|
21
app/api/mastodon/entities.rb
Normal file
21
app/api/mastodon/entities.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module Mastodon
|
||||
module Entities
|
||||
class Account < Grape::Entity
|
||||
expose :username
|
||||
expose :domain
|
||||
end
|
||||
|
||||
class Status < Grape::Entity
|
||||
format_with(:iso_timestamp) { |dt| dt.iso8601 }
|
||||
|
||||
expose :uri
|
||||
expose :text
|
||||
expose :account, using: Mastodon::Entities::Account
|
||||
|
||||
with_options(format_with: :iso_timestamp) do
|
||||
expose :created_at
|
||||
expose :updated_at
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
63
app/api/mastodon/ostatus.rb
Normal file
63
app/api/mastodon/ostatus.rb
Normal file
|
@ -0,0 +1,63 @@
|
|||
module Mastodon
|
||||
class Ostatus < Grape::API
|
||||
format :txt
|
||||
|
||||
before do
|
||||
@account = Account.find(params[:id])
|
||||
end
|
||||
|
||||
resource :subscriptions do
|
||||
helpers do
|
||||
def subscription_url(account)
|
||||
"https://649841dc.ngrok.io/api#{subscriptions_path(id: account.id)}"
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Receive updates from a feed'
|
||||
|
||||
params do
|
||||
requires :id, type: String, desc: 'Account ID'
|
||||
end
|
||||
|
||||
post ':id' do
|
||||
body = request.body.read
|
||||
|
||||
if @account.subscription(subscription_url(@account)).verify(body, env['HTTP_X_HUB_SIGNATURE'])
|
||||
ProcessFeedUpdateService.new.(body, @account)
|
||||
status 201
|
||||
else
|
||||
status 202
|
||||
end
|
||||
end
|
||||
|
||||
desc 'Confirm PuSH subscription to a feed'
|
||||
|
||||
params do
|
||||
requires :id, type: String, desc: 'Account ID'
|
||||
requires 'hub.topic', type: String, desc: 'Topic URL'
|
||||
requires 'hub.verify_token', type: String, desc: 'Verification token'
|
||||
requires 'hub.challenge', type: String, desc: 'Hub challenge'
|
||||
end
|
||||
|
||||
get ':id' do
|
||||
if @account.subscription(subscription_url(@account)).valid?(params['hub.topic'], params['hub.verify_token'])
|
||||
params['hub.challenge']
|
||||
else
|
||||
error! :not_found, 404
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
resource :salmon do
|
||||
desc 'Receive Salmon updates'
|
||||
|
||||
params do
|
||||
requires :id, type: String, desc: 'Account ID'
|
||||
end
|
||||
|
||||
post ':id' do
|
||||
# todo
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
13
app/api/mastodon/rest.rb
Normal file
13
app/api/mastodon/rest.rb
Normal file
|
@ -0,0 +1,13 @@
|
|||
module Mastodon
|
||||
class Rest < Grape::API
|
||||
version 'v1', using: :path
|
||||
format :json
|
||||
|
||||
resource :statuses do
|
||||
desc 'Return a public timeline'
|
||||
get :all do
|
||||
present Status.all, with: Mastodon::Entities::Status
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
0
app/assets/images/.keep
Normal file
0
app/assets/images/.keep
Normal file
16
app/assets/javascripts/application.js
Normal file
16
app/assets/javascripts/application.js
Normal file
|
@ -0,0 +1,16 @@
|
|||
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
||||
// listed below.
|
||||
//
|
||||
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
||||
// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
|
||||
//
|
||||
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
||||
// compiled file.
|
||||
//
|
||||
// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
|
||||
// about supported directives.
|
||||
//
|
||||
//= require jquery
|
||||
//= require jquery_ujs
|
||||
//= require turbolinks
|
||||
//= require_tree .
|
15
app/assets/stylesheets/application.css
Normal file
15
app/assets/stylesheets/application.css
Normal file
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* This is a manifest file that'll be compiled into application.css, which will include all the files
|
||||
* listed below.
|
||||
*
|
||||
* Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
|
||||
* or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
|
||||
*
|
||||
* You're free to add application-wide styles to this file and they'll appear at the bottom of the
|
||||
* compiled file so the styles you add here take precedence over styles defined in any styles
|
||||
* defined in the other CSS/SCSS files in this directory. It is generally better to create a new
|
||||
* file per style scope.
|
||||
*
|
||||
*= require_tree .
|
||||
*= require_self
|
||||
*/
|
5
app/controllers/application_controller.rb
Normal file
5
app/controllers/application_controller.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class ApplicationController < ActionController::Base
|
||||
# Prevent CSRF attacks by raising an exception.
|
||||
# For APIs, you may want to use :null_session instead.
|
||||
protect_from_forgery with: :exception
|
||||
end
|
0
app/controllers/concerns/.keep
Normal file
0
app/controllers/concerns/.keep
Normal file
2
app/helpers/application_helper.rb
Normal file
2
app/helpers/application_helper.rb
Normal file
|
@ -0,0 +1,2 @@
|
|||
module ApplicationHelper
|
||||
end
|
0
app/mailers/.keep
Normal file
0
app/mailers/.keep
Normal file
0
app/models/.keep
Normal file
0
app/models/.keep
Normal file
7
app/models/account.rb
Normal file
7
app/models/account.rb
Normal file
|
@ -0,0 +1,7 @@
|
|||
class Account < ActiveRecord::Base
|
||||
has_many :statuses, inverse_of: :account
|
||||
|
||||
def subscription(webhook_url)
|
||||
@subscription ||= OStatus2::Subscription.new(self.remote_url, secret: self.secret, token: self.verify_token, webhook: webhook_url, hub: self.hub_url)
|
||||
end
|
||||
end
|
0
app/models/concerns/.keep
Normal file
0
app/models/concerns/.keep
Normal file
3
app/models/status.rb
Normal file
3
app/models/status.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class Status < ActiveRecord::Base
|
||||
belongs_to :account, inverse_of: :statuses
|
||||
end
|
5
app/services/fetch_feed_service.rb
Normal file
5
app/services/fetch_feed_service.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class FetchFeedService
|
||||
def call(account)
|
||||
# todo
|
||||
end
|
||||
end
|
60
app/services/follow_remote_user_service.rb
Normal file
60
app/services/follow_remote_user_service.rb
Normal file
|
@ -0,0 +1,60 @@
|
|||
class FollowRemoteUserService
|
||||
include GrapeRouteHelpers::NamedRouteMatcher
|
||||
|
||||
def call(user)
|
||||
username, domain = user.split('@')
|
||||
account = Account.where(username: username, domain: domain).first
|
||||
|
||||
return account unless account.nil?
|
||||
|
||||
account = Account.new(username: username, domain: domain)
|
||||
data = Goldfinger.finger("acct:#{user}")
|
||||
|
||||
account.remote_url = data.link('http://schemas.google.com/g/2010#updates-from').href
|
||||
account.salmon_url = data.link('salmon').href
|
||||
account.public_key = magic_key_to_pem(data.link('magic-public-key').href)
|
||||
account.private_key = nil
|
||||
|
||||
account.secret = SecureRandom.hex
|
||||
account.verify_token = SecureRandom.hex
|
||||
|
||||
feed = get_feed(account.remote_url)
|
||||
hubs = feed.xpath('//xmlns:link[@rel="hub"]')
|
||||
|
||||
return false if hubs.empty? || hubs.first.attribute('href').nil?
|
||||
|
||||
account.hub_url = hubs.first.attribute('href').value
|
||||
account.save!
|
||||
|
||||
subscription = account.subscription(subscription_url(account))
|
||||
subscription.subscribe
|
||||
rescue Goldfinger::Error, HTTP::Error => e
|
||||
false
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def get_feed(url)
|
||||
response = http_client.get(Addressable::URI.parse(url))
|
||||
Nokogiri::XML(response)
|
||||
end
|
||||
|
||||
def magic_key_to_pem(magic_key)
|
||||
_, modulus, exponent = magic_key.split('.')
|
||||
modulus, exponent = [modulus, exponent].map { |n| Base64.urlsafe_decode64(n).bytes.inject(0) { |num, byte| (num << 8) | byte } }
|
||||
|
||||
key = OpenSSL::PKey::RSA.new
|
||||
key.n = modulus
|
||||
key.d = exponent
|
||||
|
||||
key.to_pem
|
||||
end
|
||||
|
||||
def http_client
|
||||
HTTP
|
||||
end
|
||||
|
||||
def subscription_url(account)
|
||||
"https://649841dc.ngrok.io/api#{subscriptions_path(id: account.id)}"
|
||||
end
|
||||
end
|
20
app/services/process_feed_update_service.rb
Normal file
20
app/services/process_feed_update_service.rb
Normal file
|
@ -0,0 +1,20 @@
|
|||
class ProcessFeedUpdateService
|
||||
def call(body, account)
|
||||
xml = Nokogiri::XML(body)
|
||||
|
||||
xml.xpath('/xmlns:feed/xmlns:entry').each do |entry|
|
||||
uri = entry.at_xpath('./xmlns:id').content
|
||||
status = Status.find_by(uri: uri)
|
||||
|
||||
next unless status.nil?
|
||||
|
||||
status = Status.new
|
||||
status.account = account
|
||||
status.uri = uri
|
||||
status.text = entry.at_xpath('./xmlns:content').content
|
||||
status.created_at = entry.at_xpath('./xmlns:published').content
|
||||
status.updated_at = entry.at_xpath('./xmlns:updated').content
|
||||
status.save!
|
||||
end
|
||||
end
|
||||
end
|
14
app/views/layouts/application.html.erb
Normal file
14
app/views/layouts/application.html.erb
Normal file
|
@ -0,0 +1,14 @@
|
|||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>Mastodon</title>
|
||||
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
|
||||
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
|
||||
<%= csrf_meta_tags %>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<%= yield %>
|
||||
|
||||
</body>
|
||||
</html>
|
Loading…
Add table
Add a link
Reference in a new issue