Skip to content

Commit

Permalink
Add option for customizing the status length
Browse files Browse the repository at this point in the history
  • Loading branch information
dhelonious committed Jul 29, 2024
1 parent 1fa3b41 commit 02d29c4
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 31 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ An initial setup command is required to initialize the database and configuratio

sudo mastodon-server.setup

> Note: Be patient if you have changed the `status.char-limit`, as it takes some time to update the assets.
## Create admin user

Once the snap is set up, an administrator account with a randomly generated password can be created using the `tootctl` command:
Expand Down Expand Up @@ -117,6 +119,7 @@ The following settings are available:
|----------------------|-------------------------------|------------------------|-----------------------------------------------------------------------|
| `domain` | valid FQDN | | FQDN of the Mastodon instance |
| `email` | valid e-mail | | E-mail address of the owner of the Mastodon instance |
| `status.char-limit` | integer | 500 | Character limit of statuses (toots) |
| `ports.http` | 0 to 65353 | 80 | HTTP port |
| `ports.https` | 0 to 65353 | 443 | HTTPS port |
| `acme.server` | letsencrypt, zerossl | letsencrypt | CA used for acquiring an SSL certificate |
Expand Down
8 changes: 3 additions & 5 deletions RELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ This release contains:
* mastodon-bird-ui 1.8.5
* tangerine-ui 1.9.5

Changelog for 4.2.10snap2:
Changelog for 4.2.10snap3:

* The PostgreSQL log file now has permissions that allow it to be rotated by logrotate.
* The snap now correctly detects and handles updates where the PostgreSQL major version changes.
* Huge pages are disabled in PostgreSQL because it does not have access to mmap in snap.
* Redis is prevented from trying to access the current_clocksource file on every restart, which is not possible in snap.
* Adds an option to change the character limit of toots (`status.characters`).
* Updates the browserlist DB for npm and yarn on build (see https://github.com/browserslist/update-db#readme)
14 changes: 12 additions & 2 deletions snap/hooks/configure
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
. "$SNAP/nginx.env"
. "$SNAP/mastodon.env"

export CURRENT_DOMAIN="$(mastodon_get_var LOCAL_DOMAIN)"
export CURRENT_HTTP_PORT="$(nginx_get_http_port)"
export CURRENT_HTTPS_PORT="$(nginx_get_https_port)"
export CURRENT_NGINX_ACCESS_LOG="$(nginx_get_access_log)"
export CURRENT_MEDIA_DIR="$(readlink -f $SNAP_DATA/mastodon/public/system)"
export CURRENT_BACKUP_DIR="$(get_backup_dir_file)"
export CURRENT_STATUS_MAX_CHARS="$(get_status_max_chars_file)"

if [ "$NGINX_ACCESS_LOG" != "$CURRENT_NGINX_ACCESS_LOG" ]; then
restart_required nginx
Expand All @@ -33,12 +35,20 @@ if [ "$BACKUP_DIR" != "$CURRENT_BACKUP_DIR" ]; then
fi

if $(mastodon_config_exists); then
export CURRENT_DOMAIN="$(mastodon_get_var LOCAL_DOMAIN)"

if [ "$DOMAIN" != "$CURRENT_DOMAIN" ]; then
echo_error "DOMAIN cannot be changed safely! Setup a new instance if you really want to change it."
exit 1
fi

if [ "$STATUS_MAX_CHARS" != "$CURRENT_STATUS_MAX_CHARS" ]; then
echo Update status character limit
update_status_max_chars
echo "Update assets (this takes some time)"
precompile_assets

restart_required backend
restart_required sidekiq
fi
fi

echo Restart services
Expand Down
3 changes: 2 additions & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ description: |
implements ActivityPub!)
base: core20
version: 4.2.10snap2
version: 4.2.10snap3
grade: stable
confinement: strict

Expand Down Expand Up @@ -747,6 +747,7 @@ parts:
bundle config build.openssl --with-openssl-dir=$SNAPCRAFT_STAGE/openssl1
bundle config build.pg --with-pg-config=$SNAPCRAFT_STAGE/postgres/bin/pg_config
bundle install -j"$(nproc)"
npx browserslist@latest --update-db --yes
yarn install --pure-lockfile
yarn cache clean
bundle exec rails assets:precompile
Expand Down
7 changes: 7 additions & 0 deletions src/mastodon/bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ $SNAP/bin/psql.wrapper -d "$MASTODON_DBNAME" -c "create extension pg_stat_statem
echo Remove old logs and tempfiles
mastodon_rails log:clear tmp:clear

if [ "$STATUS_MAX_CHARS" != "$(get_status_max_chars_file)" ]; then
echo Update status character limit
update_status_max_chars
echo "Update assets (this takes some time)"
precompile_assets
fi

echo Restart services
snapctl restart "$SNAP_NAME.backend"
snapctl restart "$SNAP_NAME.streaming"
Expand Down
80 changes: 57 additions & 23 deletions src/mastodon/mastodon.env
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export BUNDLE_GEMFILE="$SNAP_DATA/mastodon/Gemfile"
export BUNDLE_RAKEFILE="$SNAP_DATA/mastodon/Rakefile"
export GEM_HOME="$SNAP_DATA/mastodon/vendor/ruby"
export GEM_PATH="$GEM_HOME"
export YARN_RC_FILENAME="$SNAP_DATA/mastodon/.yarnrc.yml"
export YARN_CACHE_FOLDER=/tmp/yarn/cache

