Compare commits
No commits in common. "760e89cb685a35a0f01119821757860accca5a20" and "c43e43d89c1dbaecf4a77d29078b01ae86a6945d" have entirely different histories.
760e89cb68
...
c43e43d89c
@ -11,9 +11,6 @@ module Settings
|
||||
|
||||
field :mastodon_address_domain, type: :string,
|
||||
default: ENV["MASTODON_ADDRESS_DOMAIN"].presence || self.primary_domain
|
||||
|
||||
field :mastodon_auth_token, type: :string,
|
||||
default: ENV["MASTODON_AUTH_TOKEN"].presence
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
#
|
||||
# API Docs: https://docs.btcpayserver.org/API/Greenfield/v1/
|
||||
#
|
||||
class BtcpayManagerService < RestApiService
|
||||
class BtcpayManagerService < ApplicationService
|
||||
private
|
||||
|
||||
def base_url
|
||||
@ -19,4 +19,18 @@ class BtcpayManagerService < RestApiService
|
||||
"Authorization" => "token #{auth_token}"
|
||||
}
|
||||
end
|
||||
|
||||
def endpoint_url(path)
|
||||
"#{base_url}/#{path.gsub(/^\//, '')}"
|
||||
end
|
||||
|
||||
def get(path, params = {})
|
||||
res = Faraday.get endpoint_url(path), params, headers
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
def post(path, payload)
|
||||
res = Faraday.post endpoint_url(path), payload.to_json, headers
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
end
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
module MastodonManager
|
||||
class FetchUser < MastodonManagerService
|
||||
def initialize(mastodon_id:)
|
||||
@mastodon_id = mastodon_id
|
||||
end
|
||||
|
||||
def call
|
||||
user = get "v1/admin/accounts/#{@mastodon_id}"
|
||||
user.with_indifferent_access
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,14 +0,0 @@
|
||||
module MastodonManager
|
||||
class FindUser < MastodonManagerService
|
||||
def initialize(username:)
|
||||
@username = username
|
||||
end
|
||||
|
||||
def call
|
||||
users = get "v2/admin/accounts?username=#{@username}&origin=local"
|
||||
users = users.map { |u| u.with_indifferent_access }
|
||||
# Results may contain partial matches
|
||||
users.find { |u| u.dig(:username) == @username }
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,58 +0,0 @@
|
||||
module MastodonManager
|
||||
class SyncAccountProfiles < MastodonManagerService
|
||||
def initialize(direction: "down", overwrite: false)
|
||||
@direction = direction
|
||||
@overwrite = overwrite
|
||||
|
||||
if @direction != "down"
|
||||
raise NotImplementedError
|
||||
end
|
||||
end
|
||||
|
||||
def call
|
||||
Rails.logger.debug { "Syncing account profiles (direction: #{@direction}, overwrite: #{@overwrite})"}
|
||||
|
||||
User.where(cn: "colin").find_each do |user|
|
||||
# User.find_each do |user|
|
||||
if user.mastodon_id.blank?
|
||||
mastodon_user = MastodonManager::FindUser.call username: user.cn
|
||||
if mastodon_user
|
||||
Rails.logger.debug { "Setting mastodon_id for user #{user.cn}" }
|
||||
user.update! mastodon_id: @user[:id]
|
||||
else
|
||||
Rails.logger.debug { "No Mastodon user found for username #{user.cn}" }
|
||||
next
|
||||
end
|
||||
end
|
||||
|
||||
next if user.avatar.attached? && user.display_name.present?
|
||||
|
||||
unless mastodon_user
|
||||
Rails.logger.debug { "Fetching Mastodon account with ID #{user.mastodon_id} for #{user.cn}" }
|
||||
mastodon_user = MastodonManager::FetchUser.call mastodon_id: user.mastodon_id
|
||||
end
|
||||
|
||||
if user.display_name.blank?
|
||||
if mastodon_display_name = mastodon_user.dig(:account, :display_name)
|
||||
Rails.logger.debug { "Setting display name for user #{user.cn} from Mastodon" }
|
||||
LdapManager::UpdateDisplayName.call(
|
||||
dn: user.dn, display_name: mastodon_display_name
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
if !user.avatar.attached?
|
||||
if avatar_url = mastodon_user.dig(:account, :avatar_static)
|
||||
Rails.logger.debug { "Importing Mastodon avatar for user #{user.cn}" }
|
||||
UserManager::ImportRemoteAvatar.call(
|
||||
user: user, avatar_url: avatar_url
|
||||
)
|
||||
end
|
||||
end
|
||||
rescue => e
|
||||
Sentry.capture_exception(e) if Setting.sentry_enabled?
|
||||
Rails.logger.error e
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -1,22 +0,0 @@
|
||||
#
|
||||
# API Docs: https://docs.joinmastodon.org/methods/
|
||||
#
|
||||
class MastodonManagerService < RestApiService
|
||||
private
|
||||
|
||||
def base_url
|
||||
@base_url ||= "#{Setting.mastodon_public_url}/api"
|
||||
end
|
||||
|
||||
def auth_token
|
||||
@auth_token ||= Setting.mastodon_auth_token
|
||||
end
|
||||
|
||||
def headers
|
||||
{
|
||||
"Content-Type" => "application/json",
|
||||
"Accept" => "application/json",
|
||||
"Authorization" => "Bearer #{auth_token}"
|
||||
}
|
||||
end
|
||||
end
|
||||
@ -1,27 +0,0 @@
|
||||
class RestApiService < ApplicationService
|
||||
private
|
||||
|
||||
def base_url
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def headers
|
||||
raise NotImplementedError
|
||||
end
|
||||
|
||||
def endpoint_url(path)
|
||||
"#{base_url}/#{path.gsub(/^\//, '')}"
|
||||
end
|
||||
|
||||
def get(path, params = {})
|
||||
res = Faraday.get endpoint_url(path), params, headers
|
||||
# TODO handle unsuccessful responses with no valid JSON body
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
|
||||
def post(path, payload)
|
||||
res = Faraday.post endpoint_url(path), payload.to_json, headers
|
||||
# TODO handle unsuccessful responses with no valid JSON body
|
||||
JSON.parse(res.body)
|
||||
end
|
||||
end
|
||||
@ -1,12 +0,0 @@
|
||||
module UserManager
|
||||
class ImportRemoteAvatar < UserManagerService
|
||||
def initialize(user:, avatar_url:)
|
||||
@user = user
|
||||
@avatar_url = avatar_url
|
||||
end
|
||||
|
||||
def call
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@ -16,10 +16,5 @@
|
||||
key: :mastodon_address_domain,
|
||||
title: "User address domain"
|
||||
) %>
|
||||
<%= render FormElements::FieldsetResettableSettingComponent.new(
|
||||
key: :mastodon_auth_token,
|
||||
type: :password,
|
||||
title: "API auth token"
|
||||
) %>
|
||||
<% end %>
|
||||
</ul>
|
||||
|
||||
@ -1,5 +0,0 @@
|
||||
class AddMastodonIdToUsers < ActiveRecord::Migration[8.0]
|
||||
def change
|
||||
add_column :users, :mastodon_id, :bigint
|
||||
end
|
||||
end
|
||||
@ -10,7 +10,7 @@
|
||||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_05_17_105755) do
|
||||
ActiveRecord::Schema[8.0].define(version: 2025_05_06_154628) do
|
||||
create_table "active_storage_attachments", force: :cascade do |t|
|
||||
t.string "name", null: false
|
||||
t.string "record_type", null: false
|
||||
@ -133,7 +133,6 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_17_105755) do
|
||||
t.string "pgp_fpr"
|
||||
t.string "lndhub_username"
|
||||
t.text "lndhub_password"
|
||||
t.bigint "mastodon_id"
|
||||
t.index ["email"], name: "index_users_on_email", unique: true
|
||||
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
|
||||
end
|
||||
|
||||
@ -1,10 +0,0 @@
|
||||
# Mastodon
|
||||
|
||||
## API access
|
||||
|
||||
(Optional)
|
||||
|
||||
Log in to your Mastodon instance with an admin account and create a new
|
||||
OAuth application:
|
||||
|
||||
https://kosmos.social/settings/applications/new
|
||||
Loading…
x
Reference in New Issue
Block a user