diff --git a/scripts/nextcloud-mail-sync.sh b/scripts/nextcloud-mail-sync.sh index a3ca3b6..6c883fc 100755 --- a/scripts/nextcloud-mail-sync.sh +++ b/scripts/nextcloud-mail-sync.sh @@ -7,11 +7,48 @@ KC_ADMIN_USER="${KC_ADMIN_USER:?}" KC_ADMIN_PASS="${KC_ADMIN_PASS:?}" MAILU_DOMAIN="${MAILU_DOMAIN:?}" ONLY_USERNAME="${ONLY_USERNAME:-}" +POSTGRES_HOST="${POSTGRES_HOST:-}" +POSTGRES_DB="${POSTGRES_DB:-}" +POSTGRES_USER="${POSTGRES_USER:-}" +POSTGRES_PASSWORD="${POSTGRES_PASSWORD:-}" if ! command -v jq >/dev/null 2>&1; then apt-get update && apt-get install -y jq curl >/dev/null fi +ensure_psql() { + if command -v psql >/dev/null 2>&1; then + return 0 + fi + apt-get update && apt-get install -y postgresql-client >/dev/null +} + +set_editor_mode_richtext() { + local ids=("$@") + + if [[ ${#ids[@]} -eq 0 ]]; then + return 0 + fi + + if [[ -z "${POSTGRES_HOST}" || -z "${POSTGRES_DB}" || -z "${POSTGRES_USER}" || -z "${POSTGRES_PASSWORD}" ]]; then + echo "WARN: missing postgres env; cannot update mail editor_mode" >&2 + return 0 + fi + + ensure_psql + + local ids_csv + ids_csv=$(IFS=,; echo "${ids[*]}") + + PGPASSWORD="${POSTGRES_PASSWORD}" psql \ + -h "${POSTGRES_HOST}" \ + -U "${POSTGRES_USER}" \ + -d "${POSTGRES_DB}" \ + -v ON_ERROR_STOP=1 \ + -c "UPDATE oc_mail_accounts SET editor_mode='richtext' WHERE id IN (${ids_csv}) AND editor_mode <> 'richtext';" \ + >/dev/null +} + list_mail_accounts() { local user_id="${1}" local export_out @@ -184,8 +221,10 @@ while read -r user; do mailu_account_count="0" fi primary_email_after="" + editor_mode_ids=() if [[ -n "${mailu_accounts_after}" ]]; then while IFS=$'\t' read -r _account_id account_email; do + editor_mode_ids+=("${_account_id}") if [[ "${account_email,,}" == "${desired_email,,}" ]]; then primary_email_after="${account_email}" break @@ -195,6 +234,7 @@ while read -r user; do fi done <<<"${mailu_accounts_after}" fi + set_editor_mode_richtext "${editor_mode_ids[@]}" else mailu_account_count="0" primary_email_after="" diff --git a/services/nextcloud-mail-sync/cronjob.yaml b/services/nextcloud-mail-sync/cronjob.yaml index 55a593d..55ff608 100644 --- a/services/nextcloud-mail-sync/cronjob.yaml +++ b/services/nextcloud-mail-sync/cronjob.yaml @@ -41,6 +41,23 @@ spec: key: password - name: MAILU_DOMAIN value: bstein.dev + - name: POSTGRES_HOST + value: postgres-service.postgres.svc.cluster.local + - name: POSTGRES_DB + valueFrom: + secretKeyRef: + name: nextcloud-db + key: database + - name: POSTGRES_USER + valueFrom: + secretKeyRef: + name: nextcloud-db + key: db-username + - name: POSTGRES_PASSWORD + valueFrom: + secretKeyRef: + name: nextcloud-db + key: db-password resources: requests: cpu: 100m