export UPDATE_BACKUPS="$(snapctl get update.backups)"
export MEDIA_DIR="$(snapctl get media.dir)"
Expand All @@ -30,6 +32,38 @@ export CLEANUP_MEDIA="$(snapctl get cleanup.media)"
export CLEANUP_PREVIEWS="$(snapctl get cleanup.previews)"
export CLEANUP_STATUSES="$(snapctl get cleanup.statuses)"
export CLEANUP_ORPHANS="$(snapctl get cleanup.orphans)"
export STATUS_MAX_CHARS="$(snapctl get status.char-limit)"

compose_form="$SNAP_DATA/mastodon/app/javascript/mastodon/features/compose/components/compose_form.jsx"
status_length_validator="$SNAP_DATA/mastodon/app/validators/status_length_validator.rb"

mastodon_config_exists() {
if [ -f "$SNAP_COMMON/mastodon.conf" ]; then
return 0
else
return 1
fi
}

mastodon_set_var() {
if $(mastodon_config_exists); then
sed -i "s|^$1=.*\$|$1=$2|" "$SNAP_COMMON/mastodon.conf"
else
echo_error "$SNAP_COMMON/mastodon.conf does not exist."
exit 1
fi
}

mastodon_get_var() {
if $(mastodon_config_exists); then
sed -rn "s/$1=(.*)/\1/p" "$SNAP_COMMON/mastodon.conf"
fi
}

if mastodon_config_exists; then
export SECRET_KEY_BASE="$(mastodon_get_var SECRET_KEY_BASE)"
export OTP_SECRET="$(mastodon_get_var OTP_SECRET)"
fi

mastodon_rake() {
cd "$SNAP_DATA/mastodon" && bundle exec rake -f "$BUNDLE_RAKEFILE" "$@"
Expand All @@ -43,6 +77,11 @@ mastodon_node() {
cd "$SNAP_DATA/mastodon" && node "$@"
}

mastodon_yarn() {
mkdir -p "$YARN_CACHE_FOLDER"
cd "$SNAP_DATA/mastodon" && yarn "$@"
}

mastodon_sidekiq() {
cd "$SNAP_DATA/mastodon" && bundle exec sidekiq "$@"
}
Expand All @@ -51,14 +90,6 @@ mastodon_tootctl() {
cd "$SNAP_DATA/mastodon" && bundle exec tootctl "$@"
}

mastodon_config_exists() {
if [ -f "$SNAP_COMMON/mastodon.conf" ]; then
return 0
else
return 1
fi
}

check_setup() {
if ! $(mastodon_config_exists); then
echo "Mastodon is not yet set up"
Expand All @@ -72,21 +103,6 @@ setup_mastodon_database() {
psql.wrapper -c "alter user $MASTODON_DBUSER createdb;"
}

mastodon_set_var() {
if $(mastodon_config_exists); then
sed -i "s|^$1=.*\$|$1=$2|" "$SNAP_COMMON/mastodon.conf"
else
echo_error "$SNAP_COMMON/mastodon.conf does not exist."
exit 1
fi
}

mastodon_get_var() {
if $(mastodon_config_exists); then
sed -rn "s/$1=(.*)/\1/p" "$SNAP_COMMON/mastodon.conf"
fi
}

generate_mastodon_secrets() {
eval $(mastodon_rake mastodon:webpush:generate_vapid_key)
export SECRET_KEY_BASE="$(mastodon_rake secret)"
Expand Down Expand Up @@ -139,3 +155,21 @@ mastodon_announcement() {
psql.wrapper -d $MASTODON_DBNAME -c "insert into announcements (text, created_at, updated_at, published) values ('$1', now(), now(), '${2:-f}');"
fi
}

precompile_assets() {
mkdir -p "$YARN_CACHE_FOLDER"
mastodon_rails assets:precompile > /dev/null
}

get_status_max_chars_file() {
sed -rn "s/MAX_CHARS = ([0-9]+)/\1/p" "$status_length_validator"
}

update_status_max_chars() {
if [ "$STATUS_MAX_CHARS" -gt "0" ]; then
sed -ri "s/length\(fulltext\) > [0-9]+/length(fulltext) > $STATUS_MAX_CHARS/" "$compose_form"
sed -ri "s/CharacterCounter max=\{[0-9]+\}/CharacterCounter max={$STATUS_MAX_CHARS}/" "$compose_form"
sed -ri "s/MAX_CHARS = [0-9]+/MAX_CHARS = $STATUS_MAX_CHARS/" "$status_length_validator"
fi
}

1 change: 1 addition & 0 deletions src/snap/snap.env
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export DEFAULT_BACKUP_DIR="$SNAP_COMMON/backups"
export SETTINGS=$(cat <<-END
domain,
email,
status.length,500
ports.http,80
ports.https,443
acme.server,letsencrypt
Expand Down

0 comments on commit 02d29c4

Please sign in to comment.