Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add twtxt support #2746

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions app/controllers/accounts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

class AccountsController < ApplicationController
OFFSET = 0
PAGE_SIZE = 20
PAGE_SIZE_MAX = 200

Expand All @@ -20,17 +21,21 @@ def show
expires_in(15.seconds, public: true, stale_while_revalidate: 30.seconds, stale_if_error: 1.hour) unless user_signed_in?
end

format.rss do
format.any(:rss, :txt) do
expires_in 1.minute, public: true

limit = params[:limit].present? ? [params[:limit].to_i, PAGE_SIZE_MAX].min : PAGE_SIZE
@statuses = filtered_statuses.without_reblogs.limit(limit)
offset = params[:offset].present? ? params[:offset].to_i : OFFSET
@statuses = filtered_statuses.without_reblogs.limit(limit).offset(offset)
@statuses = preload_collection(@statuses, Status)
end

format.json do
expires_in 3.minutes, public: !(authorized_fetch_mode? && signed_request_account.present?)
render_with_cache json: @account, content_type: 'application/activity+json', serializer: ActivityPub::ActorSerializer, adapter: ActivityPub::Adapter
render_with_cache json: @account,
content_type: 'application/activity+json',
serializer: ActivityPub::ActorSerializer,
adapter: ActivityPub::Adapter
end
end
end
Expand All @@ -46,7 +51,7 @@ def filtered_statuses
end

def default_statuses
@account.statuses.not_local_only.distributable_visibility
@account.statuses.not_local_only.where(visibility: [:public, :unlisted])
end

def only_media_scope
Expand Down
39 changes: 39 additions & 0 deletions app/views/accounts/show.txt.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<% account_url = params[:tag].present? ? short_account_tag_url(@account, params[:tag]) : short_account_url(@account) %>
# Twtxt is an open, distributed microblogging platform that
# uses human-readable text files, common transport protocols,
# and free software.
#
# Learn more about twtxt at https://github.com/buckket/twtxt
#
# The numeric value between the date and the status text (#...) is the id
# of that toot, so you can easily find the URL of a toot.
#
# By default, you will only see the 20 most recent statuses.
#
# You can also set parameters "limit" and "offset" in your query string if
# you want to fetch more statuses. Please note that you can only fetch up
# to 200 statuses at a time.
#
# E.g. to get the latest 200 statuses:
# <%= account_url %>.txt?limit=200
#
# E.g. to skip the latest 200 status, and fetch the 200 previous ones:
# <%= account_url %>.txt?offset=200&limit=200
#
# nick = @<%= @account.local_username_and_domain %>
# url = <%= account_url %>.txt
# avatar = <%= full_asset_url(@account.avatar.url(:original)) %>
# description = <%= I18n.t('rss.descriptions.account', acct: @account.local_username_and_domain) %>

<% @statuses.each do |status| %>
<% plain_text_status = (extract_status_plain_text status).gsub("\n", "\u2028") %>
<% status.ordered_media_attachments.each do |media| %>
<% media_url = full_asset_url(media.file.url(:original, false)) %>
<% media_description = media.description.nil? ? "" : media.description.gsub("\n", " ") %>
<% plain_text_status += " ![#{media_description}](#{media_url})" %>
<% end %>
<% if !plain_text_status.empty? %>
<% plain_text_status = " (\##{status.id}) #{plain_text_status}" %>
<%= status.created_at.rfc3339 %> <%= plain_text_status %>
<% end %>
<% end %>
1 change: 1 addition & 0 deletions config/initializers/mime_types.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Mime::Type.register 'application/json', :json, %w(text/x-json application/jsonrequest application/jrd+json application/activity+json application/ld+json)
Mime::Type.register 'text/xml', :xml, %w(application/xml application/atom+xml application/xrd+xml)
Mime::Type.register 'text/plain', :txt, %w(text/plain)

# WebP is not defined in Rack 2.2.
Rack::Mime::MIME_TYPES['.webp'] = 'image/webp'