Merge branch 'staging' into feat/implement-admin-domain-blocks-api

This commit is contained in:
daniel 2024-07-30 22:29:28 -06:00 committed by GitHub
commit 1dbcdee289
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
435 changed files with 46242 additions and 11397 deletions

View file

@ -7,7 +7,7 @@ jobs:
build: build:
docker: docker:
# Specify the version you desire here # Specify the version you desire here
- image: cimg/php:8.2.5 - image: cimg/php:8.3.8
# Specify service dependencies here if necessary # Specify service dependencies here if necessary
# CircleCI maintains a library of pre-built images # CircleCI maintains a library of pre-built images
@ -21,7 +21,12 @@ jobs:
steps: steps:
- checkout - checkout
- run: sudo apt update && sudo apt install zlib1g-dev libsqlite3-dev - run:
name: "Create Environment file and generate app key"
command: |
mv .env.testing .env
- run: sudo apt install zlib1g-dev libsqlite3-dev
# Download and cache dependencies # Download and cache dependencies
@ -36,18 +41,17 @@ jobs:
- run: composer install -n --prefer-dist - run: composer install -n --prefer-dist
- save_cache: - save_cache:
key: composer-v2-{{ checksum "composer.lock" }} key: v2-dependencies-{{ checksum "composer.json" }}
paths: paths:
- vendor - vendor
- run: cp .env.testing .env
- run: php artisan config:cache - run: php artisan config:cache
- run: php artisan route:clear - run: php artisan route:clear
- run: php artisan storage:link - run: php artisan storage:link
- run: php artisan key:generate - run: php artisan key:generate
# run tests with phpunit or codecept # run tests with phpunit or codecept
- run: ./vendor/bin/phpunit - run: php artisan test
- store_test_results: - store_test_results:
path: tests/_output path: tests/_output
- store_artifacts: - store_artifacts:

View file

@ -60,6 +60,15 @@ ADMIN_DOMAIN="${APP_DOMAIN}"
# @dottie/validate required,boolean # @dottie/validate required,boolean
#APP_DEBUG="false" #APP_DEBUG="false"
# Disable config cache
#
# If disabled, settings must be managed by .env variables.
#
# @default "false"
# @see https://docs.pixelfed.org/technical-documentation/config/#config_cache
# @dottie/validate required,boolean
ENABLE_CONFIG_CACHE="true"
# Enable/disable new local account registrations. # Enable/disable new local account registrations.
# #
# @default "true" # @default "true"
@ -1025,7 +1034,7 @@ DOCKER_APP_HOST_CACHE_PATH="${DOCKER_ALL_HOST_DATA_ROOT_PATH:?error}/pixelfed/ca
# Automatically run "One-time setup tasks" commands. # Automatically run "One-time setup tasks" commands.
# #
# If you are migrating to this docker-compose setup or have manually run the "One time seutp" # If you are migrating to this docker-compose setup or have manually run the "One time setup"
# tasks (https://docs.pixelfed.org/running-pixelfed/installation/#setting-up-services) # tasks (https://docs.pixelfed.org/running-pixelfed/installation/#setting-up-services)
# you can set this to "0" to prevent them from running. # you can set this to "0" to prevent them from running.
# #
@ -1122,6 +1131,13 @@ DOCKER_APP_HOST_CACHE_PATH="${DOCKER_ALL_HOST_DATA_ROOT_PATH:?error}/pixelfed/ca
# @dottie/validate required,oneof=0 1 2 # @dottie/validate required,oneof=0 1 2
#DOCKER_APP_PHP_OPCACHE_REVALIDATE_FREQ="2" #DOCKER_APP_PHP_OPCACHE_REVALIDATE_FREQ="2"
# When doing [docker compose build], should the frontend be built in the Dockerfile?
# If set to "0" the included pre-compiled frontend will be used.
#
# @default "0"
# @dottie/validate required,oneof=0 1
#DOCKER_APP_BUILD_FRONTEND="0"
################################################################################ ################################################################################
# docker redis # docker redis
################################################################################ ################################################################################
@ -1202,6 +1218,14 @@ DOCKER_DB_HOST_PORT="${DB_PORT:?error}"
# @dottie/validate required,number # @dottie/validate required,number
DOCKER_DB_CONTAINER_PORT="${DB_PORT:?error}" DOCKER_DB_CONTAINER_PORT="${DB_PORT:?error}"
# root password for the database. By default uses DB_PASSWORD
# but can be changed in situations where you are migrating
# to the included docker-compose and have a different password
# set already
#
# @dottie/validate required
DOCKER_DB_ROOT_PASSWORD="${DB_PASSWORD:?error}"
# How often Docker health check should run for [db] service # How often Docker health check should run for [db] service
# @dottie/validate required # @dottie/validate required
DOCKER_DB_HEALTHCHECK_INTERVAL="${DOCKER_ALL_DEFAULT_HEALTHCHECK_INTERVAL:?error}" DOCKER_DB_HEALTHCHECK_INTERVAL="${DOCKER_ALL_DEFAULT_HEALTHCHECK_INTERVAL:?error}"

79
.env.example Normal file
View file

@ -0,0 +1,79 @@
APP_NAME="Pixelfed"
APP_ENV="production"
APP_KEY=
APP_DEBUG="false"
# Instance Configuration
OPEN_REGISTRATION="false"
ENFORCE_EMAIL_VERIFICATION="false"
PF_MAX_USERS="1000"
OAUTH_ENABLED="true"
ENABLE_CONFIG_CACHE=true
# Media Configuration
PF_OPTIMIZE_IMAGES="true"
IMAGE_QUALITY="80"
MAX_PHOTO_SIZE="15000"
MAX_CAPTION_LENGTH="500"
MAX_ALBUM_LENGTH="4"
# Instance URL Configuration
APP_URL="http://localhost"
APP_DOMAIN="localhost"
ADMIN_DOMAIN="localhost"
SESSION_DOMAIN="localhost"
TRUST_PROXIES="*"
# Database Configuration
DB_CONNECTION="mysql"
DB_HOST="127.0.0.1"
DB_PORT="3306"
DB_DATABASE="pixelfed"
DB_USERNAME="pixelfed"
DB_PASSWORD="pixelfed"
# Redis Configuration
REDIS_CLIENT="predis"
REDIS_SCHEME="tcp"
REDIS_HOST="127.0.0.1"
REDIS_PASSWORD="null"
REDIS_PORT="6379"
# Laravel Configuration
SESSION_DRIVER="database"
CACHE_DRIVER="redis"
QUEUE_DRIVER="redis"
BROADCAST_DRIVER="log"
LOG_CHANNEL="stack"
HORIZON_PREFIX="horizon-"
# ActivityPub Configuration
ACTIVITY_PUB="false"
AP_REMOTE_FOLLOW="false"
AP_INBOX="false"
AP_OUTBOX="false"
AP_SHAREDINBOX="false"
# Experimental Configuration
EXP_EMC="true"
## Mail Configuration (Post-Installer)
MAIL_DRIVER=log
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS="pixelfed@example.com"
MAIL_FROM_NAME="Pixelfed"
## S3 Configuration (Post-Installer)
PF_ENABLE_CLOUD=false
FILESYSTEM_CLOUD=s3
#AWS_ACCESS_KEY_ID=
#AWS_SECRET_ACCESS_KEY=
#AWS_DEFAULT_REGION=
#AWS_BUCKET=<BucketName>
#AWS_URL=
#AWS_ENDPOINT=
#AWS_USE_PATH_STYLE_ENDPOINT=false

View file

@ -1,5 +1,6 @@
ignored: ignored:
- DL3002 # warning: Last USER should not be root - DL3002 # warning: Last USER should not be root
- DL3008 # warning: Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>` - DL3008 # warning: Pin versions in apt get install. Instead of `apt-get install <package>` use `apt-get install <package>=<version>`
- DL3029 # warning: Do not use --platform flag with FROM
- SC2046 # warning: Quote this to prevent word splitting. - SC2046 # warning: Quote this to prevent word splitting.
- SC2086 # info: Double quote to prevent globbing and word splitting. - SC2086 # info: Double quote to prevent globbing and word splitting.

View file

@ -1,6 +1,66 @@
# Release Notes # Release Notes
## [Unreleased](https://github.com/pixelfed/pixelfed/compare/v0.11.13...dev) ## [Unreleased](https://github.com/pixelfed/pixelfed/compare/v0.12.3...dev)
### Updates
- Update ApiV1Controller, add support for notification filter types ([f61159a1](https://github.com/pixelfed/pixelfed/commit/f61159a1))
- Update ApiV1Dot1Controller, fix mutual api ([a8bb97b2](https://github.com/pixelfed/pixelfed/commit/a8bb97b2))
- Update ApiV1Controller, fix /api/v1/favourits pagination ([72f68160](https://github.com/pixelfed/pixelfed/commit/72f68160))
- Update RegisterController, update username constraints, require atleast one alpha char ([dd6e3cc2](https://github.com/pixelfed/pixelfed/commit/dd6e3cc2))
- Update AdminUser, fix entity casting ([cb5620d4](https://github.com/pixelfed/pixelfed/commit/cb5620d4))
- ([](https://github.com/pixelfed/pixelfed/commit/))
- ([](https://github.com/pixelfed/pixelfed/commit/))
## [v0.12.3 (2024-07-01)](https://github.com/pixelfed/pixelfed/compare/v0.12.2...v0.12.3)
### Updates
- Fix migrations bug ([4d1180b1](https://github.com/pixelfed/pixelfed/commit/4d1180b1))
## [v0.12.2 (2024-07-01)](https://github.com/pixelfed/pixelfed/compare/v0.12.1...v0.12.2)
### Framework
- Updated to Laravel 11 (requires php 8.2+)
### Added
- New api/v1/instance/peers API endpoint, disabled by default ([4aad1c22](https://github.com/pixelfed/pixelfed/commit/4aad1c22))
- Added disable_embeds setting, and fix cache invalidation in other settings ([c5e7e917](https://github.com/pixelfed/pixelfed/commit/c5e7e917))
### Updates
- Update DirectMessageController, add 72 hour delay for new accounts before they can send a DM ([61d105fd](https://github.com/pixelfed/pixelfed/commit/61d105fd))
- Update AdminCuratedRegisterController, increase message length from 1000 to 3000 ([9a5e3471](https://github.com/pixelfed/pixelfed/commit/9a5e3471))
- Update ApiV1Controller, add pe (pixelfed entity) support to /api/v1/statuses/{id}/context endpoint ([d645d6ca](https://github.com/pixelfed/pixelfed/commit/d645d6ca))
- Update Admin Curated Onboarding, add select-all/mass action operations ([b22cac94](https://github.com/pixelfed/pixelfed/commit/b22cac94))
- Update AdminCuratedRegisterController, fix existing account approval ([cbb96cfd](https://github.com/pixelfed/pixelfed/commit/cbb96cfd))
- Update ActivityPubFetchService, fix Friendica bug ([e4edc6f1](https://github.com/pixelfed/pixelfed/commit/e4edc6f1))
- Update ProfileController, fix atom feed cache ttl. Fixes #5093 ([921e2965](https://github.com/pixelfed/pixelfed/commit/921e2965))
- Update CollectionsController, add new self route ([bc2495c6](https://github.com/pixelfed/pixelfed/commit/bc2495c6))
- Update FederationController, add webfinger support for actor uri. Fixes #5068 ([24194f7d](https://github.com/pixelfed/pixelfed/commit/24194f7d))
- Update FetchNodeinfoPipeline, set last_fetched_at timestamp ([a7fce91e](https://github.com/pixelfed/pixelfed/commit/a7fce91e))
- Update task scheduler, add weekly instance scan to check nodeinfo for known instances ([dc6b9f46](https://github.com/pixelfed/pixelfed/commit/dc6b9f46))
- Update AP fetch service and domain service ([42915ff9](https://github.com/pixelfed/pixelfed/commit/42915ff9))
- Update ApiV1Controller, add settings to verify_credentials endpoint ([3f4e0b94](https://github.com/pixelfed/pixelfed/commit/3f4e0b94))
- Update ApiV1Controller, fix update_credentials boolean handling ([19c62aaa](https://github.com/pixelfed/pixelfed/commit/19c62aaa))
- Update ApiV1Controller, fix cache invalidation bug in update_credentials ([d56a4108](https://github.com/pixelfed/pixelfed/commit/d56a4108))
- Update ApiV1Controller, fix self relationship response ([28bc7aa4](https://github.com/pixelfed/pixelfed/commit/28bc7aa4))
- Update ApiController, add pe support to like/unlike endpoints ([679ef677](https://github.com/pixelfed/pixelfed/commit/679ef677))
- Update ApiV1Dot1Controller, fix username to id endpoint ([4d6cea9a](https://github.com/pixelfed/pixelfed/commit/4d6cea9a))
- Update StatusController, cache AP object ([a75b89b2](https://github.com/pixelfed/pixelfed/commit/a75b89b2))
- Update status embed, add support for album carousels ([f4898db9](https://github.com/pixelfed/pixelfed/commit/f4898db9))
- Update profile embeds, add support for albums ([4fd156c4](https://github.com/pixelfed/pixelfed/commit/4fd156c4))
- Update DirectMessageController, add timestamps to threads ([b24d2554](https://github.com/pixelfed/pixelfed/commit/b24d2554))
- Update DirectMessageController, add carousel entity to threads ([96f24f33](https://github.com/pixelfed/pixelfed/commit/96f24f33))
- Update and refactor total local post count logic, cache value and schedule updates twice daily to eliminate the perf issue on larger instances ([4f2b8ed2](https://github.com/pixelfed/pixelfed/commit/4f2b8ed2))
- Update Media model, fix broken thumbnail/gray thumbnail bug ([e33643c2](https://github.com/pixelfed/pixelfed/commit/e33643c2))
- Update StatusController, fix unlisted post guest/ap access bug ([83098428](https://github.com/pixelfed/pixelfed/commit/83098428))
- Update discover, add network trending using Beagle API ([2cae8b48](https://github.com/pixelfed/pixelfed/commit/2cae8b48))
## [v0.12.1 (2024-05-07)](https://github.com/pixelfed/pixelfed/compare/v0.12.0...v0.12.1)
### Updates
- Update ApiV1Dot1Controller, fix in app registration bug that prevents proper auth flow due to missing oauth scopes ([cbf996c9](https://github.com/pixelfed/pixelfed/commit/cbf996c9))
- Update ConfigCacheService, fix database race condition and fallback to file config and enable by default ([60a62b59](https://github.com/pixelfed/pixelfed/commit/60a62b59))
## [v0.12.0 (2024-04-29)](https://github.com/pixelfed/pixelfed/compare/v0.11.13...v0.12.0)
### Updates ### Updates
@ -19,7 +79,58 @@
- Update SiteController, add curatedOnboarding method that gracefully falls back to open registration when applicable ([95199843](https://github.com/pixelfed/pixelfed/commit/95199843)) - Update SiteController, add curatedOnboarding method that gracefully falls back to open registration when applicable ([95199843](https://github.com/pixelfed/pixelfed/commit/95199843))
- Update AP transformers, add DeleteActor activity ([bcce1df6](https://github.com/pixelfed/pixelfed/commit/bcce1df6)) - Update AP transformers, add DeleteActor activity ([bcce1df6](https://github.com/pixelfed/pixelfed/commit/bcce1df6))
- Update commands, add user account delete cli command to federate account deletion ([4aa0e25f](https://github.com/pixelfed/pixelfed/commit/4aa0e25f)) - Update commands, add user account delete cli command to federate account deletion ([4aa0e25f](https://github.com/pixelfed/pixelfed/commit/4aa0e25f))
- ([](https://github.com/pixelfed/pixelfed/commit/)) - Update web-api popular accounts route to its own method to remove the breaking oauth scope bug ([a4bc5ce3](https://github.com/pixelfed/pixelfed/commit/a4bc5ce3))
- Update config cache ([5e4d4eff](https://github.com/pixelfed/pixelfed/commit/5e4d4eff))
- Update Config, use config_cache ([7785a2da](https://github.com/pixelfed/pixelfed/commit/7785a2da))
- Update ApiV1Dot1Controller, use config_cache for in-app registration ([b0cb4456](https://github.com/pixelfed/pixelfed/commit/b0cb4456))
- Update captcha, use config_cache helper ([8a89e3c9](https://github.com/pixelfed/pixelfed/commit/8a89e3c9))
- Update custom emoji, add config_cache support ([481314cd](https://github.com/pixelfed/pixelfed/commit/481314cd))
- Update ProfileController, fix permalink redirect bug ([75081e60](https://github.com/pixelfed/pixelfed/commit/75081e60))
- Update admin css, use font-display:swap for nucleo icons ([8a0c456e](https://github.com/pixelfed/pixelfed/commit/8a0c456e))
- Update PixelfedDirectoryController, fix boolean cast bug ([f08aab22](https://github.com/pixelfed/pixelfed/commit/f08aab22))
- Update PixelfedDirectoryController, use cached stats ([f2f2a809](https://github.com/pixelfed/pixelfed/commit/f2f2a809))
- Update AdminDirectoryController, fix type casting ([ad506e90](https://github.com/pixelfed/pixelfed/commit/ad506e90))
- Update image pipeline, use config_cache ([a72188a7](https://github.com/pixelfed/pixelfed/commit/a72188a7))
- Update cloud storage, use config_cache ([665581d8](https://github.com/pixelfed/pixelfed/commit/665581d8))
- Update pixelfed.max_album_length, use config_cache ([fecbe189](https://github.com/pixelfed/pixelfed/commit/fecbe189))
- Update media_types, use config_cache ([d670de17](https://github.com/pixelfed/pixelfed/commit/d670de17))
- Update landing settings, use config_cache ([40478f25](https://github.com/pixelfed/pixelfed/commit/40478f25))
- Update activitypub setting, use config_cache ([5071aaf4](https://github.com/pixelfed/pixelfed/commit/5071aaf4))
- Update oauth setting, use config_cache ([ce228f7f](https://github.com/pixelfed/pixelfed/commit/ce228f7f))
- Update stories config, use config_cache ([d1adb109](https://github.com/pixelfed/pixelfed/commit/d1adb109))
- Update ig import, use config_cache ([da0e0ffa](https://github.com/pixelfed/pixelfed/commit/da0e0ffa))
- Update autospam config, use config_cache ([a76cb5f4](https://github.com/pixelfed/pixelfed/commit/a76cb5f4))
- Update app.name config, use config_cache ([911446c0](https://github.com/pixelfed/pixelfed/commit/911446c0))
- Update UserObserver, fix type casting ([949e9979](https://github.com/pixelfed/pixelfed/commit/949e9979))
- Update user_filters, use config_cache ([6ce513f8](https://github.com/pixelfed/pixelfed/commit/6ce513f8))
- Update filesystems config, add to config_cache ([087b2791](https://github.com/pixelfed/pixelfed/commit/087b2791))
- Update web-admin routes, add setting api routes ([828a456f](https://github.com/pixelfed/pixelfed/commit/828a456f))
- Update hashtag component ([cee979ed](https://github.com/pixelfed/pixelfed/commit/cee979ed))
- Update AdminReadMore component, add .prevent to click action ([704e7b12](https://github.com/pixelfed/pixelfed/commit/704e7b12))
- Update admin dashboard, add admin settings partials ([eb487123](https://github.com/pixelfed/pixelfed/commit/eb487123))
- Update admin settings, refactor to vue component ([674e560f](https://github.com/pixelfed/pixelfed/commit/674e560f))
- Update ConfigCacheService, encrypt keys at rest ([3628b462](https://github.com/pixelfed/pixelfed/commit/3628b462))
- Update RemoteFollowImportRecent, use MediaPathService ([5162c070](https://github.com/pixelfed/pixelfed/commit/5162c070))
- Update AdminSettingsController, add user filter max limit settings ([ac1f0748](https://github.com/pixelfed/pixelfed/commit/ac1f0748))
- Update AdminSettingsController, add AdminSettingsService ([dcc5f416](https://github.com/pixelfed/pixelfed/commit/dcc5f416))
- Update AdminSettings component, fix user settings ([aba1e13d](https://github.com/pixelfed/pixelfed/commit/aba1e13d))
- Update AdminInstances component ([ec2fdd61](https://github.com/pixelfed/pixelfed/commit/ec2fdd61))
- Update AdminSettings, add max_account_size support ([2dcbc1d5](https://github.com/pixelfed/pixelfed/commit/2dcbc1d5))
- Update AdminSettings, use better validation for user integer settings ([d946afcc](https://github.com/pixelfed/pixelfed/commit/d946afcc))
- Update spa sass, fix timestamp dark mode bug ([4147f7c5](https://github.com/pixelfed/pixelfed/commit/4147f7c5))
- Update relationships view, fix unfollow hashtag bug. Fixes #5008 ([8c693640](https://github.com/pixelfed/pixelfed/commit/8c693640))
- Update PrivacySettings controller, refresh RelationshipService when unmute/unblocking ([b7322b68](https://github.com/pixelfed/pixelfed/commit/b7322b68))
- Update ApiV1Controller, improve refresh relations logic when (un)muting or (un)blocking ([b8e96a5f](https://github.com/pixelfed/pixelfed/commit/b8e96a5f))
- Update context menu, add mute/block/unfollow actions and update relationship store accordingly ([81d1e0fd](https://github.com/pixelfed/pixelfed/commit/81d1e0fd))
- Update docker env, fix config_cache. Fixes #5033 ([858fcbf6](https://github.com/pixelfed/pixelfed/commit/858fcbf6))
- Update UnfollowPipeline, fix follower count cache bug ([6bdf73de](https://github.com/pixelfed/pixelfed/commit/6bdf73de))
- Update VideoPresenter component, add webkit-playsinline attribute to video element to prevent the full screen video player ([ad032916](https://github.com/pixelfed/pixelfed/commit/ad032916))
- Update VideoPlayer component, add playsinline attribute to video element ([8af23607](https://github.com/pixelfed/pixelfed/commit/8af23607))
- Update StatusController, refactor status embeds ([9a7acc12](https://github.com/pixelfed/pixelfed/commit/9a7acc12))
- Update ProfileController, refactor profile embeds ([8b8b1ffc](https://github.com/pixelfed/pixelfed/commit/8b8b1ffc))
- Update profile embed view, fix height bug ([65166570](https://github.com/pixelfed/pixelfed/commit/65166570))
- Update CustomEmojiService, only return local emoji ([7f8bba44](https://github.com/pixelfed/pixelfed/commit/7f8bba44))
- Update Like model, increase max likes per day from 500 to 1500 ([4223119f](https://github.com/pixelfed/pixelfed/commit/4223119f))
## [v0.11.13 (2024-03-05)](https://github.com/pixelfed/pixelfed/compare/v0.11.12...v0.11.13) ## [v0.11.13 (2024-03-05)](https://github.com/pixelfed/pixelfed/compare/v0.11.12...v0.11.13)

View file

@ -132,6 +132,10 @@ ENV DEBIAN_FRONTEND="noninteractive"
# Ensure we run all scripts through 'bash' rather than 'sh' # Ensure we run all scripts through 'bash' rather than 'sh'
SHELL ["/bin/bash", "-c"] SHELL ["/bin/bash", "-c"]
# Set www-data to be RUNTIME_UID/RUNTIME_GID
RUN groupmod --gid ${RUNTIME_GID} www-data \
&& usermod --uid ${RUNTIME_UID} --gid ${RUNTIME_GID} www-data
RUN set -ex \ RUN set -ex \
&& mkdir -pv /var/www/ \ && mkdir -pv /var/www/ \
&& chown -R ${RUNTIME_UID}:${RUNTIME_GID} /var/www && chown -R ${RUNTIME_UID}:${RUNTIME_GID} /var/www
@ -176,6 +180,55 @@ RUN --mount=type=cache,id=pixelfed-pear-${PHP_VERSION}-${PHP_DEBIAN_RELEASE}-${T
PHP_PECL_EXTENSIONS_EXTRA=${PHP_PECL_EXTENSIONS_EXTRA} \ PHP_PECL_EXTENSIONS_EXTRA=${PHP_PECL_EXTENSIONS_EXTRA} \
/docker/install/php-extensions.sh /docker/install/php-extensions.sh
#######################################################
# Node: Build frontend
#######################################################
# NOTE: Since the nodejs build is CPU architecture agnostic,
# we only want to build once and cache it for other architectures.
# We force the (CPU) [--platform] here to be architecture
# of the "builder"/"server" and not the *target* CPU architecture
# (e.g.) building the ARM version of Pixelfed on AMD64.
FROM --platform=${BUILDARCH} node:lts AS frontend-build
ARG BUILDARCH
ARG BUILD_FRONTEND=0
ARG RUNTIME_UID
ARG NODE_ENV=production
ENV NODE_ENV=$NODE_ENV
WORKDIR /var/www/
SHELL [ "/usr/bin/bash", "-c" ]
# Install NPM dependencies
RUN --mount=type=cache,id=pixelfed-node-${BUILDARCH},sharing=locked,target=/tmp/cache \
--mount=type=bind,source=package.json,target=/var/www/package.json \
--mount=type=bind,source=package-lock.json,target=/var/www/package-lock.json \
<<EOF
if [[ $BUILD_FRONTEND -eq 1 ]];
then
npm install --cache /tmp/cache --no-save --dev
else
echo "Skipping [npm install] as --build-arg [BUILD_FRONTEND] is not set to '1'"
fi
EOF
# Copy the frontend source into the image before building
COPY --chown=${RUNTIME_UID}:${RUNTIME_GID} . /var/www
# Build the frontend with "mix" (See package.json)
RUN \
<<EOF
if [[ $BUILD_FRONTEND -eq 1 ]];
then
npm run production
else
echo "Skipping [npm run production] as --build-arg [BUILD_FRONTEND] is not set to '1'"
fi
EOF
####################################################### #######################################################
# PHP: composer and source code # PHP: composer and source code
####################################################### #######################################################
@ -231,13 +284,14 @@ COPY --link --from=dottie-image /dottie /usr/local/bin/dottie
COPY --link --from=gomplate-image /usr/local/bin/gomplate /usr/local/bin/gomplate COPY --link --from=gomplate-image /usr/local/bin/gomplate /usr/local/bin/gomplate
COPY --link --from=composer-image /usr/bin/composer /usr/bin/composer COPY --link --from=composer-image /usr/bin/composer /usr/bin/composer
COPY --link --from=composer-and-src --chown=${RUNTIME_UID}:${RUNTIME_GID} /var/www /var/www COPY --link --from=composer-and-src --chown=${RUNTIME_UID}:${RUNTIME_GID} /var/www /var/www
COPY --link --from=frontend-build --chown=${RUNTIME_UID}:${RUNTIME_GID} /var/www/public /var/www/public
#! Changing user to runtime user #! Changing user to runtime user
USER ${RUNTIME_UID}:${RUNTIME_GID} USER ${RUNTIME_UID}:${RUNTIME_GID}
# Generate optimized autoloader now that we have all files around # Generate optimized autoloader now that we have all files around
RUN set -ex \ RUN set -ex \
&& composer dump-autoload --optimize && ENABLE_CONFIG_CACHE=false composer dump-autoload --optimize
USER root USER root

View file

@ -82,7 +82,7 @@ class AvatarStorage extends Command
$this->line(' '); $this->line(' ');
if(config_cache('pixelfed.cloud_storage')) { if((bool) config_cache('pixelfed.cloud_storage')) {
$this->info('✅ - Cloud storage configured'); $this->info('✅ - Cloud storage configured');
$this->line(' '); $this->line(' ');
} }
@ -92,7 +92,7 @@ class AvatarStorage extends Command
$this->line(' '); $this->line(' ');
} }
if(config_cache('pixelfed.cloud_storage') && config('instance.avatar.local_to_cloud')) { if((bool) config_cache('pixelfed.cloud_storage') && config('instance.avatar.local_to_cloud')) {
$disk = Storage::disk(config_cache('filesystems.cloud')); $disk = Storage::disk(config_cache('filesystems.cloud'));
$exists = $disk->exists('cache/avatars/default.jpg'); $exists = $disk->exists('cache/avatars/default.jpg');
$state = $exists ? '✅' : '❌'; $state = $exists ? '✅' : '❌';
@ -100,7 +100,7 @@ class AvatarStorage extends Command
$this->info($msg); $this->info($msg);
} }
$options = config_cache('pixelfed.cloud_storage') && config('instance.avatar.local_to_cloud') ? $options = (bool) config_cache('pixelfed.cloud_storage') && config('instance.avatar.local_to_cloud') ?
[ [
'Cancel', 'Cancel',
'Upload default avatar to cloud', 'Upload default avatar to cloud',
@ -164,7 +164,7 @@ class AvatarStorage extends Command
protected function uploadAvatarsToCloud() protected function uploadAvatarsToCloud()
{ {
if(!config_cache('pixelfed.cloud_storage') || !config('instance.avatar.local_to_cloud')) { if(!(bool) config_cache('pixelfed.cloud_storage') || !config('instance.avatar.local_to_cloud')) {
$this->error('Enable cloud storage and avatar cloud storage to perform this action'); $this->error('Enable cloud storage and avatar cloud storage to perform this action');
return; return;
} }
@ -213,7 +213,7 @@ class AvatarStorage extends Command
return; return;
} }
if(config_cache('pixelfed.cloud_storage') == false && config_cache('federation.avatars.store_local') == false) { if((bool) config_cache('pixelfed.cloud_storage') == false && config_cache('federation.avatars.store_local') == false) {
$this->error('You have cloud storage disabled and local avatar storage disabled, we cannot refetch avatars.'); $this->error('You have cloud storage disabled and local avatar storage disabled, we cannot refetch avatars.');
return; return;
} }

View file

@ -44,7 +44,7 @@ class AvatarStorageDeepClean extends Command
$this->line(' '); $this->line(' ');
$storage = [ $storage = [
'cloud' => boolval(config_cache('pixelfed.cloud_storage')), 'cloud' => (bool) config_cache('pixelfed.cloud_storage'),
'local' => boolval(config_cache('federation.avatars.store_local')) 'local' => boolval(config_cache('federation.avatars.store_local'))
]; ];

View file

@ -0,0 +1,52 @@
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use function Laravel\Prompts\info;
use function Laravel\Prompts\confirm;
use App\Services\ConfigCacheService;
class CaptchaToggleCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:captcha-toggle-command';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Execute the console command.
*/
public function handle()
{
$captchaEnabled = (bool) config_cache('captcha.enabled');
info($captchaEnabled ? 'Captcha is enabled' : 'Captcha is not enabled');
if(!$captchaEnabled) {
info('Enable the Captcha from the admin settings dashboard.');
return;
}
$confirmed = confirm(
label: 'Do you want to disable the captcha?',
default: false,
yes: 'Yes',
no: 'No',
hint: 'Select an option to proceed.'
);
if($confirmed) {
ConfigCacheService::put('captcha.enabled', false);
}
}
}

View file

@ -35,12 +35,16 @@ class CloudMediaMigrate extends Command
*/ */
public function handle() public function handle()
{ {
$enabled = config('pixelfed.cloud_storage'); $enabled = (bool) config_cache('pixelfed.cloud_storage');
if(!$enabled) { if(!$enabled) {
$this->error('Cloud storage not enabled. Exiting...'); $this->error('Cloud storage not enabled. Exiting...');
return; return;
} }
if(!$this->confirm('Are you sure you want to proceed?')) {
return;
}
$limit = $this->option('limit'); $limit = $this->option('limit');
$hugeMode = $this->option('huge'); $hugeMode = $this->option('huge');

View file

@ -0,0 +1,51 @@
<?php
namespace App\Console\Commands;
use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
use App\Profile;
use Illuminate\Console\Command;
use function Laravel\Prompts\confirm;
use function Laravel\Prompts\search;
class DeleteRemoteProfile extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:delete-remote-profile';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete remote profile';
/**
* Execute the console command.
*/
public function handle()
{
$id = search(
'Search for the account',
fn (string $value) => strlen($value) > 2
? Profile::whereNotNull('domain')->where('username', 'like', $value.'%')->pluck('username', 'id')->all()
: []
);
$profile = Profile::whereNotNull('domain')->find($id);
if (! $profile) {
$this->error('Could not find profile.');
exit;
}
$confirmed = confirm('Are you sure you want to delete '.$profile->username.'\'s account? This action cannot be reversed.');
DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('adelete');
$this->info('Dispatched delete job, it may take a few minutes...');
exit;
}
}

View file

@ -2,11 +2,11 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Illuminate\Console\Command;
use App\Media; use App\Media;
use Illuminate\Support\Facades\Http;
use App\Services\MediaService; use App\Services\MediaService;
use App\Services\StatusService; use App\Services\StatusService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
class FetchMissingMediaMimeType extends Command class FetchMissingMediaMimeType extends Command
{ {
@ -29,20 +29,20 @@ class FetchMissingMediaMimeType extends Command
*/ */
public function handle() public function handle()
{ {
foreach(Media::whereNotNull(['remote_url', 'status_id'])->whereNull('mime')->lazyByIdDesc(50, 'id') as $media) { foreach (Media::whereNotNull(['remote_url', 'status_id'])->whereNull('mime')->lazyByIdDesc(50, 'id') as $media) {
$res = Http::retry(2, 100, throw: false)->head($media->remote_url); $res = Http::retry(2, 100, throw: false)->head($media->remote_url);
if(!$res->successful()) { if (! $res->successful()) {
continue; continue;
} }
if(!in_array($res->header('content-type'), explode(',',config('pixelfed.media_types')))) { if (! in_array($res->header('content-type'), explode(',', config_cache('pixelfed.media_types')))) {
continue; continue;
} }
$media->mime = $res->header('content-type'); $media->mime = $res->header('content-type');
if($res->hasHeader('content-length')) { if ($res->hasHeader('content-length')) {
$media->size = $res->header('content-length'); $media->size = $res->header('content-length');
} }
@ -50,7 +50,7 @@ class FetchMissingMediaMimeType extends Command
MediaService::del($media->status_id); MediaService::del($media->status_id);
StatusService::del($media->status_id); StatusService::del($media->status_id);
$this->info('mid:'.$media->id . ' (' . $res->header('content-type') . ':' . $res->header('content-length') . ' bytes)'); $this->info('mid:'.$media->id.' ('.$res->header('content-type').':'.$res->header('content-length').' bytes)');
} }
} }
} }

View file

@ -37,7 +37,7 @@ class FixMediaDriver extends Command
return Command::SUCCESS; return Command::SUCCESS;
} }
if(config_cache('pixelfed.cloud_storage') == false) { if((bool) config_cache('pixelfed.cloud_storage') == false) {
$this->error('Cloud storage not enabled, exiting...'); $this->error('Cloud storage not enabled, exiting...');
return Command::SUCCESS; return Command::SUCCESS;
} }

View file

@ -0,0 +1,79 @@
<?php
namespace App\Console\Commands;
use App\Services\ConfigCacheService;
use Cache;
use DB;
use Illuminate\Console\Command;
use Storage;
class InstanceUpdateTotalLocalPosts extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:instance-update-total-local-posts';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Update the total number of local statuses/post count';
/**
* Execute the console command.
*/
public function handle()
{
$cached = $this->checkForCache();
if (! $cached) {
$this->initCache();
return;
}
$cache = $this->getCached();
if (! $cache || ! isset($cache['count'])) {
$this->error('Problem fetching cache');
return;
}
$this->updateAndCache();
Cache::forget('api:nodeinfo');
}
protected function checkForCache()
{
return Storage::exists('total_local_posts.json');
}
protected function initCache()
{
$count = DB::table('statuses')->whereNull(['url', 'deleted_at'])->count();
$res = [
'count' => $count,
];
Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
}
protected function getCached()
{
return Storage::json('total_local_posts.json');
}
protected function updateAndCache()
{
$count = DB::table('statuses')->whereNull(['url', 'deleted_at'])->count();
$res = [
'count' => $count,
];
Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
}
}

View file

@ -47,7 +47,7 @@ class MediaCloudUrlRewrite extends Command implements PromptsForMissingInput
protected function preflightCheck() protected function preflightCheck()
{ {
if(config_cache('pixelfed.cloud_storage') != true) { if(!(bool) config_cache('pixelfed.cloud_storage')) {
$this->info('Error: Cloud storage is not enabled!'); $this->info('Error: Cloud storage is not enabled!');
$this->error('Aborting...'); $this->error('Aborting...');
exit; exit;

View file

@ -45,7 +45,7 @@ class MediaS3GarbageCollector extends Command
*/ */
public function handle() public function handle()
{ {
$enabled = in_array(config_cache('pixelfed.cloud_storage'), ['1', true, 'true']); $enabled = (bool) config_cache('pixelfed.cloud_storage');
if(!$enabled) { if(!$enabled) {
$this->error('Cloud storage not enabled. Exiting...'); $this->error('Cloud storage not enabled. Exiting...');
return; return;

View file

@ -0,0 +1,47 @@
<?php
namespace App\Console\Commands;
use App\Instance;
use App\Jobs\InstancePipeline\FetchNodeinfoPipeline;
use Illuminate\Console\Command;
use function Laravel\Prompts\progress;
class WeeklyInstanceScan extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'app:weekly-instance-scan';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Scan instance nodeinfo';
/**
* Execute the console command.
*/
public function handle()
{
if ((bool) config_cache('federation.activitypub.enabled') == false) {
return;
}
$users = progress(
label: 'Updating instance stats...',
steps: Instance::all(),
callback: fn ($instance) => $this->updateInstanceStats($instance),
);
}
protected function updateInstanceStats($instance)
{
FetchNodeinfoPipeline::dispatch($instance)->onQueue('intbg');
}
}

View file

@ -19,7 +19,6 @@ class Kernel extends ConsoleKernel
/** /**
* Define the application's command schedule. * Define the application's command schedule.
* *
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* *
* @return void * @return void
*/ */
@ -32,8 +31,9 @@ class Kernel extends ConsoleKernel
$schedule->command('gc:failedjobs')->dailyAt(3)->onOneServer(); $schedule->command('gc:failedjobs')->dailyAt(3)->onOneServer();
$schedule->command('gc:passwordreset')->dailyAt('09:41')->onOneServer(); $schedule->command('gc:passwordreset')->dailyAt('09:41')->onOneServer();
$schedule->command('gc:sessions')->twiceDaily(13, 23)->onOneServer(); $schedule->command('gc:sessions')->twiceDaily(13, 23)->onOneServer();
$schedule->command('app:weekly-instance-scan')->weeklyOn(2, '4:20')->onOneServer();
if (in_array(config_cache('pixelfed.cloud_storage'), ['1', true, 'true']) && config('media.delete_local_after_cloud')) { if ((bool) config_cache('pixelfed.cloud_storage') && (bool) config_cache('media.delete_local_after_cloud')) {
$schedule->command('media:s3gc')->hourlyAt(15); $schedule->command('media:s3gc')->hourlyAt(15);
} }
@ -51,6 +51,7 @@ class Kernel extends ConsoleKernel
$schedule->command('app:notification-epoch-update')->weeklyOn(1, '2:21')->onOneServer(); $schedule->command('app:notification-epoch-update')->weeklyOn(1, '2:21')->onOneServer();
$schedule->command('app:hashtag-cached-count-update')->hourlyAt(25)->onOneServer(); $schedule->command('app:hashtag-cached-count-update')->hourlyAt(25)->onOneServer();
$schedule->command('app:account-post-count-stat-update')->everySixHours(25)->onOneServer(); $schedule->command('app:account-post-count-stat-update')->everySixHours(25)->onOneServer();
$schedule->command('app:instance-update-total-local-posts')->twiceDailyAt(1, 13, 45)->onOneServer();
} }
/** /**
@ -60,7 +61,7 @@ class Kernel extends ConsoleKernel
*/ */
protected function commands() protected function commands()
{ {
$this->load(__DIR__ . '/Commands'); $this->load(__DIR__.'/Commands');
require base_path('routes/console.php'); require base_path('routes/console.php');
} }

View file

@ -157,7 +157,7 @@ class AccountController extends Controller
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$count = UserFilterService::muteCount($pid); $count = UserFilterService::muteCount($pid);
$maxLimit = intval(config('instance.user_filters.max_user_mutes')); $maxLimit = (int) config_cache('instance.user_filters.max_user_mutes');
abort_if($count >= $maxLimit, 422, self::FILTER_LIMIT_MUTE_TEXT . $maxLimit . ' accounts'); abort_if($count >= $maxLimit, 422, self::FILTER_LIMIT_MUTE_TEXT . $maxLimit . ' accounts');
if($count == 0) { if($count == 0) {
$filterCount = UserFilter::whereUserId($pid)->count(); $filterCount = UserFilter::whereUserId($pid)->count();
@ -260,7 +260,7 @@ class AccountController extends Controller
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$count = UserFilterService::blockCount($pid); $count = UserFilterService::blockCount($pid);
$maxLimit = intval(config('instance.user_filters.max_user_blocks')); $maxLimit = (int) config_cache('instance.user_filters.max_user_blocks');
abort_if($count >= $maxLimit, 422, self::FILTER_LIMIT_BLOCK_TEXT . $maxLimit . ' accounts'); abort_if($count >= $maxLimit, 422, self::FILTER_LIMIT_BLOCK_TEXT . $maxLimit . ' accounts');
if($count == 0) { if($count == 0) {
$filterCount = UserFilter::whereUserId($pid)->whereFilterType('block')->count(); $filterCount = UserFilter::whereUserId($pid)->whereFilterType('block')->count();

View file

@ -2,30 +2,20 @@
namespace App\Http\Controllers\Admin; namespace App\Http\Controllers\Admin;
use DB, Cache; use App\Http\Controllers\PixelfedDirectoryController;
use App\{
DiscoverCategory,
DiscoverCategoryHashtag,
Hashtag,
Media,
Profile,
Status,
StatusHashtag,
User
};
use App\Models\ConfigCache; use App\Models\ConfigCache;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\ConfigCacheService; use App\Services\ConfigCacheService;
use App\Services\StatusService; use App\Services\StatusService;
use Carbon\Carbon; use App\Status;
use App\User;
use Cache;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Validation\Rule; use Illuminate\Support\Facades\Http;
use League\ISO3166\ISO3166;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Http; use Illuminate\Support\Str;
use App\Http\Controllers\PixelfedDirectoryController; use League\ISO3166\ISO3166;
trait AdminDirectoryController trait AdminDirectoryController
{ {
@ -41,34 +31,34 @@ trait AdminDirectoryController
$res['countries'] = collect((new ISO3166)->all())->pluck('name'); $res['countries'] = collect((new ISO3166)->all())->pluck('name');
$res['admins'] = User::whereIsAdmin(true) $res['admins'] = User::whereIsAdmin(true)
->where('2fa_enabled', true) ->where('2fa_enabled', true)
->get()->map(function($user) { ->get()->map(function ($user) {
return [ return [
'uid' => (string) $user->id, 'uid' => (string) $user->id,
'pid' => (string) $user->profile_id, 'pid' => (string) $user->profile_id,
'username' => $user->username, 'username' => $user->username,
'created_at' => $user->created_at 'created_at' => $user->created_at,
]; ];
}); });
$config = ConfigCache::whereK('pixelfed.directory')->first(); $config = ConfigCache::whereK('pixelfed.directory')->first();
if($config) { if ($config) {
$data = $config->v ? json_decode($config->v, true) : []; $data = $config->v ? json_decode($config->v, true) : [];
$res = array_merge($res, $data); $res = array_merge($res, $data);
} }
if(empty($res['summary'])) { if (empty($res['summary'])) {
$summary = ConfigCache::whereK('app.short_description')->pluck('v'); $summary = ConfigCache::whereK('app.short_description')->pluck('v');
$res['summary'] = $summary ? $summary[0] : null; $res['summary'] = $summary ? $summary[0] : null;
} }
if(isset($res['banner_image']) && !empty($res['banner_image'])) { if (isset($res['banner_image']) && ! empty($res['banner_image'])) {
$res['banner_image'] = url(Storage::url($res['banner_image'])); $res['banner_image'] = url(Storage::url($res['banner_image']));
} }
if(isset($res['favourite_posts'])) { if (isset($res['favourite_posts'])) {
$res['favourite_posts'] = collect($res['favourite_posts'])->map(function($id) { $res['favourite_posts'] = collect($res['favourite_posts'])->map(function ($id) {
return StatusService::get($id); return StatusService::get($id);
}) })
->filter(function($post) { ->filter(function ($post) {
return $post && isset($post['account']); return $post && isset($post['account']);
}) })
->values(); ->values();
@ -84,22 +74,22 @@ trait AdminDirectoryController
$res['feature_config'] = [ $res['feature_config'] = [
'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','), 'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','),
'image_quality' => config_cache('pixelfed.image_quality'), 'image_quality' => config_cache('pixelfed.image_quality'),
'optimize_image' => config_cache('pixelfed.optimize_image'), 'optimize_image' => (bool) config_cache('pixelfed.optimize_image'),
'max_photo_size' => config_cache('pixelfed.max_photo_size'), 'max_photo_size' => config_cache('pixelfed.max_photo_size'),
'max_caption_length' => config_cache('pixelfed.max_caption_length'), 'max_caption_length' => config_cache('pixelfed.max_caption_length'),
'max_altext_length' => config_cache('pixelfed.max_altext_length'), 'max_altext_length' => config_cache('pixelfed.max_altext_length'),
'enforce_account_limit' => config_cache('pixelfed.enforce_account_limit'), 'enforce_account_limit' => (bool) config_cache('pixelfed.enforce_account_limit'),
'max_account_size' => config_cache('pixelfed.max_account_size'), 'max_account_size' => config_cache('pixelfed.max_account_size'),
'max_album_length' => config_cache('pixelfed.max_album_length'), 'max_album_length' => config_cache('pixelfed.max_album_length'),
'account_deletion' => config_cache('pixelfed.account_deletion'), 'account_deletion' => (bool) config_cache('pixelfed.account_deletion'),
]; ];
if(config_cache('pixelfed.directory.testimonials')) { if (config_cache('pixelfed.directory.testimonials')) {
$testimonials = collect(json_decode(config_cache('pixelfed.directory.testimonials'),true)) $testimonials = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true))
->map(function($t) { ->map(function ($t) {
return [ return [
'profile' => AccountService::get($t['profile_id']), 'profile' => AccountService::get($t['profile_id']),
'body' => $t['body'] 'body' => $t['body'],
]; ];
}); });
$res['testimonials'] = $testimonials; $res['testimonials'] = $testimonials;
@ -109,7 +99,7 @@ trait AdminDirectoryController
'media_types' => [ 'media_types' => [
'required', 'required',
function ($attribute, $value, $fail) { function ($attribute, $value, $fail) {
if (!in_array('image/jpeg', $value->toArray()) || !in_array('image/png', $value->toArray())) { if (! in_array('image/jpeg', $value->toArray()) || ! in_array('image/png', $value->toArray())) {
$fail('You must enable image/jpeg and image/png support.'); $fail('You must enable image/jpeg and image/png support.');
} }
}, },
@ -120,7 +110,7 @@ trait AdminDirectoryController
'max_account_size' => 'required_if:enforce_account_limit,true|integer|min:1000000', 'max_account_size' => 'required_if:enforce_account_limit,true|integer|min:1000000',
'max_album_length' => 'required|integer|min:4|max:20', 'max_album_length' => 'required|integer|min:4|max:20',
'account_deletion' => 'required|accepted', 'account_deletion' => 'required|accepted',
'max_caption_length' => 'required|integer|min:500|max:10000' 'max_caption_length' => 'required|integer|min:500|max:10000',
]); ]);
$res['requirements_validator'] = $validator->errors(); $res['requirements_validator'] = $validator->errors();
@ -146,11 +136,11 @@ trait AdminDirectoryController
foreach (new \DirectoryIterator($path) as $io) { foreach (new \DirectoryIterator($path) as $io) {
$name = $io->getFilename(); $name = $io->getFilename();
$skip = ['vendor']; $skip = ['vendor'];
if($io->isDot() || in_array($name, $skip)) { if ($io->isDot() || in_array($name, $skip)) {
continue; continue;
} }
if($io->isDir()) { if ($io->isDir()) {
$langs->push(['code' => $name, 'name' => locale_get_display_name($name)]); $langs->push(['code' => $name, 'name' => locale_get_display_name($name)]);
} }
} }
@ -160,24 +150,25 @@ trait AdminDirectoryController
$submissionState = Http::withoutVerifying() $submissionState = Http::withoutVerifying()
->post('https://pixelfed.org/api/v1/directory/check-submission', [ ->post('https://pixelfed.org/api/v1/directory/check-submission', [
'domain' => config('pixelfed.domain.app') 'domain' => config('pixelfed.domain.app'),
]); ]);
$res['submission_state'] = $submissionState->json(); $res['submission_state'] = $submissionState->json();
return $res; return $res;
} }
protected function validVal($res, $val, $count = false, $minLen = false) protected function validVal($res, $val, $count = false, $minLen = false)
{ {
if(!isset($res[$val])) { if (! isset($res[$val])) {
return false; return false;
} }
if($count) { if ($count) {
return count($res[$val]) >= $count; return count($res[$val]) >= $count;
} }
if($minLen) { if ($minLen) {
return strlen($res[$val]) >= $minLen; return strlen($res[$val]) >= $minLen;
} }
@ -194,11 +185,11 @@ trait AdminDirectoryController
'favourite_posts' => 'array|max:12', 'favourite_posts' => 'array|max:12',
'favourite_posts.*' => 'distinct', 'favourite_posts.*' => 'distinct',
'privacy_pledge' => 'sometimes', 'privacy_pledge' => 'sometimes',
'banner_image' => 'sometimes|mimes:jpg,png|dimensions:width=1920,height:1080|max:5000' 'banner_image' => 'sometimes|mimes:jpg,png|dimensions:width=1920,height:1080|max:5000',
]); ]);
$config = ConfigCache::firstOrNew([ $config = ConfigCache::firstOrNew([
'k' => 'pixelfed.directory' 'k' => 'pixelfed.directory',
]); ]);
$res = $config->v ? json_decode($config->v, true) : []; $res = $config->v ? json_decode($config->v, true) : [];
@ -208,24 +199,25 @@ trait AdminDirectoryController
$res['contact_email'] = $request->input('contact_email'); $res['contact_email'] = $request->input('contact_email');
$res['privacy_pledge'] = (bool) $request->input('privacy_pledge'); $res['privacy_pledge'] = (bool) $request->input('privacy_pledge');
if($request->filled('location')) { if ($request->filled('location')) {
$exists = (new ISO3166)->name($request->location); $exists = (new ISO3166)->name($request->location);
if($exists) { if ($exists) {
$res['location'] = $request->input('location'); $res['location'] = $request->input('location');
} }
} }
if($request->hasFile('banner_image')) { if ($request->hasFile('banner_image')) {
collect(Storage::files('public/headers')) collect(Storage::files('public/headers'))
->filter(function($name) { ->filter(function ($name) {
$protected = [ $protected = [
'public/headers/.gitignore', 'public/headers/.gitignore',
'public/headers/default.jpg', 'public/headers/default.jpg',
'public/headers/missing.png' 'public/headers/missing.png',
]; ];
return !in_array($name, $protected);
return ! in_array($name, $protected);
}) })
->each(function($name) { ->each(function ($name) {
Storage::delete($name); Storage::delete($name);
}); });
$path = $request->file('banner_image')->storePublicly('public/headers'); $path = $request->file('banner_image')->storePublicly('public/headers');
@ -240,9 +232,10 @@ trait AdminDirectoryController
ConfigCacheService::put('pixelfed.directory', $config->v); ConfigCacheService::put('pixelfed.directory', $config->v);
$updated = json_decode($config->v, true); $updated = json_decode($config->v, true);
if(isset($updated['banner_image'])) { if (isset($updated['banner_image'])) {
$updated['banner_image'] = url(Storage::url($updated['banner_image'])); $updated['banner_image'] = url(Storage::url($updated['banner_image']));
} }
return $updated; return $updated;
} }
@ -253,7 +246,7 @@ trait AdminDirectoryController
'open_registration' => (bool) config_cache('pixelfed.open_registration'), 'open_registration' => (bool) config_cache('pixelfed.open_registration'),
'curated_onboarding' => (bool) config_cache('instance.curated_registration.enabled'), 'curated_onboarding' => (bool) config_cache('instance.curated_registration.enabled'),
'activitypub_enabled' => config_cache('federation.activitypub.enabled'), 'activitypub_enabled' => config_cache('federation.activitypub.enabled'),
'oauth_enabled' => config_cache('pixelfed.oauth_enabled'), 'oauth_enabled' => (bool) config_cache('pixelfed.oauth_enabled'),
'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','), 'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','),
'image_quality' => config_cache('pixelfed.image_quality'), 'image_quality' => config_cache('pixelfed.image_quality'),
'optimize_image' => config_cache('pixelfed.optimize_image'), 'optimize_image' => config_cache('pixelfed.optimize_image'),
@ -274,7 +267,7 @@ trait AdminDirectoryController
'media_types' => [ 'media_types' => [
'required', 'required',
function ($attribute, $value, $fail) { function ($attribute, $value, $fail) {
if (!in_array('image/jpeg', $value->toArray()) || !in_array('image/png', $value->toArray())) { if (! in_array('image/jpeg', $value->toArray()) || ! in_array('image/png', $value->toArray())) {
$fail('You must enable image/jpeg and image/png support.'); $fail('You must enable image/jpeg and image/png support.');
} }
}, },
@ -285,10 +278,10 @@ trait AdminDirectoryController
'max_account_size' => 'required_if:enforce_account_limit,true|integer|min:1000000', 'max_account_size' => 'required_if:enforce_account_limit,true|integer|min:1000000',
'max_album_length' => 'required|integer|min:4|max:20', 'max_album_length' => 'required|integer|min:4|max:20',
'account_deletion' => 'required|accepted', 'account_deletion' => 'required|accepted',
'max_caption_length' => 'required|integer|min:500|max:10000' 'max_caption_length' => 'required|integer|min:500|max:10000',
]); ]);
if(!$validator->validate()) { if (! $validator->validate()) {
return response()->json($validator->errors(), 422); return response()->json($validator->errors(), 422);
} }
@ -297,6 +290,7 @@ trait AdminDirectoryController
$data = (new PixelfedDirectoryController())->buildListing(); $data = (new PixelfedDirectoryController())->buildListing();
$res = Http::withoutVerifying()->post('https://pixelfed.org/api/v1/directory/submission', $data); $res = Http::withoutVerifying()->post('https://pixelfed.org/api/v1/directory/submission', $data);
return 200; return 200;
} }
@ -304,7 +298,7 @@ trait AdminDirectoryController
{ {
$bannerImage = ConfigCache::whereK('app.banner_image')->first(); $bannerImage = ConfigCache::whereK('app.banner_image')->first();
$directory = ConfigCache::whereK('pixelfed.directory')->first(); $directory = ConfigCache::whereK('pixelfed.directory')->first();
if(!$bannerImage && !$directory || empty($directory->v)) { if (! $bannerImage && ! $directory || empty($directory->v)) {
return; return;
} }
$directoryArr = json_decode($directory->v, true); $directoryArr = json_decode($directory->v, true);
@ -312,12 +306,12 @@ trait AdminDirectoryController
$protected = [ $protected = [
'public/headers/.gitignore', 'public/headers/.gitignore',
'public/headers/default.jpg', 'public/headers/default.jpg',
'public/headers/missing.png' 'public/headers/missing.png',
]; ];
if(!$path || in_array($path, $protected)) { if (! $path || in_array($path, $protected)) {
return; return;
} }
if(Storage::exists($directoryArr['banner_image'])) { if (Storage::exists($directoryArr['banner_image'])) {
Storage::delete($directoryArr['banner_image']); Storage::delete($directoryArr['banner_image']);
} }
@ -328,12 +322,13 @@ trait AdminDirectoryController
$bannerImage->save(); $bannerImage->save();
Cache::forget('api:v1:instance-data-response-v1'); Cache::forget('api:v1:instance-data-response-v1');
ConfigCacheService::put('pixelfed.directory', $directory); ConfigCacheService::put('pixelfed.directory', $directory);
return $bannerImage->v; return $bannerImage->v;
} }
public function directoryGetPopularPosts(Request $request) public function directoryGetPopularPosts(Request $request)
{ {
$ids = Cache::remember('admin:api:popular_posts', 86400, function() { $ids = Cache::remember('admin:api:popular_posts', 86400, function () {
return Status::whereLocal(true) return Status::whereLocal(true)
->whereScope('public') ->whereScope('public')
->whereType('photo') ->whereType('photo')
@ -343,21 +338,21 @@ trait AdminDirectoryController
->pluck('id'); ->pluck('id');
}); });
$res = $ids->map(function($id) { $res = $ids->map(function ($id) {
return StatusService::get($id); return StatusService::get($id);
}) })
->filter(function($post) { ->filter(function ($post) {
return $post && isset($post['account']); return $post && isset($post['account']);
}) })
->values(); ->values();
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function directoryGetAddPostByIdSearch(Request $request) public function directoryGetAddPostByIdSearch(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'q' => 'required|integer' 'q' => 'required|integer',
]); ]);
$id = $request->input('q'); $id = $request->input('q');
@ -380,11 +375,12 @@ trait AdminDirectoryController
$profile_id = $request->input('profile_id'); $profile_id = $request->input('profile_id');
$testimonials = ConfigCache::whereK('pixelfed.directory.testimonials')->firstOrFail(); $testimonials = ConfigCache::whereK('pixelfed.directory.testimonials')->firstOrFail();
$existing = collect(json_decode($testimonials->v, true)) $existing = collect(json_decode($testimonials->v, true))
->filter(function($t) use($profile_id) { ->filter(function ($t) use ($profile_id) {
return $t['profile_id'] !== $profile_id; return $t['profile_id'] !== $profile_id;
}) })
->values(); ->values();
ConfigCacheService::put('pixelfed.directory.testimonials', $existing); ConfigCacheService::put('pixelfed.directory.testimonials', $existing);
return $existing; return $existing;
} }
@ -392,13 +388,13 @@ trait AdminDirectoryController
{ {
$this->validate($request, [ $this->validate($request, [
'username' => 'required', 'username' => 'required',
'body' => 'required|string|min:5|max:500' 'body' => 'required|string|min:5|max:500',
]); ]);
$user = User::whereUsername($request->input('username'))->whereNull('status')->firstOrFail(); $user = User::whereUsername($request->input('username'))->whereNull('status')->firstOrFail();
$configCache = ConfigCache::firstOrCreate([ $configCache = ConfigCache::firstOrCreate([
'k' => 'pixelfed.directory.testimonials' 'k' => 'pixelfed.directory.testimonials',
]); ]);
$testimonials = $configCache->v ? collect(json_decode($configCache->v, true)) : collect([]); $testimonials = $configCache->v ? collect(json_decode($configCache->v, true)) : collect([]);
@ -409,7 +405,7 @@ trait AdminDirectoryController
$testimonials->push([ $testimonials->push([
'profile_id' => (string) $user->profile_id, 'profile_id' => (string) $user->profile_id,
'username' => $request->input('username'), 'username' => $request->input('username'),
'body' => $request->input('body') 'body' => $request->input('body'),
]); ]);
$configCache->v = json_encode($testimonials->toArray()); $configCache->v = json_encode($testimonials->toArray());
@ -417,8 +413,9 @@ trait AdminDirectoryController
ConfigCacheService::put('pixelfed.directory.testimonials', $configCache->v); ConfigCacheService::put('pixelfed.directory.testimonials', $configCache->v);
$res = [ $res = [
'profile' => AccountService::get($user->profile_id), 'profile' => AccountService::get($user->profile_id),
'body' => $request->input('body') 'body' => $request->input('body'),
]; ];
return $res; return $res;
} }
@ -426,7 +423,7 @@ trait AdminDirectoryController
{ {
$this->validate($request, [ $this->validate($request, [
'profile_id' => 'required', 'profile_id' => 'required',
'body' => 'required|string|min:5|max:500' 'body' => 'required|string|min:5|max:500',
]); ]);
$profile_id = $request->input('profile_id'); $profile_id = $request->input('profile_id');
@ -434,15 +431,16 @@ trait AdminDirectoryController
$user = User::whereProfileId($profile_id)->firstOrFail(); $user = User::whereProfileId($profile_id)->firstOrFail();
$configCache = ConfigCache::firstOrCreate([ $configCache = ConfigCache::firstOrCreate([
'k' => 'pixelfed.directory.testimonials' 'k' => 'pixelfed.directory.testimonials',
]); ]);
$testimonials = $configCache->v ? collect(json_decode($configCache->v, true)) : collect([]); $testimonials = $configCache->v ? collect(json_decode($configCache->v, true)) : collect([]);
$updated = $testimonials->map(function($t) use($profile_id, $body) { $updated = $testimonials->map(function ($t) use ($profile_id, $body) {
if($t['profile_id'] == $profile_id) { if ($t['profile_id'] == $profile_id) {
$t['body'] = $body; $t['body'] = $body;
} }
return $t; return $t;
}) })
->values(); ->values();

View file

@ -0,0 +1,49 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Models\Group;
use App\Models\GroupCategory;
use App\Models\GroupInteraction;
use App\Models\GroupMember;
use App\Models\GroupPost;
use App\Models\GroupReport;
use Cache;
use Illuminate\Http\Request;
trait AdminGroupsController
{
public function groupsHome(Request $request)
{
$stats = $this->groupAdminStats();
return view('admin.groups.home', compact('stats'));
}
protected function groupAdminStats()
{
return Cache::remember('admin:groups:stats', 3, function () {
$res = [
'total' => Group::count(),
'local' => Group::whereLocal(true)->count(),
];
$res['remote'] = $res['total'] - $res['local'];
$res['categories'] = GroupCategory::count();
$res['posts'] = GroupPost::count();
$res['members'] = GroupMember::count();
$res['interactions'] = GroupInteraction::count();
$res['reports'] = GroupReport::count();
$res['local_30d'] = Cache::remember('admin:groups:stats:local_30d', 43200, function () {
return Group::whereLocal(true)->where('created_at', '>', now()->subMonth())->count();
});
$res['remote_30d'] = Cache::remember('admin:groups:stats:remote_30d', 43200, function () {
return Group::whereLocal(false)->where('created_at', '>', now()->subMonth())->count();
});
return $res;
});
}
}

View file

@ -7,7 +7,9 @@ use App\Models\InstanceActor;
use App\Page; use App\Page;
use App\Profile; use App\Profile;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\AdminSettingsService;
use App\Services\ConfigCacheService; use App\Services\ConfigCacheService;
use App\Services\FilesystemService;
use App\User; use App\User;
use App\Util\Site\Config; use App\Util\Site\Config;
use Artisan; use Artisan;
@ -71,6 +73,7 @@ trait AdminSettingsController
'admin_account_id' => 'nullable', 'admin_account_id' => 'nullable',
'regs' => 'required|in:open,filtered,closed', 'regs' => 'required|in:open,filtered,closed',
'account_migration' => 'nullable', 'account_migration' => 'nullable',
'rule_delete' => 'sometimes',
]); ]);
$orb = false; $orb = false;
@ -310,4 +313,573 @@ trait AdminSettingsController
return view('admin.settings.system', compact('sys')); return view('admin.settings.system', compact('sys'));
} }
public function settingsApiFetch(Request $request)
{
$cloud_storage = ConfigCacheService::get('pixelfed.cloud_storage');
$cloud_disk = config('filesystems.cloud');
$cloud_ready = ! empty(config('filesystems.disks.'.$cloud_disk.'.key')) && ! empty(config('filesystems.disks.'.$cloud_disk.'.secret'));
$types = explode(',', ConfigCacheService::get('pixelfed.media_types'));
$rules = ConfigCacheService::get('app.rules') ? json_decode(ConfigCacheService::get('app.rules'), true) : [];
$jpeg = in_array('image/jpg', $types) || in_array('image/jpeg', $types);
$png = in_array('image/png', $types);
$gif = in_array('image/gif', $types);
$mp4 = in_array('video/mp4', $types);
$webp = in_array('image/webp', $types);
$availableAdmins = User::whereIsAdmin(true)->get();
$currentAdmin = config_cache('instance.admin.pid') ? AccountService::get(config_cache('instance.admin.pid'), true) : null;
$openReg = (bool) config_cache('pixelfed.open_registration');
$curOnboarding = (bool) config_cache('instance.curated_registration.enabled');
$regState = $openReg ? 'open' : ($curOnboarding ? 'filtered' : 'closed');
$accountMigration = (bool) config_cache('federation.migration');
$autoFollow = config_cache('account.autofollow_usernames');
if (strlen($autoFollow) > 3) {
$autoFollow = explode(',', $autoFollow);
}
$res = AdminSettingsService::getAll();
return response()->json($res);
}
public function settingsApiRulesAdd(Request $request)
{
$this->validate($request, [
'rule' => 'required|string|min:5|max:1000',
]);
$rules = ConfigCacheService::get('app.rules');
$val = $request->input('rule');
if (! $rules) {
ConfigCacheService::put('app.rules', json_encode([$val]));
} else {
$json = json_decode($rules, true);
$count = count($json);
if ($count >= 30) {
return response()->json(['message' => 'Max rules limit reached, you can set up to 30 rules at a time.'], 400);
}
$json[] = $val;
ConfigCacheService::put('app.rules', json_encode(array_values($json)));
}
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Config::refresh();
return [$val];
}
public function settingsApiRulesDelete(Request $request)
{
$this->validate($request, [
'rule' => 'required|string',
]);
$rules = ConfigCacheService::get('app.rules');
$val = $request->input('rule');
if (! $rules) {
return [];
} else {
$json = json_decode($rules, true);
$idx = array_search($val, $json);
if ($idx !== false) {
unset($json[$idx]);
$json = array_values($json);
}
ConfigCacheService::put('app.rules', json_encode(array_values($json)));
}
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Config::refresh();
return response()->json($json);
}
public function settingsApiRulesDeleteAll(Request $request)
{
$rules = ConfigCacheService::get('app.rules');
if (! $rules) {
return [];
} else {
ConfigCacheService::put('app.rules', json_encode([]));
}
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Config::refresh();
return response()->json([]);
}
public function settingsApiAutofollowDelete(Request $request)
{
$this->validate($request, [
'username' => 'required|string',
]);
$username = $request->input('username');
$names = [];
$existing = config_cache('account.autofollow_usernames');
if ($existing) {
$names = explode(',', $existing);
}
if (in_array($username, $names)) {
$key = array_search($username, $names);
if ($key !== false) {
unset($names[$key]);
}
}
ConfigCacheService::put('account.autofollow_usernames', implode(',', $names));
return response()->json(['accounts' => array_values($names)]);
}
public function settingsApiAutofollowAdd(Request $request)
{
$this->validate($request, [
'username' => 'required|string',
]);
$username = $request->input('username');
$names = [];
$existing = config_cache('account.autofollow_usernames');
if ($existing) {
$names = explode(',', $existing);
}
if ($existing && count($names)) {
if (count($names) >= 5) {
return response()->json(['message' => 'You can only add up to 5 accounts to be autofollowed.'], 400);
}
if (in_array(strtolower($username), array_map('strtolower', $names))) {
return response()->json(['message' => 'User already exists, please try again.'], 400);
}
}
$p = User::whereUsername($username)->whereNull('status')->first();
if (! $p || in_array($p->username, $names)) {
abort(404);
}
array_push($names, $p->username);
ConfigCacheService::put('account.autofollow_usernames', implode(',', $names));
return response()->json(['accounts' => array_values($names)]);
}
public function settingsApiUpdateType(Request $request, $type)
{
abort_unless(in_array($type, [
'posts',
'platform',
'home',
'landing',
'branding',
'media',
'users',
'storage',
]), 400);
switch ($type) {
case 'home':
return $this->settingsApiUpdateHomeType($request);
break;
case 'landing':
return $this->settingsApiUpdateLandingType($request);
break;
case 'posts':
return $this->settingsApiUpdatePostsType($request);
break;
case 'platform':
return $this->settingsApiUpdatePlatformType($request);
break;
case 'branding':
return $this->settingsApiUpdateBrandingType($request);
break;
case 'media':
return $this->settingsApiUpdateMediaType($request);
break;
case 'users':
return $this->settingsApiUpdateUsersType($request);
break;
case 'storage':
return $this->settingsApiUpdateStorageType($request);
break;
default:
abort(404);
break;
}
}
public function settingsApiUpdateHomeType($request)
{
$this->validate($request, [
'registration_status' => 'required|in:open,filtered,closed',
'cloud_storage' => 'required',
'activitypub_enabled' => 'required',
'account_migration' => 'required',
'mobile_apis' => 'required',
'stories' => 'required',
'instagram_import' => 'required',
'autospam_enabled' => 'required',
]);
$regStatus = $request->input('registration_status');
ConfigCacheService::put('pixelfed.open_registration', $regStatus === 'open');
ConfigCacheService::put('instance.curated_registration.enabled', $regStatus === 'filtered');
$cloudStorage = $request->boolean('cloud_storage');
if ($cloudStorage !== (bool) config_cache('pixelfed.cloud_storage')) {
if (! $cloudStorage) {
ConfigCacheService::put('pixelfed.cloud_storage', false);
} else {
$cloud_disk = config('filesystems.cloud');
$cloud_ready = ! empty(config('filesystems.disks.'.$cloud_disk.'.key')) && ! empty(config('filesystems.disks.'.$cloud_disk.'.secret'));
if (! $cloud_ready) {
return redirect()->back()->withErrors(['cloud_storage' => 'Must configure cloud storage before enabling!']);
} else {
ConfigCacheService::put('pixelfed.cloud_storage', true);
}
}
}
ConfigCacheService::put('federation.activitypub.enabled', $request->boolean('activitypub_enabled'));
ConfigCacheService::put('federation.migration', $request->boolean('account_migration'));
ConfigCacheService::put('pixelfed.oauth_enabled', $request->boolean('mobile_apis'));
ConfigCacheService::put('instance.stories.enabled', $request->boolean('stories'));
ConfigCacheService::put('pixelfed.import.instagram.enabled', $request->boolean('instagram_import'));
ConfigCacheService::put('pixelfed.bouncer.enabled', $request->boolean('autospam_enabled'));
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Cache::forget('api:v1:instance-data:contact');
Config::refresh();
return $request->all();
}
public function settingsApiUpdateLandingType($request)
{
$this->validate($request, [
'current_admin' => 'required',
'show_directory' => 'required',
'show_explore' => 'required',
]);
ConfigCacheService::put('instance.admin.pid', $request->input('current_admin'));
ConfigCacheService::put('instance.landing.show_directory', $request->boolean('show_directory'));
ConfigCacheService::put('instance.landing.show_explore', $request->boolean('show_explore'));
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Cache::forget('api:v1:instance-data:contact');
Config::refresh();
return $request->all();
}
public function settingsApiUpdateMediaType($request)
{
$this->validate($request, [
'image_quality' => 'required|integer|min:1|max:100',
'max_album_length' => 'required|integer|min:1|max:20',
'max_photo_size' => 'required|integer|min:100|max:50000',
'media_types' => 'required',
'optimize_image' => 'required',
'optimize_video' => 'required',
]);
$mediaTypes = $request->input('media_types');
$mediaArray = explode(',', $mediaTypes);
foreach ($mediaArray as $mediaType) {
if (! in_array($mediaType, ['image/jpeg', 'image/png', 'image/gif', 'image/webp', 'video/mp4'])) {
return redirect()->back()->withErrors(['media_types' => 'Invalid media type']);
}
}
ConfigCacheService::put('pixelfed.media_types', $request->input('media_types'));
ConfigCacheService::put('pixelfed.image_quality', $request->input('image_quality'));
ConfigCacheService::put('pixelfed.max_album_length', $request->input('max_album_length'));
ConfigCacheService::put('pixelfed.max_photo_size', $request->input('max_photo_size'));
ConfigCacheService::put('pixelfed.optimize_image', $request->boolean('optimize_image'));
ConfigCacheService::put('pixelfed.optimize_video', $request->boolean('optimize_video'));
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Cache::forget('api:v1:instance-data:contact');
Config::refresh();
return $request->all();
}
public function settingsApiUpdateBrandingType($request)
{
$this->validate($request, [
'name' => 'required',
'short_description' => 'required',
'long_description' => 'required',
]);
ConfigCacheService::put('app.name', $request->input('name'));
ConfigCacheService::put('app.short_description', $request->input('short_description'));
ConfigCacheService::put('app.description', $request->input('long_description'));
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Cache::forget('api:v1:instance-data:contact');
Config::refresh();
return $request->all();
}
public function settingsApiUpdatePostsType($request)
{
$this->validate($request, [
'max_caption_length' => 'required|integer|min:5|max:10000',
'max_altext_length' => 'required|integer|min:5|max:40000',
]);
ConfigCacheService::put('pixelfed.max_caption_length', $request->input('max_caption_length'));
ConfigCacheService::put('pixelfed.max_altext_length', $request->input('max_altext_length'));
$res = [
'max_caption_length' => $request->input('max_caption_length'),
'max_altext_length' => $request->input('max_altext_length'),
];
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Config::refresh();
return $res;
}
public function settingsApiUpdatePlatformType($request)
{
$this->validate($request, [
'allow_app_registration' => 'required',
'app_registration_rate_limit_attempts' => 'required|integer|min:1',
'app_registration_rate_limit_decay' => 'required|integer|min:1',
'app_registration_confirm_rate_limit_attempts' => 'required|integer|min:1',
'app_registration_confirm_rate_limit_decay' => 'required|integer|min:1',
'allow_post_embeds' => 'required',
'allow_profile_embeds' => 'required',
'captcha_enabled' => 'required',
'captcha_on_login' => 'required_if_accepted:captcha_enabled',
'captcha_on_register' => 'required_if_accepted:captcha_enabled',
'captcha_secret' => 'required_if_accepted:captcha_enabled',
'captcha_sitekey' => 'required_if_accepted:captcha_enabled',
'custom_emoji_enabled' => 'required',
]);
ConfigCacheService::put('pixelfed.allow_app_registration', $request->boolean('allow_app_registration'));
ConfigCacheService::put('pixelfed.app_registration_rate_limit_attempts', $request->input('app_registration_rate_limit_attempts'));
ConfigCacheService::put('pixelfed.app_registration_rate_limit_decay', $request->input('app_registration_rate_limit_decay'));
ConfigCacheService::put('pixelfed.app_registration_confirm_rate_limit_attempts', $request->input('app_registration_confirm_rate_limit_attempts'));
ConfigCacheService::put('pixelfed.app_registration_confirm_rate_limit_decay', $request->input('app_registration_confirm_rate_limit_decay'));
ConfigCacheService::put('instance.embed.post', $request->boolean('allow_post_embeds'));
ConfigCacheService::put('instance.embed.profile', $request->boolean('allow_profile_embeds'));
ConfigCacheService::put('federation.custom_emoji.enabled', $request->boolean('custom_emoji_enabled'));
$captcha = $request->boolean('captcha_enabled');
if ($captcha) {
$secret = $request->input('captcha_secret');
$sitekey = $request->input('captcha_sitekey');
if (config_cache('captcha.secret') != $secret && strpos($secret, '*') === false) {
ConfigCacheService::put('captcha.secret', $secret);
}
if (config_cache('captcha.sitekey') != $sitekey && strpos($sitekey, '*') === false) {
ConfigCacheService::put('captcha.sitekey', $sitekey);
}
ConfigCacheService::put('captcha.active.login', $request->boolean('captcha_on_login'));
ConfigCacheService::put('captcha.active.register', $request->boolean('captcha_on_register'));
ConfigCacheService::put('captcha.triggers.login.enabled', $request->boolean('captcha_on_login'));
ConfigCacheService::put('captcha.enabled', true);
} else {
ConfigCacheService::put('captcha.enabled', false);
}
$res = [
'allow_app_registration' => $request->boolean('allow_app_registration'),
'app_registration_rate_limit_attempts' => $request->input('app_registration_rate_limit_attempts'),
'app_registration_rate_limit_decay' => $request->input('app_registration_rate_limit_decay'),
'app_registration_confirm_rate_limit_attempts' => $request->input('app_registration_confirm_rate_limit_attempts'),
'app_registration_confirm_rate_limit_decay' => $request->input('app_registration_confirm_rate_limit_decay'),
'allow_post_embeds' => $request->boolean('allow_post_embeds'),
'allow_profile_embeds' => $request->boolean('allow_profile_embeds'),
'captcha_enabled' => $request->boolean('captcha_enabled'),
'captcha_on_login' => $request->boolean('captcha_on_login'),
'captcha_on_register' => $request->boolean('captcha_on_register'),
'captcha_secret' => $request->input('captcha_secret'),
'captcha_sitekey' => $request->input('captcha_sitekey'),
'custom_emoji_enabled' => $request->boolean('custom_emoji_enabled'),
];
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Config::refresh();
return $res;
}
public function settingsApiUpdateUsersType($request)
{
$this->validate($request, [
'require_email_verification' => 'required',
'enforce_account_limit' => 'required',
'max_account_size' => 'required|integer|min:50000',
'admin_autofollow' => 'required',
'admin_autofollow_accounts' => 'sometimes',
'max_user_blocks' => 'required|integer|min:0|max:5000',
'max_user_mutes' => 'required|integer|min:0|max:5000',
'max_domain_blocks' => 'required|integer|min:0|max:5000',
]);
$adminAutofollow = $request->boolean('admin_autofollow');
$adminAutofollowAccounts = $request->input('admin_autofollow_accounts');
if ($adminAutofollow) {
if ($request->filled('admin_autofollow_accounts')) {
$names = [];
$existing = config_cache('account.autofollow_usernames');
if ($existing) {
$names = explode(',', $existing);
foreach (array_map('strtolower', $adminAutofollowAccounts) as $afc) {
if (in_array(strtolower($afc), array_map('strtolower', $names))) {
continue;
}
$names[] = $afc;
}
} else {
$names = $adminAutofollowAccounts;
}
if (! $names || count($names) == 0) {
return response()->json(['message' => 'You need to assign autofollow accounts before you can enable it.'], 400);
}
if (count($names) > 5) {
return response()->json(['message' => 'You can only add up to 5 accounts to be autofollowed.'.json_encode($names)], 400);
}
$autofollows = User::whereIn('username', $names)->whereNull('status')->pluck('username');
$adminAutofollowAccounts = $autofollows->implode(',');
ConfigCacheService::put('account.autofollow_usernames', $adminAutofollowAccounts);
} else {
return response()->json(['message' => 'You need to assign autofollow accounts before you can enable it.'], 400);
}
}
ConfigCacheService::put('pixelfed.enforce_email_verification', $request->boolean('require_email_verification'));
ConfigCacheService::put('pixelfed.enforce_account_limit', $request->boolean('enforce_account_limit'));
ConfigCacheService::put('pixelfed.max_account_size', $request->input('max_account_size'));
ConfigCacheService::put('account.autofollow', $request->boolean('admin_autofollow'));
ConfigCacheService::put('instance.user_filters.max_user_blocks', (int) $request->input('max_user_blocks'));
ConfigCacheService::put('instance.user_filters.max_user_mutes', (int) $request->input('max_user_mutes'));
ConfigCacheService::put('instance.user_filters.max_domain_blocks', (int) $request->input('max_domain_blocks'));
$res = [
'require_email_verification' => $request->boolean('require_email_verification'),
'enforce_account_limit' => $request->boolean('enforce_account_limit'),
'admin_autofollow' => $request->boolean('admin_autofollow'),
'admin_autofollow_accounts' => $adminAutofollowAccounts,
'max_user_blocks' => $request->input('max_user_blocks'),
'max_user_mutes' => $request->input('max_user_mutes'),
'max_domain_blocks' => $request->input('max_domain_blocks'),
];
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Config::refresh();
return $res;
}
public function settingsApiUpdateStorageType($request)
{
$this->validate($request, [
'primary_disk' => 'required|in:local,cloud',
'update_disk' => 'sometimes',
'disk_config' => 'required_if_accepted:update_disk',
'disk_config.driver' => 'required|in:s3,spaces',
'disk_config.key' => 'required',
'disk_config.secret' => 'required',
'disk_config.region' => 'required',
'disk_config.bucket' => 'required',
'disk_config.visibility' => 'required',
'disk_config.endpoint' => 'required',
'disk_config.url' => 'nullable',
]);
ConfigCacheService::put('pixelfed.cloud_storage', $request->input('primary_disk') === 'cloud');
$res = [
'primary_disk' => $request->input('primary_disk'),
];
if ($request->has('update_disk')) {
$res['disk_config'] = $request->input('disk_config');
$changes = [];
$dkey = $request->input('disk_config.driver') === 's3' ? 'filesystems.disks.s3.' : 'filesystems.disks.spaces.';
$key = $request->input('disk_config.key');
$ckey = null;
$secret = $request->input('disk_config.secret');
$csecret = null;
$region = $request->input('disk_config.region');
$bucket = $request->input('disk_config.bucket');
$visibility = $request->input('disk_config.visibility');
$url = $request->input('disk_config.url');
$endpoint = $request->input('disk_config.endpoint');
if (strpos($key, '*') === false && $key != config_cache($dkey.'key')) {
array_push($changes, 'key');
} else {
$ckey = config_cache($dkey.'key');
}
if (strpos($secret, '*') === false && $secret != config_cache($dkey.'secret')) {
array_push($changes, 'secret');
} else {
$csecret = config_cache($dkey.'secret');
}
if ($region != config_cache($dkey.'region')) {
array_push($changes, 'region');
}
if ($bucket != config_cache($dkey.'bucket')) {
array_push($changes, 'bucket');
}
if ($visibility != config_cache($dkey.'visibility')) {
array_push($changes, 'visibility');
}
if ($url != config_cache($dkey.'url')) {
array_push($changes, 'url');
}
if ($endpoint != config_cache($dkey.'endpoint')) {
array_push($changes, 'endpoint');
}
if ($changes && count($changes)) {
$isValid = FilesystemService::getVerifyCredentials(
$ckey ?? $key,
$csecret ?? $secret,
$region,
$bucket,
$endpoint,
);
if (! $isValid) {
return response()->json(['error' => true, 's3_vce' => true, 'message' => "<div class='border border-danger text-danger p-3 font-weight-bold rounded-lg'>The S3/Spaces credentials you provided are invalid, or the bucket does not have the proper permissions.</div><br/>Please check all fields and try again.<br/><br/><strong>Any cloud storage configuration changes you made have NOT been saved due to invalid credentials.</strong>"], 400);
}
}
$res['changes'] = json_encode($changes);
}
Cache::forget('api:v1:instance-data:rules');
Cache::forget('api:v1:instance-data-response-v1');
Cache::forget('api:v2:instance-data-response-v2');
Config::refresh();
return $res;
}
} }

View file

@ -2,57 +2,45 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\{ use App\Contact;
AccountInterstitial, use App\Http\Controllers\Admin\AdminAutospamController;
Contact, use App\Http\Controllers\Admin\AdminDirectoryController;
Hashtag, use App\Http\Controllers\Admin\AdminDiscoverController;
Instance, use App\Http\Controllers\Admin\AdminHashtagsController;
Newsroom, use App\Http\Controllers\Admin\AdminInstanceController;
OauthClient, use App\Http\Controllers\Admin\AdminMediaController;
Profile, use App\Http\Controllers\Admin\AdminReportController;
Report, use App\Http\Controllers\Admin\AdminSettingsController;
Status, use App\Http\Controllers\Admin\AdminUserController;
StatusHashtag, use App\Instance;
Story, use App\Models\CustomEmoji;
User use App\Newsroom;
}; use App\OauthClient;
use DB, Cache, Storage; use App\Profile;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Http\Controllers\Admin\{
AdminAutospamController,
AdminDirectoryController,
AdminDiscoverController,
AdminHashtagsController,
AdminInstanceController,
AdminReportController,
// AdminGroupsController,
AdminMediaController,
AdminSettingsController,
// AdminStorageController,
AdminSupportController,
AdminUserController
};
use Illuminate\Validation\Rule;
use App\Services\AdminStatsService;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\AdminStatsService;
use App\Services\ConfigCacheService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\StoryService; use App\Services\StoryService;
use App\Models\CustomEmoji; use App\Status;
use App\Story;
use App\User;
use Cache;
use DB;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Storage;
class AdminController extends Controller class AdminController extends Controller
{ {
use AdminReportController, use AdminAutospamController,
AdminAutospamController,
AdminDirectoryController, AdminDirectoryController,
AdminDiscoverController, AdminDiscoverController,
AdminHashtagsController, AdminHashtagsController,
// AdminGroupsController,
AdminMediaController,
AdminSettingsController,
AdminInstanceController, AdminInstanceController,
// AdminStorageController, AdminMediaController,
AdminReportController,
AdminSettingsController,
AdminUserController; AdminUserController;
public function __construct() public function __construct()
@ -67,9 +55,27 @@ class AdminController extends Controller
return view('admin.home'); return view('admin.home');
} }
public function customCss()
{
return view('admin.settings.customcss');
}
public function saveCustomCss(Request $request)
{
$this->validate($request, [
'css' => 'sometimes|max:5000',
'show' => 'sometimes',
]);
ConfigCacheService::put('uikit.custom.css', $request->input('css'));
ConfigCacheService::put('uikit.show_custom.css', $request->boolean('show'));
return view('admin.settings.customcss');
}
public function stats() public function stats()
{ {
$data = AdminStatsService::get(); $data = AdminStatsService::get();
return view('admin.stats', compact('data')); return view('admin.stats', compact('data'));
} }
@ -83,25 +89,27 @@ class AdminController extends Controller
$users = User::orderByDesc('id')->cursorPaginate(10); $users = User::orderByDesc('id')->cursorPaginate(10);
$res = [ $res = [
"next_page_url" => $users->nextPageUrl(), 'next_page_url' => $users->nextPageUrl(),
"data" => $users->map(function($user) { 'data' => $users->map(function ($user) {
$account = AccountService::get($user->profile_id, true); $account = AccountService::get($user->profile_id, true);
if(!$account) { if (! $account) {
return [ return [
"id" => $user->profile_id, 'id' => $user->profile_id,
"username" => $user->username, 'username' => $user->username,
"status" => "deleted", 'status' => 'deleted',
"avatar" => "/storage/avatars/default.jpg", 'avatar' => '/storage/avatars/default.jpg',
"created_at" => $user->created_at 'created_at' => $user->created_at,
]; ];
} }
$account['user_id'] = $user->id; $account['user_id'] = $user->id;
return $account; return $account;
}) })
->filter(function($user) { ->filter(function ($user) {
return $user; return $user;
}) }),
]; ];
return $res; return $res;
} }
@ -112,14 +120,15 @@ class AdminController extends Controller
->cursorPaginate(10); ->cursorPaginate(10);
$res = [ $res = [
"next_page_url" => $posts->nextPageUrl(), 'next_page_url' => $posts->nextPageUrl(),
"data" => $posts->map(function($post) { 'data' => $posts->map(function ($post) {
$status = StatusService::get($post->id, false); $status = StatusService::get($post->id, false);
if(!$status) { if (! $status) {
return ["id" => $post->id, "created_at" => $post->created_at]; return ['id' => $post->id, 'created_at' => $post->created_at];
} }
return $status; return $status;
}) }),
]; ];
return $res; return $res;
@ -133,13 +142,14 @@ class AdminController extends Controller
public function statuses(Request $request) public function statuses(Request $request)
{ {
$statuses = Status::orderBy('id', 'desc')->cursorPaginate(10); $statuses = Status::orderBy('id', 'desc')->cursorPaginate(10);
$data = $statuses->map(function($status) { $data = $statuses->map(function ($status) {
return StatusService::get($status->id, false); return StatusService::get($status->id, false);
}) })
->filter(function($s) { ->filter(function ($s) {
return $s; return $s;
}) })
->toArray(); ->toArray();
return view('admin.statuses.home', compact('statuses', 'data')); return view('admin.statuses.home', compact('statuses', 'data'));
} }
@ -157,23 +167,24 @@ class AdminController extends Controller
'filter' => [ 'filter' => [
'nullable', 'nullable',
'string', 'string',
Rule::in(['all', 'local', 'remote']) Rule::in(['all', 'local', 'remote']),
] ],
]); ]);
$search = $request->input('search'); $search = $request->input('search');
$filter = $request->input('filter'); $filter = $request->input('filter');
$limit = 12; $limit = 12;
$profiles = Profile::select('id','username') $profiles = Profile::select('id', 'username')
->whereNull('status') ->whereNull('status')
->when($search, function($q, $search) { ->when($search, function ($q, $search) {
return $q->where('username', 'like', "%$search%"); return $q->where('username', 'like', "%$search%");
})->when($filter, function($q, $filter) { })->when($filter, function ($q, $filter) {
if($filter == 'local') { if ($filter == 'local') {
return $q->whereNull('domain'); return $q->whereNull('domain');
} }
if($filter == 'remote') { if ($filter == 'remote') {
return $q->whereNotNull('domain'); return $q->whereNotNull('domain');
} }
return $q; return $q;
})->orderByDesc('id') })->orderByDesc('id')
->simplePaginate($limit); ->simplePaginate($limit);
@ -185,13 +196,14 @@ class AdminController extends Controller
{ {
$profile = Profile::findOrFail($id); $profile = Profile::findOrFail($id);
$user = $profile->user; $user = $profile->user;
return view('admin.profiles.edit', compact('profile', 'user')); return view('admin.profiles.edit', compact('profile', 'user'));
} }
public function appsHome(Request $request) public function appsHome(Request $request)
{ {
$filter = $request->input('filter'); $filter = $request->input('filter');
if($filter == 'revoked') { if ($filter == 'revoked') {
$apps = OauthClient::with('user') $apps = OauthClient::with('user')
->whereNotNull('user_id') ->whereNotNull('user_id')
->whereRevoked(true) ->whereRevoked(true)
@ -203,39 +215,43 @@ class AdminController extends Controller
->orderByDesc('id') ->orderByDesc('id')
->paginate(10); ->paginate(10);
} }
return view('admin.apps.home', compact('apps')); return view('admin.apps.home', compact('apps'));
} }
public function messagesHome(Request $request) public function messagesHome(Request $request)
{ {
$messages = Contact::orderByDesc('id')->paginate(10); $messages = Contact::orderByDesc('id')->paginate(10);
return view('admin.messages.home', compact('messages')); return view('admin.messages.home', compact('messages'));
} }
public function messagesShow(Request $request, $id) public function messagesShow(Request $request, $id)
{ {
$message = Contact::findOrFail($id); $message = Contact::findOrFail($id);
return view('admin.messages.show', compact('message')); return view('admin.messages.show', compact('message'));
} }
public function messagesMarkRead(Request $request) public function messagesMarkRead(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'id' => 'required|integer|min:1' 'id' => 'required|integer|min:1',
]); ]);
$id = $request->input('id'); $id = $request->input('id');
$message = Contact::findOrFail($id); $message = Contact::findOrFail($id);
if($message->read_at) { if ($message->read_at) {
return; return;
} }
$message->read_at = now(); $message->read_at = now();
$message->save(); $message->save();
return;
} }
public function newsroomHome(Request $request) public function newsroomHome(Request $request)
{ {
$newsroom = Newsroom::latest()->paginate(10); $newsroom = Newsroom::latest()->paginate(10);
return view('admin.newsroom.home', compact('newsroom')); return view('admin.newsroom.home', compact('newsroom'));
} }
@ -247,6 +263,7 @@ class AdminController extends Controller
public function newsroomEdit(Request $request, $id) public function newsroomEdit(Request $request, $id)
{ {
$news = Newsroom::findOrFail($id); $news = Newsroom::findOrFail($id);
return view('admin.newsroom.edit', compact('news')); return view('admin.newsroom.edit', compact('news'));
} }
@ -254,6 +271,7 @@ class AdminController extends Controller
{ {
$news = Newsroom::findOrFail($id); $news = Newsroom::findOrFail($id);
$news->delete(); $news->delete();
return redirect('/i/admin/newsroom'); return redirect('/i/admin/newsroom');
} }
@ -262,13 +280,13 @@ class AdminController extends Controller
$this->validate($request, [ $this->validate($request, [
'title' => 'required|string|min:1|max:100', 'title' => 'required|string|min:1|max:100',
'summary' => 'nullable|string|max:200', 'summary' => 'nullable|string|max:200',
'body' => 'nullable|string' 'body' => 'nullable|string',
]); ]);
$changed = false; $changed = false;
$changedFields = []; $changedFields = [];
$slug = str_slug($request->input('title')); $slug = str_slug($request->input('title'));
if(Newsroom::whereSlug($slug)->exists()) { if (Newsroom::whereSlug($slug)->exists()) {
$slug = $slug . '-' . str_random(4); $slug = $slug.'-'.str_random(4);
} }
$news = Newsroom::findOrFail($id); $news = Newsroom::findOrFail($id);
$fields = [ $fields = [
@ -280,13 +298,13 @@ class AdminController extends Controller
'auth_only' => 'boolean', 'auth_only' => 'boolean',
'show_link' => 'boolean', 'show_link' => 'boolean',
'force_modal' => 'boolean', 'force_modal' => 'boolean',
'published' => 'published' 'published' => 'published',
]; ];
foreach($fields as $field => $type) { foreach ($fields as $field => $type) {
switch ($type) { switch ($type) {
case 'string': case 'string':
if($request->{$field} != $news->{$field}) { if ($request->{$field} != $news->{$field}) {
if($field == 'title') { if ($field == 'title') {
$news->slug = $slug; $news->slug = $slug;
} }
$news->{$field} = $request->{$field}; $news->{$field} = $request->{$field};
@ -297,7 +315,7 @@ class AdminController extends Controller
case 'boolean': case 'boolean':
$state = $request->{$field} == 'on' ? true : false; $state = $request->{$field} == 'on' ? true : false;
if($state != $news->{$field}) { if ($state != $news->{$field}) {
$news->{$field} = $state; $news->{$field} = $state;
$changed = true; $changed = true;
array_push($changedFields, $field); array_push($changedFields, $field);
@ -306,7 +324,7 @@ class AdminController extends Controller
case 'published': case 'published':
$state = $request->{$field} == 'on' ? true : false; $state = $request->{$field} == 'on' ? true : false;
$published = $news->published_at != null; $published = $news->published_at != null;
if($state != $published) { if ($state != $published) {
$news->published_at = $state ? now() : null; $news->published_at = $state ? now() : null;
$changed = true; $changed = true;
array_push($changedFields, $field); array_push($changedFields, $field);
@ -316,26 +334,26 @@ class AdminController extends Controller
} }
} }
if($changed) { if ($changed) {
$news->save(); $news->save();
} }
$redirect = $news->published_at ? $news->permalink() : $news->editUrl(); $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
return redirect($redirect); return redirect($redirect);
} }
public function newsroomStore(Request $request) public function newsroomStore(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'title' => 'required|string|min:1|max:100', 'title' => 'required|string|min:1|max:100',
'summary' => 'nullable|string|max:200', 'summary' => 'nullable|string|max:200',
'body' => 'nullable|string' 'body' => 'nullable|string',
]); ]);
$changed = false; $changed = false;
$changedFields = []; $changedFields = [];
$slug = str_slug($request->input('title')); $slug = str_slug($request->input('title'));
if(Newsroom::whereSlug($slug)->exists()) { if (Newsroom::whereSlug($slug)->exists()) {
$slug = $slug . '-' . str_random(4); $slug = $slug.'-'.str_random(4);
} }
$news = new Newsroom(); $news = new Newsroom();
$fields = [ $fields = [
@ -347,13 +365,13 @@ class AdminController extends Controller
'auth_only' => 'boolean', 'auth_only' => 'boolean',
'show_link' => 'boolean', 'show_link' => 'boolean',
'force_modal' => 'boolean', 'force_modal' => 'boolean',
'published' => 'published' 'published' => 'published',
]; ];
foreach($fields as $field => $type) { foreach ($fields as $field => $type) {
switch ($type) { switch ($type) {
case 'string': case 'string':
if($request->{$field} != $news->{$field}) { if ($request->{$field} != $news->{$field}) {
if($field == 'title') { if ($field == 'title') {
$news->slug = $slug; $news->slug = $slug;
} }
$news->{$field} = $request->{$field}; $news->{$field} = $request->{$field};
@ -364,7 +382,7 @@ class AdminController extends Controller
case 'boolean': case 'boolean':
$state = $request->{$field} == 'on' ? true : false; $state = $request->{$field} == 'on' ? true : false;
if($state != $news->{$field}) { if ($state != $news->{$field}) {
$news->{$field} = $state; $news->{$field} = $state;
$changed = true; $changed = true;
array_push($changedFields, $field); array_push($changedFields, $field);
@ -373,7 +391,7 @@ class AdminController extends Controller
case 'published': case 'published':
$state = $request->{$field} == 'on' ? true : false; $state = $request->{$field} == 'on' ? true : false;
$published = $news->published_at != null; $published = $news->published_at != null;
if($state != $published) { if ($state != $published) {
$news->published_at = $state ? now() : null; $news->published_at = $state ? now() : null;
$changed = true; $changed = true;
array_push($changedFields, $field); array_push($changedFields, $field);
@ -383,10 +401,11 @@ class AdminController extends Controller
} }
} }
if($changed) { if ($changed) {
$news->save(); $news->save();
} }
$redirect = $news->published_at ? $news->permalink() : $news->editUrl(); $redirect = $news->published_at ? $news->permalink() : $news->editUrl();
return redirect($redirect); return redirect($redirect);
} }
@ -398,18 +417,18 @@ class AdminController extends Controller
public function diagnosticsDecrypt(Request $request) public function diagnosticsDecrypt(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'payload' => 'required' 'payload' => 'required',
]); ]);
$key = 'exception_report:'; $key = 'exception_report:';
$decrypted = decrypt($request->input('payload')); $decrypted = decrypt($request->input('payload'));
if(!starts_with($decrypted, $key)) { if (! starts_with($decrypted, $key)) {
abort(403, 'Can only decrypt error diagnostics'); abort(403, 'Can only decrypt error diagnostics');
} }
$res = [ $res = [
'decrypted' => substr($decrypted, strlen($key)) 'decrypted' => substr($decrypted, strlen($key)),
]; ];
return response()->json($res); return response()->json($res);
@ -419,71 +438,74 @@ class AdminController extends Controller
{ {
$stories = Story::with('profile')->latest()->paginate(10); $stories = Story::with('profile')->latest()->paginate(10);
$stats = StoryService::adminStats(); $stats = StoryService::adminStats();
return view('admin.stories.home', compact('stories', 'stats')); return view('admin.stories.home', compact('stories', 'stats'));
} }
public function customEmojiHome(Request $request) public function customEmojiHome(Request $request)
{ {
if(!config('federation.custom_emoji.enabled')) { if (! (bool) config_cache('federation.custom_emoji.enabled')) {
return view('admin.custom-emoji.not-enabled'); return view('admin.custom-emoji.not-enabled');
} }
$this->validate($request, [ $this->validate($request, [
'sort' => 'sometimes|in:all,local,remote,duplicates,disabled,search' 'sort' => 'sometimes|in:all,local,remote,duplicates,disabled,search',
]); ]);
if($request->has('cc')) { if ($request->has('cc')) {
Cache::forget('pf:admin:custom_emoji:stats'); Cache::forget('pf:admin:custom_emoji:stats');
Cache::forget('pf:custom_emoji'); Cache::forget('pf:custom_emoji');
return redirect(route('admin.custom-emoji')); return redirect(route('admin.custom-emoji'));
} }
$sort = $request->input('sort') ?? 'all'; $sort = $request->input('sort') ?? 'all';
if($sort == 'search' && empty($request->input('q'))) { if ($sort == 'search' && empty($request->input('q'))) {
return redirect(route('admin.custom-emoji')); return redirect(route('admin.custom-emoji'));
} }
$pg = config('database.default') == 'pgsql'; $pg = config('database.default') == 'pgsql';
$emojis = CustomEmoji::when($sort, function($query, $sort) use($request, $pg) { $emojis = CustomEmoji::when($sort, function ($query, $sort) use ($request, $pg) {
if($sort == 'all') { if ($sort == 'all') {
if($pg) { if ($pg) {
return $query->latest(); return $query->latest();
} else { } else {
return $query->groupBy('shortcode')->latest(); return $query->groupBy('shortcode')->latest();
} }
} else if($sort == 'local') { } elseif ($sort == 'local') {
return $query->latest()->where('domain', '=', config('pixelfed.domain.app')); return $query->latest()->where('domain', '=', config('pixelfed.domain.app'));
} else if($sort == 'remote') { } elseif ($sort == 'remote') {
return $query->latest()->where('domain', '!=', config('pixelfed.domain.app')); return $query->latest()->where('domain', '!=', config('pixelfed.domain.app'));
} else if($sort == 'duplicates') { } elseif ($sort == 'duplicates') {
return $query->latest()->groupBy('shortcode')->havingRaw('count(*) > 1'); return $query->latest()->groupBy('shortcode')->havingRaw('count(*) > 1');
} else if($sort == 'disabled') { } elseif ($sort == 'disabled') {
return $query->latest()->whereDisabled(true); return $query->latest()->whereDisabled(true);
} else if($sort == 'search') { } elseif ($sort == 'search') {
$q = $query $q = $query
->latest() ->latest()
->where('shortcode', 'like', '%' . $request->input('q') . '%') ->where('shortcode', 'like', '%'.$request->input('q').'%')
->orWhere('domain', 'like', '%' . $request->input('q') . '%'); ->orWhere('domain', 'like', '%'.$request->input('q').'%');
if(!$request->has('dups')) { if (! $request->has('dups')) {
if(!$pg) { if (! $pg) {
$q = $q->groupBy('shortcode'); $q = $q->groupBy('shortcode');
} }
} }
return $q; return $q;
} }
}) })
->simplePaginate(10) ->simplePaginate(10)
->withQueryString(); ->withQueryString();
$stats = Cache::remember('pf:admin:custom_emoji:stats', 43200, function() use($pg) { $stats = Cache::remember('pf:admin:custom_emoji:stats', 43200, function () use ($pg) {
$res = [ $res = [
'total' => CustomEmoji::count(), 'total' => CustomEmoji::count(),
'active' => CustomEmoji::whereDisabled(false)->count(), 'active' => CustomEmoji::whereDisabled(false)->count(),
'remote' => CustomEmoji::where('domain', '!=', config('pixelfed.domain.app'))->count(), 'remote' => CustomEmoji::where('domain', '!=', config('pixelfed.domain.app'))->count(),
]; ];
if($pg) { if ($pg) {
$res['duplicate'] = CustomEmoji::select('shortcode')->groupBy('shortcode')->havingRaw('count(*) > 1')->count(); $res['duplicate'] = CustomEmoji::select('shortcode')->groupBy('shortcode')->havingRaw('count(*) > 1')->count();
} else { } else {
$res['duplicate'] = CustomEmoji::groupBy('shortcode')->havingRaw('count(*) > 1')->count(); $res['duplicate'] = CustomEmoji::groupBy('shortcode')->havingRaw('count(*) > 1')->count();
@ -497,24 +519,26 @@ class AdminController extends Controller
public function customEmojiToggleActive(Request $request, $id) public function customEmojiToggleActive(Request $request, $id)
{ {
abort_unless(config('federation.custom_emoji.enabled'), 404); abort_unless((bool) config_cache('federation.custom_emoji.enabled'), 404);
$emoji = CustomEmoji::findOrFail($id); $emoji = CustomEmoji::findOrFail($id);
$emoji->disabled = !$emoji->disabled; $emoji->disabled = ! $emoji->disabled;
$emoji->save(); $emoji->save();
$key = CustomEmoji::CACHE_KEY . str_replace(':', '', $emoji->shortcode); $key = CustomEmoji::CACHE_KEY.str_replace(':', '', $emoji->shortcode);
Cache::forget($key); Cache::forget($key);
return redirect()->back(); return redirect()->back();
} }
public function customEmojiAdd(Request $request) public function customEmojiAdd(Request $request)
{ {
abort_unless(config('federation.custom_emoji.enabled'), 404); abort_unless((bool) config_cache('federation.custom_emoji.enabled'), 404);
return view('admin.custom-emoji.add'); return view('admin.custom-emoji.add');
} }
public function customEmojiStore(Request $request) public function customEmojiStore(Request $request)
{ {
abort_unless(config('federation.custom_emoji.enabled'), 404); abort_unless((bool) config_cache('federation.custom_emoji.enabled'), 404);
$this->validate($request, [ $this->validate($request, [
'shortcode' => [ 'shortcode' => [
'required', 'required',
@ -522,12 +546,12 @@ class AdminController extends Controller
'max:80', 'max:80',
'starts_with::', 'starts_with::',
'ends_with::', 'ends_with::',
Rule::unique('custom_emoji')->where(function ($query) use($request) { Rule::unique('custom_emoji')->where(function ($query) use ($request) {
return $query->whereDomain(config('pixelfed.domain.app')) return $query->whereDomain(config('pixelfed.domain.app'))
->whereShortcode($request->input('shortcode')); ->whereShortcode($request->input('shortcode'));
}) }),
], ],
'emoji' => 'required|file|mimes:jpg,png|max:' . (config('federation.custom_emoji.max_size') / 1000) 'emoji' => 'required|file|mimes:jpg,png|max:'.(config('federation.custom_emoji.max_size') / 1000),
]); ]);
$emoji = new CustomEmoji; $emoji = new CustomEmoji;
@ -535,29 +559,32 @@ class AdminController extends Controller
$emoji->domain = config('pixelfed.domain.app'); $emoji->domain = config('pixelfed.domain.app');
$emoji->save(); $emoji->save();
$fileName = $emoji->id . '.' . $request->emoji->extension(); $fileName = $emoji->id.'.'.$request->emoji->extension();
$request->emoji->storePubliclyAs('public/emoji', $fileName); $request->emoji->storePubliclyAs('public/emoji', $fileName);
$emoji->media_path = 'emoji/' . $fileName; $emoji->media_path = 'emoji/'.$fileName;
$emoji->save(); $emoji->save();
Cache::forget('pf:custom_emoji'); Cache::forget('pf:custom_emoji');
return redirect(route('admin.custom-emoji')); return redirect(route('admin.custom-emoji'));
} }
public function customEmojiDelete(Request $request, $id) public function customEmojiDelete(Request $request, $id)
{ {
abort_unless(config('federation.custom_emoji.enabled'), 404); abort_unless((bool) config_cache('federation.custom_emoji.enabled'), 404);
$emoji = CustomEmoji::findOrFail($id); $emoji = CustomEmoji::findOrFail($id);
Storage::delete("public/{$emoji->media_path}"); Storage::delete("public/{$emoji->media_path}");
Cache::forget('pf:custom_emoji'); Cache::forget('pf:custom_emoji');
$emoji->delete(); $emoji->delete();
return redirect(route('admin.custom-emoji')); return redirect(route('admin.custom-emoji'));
} }
public function customEmojiShowDuplicates(Request $request, $id) public function customEmojiShowDuplicates(Request $request, $id)
{ {
abort_unless(config('federation.custom_emoji.enabled'), 404); abort_unless((bool) config_cache('federation.custom_emoji.enabled'), 404);
$emoji = CustomEmoji::orderBy('id')->whereDisabled(false)->whereShortcode($id)->firstOrFail(); $emoji = CustomEmoji::orderBy('id')->whereDisabled(false)->whereShortcode($id)->firstOrFail();
$emojis = CustomEmoji::whereShortcode($id)->where('id', '!=', $emoji->id)->cursorPaginate(10); $emojis = CustomEmoji::whereShortcode($id)->where('id', '!=', $emoji->id)->cursorPaginate(10);
return view('admin.custom-emoji.duplicates', compact('emoji', 'emojis')); return view('admin.custom-emoji.duplicates', compact('emoji', 'emojis'));
} }
} }

View file

@ -174,7 +174,7 @@ class AdminCuratedRegisterController extends Controller
public function apiMessageSendStore(Request $request, $id) public function apiMessageSendStore(Request $request, $id)
{ {
$this->validate($request, [ $this->validate($request, [
'message' => 'required|string|min:5|max:1000', 'message' => 'required|string|min:5|max:3000',
]); ]);
$record = CuratedRegister::findOrFail($id); $record = CuratedRegister::findOrFail($id);
abort_if($record->email_verified_at === null, 400, 'Cannot message an unverified email'); abort_if($record->email_verified_at === null, 400, 'Cannot message an unverified email');
@ -240,6 +240,11 @@ class AdminCuratedRegisterController extends Controller
$record->is_closed = true; $record->is_closed = true;
$record->action_taken_at = now(); $record->action_taken_at = now();
$record->save(); $record->save();
if (User::withTrashed()->whereEmail($record->email)->exists()) {
return [200];
}
$user = User::create([ $user = User::create([
'name' => $record->username, 'name' => $record->username,
'username' => $record->username, 'username' => $record->username,

View file

@ -2,45 +2,40 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use Illuminate\Http\Request; use App\AccountInterstitial;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Http\Resources\AdminInstance;
use App\Http\Resources\AdminUser;
use App\Instance;
use App\Jobs\DeletePipeline\DeleteAccountPipeline;
use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline;
use App\Jobs\StatusPipeline\StatusDelete; use App\Jobs\StatusPipeline\StatusDelete;
use Auth, Cache, DB;
use Carbon\Carbon;
use App\{
AccountInterstitial,
Instance,
Like,
Notification,
Media,
Profile,
Report,
Status,
User
};
use App\Models\Conversation; use App\Models\Conversation;
use App\Models\RemoteReport; use App\Models\RemoteReport;
use App\Notification;
use App\Profile;
use App\Report;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\AdminStatsService; use App\Services\AdminStatsService;
use App\Services\ConfigCacheService; use App\Services\ConfigCacheService;
use App\Services\InstanceService; use App\Services\InstanceService;
use App\Services\ModLogService; use App\Services\ModLogService;
use App\Services\SnowflakeService;
use App\Services\StatusService;
use App\Services\PublicTimelineService;
use App\Services\NetworkTimelineService; use App\Services\NetworkTimelineService;
use App\Services\NotificationService; use App\Services\NotificationService;
use App\Http\Resources\AdminInstance; use App\Services\PublicTimelineService;
use App\Http\Resources\AdminUser; use App\Services\SnowflakeService;
use App\Jobs\DeletePipeline\DeleteAccountPipeline; use App\Services\StatusService;
use App\Jobs\DeletePipeline\DeleteRemoteProfilePipeline; use App\Status;
use App\Jobs\DeletePipeline\DeleteRemoteStatusPipeline; use App\User;
use Cache;
use DB;
use Illuminate\Http\Request;
class AdminApiController extends Controller class AdminApiController extends Controller
{ {
public function supported(Request $request) public function supported(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
@ -50,7 +45,7 @@ class AdminApiController extends Controller
public function getStats(Request $request) public function getStats(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
@ -59,12 +54,13 @@ class AdminApiController extends Controller
$res['autospam_count'] = AccountInterstitial::whereType('post.autospam') $res['autospam_count'] = AccountInterstitial::whereType('post.autospam')
->whereNull('appeal_handled_at') ->whereNull('appeal_handled_at')
->count(); ->count();
return $res; return $res;
} }
public function autospam(Request $request) public function autospam(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
@ -73,26 +69,27 @@ class AdminApiController extends Controller
->whereNull('appeal_handled_at') ->whereNull('appeal_handled_at')
->latest() ->latest()
->simplePaginate(6) ->simplePaginate(6)
->map(function($report) { ->map(function ($report) {
$r = [ $r = [
'id' => $report->id, 'id' => $report->id,
'type' => $report->type, 'type' => $report->type,
'item_id' => $report->item_id, 'item_id' => $report->item_id,
'item_type' => $report->item_type, 'item_type' => $report->item_type,
'created_at' => $report->created_at 'created_at' => $report->created_at,
]; ];
if($report->item_type === 'App\\Status') { if ($report->item_type === 'App\\Status') {
$status = StatusService::get($report->item_id, false); $status = StatusService::get($report->item_id, false);
if(!$status) { if (! $status) {
return; return;
} }
$r['status'] = $status; $r['status'] = $status;
if($status['in_reply_to_id']) { if ($status['in_reply_to_id']) {
$r['parent'] = StatusService::get($status['in_reply_to_id'], false); $r['parent'] = StatusService::get($status['in_reply_to_id'], false);
} }
} }
return $r; return $r;
}); });
@ -101,14 +98,14 @@ class AdminApiController extends Controller
public function autospamHandle(Request $request) public function autospamHandle(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404); abort_unless($request->user()->tokenCan('admin:write'), 404);
$this->validate($request, [ $this->validate($request, [
'action' => 'required|in:dismiss,approve,dismiss-all,approve-all,delete-post,delete-account', 'action' => 'required|in:dismiss,approve,dismiss-all,approve-all,delete-post,delete-account',
'id' => 'required' 'id' => 'required',
]); ]);
$action = $request->input('action'); $action = $request->input('action');
@ -122,18 +119,19 @@ class AdminApiController extends Controller
$user = $appeal->user; $user = $appeal->user;
$profile = $user->profile; $profile = $user->profile;
if($action == 'dismiss') { if ($action == 'dismiss') {
$appeal->is_spam = true; $appeal->is_spam = true;
$appeal->appeal_handled_at = $now; $appeal->appeal_handled_at = $now;
$appeal->save(); $appeal->save();
Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $profile->id); Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$profile->id);
Cache::forget('pf:bouncer_v0:recent_by_pid:' . $profile->id); Cache::forget('pf:bouncer_v0:recent_by_pid:'.$profile->id);
Cache::forget('admin-dash:reports:spam-count'); Cache::forget('admin-dash:reports:spam-count');
return $res; return $res;
} }
if($action == 'delete-post') { if ($action == 'delete-post') {
$appeal->appeal_handled_at = now(); $appeal->appeal_handled_at = now();
$appeal->is_spam = true; $appeal->is_spam = true;
$appeal->save(); $appeal->save();
@ -148,10 +146,11 @@ class AdminApiController extends Controller
PublicTimelineService::deleteByProfileId($profile->id); PublicTimelineService::deleteByProfileId($profile->id);
StatusDelete::dispatch($appeal->status)->onQueue('high'); StatusDelete::dispatch($appeal->status)->onQueue('high');
Cache::forget('admin-dash:reports:spam-count'); Cache::forget('admin-dash:reports:spam-count');
return $res; return $res;
} }
if($action == 'delete-account') { if ($action == 'delete-account') {
abort_if($user->is_admin, 400, 'Cannot delete an admin account.'); abort_if($user->is_admin, 400, 'Cannot delete an admin account.');
$appeal->appeal_handled_at = now(); $appeal->appeal_handled_at = now();
$appeal->is_spam = true; $appeal->is_spam = true;
@ -167,22 +166,24 @@ class AdminApiController extends Controller
PublicTimelineService::deleteByProfileId($profile->id); PublicTimelineService::deleteByProfileId($profile->id);
DeleteAccountPipeline::dispatch($appeal->user)->onQueue('high'); DeleteAccountPipeline::dispatch($appeal->user)->onQueue('high');
Cache::forget('admin-dash:reports:spam-count'); Cache::forget('admin-dash:reports:spam-count');
return $res; return $res;
} }
if($action == 'dismiss-all') { if ($action == 'dismiss-all') {
AccountInterstitial::whereType('post.autospam') AccountInterstitial::whereType('post.autospam')
->whereItemType('App\Status') ->whereItemType('App\Status')
->whereNull('appeal_handled_at') ->whereNull('appeal_handled_at')
->whereUserId($appeal->user_id) ->whereUserId($appeal->user_id)
->update(['appeal_handled_at' => $now, 'is_spam' => true]); ->update(['appeal_handled_at' => $now, 'is_spam' => true]);
Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id); Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$appeal->user->profile_id);
Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id); Cache::forget('pf:bouncer_v0:recent_by_pid:'.$appeal->user->profile_id);
Cache::forget('admin-dash:reports:spam-count'); Cache::forget('admin-dash:reports:spam-count');
return $res; return $res;
} }
if($action == 'approve') { if ($action == 'approve') {
$status = $appeal->status; $status = $appeal->status;
$status->is_nsfw = $meta->is_nsfw; $status->is_nsfw = $meta->is_nsfw;
$status->scope = 'public'; $status->scope = 'public';
@ -198,29 +199,30 @@ class AdminApiController extends Controller
Notification::whereAction('autospam.warning') Notification::whereAction('autospam.warning')
->whereProfileId($appeal->user->profile_id) ->whereProfileId($appeal->user->profile_id)
->get() ->get()
->each(function($n) use($appeal) { ->each(function ($n) use ($appeal) {
NotificationService::del($appeal->user->profile_id, $n->id); NotificationService::del($appeal->user->profile_id, $n->id);
$n->forceDelete(); $n->forceDelete();
}); });
Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id); Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$appeal->user->profile_id);
Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id); Cache::forget('pf:bouncer_v0:recent_by_pid:'.$appeal->user->profile_id);
Cache::forget('admin-dash:reports:spam-count'); Cache::forget('admin-dash:reports:spam-count');
return $res; return $res;
} }
if($action == 'approve-all') { if ($action == 'approve-all') {
AccountInterstitial::whereType('post.autospam') AccountInterstitial::whereType('post.autospam')
->whereItemType('App\Status') ->whereItemType('App\Status')
->whereNull('appeal_handled_at') ->whereNull('appeal_handled_at')
->whereUserId($appeal->user_id) ->whereUserId($appeal->user_id)
->get() ->get()
->each(function($report) use($meta) { ->each(function ($report) use ($meta) {
$report->is_spam = false; $report->is_spam = false;
$report->appeal_handled_at = now(); $report->appeal_handled_at = now();
$report->save(); $report->save();
$status = Status::find($report->item_id); $status = Status::find($report->item_id);
if($status) { if ($status) {
$status->is_nsfw = $meta->is_nsfw; $status->is_nsfw = $meta->is_nsfw;
$status->scope = 'public'; $status->scope = 'public';
$status->visibility = 'public'; $status->visibility = 'public';
@ -231,14 +233,15 @@ class AdminApiController extends Controller
Notification::whereAction('autospam.warning') Notification::whereAction('autospam.warning')
->whereProfileId($report->user->profile_id) ->whereProfileId($report->user->profile_id)
->get() ->get()
->each(function($n) use($report) { ->each(function ($n) use ($report) {
NotificationService::del($report->user->profile_id, $n->id); NotificationService::del($report->user->profile_id, $n->id);
$n->forceDelete(); $n->forceDelete();
}); });
}); });
Cache::forget('pf:bouncer_v0:exemption_by_pid:' . $appeal->user->profile_id); Cache::forget('pf:bouncer_v0:exemption_by_pid:'.$appeal->user->profile_id);
Cache::forget('pf:bouncer_v0:recent_by_pid:' . $appeal->user->profile_id); Cache::forget('pf:bouncer_v0:recent_by_pid:'.$appeal->user->profile_id);
Cache::forget('admin-dash:reports:spam-count'); Cache::forget('admin-dash:reports:spam-count');
return $res; return $res;
} }
@ -247,44 +250,48 @@ class AdminApiController extends Controller
public function modReports(Request $request) public function modReports(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
$reports = Report::whereNull('admin_seen') $reports = Report::whereNull('admin_seen')
->orderBy('created_at','desc') ->orderBy('created_at', 'desc')
->paginate(6) ->paginate(6)
->map(function($report) { ->map(function ($report) {
$r = [ $r = [
'id' => $report->id, 'id' => $report->id,
'type' => $report->type, 'type' => $report->type,
'message' => $report->message, 'message' => $report->message,
'object_id' => $report->object_id, 'object_id' => $report->object_id,
'object_type' => $report->object_type, 'object_type' => $report->object_type,
'created_at' => $report->created_at 'created_at' => $report->created_at,
]; ];
if($report->profile_id) { if ($report->profile_id) {
$r['reported_by_account'] = AccountService::get($report->profile_id, true); $r['reported_by_account'] = AccountService::get($report->profile_id, true);
} }
if($report->object_type === 'App\\Status') { if ($report->object_type === 'App\\Status') {
$status = StatusService::get($report->object_id, false); $status = StatusService::get($report->object_id, false);
if(!$status) { if (! $status) {
return; return;
} }
$r['status'] = $status; $r['status'] = $status;
if($status['in_reply_to_id']) { if (isset($status['in_reply_to_id'])) {
$r['parent'] = StatusService::get($status['in_reply_to_id'], false); $r['parent'] = StatusService::get($status['in_reply_to_id'], false);
} }
} }
if($report->object_type === 'App\\Profile') { if ($report->object_type === 'App\\Profile') {
$r['account'] = AccountService::get($report->object_id, false); $acct = AccountService::get($report->object_id, true);
if ($acct) {
$r['account'] = $acct;
} }
}
return $r; return $r;
}) })
->filter() ->filter()
@ -295,14 +302,14 @@ class AdminApiController extends Controller
public function modReportHandle(Request $request) public function modReportHandle(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404); abort_unless($request->user()->tokenCan('admin:write'), 404);
$this->validate($request, [ $this->validate($request, [
'action' => 'required|string', 'action' => 'required|string',
'id' => 'required' 'id' => 'required',
]); ]);
$action = $request->input('action'); $action = $request->input('action');
@ -311,10 +318,10 @@ class AdminApiController extends Controller
$actions = [ $actions = [
'ignore', 'ignore',
'cw', 'cw',
'unlist' 'unlist',
]; ];
if (!in_array($action, $actions)) { if (! in_array($action, $actions)) {
return abort(403); return abort(403);
} }
@ -355,7 +362,7 @@ class AdminApiController extends Controller
public function getConfiguration(Request $request) public function getConfiguration(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
@ -366,42 +373,43 @@ class AdminApiController extends Controller
[ [
'name' => 'ActivityPub Federation', 'name' => 'ActivityPub Federation',
'description' => 'Enable activitypub federation support, compatible with Pixelfed, Mastodon and other platforms.', 'description' => 'Enable activitypub federation support, compatible with Pixelfed, Mastodon and other platforms.',
'key' => 'federation.activitypub.enabled' 'key' => 'federation.activitypub.enabled',
], ],
[ [
'name' => 'Open Registration', 'name' => 'Open Registration',
'description' => 'Allow new account registrations.', 'description' => 'Allow new account registrations.',
'key' => 'pixelfed.open_registration' 'key' => 'pixelfed.open_registration',
], ],
[ [
'name' => 'Stories', 'name' => 'Stories',
'description' => 'Enable the ephemeral Stories feature.', 'description' => 'Enable the ephemeral Stories feature.',
'key' => 'instance.stories.enabled' 'key' => 'instance.stories.enabled',
], ],
[ [
'name' => 'Require Email Verification', 'name' => 'Require Email Verification',
'description' => 'Require new accounts to verify their email address.', 'description' => 'Require new accounts to verify their email address.',
'key' => 'pixelfed.enforce_email_verification' 'key' => 'pixelfed.enforce_email_verification',
], ],
[ [
'name' => 'AutoSpam Detection', 'name' => 'AutoSpam Detection',
'description' => 'Detect and remove spam from public timelines.', 'description' => 'Detect and remove spam from public timelines.',
'key' => 'pixelfed.bouncer.enabled' 'key' => 'pixelfed.bouncer.enabled',
], ],
]) ])
->map(function($s) { ->map(function ($s) {
$s['state'] = (bool) config_cache($s['key']); $s['state'] = (bool) config_cache($s['key']);
return $s; return $s;
}); });
} }
public function updateConfiguration(Request $request) public function updateConfiguration(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404); abort_unless($request->user()->tokenCan('admin:write'), 404);
@ -410,7 +418,7 @@ class AdminApiController extends Controller
$this->validate($request, [ $this->validate($request, [
'key' => 'required', 'key' => 'required',
'value' => 'required' 'value' => 'required',
]); ]);
$allowedKeys = [ $allowedKeys = [
@ -423,7 +431,7 @@ class AdminApiController extends Controller
$key = $request->input('key'); $key = $request->input('key');
$value = (bool) filter_var($request->input('value'), FILTER_VALIDATE_BOOLEAN); $value = (bool) filter_var($request->input('value'), FILTER_VALIDATE_BOOLEAN);
abort_if(!in_array($key, $allowedKeys), 400, 'Invalid cache key.'); abort_if(! in_array($key, $allowedKeys), 400, 'Invalid cache key.');
ConfigCacheService::put($key, $value); ConfigCacheService::put($key, $value);
@ -431,42 +439,43 @@ class AdminApiController extends Controller
[ [
'name' => 'ActivityPub Federation', 'name' => 'ActivityPub Federation',
'description' => 'Enable activitypub federation support, compatible with Pixelfed, Mastodon and other platforms.', 'description' => 'Enable activitypub federation support, compatible with Pixelfed, Mastodon and other platforms.',
'key' => 'federation.activitypub.enabled' 'key' => 'federation.activitypub.enabled',
], ],
[ [
'name' => 'Open Registration', 'name' => 'Open Registration',
'description' => 'Allow new account registrations.', 'description' => 'Allow new account registrations.',
'key' => 'pixelfed.open_registration' 'key' => 'pixelfed.open_registration',
], ],
[ [
'name' => 'Stories', 'name' => 'Stories',
'description' => 'Enable the ephemeral Stories feature.', 'description' => 'Enable the ephemeral Stories feature.',
'key' => 'instance.stories.enabled' 'key' => 'instance.stories.enabled',
], ],
[ [
'name' => 'Require Email Verification', 'name' => 'Require Email Verification',
'description' => 'Require new accounts to verify their email address.', 'description' => 'Require new accounts to verify their email address.',
'key' => 'pixelfed.enforce_email_verification' 'key' => 'pixelfed.enforce_email_verification',
], ],
[ [
'name' => 'AutoSpam Detection', 'name' => 'AutoSpam Detection',
'description' => 'Detect and remove spam from public timelines.', 'description' => 'Detect and remove spam from public timelines.',
'key' => 'pixelfed.bouncer.enabled' 'key' => 'pixelfed.bouncer.enabled',
], ],
]) ])
->map(function($s) { ->map(function ($s) {
$s['state'] = (bool) config_cache($s['key']); $s['state'] = (bool) config_cache($s['key']);
return $s; return $s;
}); });
} }
public function getUsers(Request $request) public function getUsers(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
@ -477,27 +486,29 @@ class AdminApiController extends Controller
$q = $request->input('q'); $q = $request->input('q');
$sort = $request->input('sort', 'desc') === 'asc' ? 'asc' : 'desc'; $sort = $request->input('sort', 'desc') === 'asc' ? 'asc' : 'desc';
$res = User::whereNull('status') $res = User::whereNull('status')
->when($q, function($query, $q) { ->when($q, function ($query, $q) {
return $query->where('username', 'like', '%' . $q . '%'); return $query->where('username', 'like', '%'.$q.'%');
}) })
->orderBy('id', $sort) ->orderBy('id', $sort)
->cursorPaginate(10); ->cursorPaginate(10);
return AdminUser::collection($res); return AdminUser::collection($res);
} }
public function getUser(Request $request) public function getUser(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
$id = $request->input('user_id'); $id = $request->input('user_id');
$key = 'pf-admin-api:getUser:byId:' . $id; $key = 'pf-admin-api:getUser:byId:'.$id;
if($request->has('refresh')) { if ($request->has('refresh')) {
Cache::forget($key); Cache::forget($key);
} }
return Cache::remember($key, 86400, function() use($id) {
return Cache::remember($key, 86400, function () use ($id) {
$user = User::findOrFail($id); $user = User::findOrFail($id);
$profile = $user->profile; $profile = $user->profile;
$account = AccountService::get($user->profile_id, true); $account = AccountService::get($user->profile_id, true);
@ -510,8 +521,8 @@ class AdminApiController extends Controller
'moderation' => [ 'moderation' => [
'unlisted' => (bool) $profile->unlisted, 'unlisted' => (bool) $profile->unlisted,
'cw' => (bool) $profile->cw, 'cw' => (bool) $profile->cw,
'no_autolink' => (bool) $profile->no_autolink 'no_autolink' => (bool) $profile->no_autolink,
] ],
]]); ]]);
return $res; return $res;
@ -520,7 +531,7 @@ class AdminApiController extends Controller
public function userAdminAction(Request $request) public function userAdminAction(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404); abort_unless($request->user()->tokenCan('admin:write'), 404);
@ -528,7 +539,7 @@ class AdminApiController extends Controller
$this->validate($request, [ $this->validate($request, [
'id' => 'required', 'id' => 'required',
'action' => 'required|in:unlisted,cw,no_autolink,refresh_stats,verify_email,delete', 'action' => 'required|in:unlisted,cw,no_autolink,refresh_stats,verify_email,delete',
'value' => 'sometimes' 'value' => 'sometimes',
]); ]);
$id = $request->input('id'); $id = $request->input('id');
@ -538,8 +549,8 @@ class AdminApiController extends Controller
abort_if($user->is_admin == true && $action !== 'refresh_stats', 400, 'Cannot moderate admin accounts'); abort_if($user->is_admin == true && $action !== 'refresh_stats', 400, 'Cannot moderate admin accounts');
if($action === 'delete') { if ($action === 'delete') {
if(config('pixelfed.account_deletion') == false) { if (config('pixelfed.account_deletion') == false) {
abort(404); abort(404);
} }
@ -567,7 +578,7 @@ class AdminApiController extends Controller
PublicTimelineService::deleteByProfileId($profile->id); PublicTimelineService::deleteByProfileId($profile->id);
NetworkTimelineService::deleteByProfileId($profile->id); NetworkTimelineService::deleteByProfileId($profile->id);
if($profile->user_id) { if ($profile->user_id) {
DB::table('oauth_access_tokens')->whereUserId($user->id)->delete(); DB::table('oauth_access_tokens')->whereUserId($user->id)->delete();
DB::table('oauth_auth_codes')->whereUserId($user->id)->delete(); DB::table('oauth_auth_codes')->whereUserId($user->id)->delete();
$user->email = $user->id; $user->email = $user->id;
@ -586,11 +597,12 @@ class AdminApiController extends Controller
AccountService::del($profile->id); AccountService::del($profile->id);
DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('high'); DeleteRemoteProfilePipeline::dispatch($profile)->onQueue('high');
} }
return [ return [
'status' => 200, 'status' => 200,
'msg' => 'deleted', 'msg' => 'deleted',
]; ];
} else if($action === 'refresh_stats') { } elseif ($action === 'refresh_stats') {
$profile->following_count = DB::table('followers')->whereProfileId($user->profile_id)->count(); $profile->following_count = DB::table('followers')->whereProfileId($user->profile_id)->count();
$profile->followers_count = DB::table('followers')->whereFollowingId($user->profile_id)->count(); $profile->followers_count = DB::table('followers')->whereFollowingId($user->profile_id)->count();
$statusCount = Status::whereProfileId($user->profile_id) $statusCount = Status::whereProfileId($user->profile_id)
@ -600,7 +612,7 @@ class AdminApiController extends Controller
->count(); ->count();
$profile->status_count = $statusCount; $profile->status_count = $statusCount;
$profile->save(); $profile->save();
} else if($action === 'verify_email') { } elseif ($action === 'verify_email') {
$user->email_verified_at = now(); $user->email_verified_at = now();
$user->save(); $user->save();
@ -612,11 +624,11 @@ class AdminApiController extends Controller
->action('admin.user.moderate') ->action('admin.user.moderate')
->metadata([ ->metadata([
'action' => 'Manually verified email address', 'action' => 'Manually verified email address',
'message' => 'Success!' 'message' => 'Success!',
]) ])
->accessLevel('admin') ->accessLevel('admin')
->save(); ->save();
} else if($action === 'unlisted') { } elseif ($action === 'unlisted') {
ModLogService::boot() ModLogService::boot()
->objectUid($profile->id) ->objectUid($profile->id)
->objectId($profile->id) ->objectId($profile->id)
@ -625,13 +637,13 @@ class AdminApiController extends Controller
->action('admin.user.moderate') ->action('admin.user.moderate')
->metadata([ ->metadata([
'action' => $action, 'action' => $action,
'message' => 'Success!' 'message' => 'Success!',
]) ])
->accessLevel('admin') ->accessLevel('admin')
->save(); ->save();
$profile->unlisted = !$profile->unlisted; $profile->unlisted = ! $profile->unlisted;
$profile->save(); $profile->save();
} else if($action === 'cw') { } elseif ($action === 'cw') {
ModLogService::boot() ModLogService::boot()
->objectUid($profile->id) ->objectUid($profile->id)
->objectId($profile->id) ->objectId($profile->id)
@ -640,13 +652,13 @@ class AdminApiController extends Controller
->action('admin.user.moderate') ->action('admin.user.moderate')
->metadata([ ->metadata([
'action' => $action, 'action' => $action,
'message' => 'Success!' 'message' => 'Success!',
]) ])
->accessLevel('admin') ->accessLevel('admin')
->save(); ->save();
$profile->cw = !$profile->cw; $profile->cw = ! $profile->cw;
$profile->save(); $profile->save();
} else if($action === 'no_autolink') { } elseif ($action === 'no_autolink') {
ModLogService::boot() ModLogService::boot()
->objectUid($profile->id) ->objectUid($profile->id)
->objectId($profile->id) ->objectId($profile->id)
@ -655,11 +667,11 @@ class AdminApiController extends Controller
->action('admin.user.moderate') ->action('admin.user.moderate')
->metadata([ ->metadata([
'action' => $action, 'action' => $action,
'message' => 'Success!' 'message' => 'Success!',
]) ])
->accessLevel('admin') ->accessLevel('admin')
->save(); ->save();
$profile->no_autolink = !$profile->no_autolink; $profile->no_autolink = ! $profile->no_autolink;
$profile->save(); $profile->save();
} else { } else {
$profile->{$action} = filter_var($request->input('value'), FILTER_VALIDATE_BOOLEAN); $profile->{$action} = filter_var($request->input('value'), FILTER_VALIDATE_BOOLEAN);
@ -673,7 +685,7 @@ class AdminApiController extends Controller
->action('admin.user.moderate') ->action('admin.user.moderate')
->metadata([ ->metadata([
'action' => $action, 'action' => $action,
'message' => 'Success!' 'message' => 'Success!',
]) ])
->accessLevel('admin') ->accessLevel('admin')
->save(); ->save();
@ -687,14 +699,14 @@ class AdminApiController extends Controller
'moderation' => [ 'moderation' => [
'unlisted' => (bool) $profile->unlisted, 'unlisted' => (bool) $profile->unlisted,
'cw' => (bool) $profile->cw, 'cw' => (bool) $profile->cw,
'no_autolink' => (bool) $profile->no_autolink 'no_autolink' => (bool) $profile->no_autolink,
] ],
]]); ]]);
} }
public function instances(Request $request) public function instances(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404); abort_unless($request->user()->tokenCan('admin:write'), 404);
@ -711,19 +723,19 @@ class AdminApiController extends Controller
$sortBy = $request->input('sort_by', 'id'); $sortBy = $request->input('sort_by', 'id');
$filter = $request->input('filter'); $filter = $request->input('filter');
$res = Instance::when($q, function($query, $q) { $res = Instance::when($q, function ($query, $q) {
return $query->where('domain', 'like', '%' . $q . '%'); return $query->where('domain', 'like', '%'.$q.'%');
}) })
->when($filter, function($query, $filter) { ->when($filter, function ($query, $filter) {
if($filter === 'all') { if ($filter === 'all') {
return $query; return $query;
} else { } else {
return $query->where($filter, true); return $query->where($filter, true);
} }
}) })
->when($sortBy, function($query, $sortBy) use($sort) { ->when($sortBy, function ($query, $sortBy) use ($sort) {
return $query->orderBy($sortBy, $sort); return $query->orderBy($sortBy, $sort);
}, function($query) { }, function ($query) {
return $query->orderBy('id', 'desc'); return $query->orderBy('id', 'desc');
}) })
->cursorPaginate(10) ->cursorPaginate(10)
@ -734,7 +746,7 @@ class AdminApiController extends Controller
public function getInstance(Request $request) public function getInstance(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
@ -747,7 +759,7 @@ class AdminApiController extends Controller
public function moderateInstance(Request $request) public function moderateInstance(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404); abort_unless($request->user()->tokenCan('admin:write'), 404);
@ -755,7 +767,7 @@ class AdminApiController extends Controller
$this->validate($request, [ $this->validate($request, [
'id' => 'required', 'id' => 'required',
'key' => 'required|in:unlisted,auto_cw,banned', 'key' => 'required|in:unlisted,auto_cw,banned',
'value' => 'required' 'value' => 'required',
]); ]);
$id = $request->input('id'); $id = $request->input('id');
@ -773,7 +785,7 @@ class AdminApiController extends Controller
public function refreshInstanceStats(Request $request) public function refreshInstanceStats(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin == 1, 404); abort_unless($request->user()->is_admin == 1, 404);
abort_unless($request->user()->tokenCan('admin:write'), 404); abort_unless($request->user()->tokenCan('admin:write'), 404);
@ -793,51 +805,51 @@ class AdminApiController extends Controller
public function getAllStats(Request $request) public function getAllStats(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 404); abort_if(! $request->user() || ! $request->user()->token(), 404);
abort_unless($request->user()->is_admin === 1, 404); abort_unless($request->user()->is_admin === 1, 404);
abort_unless($request->user()->tokenCan('admin:read'), 404); abort_unless($request->user()->tokenCan('admin:read'), 404);
if($request->has('refresh')) { if ($request->has('refresh')) {
Cache::forget('admin-api:instance-all-stats-v1'); Cache::forget('admin-api:instance-all-stats-v1');
} }
return Cache::remember('admin-api:instance-all-stats-v1', 1209600, function() { return Cache::remember('admin-api:instance-all-stats-v1', 1209600, function () {
$days = range(1, 7); $days = range(1, 7);
$res = [ $res = [
'cached_at' => now()->format('c'), 'cached_at' => now()->format('c'),
]; ];
$minStatusId = SnowflakeService::byDate(now()->subDays(7)); $minStatusId = SnowflakeService::byDate(now()->subDays(7));
foreach($days as $day) { foreach ($days as $day) {
$label = now()->subDays($day)->format('D'); $label = now()->subDays($day)->format('D');
$labelShort = substr($label, 0, 1); $labelShort = substr($label, 0, 1);
$res['users']['days'][] = [ $res['users']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'), 'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label, 'label_full' => $label,
'label' => $labelShort, 'label' => $labelShort,
'count' => User::whereDate('created_at', now()->subDays($day))->count() 'count' => User::whereDate('created_at', now()->subDays($day))->count(),
]; ];
$res['posts']['days'][] = [ $res['posts']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'), 'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label, 'label_full' => $label,
'label' => $labelShort, 'label' => $labelShort,
'count' => Status::whereNull('uri')->where('id', '>', $minStatusId)->whereDate('created_at', now()->subDays($day))->count() 'count' => Status::whereNull('uri')->where('id', '>', $minStatusId)->whereDate('created_at', now()->subDays($day))->count(),
]; ];
$res['instances']['days'][] = [ $res['instances']['days'][] = [
'date' => now()->subDays($day)->format('M j Y'), 'date' => now()->subDays($day)->format('M j Y'),
'label_full' => $label, 'label_full' => $label,
'label' => $labelShort, 'label' => $labelShort,
'count' => Instance::whereDate('created_at', now()->subDays($day))->count() 'count' => Instance::whereDate('created_at', now()->subDays($day))->count(),
]; ];
} }
$res['users']['total'] = DB::table('users')->count(); $res['users']['total'] = DB::table('users')->count();
$res['users']['min'] = collect($res['users']['days'])->min('count'); $res['users']['min'] = collect($res['users']['days'])->min('count');
$res['users']['max'] = collect($res['users']['days'])->max('count'); $res['users']['max'] = collect($res['users']['days'])->max('count');
$res['users']['change'] = collect($res['users']['days'])->sum('count');; $res['users']['change'] = collect($res['users']['days'])->sum('count');
$res['posts']['total'] = DB::table('statuses')->whereNull('uri')->count(); $res['posts']['total'] = DB::table('statuses')->whereNull('uri')->count();
$res['posts']['min'] = collect($res['posts']['days'])->min('count'); $res['posts']['min'] = collect($res['posts']['days'])->min('count');
$res['posts']['max'] = collect($res['posts']['days'])->max('count'); $res['posts']['max'] = collect($res['posts']['days'])->max('count');

View file

@ -60,6 +60,7 @@ use App\Services\SnowflakeService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Services\UserFilterService; use App\Services\UserFilterService;
use App\Services\UserRoleService; use App\Services\UserRoleService;
use App\Services\UserStorageService;
use App\Status; use App\Status;
use App\StatusHashtag; use App\StatusHashtag;
use App\Transformer\Api\Mastodon\v1\AccountTransformer; use App\Transformer\Api\Mastodon\v1\AccountTransformer;
@ -131,7 +132,7 @@ class ApiV1Controller extends Controller
*/ */
public function apps(Request $request) public function apps(Request $request)
{ {
abort_if(! config_cache('pixelfed.oauth_enabled'), 404); abort_if(! (bool) config_cache('pixelfed.oauth_enabled'), 404);
$this->validate($request, [ $this->validate($request, [
'client_name' => 'required', 'client_name' => 'required',
@ -193,6 +194,10 @@ class ApiV1Controller extends Controller
'fields' => [], 'fields' => [],
]; ];
if ($request->has(self::PF_API_ENTITY_KEY)) {
$res['settings'] = AccountService::getAccountSettings($user->profile_id);
}
return $this->json($res); return $this->json($res);
} }
@ -207,6 +212,7 @@ class ApiV1Controller extends Controller
abort_if(! $request->user() || ! $request->user()->token(), 403); abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403); abort_unless($request->user()->tokenCan('read'), 403);
$withInstanceMeta = $request->has('_wim');
$res = $request->has(self::PF_API_ENTITY_KEY) ? AccountService::get($id, true) : AccountService::getMastodon($id, true); $res = $request->has(self::PF_API_ENTITY_KEY) ? AccountService::get($id, true) : AccountService::getMastodon($id, true);
if (! $res) { if (! $res) {
return response()->json(['error' => 'Record not found'], 404); return response()->json(['error' => 'Record not found'], 404);
@ -326,7 +332,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('locked')) { if ($request->has('locked')) {
$locked = $request->input('locked') == 'true'; $locked = $request->boolean('locked');
if ($profile->is_private != $locked) { if ($profile->is_private != $locked) {
$profile->is_private = $locked; $profile->is_private = $locked;
$changes = true; $changes = true;
@ -334,7 +340,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('reduce_motion')) { if ($request->has('reduce_motion')) {
$reduced = $request->input('reduce_motion'); $reduced = $request->boolean('reduce_motion');
if ($settings->reduce_motion != $reduced) { if ($settings->reduce_motion != $reduced) {
$settings->reduce_motion = $reduced; $settings->reduce_motion = $reduced;
$changes = true; $changes = true;
@ -342,7 +348,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('high_contrast_mode')) { if ($request->has('high_contrast_mode')) {
$contrast = $request->input('high_contrast_mode'); $contrast = $request->boolean('high_contrast_mode');
if ($settings->high_contrast_mode != $contrast) { if ($settings->high_contrast_mode != $contrast) {
$settings->high_contrast_mode = $contrast; $settings->high_contrast_mode = $contrast;
$changes = true; $changes = true;
@ -350,7 +356,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('video_autoplay')) { if ($request->has('video_autoplay')) {
$autoplay = $request->input('video_autoplay'); $autoplay = $request->boolean('video_autoplay');
if ($settings->video_autoplay != $autoplay) { if ($settings->video_autoplay != $autoplay) {
$settings->video_autoplay = $autoplay; $settings->video_autoplay = $autoplay;
$changes = true; $changes = true;
@ -370,7 +376,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('media_descriptions')) { if ($request->has('media_descriptions')) {
$md = $request->input('media_descriptions') == true; $md = $request->boolean('media_descriptions');
if ($composeSettings['media_descriptions'] != $md) { if ($composeSettings['media_descriptions'] != $md) {
$composeSettings['media_descriptions'] = $md; $composeSettings['media_descriptions'] = $md;
$changes = true; $changes = true;
@ -378,7 +384,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('crawlable')) { if ($request->has('crawlable')) {
$crawlable = $request->input('crawlable'); $crawlable = $request->boolean('crawlable');
if ($settings->crawlable != $crawlable) { if ($settings->crawlable != $crawlable) {
$settings->crawlable = $crawlable; $settings->crawlable = $crawlable;
$changes = true; $changes = true;
@ -386,7 +392,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('show_profile_follower_count')) { if ($request->has('show_profile_follower_count')) {
$show_profile_follower_count = $request->input('show_profile_follower_count'); $show_profile_follower_count = $request->boolean('show_profile_follower_count');
if ($settings->show_profile_follower_count != $show_profile_follower_count) { if ($settings->show_profile_follower_count != $show_profile_follower_count) {
$settings->show_profile_follower_count = $show_profile_follower_count; $settings->show_profile_follower_count = $show_profile_follower_count;
$changes = true; $changes = true;
@ -395,7 +401,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('show_profile_following_count')) { if ($request->has('show_profile_following_count')) {
$show_profile_following_count = $request->input('show_profile_following_count'); $show_profile_following_count = $request->boolean('show_profile_following_count');
if ($settings->show_profile_following_count != $show_profile_following_count) { if ($settings->show_profile_following_count != $show_profile_following_count) {
$settings->show_profile_following_count = $show_profile_following_count; $settings->show_profile_following_count = $show_profile_following_count;
$changes = true; $changes = true;
@ -404,7 +410,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('public_dm')) { if ($request->has('public_dm')) {
$public_dm = $request->input('public_dm'); $public_dm = $request->boolean('public_dm');
if ($settings->public_dm != $public_dm) { if ($settings->public_dm != $public_dm) {
$settings->public_dm = $public_dm; $settings->public_dm = $public_dm;
$changes = true; $changes = true;
@ -422,7 +428,7 @@ class ApiV1Controller extends Controller
} }
if ($request->has('disable_embeds')) { if ($request->has('disable_embeds')) {
$disabledEmbeds = $request->input('disable_embeds'); $disabledEmbeds = $request->boolean('disable_embeds');
if ($other['disable_embeds'] != $disabledEmbeds) { if ($other['disable_embeds'] != $disabledEmbeds) {
$other['disable_embeds'] = $disabledEmbeds; $other['disable_embeds'] = $disabledEmbeds;
$changes = true; $changes = true;
@ -441,8 +447,16 @@ class ApiV1Controller extends Controller
Cache::forget('profile:following_count:'.$profile->id); Cache::forget('profile:following_count:'.$profile->id);
Cache::forget('profile:embed:'.$profile->id); Cache::forget('profile:embed:'.$profile->id);
Cache::forget('profile:compose:settings:'.$user->id); Cache::forget('profile:compose:settings:'.$user->id);
Cache::forget('profile:view:'.$user->username); Cache::forget('profile:view:'.$profile->username);
Cache::forget('profile:atom:enabled:'.$profile->id);
Cache::forget('pfc:cached-user:wt:'.strtolower($profile->username));
Cache::forget('pfc:cached-user:wot:'.strtolower($profile->username));
Cache::forget('pf:acct:settings:hidden-followers:'.$profile->id);
Cache::forget('pf:acct:settings:hidden-following:'.$profile->id);
Cache::forget('pf:acct-trans:hideFollowing:'.$profile->id);
Cache::forget('pf:acct-trans:hideFollowers:'.$profile->id);
AccountService::del($user->profile_id); AccountService::del($user->profile_id);
AccountService::forgetAccountSettings($profile->id);
} }
if ($syncLicenses && $licenseChanged) { if ($syncLicenses && $licenseChanged) {
@ -740,7 +754,15 @@ class ApiV1Controller extends Controller
$dir = $min_id ? '>' : '<'; $dir = $min_id ? '>' : '<';
$id = $min_id ?? $max_id; $id = $min_id ?? $max_id;
$res = Status::whereProfileId($profile['id']) $res = Status::select(
'profile_id',
'in_reply_to_id',
'reblog_of_id',
'type',
'id',
'scope'
)
->whereProfileId($profile['id'])
->whereNull('in_reply_to_id') ->whereNull('in_reply_to_id')
->whereNull('reblog_of_id') ->whereNull('reblog_of_id')
->whereIn('type', $scope) ->whereIn('type', $scope)
@ -960,10 +982,22 @@ class ApiV1Controller extends Controller
$napi = $request->has(self::PF_API_ENTITY_KEY); $napi = $request->has(self::PF_API_ENTITY_KEY);
$pid = $request->user()->profile_id ?? $request->user()->profile->id; $pid = $request->user()->profile_id ?? $request->user()->profile->id;
$res = collect($ids) $res = collect($ids)
->filter(function ($id) use ($pid) {
return intval($id) !== intval($pid);
})
->map(function ($id) use ($pid, $napi) { ->map(function ($id) use ($pid, $napi) {
if (intval($id) === intval($pid)) {
return [
'id' => $id,
'following' => false,
'followed_by' => false,
'blocking' => false,
'muting' => false,
'muting_notifications' => false,
'requested' => false,
'domain_blocking' => false,
'showing_reblogs' => false,
'endorsed' => false,
];
}
return $napi ? return $napi ?
RelationshipService::getWithDate($pid, $id) : RelationshipService::getWithDate($pid, $id) :
RelationshipService::get($pid, $id); RelationshipService::get($pid, $id);
@ -1103,7 +1137,7 @@ class ApiV1Controller extends Controller
} }
$count = UserFilterService::blockCount($pid); $count = UserFilterService::blockCount($pid);
$maxLimit = intval(config('instance.user_filters.max_user_blocks')); $maxLimit = (int) config_cache('instance.user_filters.max_user_blocks');
if ($count == 0) { if ($count == 0) {
$filterCount = UserFilter::whereUserId($pid) $filterCount = UserFilter::whereUserId($pid)
->whereFilterType('block') ->whereFilterType('block')
@ -1200,8 +1234,8 @@ class ApiV1Controller extends Controller
if ($filter) { if ($filter) {
$filter->delete(); $filter->delete();
UserFilterService::unblock($pid, $profile->id); UserFilterService::unblock($pid, $profile->id);
RelationshipService::refresh($pid, $id);
} }
RelationshipService::refresh($pid, $id);
$resource = new Fractal\Resource\Item($profile, new RelationshipTransformer()); $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
@ -1301,12 +1335,17 @@ class ApiV1Controller extends Controller
if ($res->count()) { if ($res->count()) {
$ids = $res->map(function ($status) { $ids = $res->map(function ($status) {
return $status['like_id']; return $status['like_id'];
}); })->filter();
$max = $ids->max();
$min = $ids->min(); $max = $ids->min() - 1;
$min = $ids->max();
$baseUrl = config('app.url').'/api/v1/favourites?limit='.$limit.'&'; $baseUrl = config('app.url').'/api/v1/favourites?limit='.$limit.'&';
if ($maxId) {
$link = '<'.$baseUrl.'max_id='.$max.'>; rel="next",<'.$baseUrl.'min_id='.$min.'>; rel="prev"'; $link = '<'.$baseUrl.'max_id='.$max.'>; rel="next",<'.$baseUrl.'min_id='.$min.'>; rel="prev"';
} else {
$link = '<'.$baseUrl.'max_id='.$max.'>; rel="next"';
}
return $this->json($res, 200, ['Link' => $link]); return $this->json($res, 200, ['Link' => $link]);
} else { } else {
@ -1328,7 +1367,8 @@ class ApiV1Controller extends Controller
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action');
$status = StatusService::getMastodon($id, false); $napi = $request->has(self::PF_API_ENTITY_KEY);
$status = $napi ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
abort_unless($status, 404); abort_unless($status, 404);
@ -1396,34 +1436,47 @@ class ApiV1Controller extends Controller
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-like', $user->id), 403, 'Invalid permissions for this action');
$napi = $request->has(self::PF_API_ENTITY_KEY);
$status = $napi ? StatusService::get($id, false) : StatusService::getMastodon($id, false);
abort_unless($status && isset($status['account']), 404);
if ($status && isset($status['account'], $status['account']['acct']) && strpos($status['account']['acct'], '@') != -1) {
$domain = parse_url($status['account']['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
}
$spid = $status['account']['id'];
AccountService::setLastActive($user->id); AccountService::setLastActive($user->id);
$status = Status::findOrFail($id); if (intval($spid) !== intval($user->profile_id)) {
if ($status['visibility'] == 'private') {
if (intval($status->profile_id) !== intval($user->profile_id)) { abort_if(! FollowerService::follows($user->profile_id, $spid), 403);
if ($status->scope == 'private') {
abort_if(! $status->profile->followedBy($user->profile), 403);
} else { } else {
abort_if(! in_array($status->scope, ['public', 'unlisted']), 403); abort_if(! in_array($status['visibility'], ['public', 'unlisted']), 403);
} }
} }
$like = Like::whereProfileId($user->profile_id) $like = Like::whereProfileId($user->profile_id)
->whereStatusId($status->id) ->whereStatusId($status['id'])
->first(); ->first();
if ($like) { if ($like) {
$like->forceDelete(); $like->forceDelete();
$status->likes_count = $status->likes_count > 1 ? $status->likes_count - 1 : 0; $ogStatus = Status::find($status['id']);
$status->save(); if ($ogStatus) {
$ogStatus->likes_count = $ogStatus->likes_count > 1 ? $ogStatus->likes_count - 1 : 0;
$ogStatus->save();
}
} }
StatusService::del($status->id); StatusService::del($status['id']);
$res = StatusService::getMastodon($status->id, false); $status['favourited'] = false;
$res['favourited'] = false; $status['favourites_count'] = isset($ogStatus) ? $ogStatus->likes_count : $status['favourites_count'] - 1;
return $this->json($res); return $this->json($status);
} }
/** /**
@ -1611,7 +1664,7 @@ class ApiV1Controller extends Controller
$stats = Cache::remember('api:v1:instance-data:stats', 43200, function () { $stats = Cache::remember('api:v1:instance-data:stats', 43200, function () {
return [ return [
'user_count' => User::count(), 'user_count' => User::count(),
'status_count' => Status::whereNull('uri')->count(), 'status_count' => StatusService::totalLocalStatuses(),
'domain_count' => Instance::count(), 'domain_count' => Instance::count(),
]; ];
}); });
@ -1632,7 +1685,7 @@ class ApiV1Controller extends Controller
return [ return [
'uri' => config('pixelfed.domain.app'), 'uri' => config('pixelfed.domain.app'),
'title' => config('app.name'), 'title' => config_cache('app.name'),
'short_description' => config_cache('app.short_description'), 'short_description' => config_cache('app.short_description'),
'description' => config_cache('app.description'), 'description' => config_cache('app.description'),
'email' => config('instance.email'), 'email' => config('instance.email'),
@ -1651,7 +1704,7 @@ class ApiV1Controller extends Controller
'media_attachments' => [ 'media_attachments' => [
'image_matrix_limit' => 16777216, 'image_matrix_limit' => 16777216,
'image_size_limit' => config_cache('pixelfed.max_photo_size') * 1024, 'image_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,
'supported_mime_types' => explode(',', config('pixelfed.media_types')), 'supported_mime_types' => explode(',', config_cache('pixelfed.media_types')),
'video_frame_rate_limit' => 120, 'video_frame_rate_limit' => 120,
'video_matrix_limit' => 2304000, 'video_matrix_limit' => 2304000,
'video_size_limit' => config_cache('pixelfed.max_photo_size') * 1024, 'video_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,
@ -1665,7 +1718,7 @@ class ApiV1Controller extends Controller
'statuses' => [ 'statuses' => [
'characters_reserved_per_url' => 23, 'characters_reserved_per_url' => 23,
'max_characters' => (int) config_cache('pixelfed.max_caption_length'), 'max_characters' => (int) config_cache('pixelfed.max_caption_length'),
'max_media_attachments' => (int) config('pixelfed.max_album_length'), 'max_media_attachments' => (int) config_cache('pixelfed.max_album_length'),
], ],
], ],
]; ];
@ -1754,12 +1807,16 @@ class ApiV1Controller extends Controller
$profile = $user->profile; $profile = $user->profile;
if (config_cache('pixelfed.enforce_account_limit') == true) { $accountSize = UserStorageService::get($user->id);
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function () use ($user) { abort_if($accountSize === -1, 403, 'Invalid request.');
return Media::whereUserId($user->id)->sum('size') / 1000; $photo = $request->file('file');
}); $fileSize = $photo->getSize();
$sizeInKbs = (int) ceil($fileSize / 1000);
$updatedAccountSize = (int) $accountSize + (int) $sizeInKbs;
if ((bool) config_cache('pixelfed.enforce_account_limit') == true) {
$limit = (int) config_cache('pixelfed.max_account_size'); $limit = (int) config_cache('pixelfed.max_account_size');
if ($size >= $limit) { if ($updatedAccountSize >= $limit) {
abort(403, 'Account size limit reached.'); abort(403, 'Account size limit reached.');
} }
} }
@ -1767,8 +1824,6 @@ class ApiV1Controller extends Controller
$filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null; $filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
$filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null; $filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
$photo = $request->file('file');
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if (in_array($photo->getMimeType(), $mimes) == false) { if (in_array($photo->getMimeType(), $mimes) == false) {
abort(403, 'Invalid or unsupported mime type.'); abort(403, 'Invalid or unsupported mime type.');
@ -1831,6 +1886,10 @@ class ApiV1Controller extends Controller
break; break;
} }
$user->storage_used = (int) $updatedAccountSize;
$user->storage_used_updated_at = now();
$user->save();
Cache::forget($limitKey); Cache::forget($limitKey);
$resource = new Fractal\Resource\Item($media, new MediaTransformer()); $resource = new Fractal\Resource\Item($media, new MediaTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
@ -1971,12 +2030,16 @@ class ApiV1Controller extends Controller
$profile = $user->profile; $profile = $user->profile;
if (config_cache('pixelfed.enforce_account_limit') == true) { $accountSize = UserStorageService::get($user->id);
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function () use ($user) { abort_if($accountSize === -1, 403, 'Invalid request.');
return Media::whereUserId($user->id)->sum('size') / 1000; $photo = $request->file('file');
}); $fileSize = $photo->getSize();
$sizeInKbs = (int) ceil($fileSize / 1000);
$updatedAccountSize = (int) $accountSize + (int) $sizeInKbs;
if ((bool) config_cache('pixelfed.enforce_account_limit') == true) {
$limit = (int) config_cache('pixelfed.max_account_size'); $limit = (int) config_cache('pixelfed.max_account_size');
if ($size >= $limit) { if ($updatedAccountSize >= $limit) {
abort(403, 'Account size limit reached.'); abort(403, 'Account size limit reached.');
} }
} }
@ -1984,8 +2047,6 @@ class ApiV1Controller extends Controller
$filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null; $filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
$filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null; $filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
$photo = $request->file('file');
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if (in_array($photo->getMimeType(), $mimes) == false) { if (in_array($photo->getMimeType(), $mimes) == false) {
abort(403, 'Invalid or unsupported mime type.'); abort(403, 'Invalid or unsupported mime type.');
@ -2053,6 +2114,10 @@ class ApiV1Controller extends Controller
break; break;
} }
$user->storage_used = (int) $updatedAccountSize;
$user->storage_used_updated_at = now();
$user->save();
Cache::forget($limitKey); Cache::forget($limitKey);
$resource = new Fractal\Resource\Item($media, new MediaTransformer()); $resource = new Fractal\Resource\Item($media, new MediaTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
@ -2145,7 +2210,7 @@ class ApiV1Controller extends Controller
} }
$count = UserFilterService::muteCount($pid); $count = UserFilterService::muteCount($pid);
$maxLimit = intval(config('instance.user_filters.max_user_mutes')); $maxLimit = (int) config_cache('instance.user_filters.max_user_mutes');
if ($count == 0) { if ($count == 0) {
$filterCount = UserFilter::whereUserId($pid) $filterCount = UserFilter::whereUserId($pid)
->whereFilterType('mute') ->whereFilterType('mute')
@ -2207,9 +2272,10 @@ class ApiV1Controller extends Controller
if ($filter) { if ($filter) {
$filter->delete(); $filter->delete();
UserFilterService::unmute($pid, $profile->id); UserFilterService::unmute($pid, $profile->id);
RelationshipService::refresh($pid, $id);
} }
RelationshipService::refresh($pid, $id);
$resource = new Fractal\Resource\Item($profile, new RelationshipTransformer()); $resource = new Fractal\Resource\Item($profile, new RelationshipTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
@ -2233,14 +2299,17 @@ class ApiV1Controller extends Controller
'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX, 'since_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
'types[]' => 'sometimes|array', 'types[]' => 'sometimes|array',
'types[].*' => 'string|in:mention,reblog,follow,favourite',
'type' => 'sometimes|string|in:mention,reblog,follow,favourite', 'type' => 'sometimes|string|in:mention,reblog,follow,favourite',
'_pe' => 'sometimes', '_pe' => 'sometimes',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$limit = $request->input('limit', 20); $limit = $request->input('limit', 20);
$ogLimit = $request->input('limit', 20);
if ($limit > 40) { if ($limit > 40) {
$limit = 40; $limit = 40;
$ogLimit = 40;
} }
$since = $request->input('since_id'); $since = $request->input('since_id');
@ -2258,6 +2327,10 @@ class ApiV1Controller extends Controller
$types = $request->input('types'); $types = $request->input('types');
if ($request->has('types')) {
$limit = 150;
}
$maxId = null; $maxId = null;
$minId = null; $minId = null;
AccountService::setLastActive($request->user()->id); AccountService::setLastActive($request->user()->id);
@ -2280,7 +2353,12 @@ class ApiV1Controller extends Controller
} }
} }
$baseUrl = config('app.url').'/api/v1/notifications?limit='.$limit.'&'; if ($request->has('types')) {
$typesParams = collect($types)->implode('&types[]=');
$baseUrl = config('app.url').'/api/v1/notifications?types[]='.$typesParams.'&limit='.$ogLimit.'&';
} else {
$baseUrl = config('app.url').'/api/v1/notifications?limit='.$ogLimit.'&';
}
if ($minId == $maxId) { if ($minId == $maxId) {
$minId = null; $minId = null;
@ -2322,7 +2400,16 @@ class ApiV1Controller extends Controller
} }
return true; return true;
})->values(); })
->filter(function ($n) use ($types) {
if (! $types) {
return true;
}
return in_array($n['type'], $types);
})
->take($ogLimit)
->values();
if ($maxId) { if ($maxId) {
$link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"'; $link = '<'.$baseUrl.'max_id='.$minId.'>; rel="next"';
@ -3019,9 +3106,9 @@ class ApiV1Controller extends Controller
abort_unless($request->user()->tokenCan('read'), 403); abort_unless($request->user()->tokenCan('read'), 403);
$user = $request->user(); $user = $request->user();
AccountService::setLastActive($user->id);
$pid = $user->profile_id; $pid = $user->profile_id;
$status = StatusService::getMastodon($id, false); $status = StatusService::getMastodon($id, false);
$pe = $request->has(self::PF_API_ENTITY_KEY);
if (! $status || ! isset($status['account'])) { if (! $status || ! isset($status['account'])) {
return response('', 404); return response('', 404);
@ -3048,7 +3135,9 @@ class ApiV1Controller extends Controller
$descendants = []; $descendants = [];
if ($status['in_reply_to_id']) { if ($status['in_reply_to_id']) {
$ancestors[] = StatusService::getMastodon($status['in_reply_to_id'], false); $ancestors[] = $pe ?
StatusService::get($status['in_reply_to_id'], false) :
StatusService::getMastodon($status['in_reply_to_id'], false);
} }
if ($status['replies_count']) { if ($status['replies_count']) {
@ -3058,8 +3147,10 @@ class ApiV1Controller extends Controller
->where('in_reply_to_id', $id) ->where('in_reply_to_id', $id)
->limit(20) ->limit(20)
->pluck('id') ->pluck('id')
->map(function ($sid) { ->map(function ($sid) use ($pe) {
return StatusService::getMastodon($sid, false); return $pe ?
StatusService::get($sid, false) :
StatusService::getMastodon($sid, false);
}) })
->filter(function ($post) use ($filters) { ->filter(function ($post) use ($filters) {
return $post && isset($post['account'], $post['account']['id']) && ! in_array($post['account']['id'], $filters); return $post && isset($post['account'], $post['account']['id']) && ! in_array($post['account']['id'], $filters);
@ -3308,9 +3399,9 @@ class ApiV1Controller extends Controller
abort_unless($request->user()->tokenCan('write'), 403); abort_unless($request->user()->tokenCan('write'), 403);
$this->validate($request, [ $this->validate($request, [
'status' => 'nullable|string|max:' . config_cache('pixelfed.max_caption_length'), 'status' => 'nullable|string|max:'.(int) config_cache('pixelfed.max_caption_length'),
'in_reply_to_id' => 'nullable', 'in_reply_to_id' => 'nullable',
'media_ids' => 'sometimes|array|max:'.config_cache('pixelfed.max_album_length'), 'media_ids' => 'sometimes|array|max:'.(int) config_cache('pixelfed.max_album_length'),
'sensitive' => 'nullable', 'sensitive' => 'nullable',
'visibility' => 'string|in:private,unlisted,public', 'visibility' => 'string|in:private,unlisted,public',
'spoiler_text' => 'sometimes|max:140', 'spoiler_text' => 'sometimes|max:140',
@ -3360,10 +3451,9 @@ class ApiV1Controller extends Controller
$limitKey = 'compose:rate-limit:store:'.$user->id; $limitKey = 'compose:rate-limit:store:'.$user->id;
$limitTtl = now()->addMinutes(15); $limitTtl = now()->addMinutes(15);
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) { $limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$minId = SnowflakeService::byDate(now()->subDays(1));
$dailyLimit = Status::whereProfileId($user->profile_id) $dailyLimit = Status::whereProfileId($user->profile_id)
->whereNull('in_reply_to_id') ->where('id', '>', $minId)
->whereNull('reblog_of_id')
->where('created_at', '>', now()->subDays(1))
->count(); ->count();
return $dailyLimit >= 1000; return $dailyLimit >= 1000;
@ -3436,7 +3526,7 @@ class ApiV1Controller extends Controller
$mimes = []; $mimes = [];
foreach ($ids as $k => $v) { foreach ($ids as $k => $v) {
if ($k + 1 > config_cache('pixelfed.max_album_length')) { if ($k + 1 > (int) config_cache('pixelfed.max_album_length')) {
continue; continue;
} }
$m = Media::whereUserId($user->id)->whereNull('status_id')->findOrFail($v); $m = Media::whereUserId($user->id)->whereNull('status_id')->findOrFail($v);
@ -3712,7 +3802,6 @@ class ApiV1Controller extends Controller
} }
$res = StatusHashtag::whereHashtagId($tag->id) $res = StatusHashtag::whereHashtagId($tag->id)
->whereIn('status_visibility', ['public', 'private', 'unlisted'])
->where('status_id', $dir, $id) ->where('status_id', $dir, $id)
->orderBy('status_id', 'desc') ->orderBy('status_id', 'desc')
->limit(100) ->limit(100)
@ -3729,11 +3818,11 @@ class ApiV1Controller extends Controller
return false; return false;
} }
} }
if ($i['visibility'] === 'private') { // if ($i['visibility'] === 'private') {
if ((int) $i['account']['id'] !== $pid) { // if ((int) $i['account']['id'] !== $pid) {
return FollowerService::follows($pid, $i['account']['id'], true); // return FollowerService::follows($pid, $i['account']['id'], true);
} // }
} // }
if ($onlyMedia == true) { if ($onlyMedia == true) {
if (! isset($i['media_attachments']) || ! count($i['media_attachments'])) { if (! isset($i['media_attachments']) || ! count($i['media_attachments'])) {
return false; return false;
@ -4199,4 +4288,26 @@ class ApiV1Controller extends Controller
return $this->json([]); return $this->json([]);
} }
/**
* GET /api/v1/instance/peers
*
*
* @return array
*/
public function instancePeers(Request $request)
{
if ((bool) config('instance.show_peers') == false) {
return $this->json([]);
}
return $this->json(
Cache::remember(InstanceService::CACHE_KEY_API_PEERS_LIST, now()->addHours(24), function () {
return Instance::whereNotNull('nodeinfo_last_fetched')
->whereBanned(false)
->where('nodeinfo_last_fetched', '>', now()->subDays(8))
->pluck('domain');
})
);
}
} }

File diff suppressed because it is too large Load diff

View file

@ -2,44 +2,32 @@
namespace App\Http\Controllers\Api; namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
use App\Jobs\MediaPipeline\MediaDeletePipeline;
use App\Jobs\VideoPipeline\VideoThumbnail;
use App\Media; use App\Media;
use App\UserSetting;
use App\User;
use Illuminate\Support\Facades\Cache;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\BouncerService;
use App\Services\InstanceService; use App\Services\InstanceService;
use App\Services\MediaBlocklistService; use App\Services\MediaBlocklistService;
use App\Services\MediaPathService; use App\Services\MediaPathService;
use App\Services\SearchApiV2Service; use App\Services\SearchApiV2Service;
use App\Services\UserRoleService;
use App\Services\UserStorageService;
use App\Transformer\Api\Mastodon\v1\MediaTransformer;
use App\User;
use App\UserSetting;
use App\Util\Media\Filter; use App\Util\Media\Filter;
use App\Jobs\MediaPipeline\MediaDeletePipeline; use App\Util\Site\Nodeinfo;
use App\Jobs\VideoPipeline\{ use Illuminate\Http\Request;
VideoOptimize, use Illuminate\Support\Facades\Cache;
VideoPostProcess, use Illuminate\Support\Facades\Storage;
VideoThumbnail
};
use App\Jobs\ImageOptimizePipeline\ImageOptimize;
use League\Fractal; use League\Fractal;
use League\Fractal\Serializer\ArraySerializer; use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Transformer\Api\Mastodon\v1\{
AccountTransformer,
MediaTransformer,
NotificationTransformer,
StatusTransformer,
};
use App\Transformer\Api\{
RelationshipTransformer,
};
use App\Util\Site\Nodeinfo;
use App\Services\UserRoleService;
class ApiV2Controller extends Controller class ApiV2Controller extends Controller
{ {
const PF_API_ENTITY_KEY = "_pe"; const PF_API_ENTITY_KEY = '_pe';
public function json($res, $code = 200, $headers = []) public function json($res, $code = 200, $headers = [])
{ {
@ -49,10 +37,11 @@ class ApiV2Controller extends Controller
public function instance(Request $request) public function instance(Request $request)
{ {
$contact = Cache::remember('api:v1:instance-data:contact', 604800, function () { $contact = Cache::remember('api:v1:instance-data:contact', 604800, function () {
if(config_cache('instance.admin.pid')) { if (config_cache('instance.admin.pid')) {
return AccountService::getMastodon(config_cache('instance.admin.pid'), true); return AccountService::getMastodon(config_cache('instance.admin.pid'), true);
} }
$admin = User::whereIsAdmin(true)->first(); $admin = User::whereIsAdmin(true)->first();
return $admin && isset($admin->profile_id) ? return $admin && isset($admin->profile_id) ?
AccountService::getMastodon($admin->profile_id, true) : AccountService::getMastodon($admin->profile_id, true) :
null; null;
@ -61,41 +50,42 @@ class ApiV2Controller extends Controller
$rules = Cache::remember('api:v1:instance-data:rules', 604800, function () { $rules = Cache::remember('api:v1:instance-data:rules', 604800, function () {
return config_cache('app.rules') ? return config_cache('app.rules') ?
collect(json_decode(config_cache('app.rules'), true)) collect(json_decode(config_cache('app.rules'), true))
->map(function($rule, $key) { ->map(function ($rule, $key) {
$id = $key + 1; $id = $key + 1;
return [ return [
'id' => "{$id}", 'id' => "{$id}",
'text' => $rule 'text' => $rule,
]; ];
}) })
->toArray() : []; ->toArray() : [];
}); });
$res = Cache::remember('api:v2:instance-data-response-v2', 1800, function () use($contact, $rules) { $res = Cache::remember('api:v2:instance-data-response-v2', 1800, function () use ($contact, $rules) {
return [ return [
'domain' => config('pixelfed.domain.app'), 'domain' => config('pixelfed.domain.app'),
'title' => config_cache('app.name'), 'title' => config_cache('app.name'),
'version' => '3.5.3 (compatible; Pixelfed ' . config('pixelfed.version') .')', 'version' => '3.5.3 (compatible; Pixelfed '.config('pixelfed.version').')',
'source_url' => 'https://github.com/pixelfed/pixelfed', 'source_url' => 'https://github.com/pixelfed/pixelfed',
'description' => config_cache('app.short_description'), 'description' => config_cache('app.short_description'),
'usage' => [ 'usage' => [
'users' => [ 'users' => [
'active_month' => (int) Nodeinfo::activeUsersMonthly() 'active_month' => (int) Nodeinfo::activeUsersMonthly(),
] ],
], ],
'thumbnail' => [ 'thumbnail' => [
'url' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')), 'url' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')),
'blurhash' => InstanceService::headerBlurhash(), 'blurhash' => InstanceService::headerBlurhash(),
'versions' => [ 'versions' => [
'@1x' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')), '@1x' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')),
'@2x' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')) '@2x' => config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg')),
] ],
], ],
'languages' => [config('app.locale')], 'languages' => [config('app.locale')],
'configuration' => [ 'configuration' => [
'urls' => [ 'urls' => [
'streaming' => null, 'streaming' => null,
'status' => null 'status' => null,
], ],
'vapid' => [ 'vapid' => [
'public_key' => config('webpush.vapid.public_key'), 'public_key' => config('webpush.vapid.public_key'),
@ -106,7 +96,7 @@ class ApiV2Controller extends Controller
'statuses' => [ 'statuses' => [
'max_characters' => (int) config_cache('pixelfed.max_caption_length'), 'max_characters' => (int) config_cache('pixelfed.max_caption_length'),
'max_media_attachments' => (int) config_cache('pixelfed.max_album_length'), 'max_media_attachments' => (int) config_cache('pixelfed.max_album_length'),
'characters_reserved_per_url' => 23 'characters_reserved_per_url' => 23,
], ],
'media_attachments' => [ 'media_attachments' => [
'supported_mime_types' => explode(',', config_cache('pixelfed.media_types')), 'supported_mime_types' => explode(',', config_cache('pixelfed.media_types')),
@ -114,7 +104,7 @@ class ApiV2Controller extends Controller
'image_matrix_limit' => 3686400, 'image_matrix_limit' => 3686400,
'video_size_limit' => config_cache('pixelfed.max_photo_size') * 1024, 'video_size_limit' => config_cache('pixelfed.max_photo_size') * 1024,
'video_frame_rate_limit' => 240, 'video_frame_rate_limit' => 240,
'video_matrix_limit' => 3686400 'video_matrix_limit' => 3686400,
], ],
'polls' => [ 'polls' => [
'max_options' => 0, 'max_options' => 0,
@ -134,14 +124,15 @@ class ApiV2Controller extends Controller
], ],
'contact' => [ 'contact' => [
'email' => config('instance.email'), 'email' => config('instance.email'),
'account' => $contact 'account' => $contact,
], ],
'rules' => $rules 'rules' => $rules,
]; ];
}); });
$res['registrations']['enabled'] = (bool) config_cache('pixelfed.open_registration'); $res['registrations']['enabled'] = (bool) config_cache('pixelfed.open_registration');
$res['registrations']['approval_required'] = (bool) config_cache('instance.curated_registration.enabled'); $res['registrations']['approval_required'] = (bool) config_cache('instance.curated_registration.enabled');
return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES); return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
} }
@ -153,7 +144,7 @@ class ApiV2Controller extends Controller
*/ */
public function search(Request $request) public function search(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 403); abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('read'), 403); abort_unless($request->user()->tokenCan('read'), 403);
$this->validate($request, [ $this->validate($request, [
@ -166,18 +157,19 @@ class ApiV2Controller extends Controller
'resolve' => 'nullable', 'resolve' => 'nullable',
'limit' => 'nullable|integer|max:40', 'limit' => 'nullable|integer|max:40',
'offset' => 'nullable|integer', 'offset' => 'nullable|integer',
'following' => 'nullable' 'following' => 'nullable',
]); ]);
if($request->user()->has_roles && !UserRoleService::can('can-view-discover', $request->user()->id)) { if ($request->user()->has_roles && ! UserRoleService::can('can-view-discover', $request->user()->id)) {
return [ return [
'accounts' => [], 'accounts' => [],
'hashtags' => [], 'hashtags' => [],
'statuses' => [] 'statuses' => [],
]; ];
} }
$mastodonMode = !$request->has('_pe'); $mastodonMode = ! $request->has('_pe');
return $this->json(SearchApiV2Service::query($request, $mastodonMode)); return $this->json(SearchApiV2Service::query($request, $mastodonMode));
} }
@ -193,7 +185,7 @@ class ApiV2Controller extends Controller
'host' => config('broadcasting.connections.pusher.options.host'), 'host' => config('broadcasting.connections.pusher.options.host'),
'port' => config('broadcasting.connections.pusher.options.port'), 'port' => config('broadcasting.connections.pusher.options.port'),
'key' => config('broadcasting.connections.pusher.key'), 'key' => config('broadcasting.connections.pusher.key'),
'cluster' => config('broadcasting.connections.pusher.options.cluster') 'cluster' => config('broadcasting.connections.pusher.options.cluster'),
] : []; ] : [];
} }
@ -205,39 +197,39 @@ class ApiV2Controller extends Controller
*/ */
public function mediaUploadV2(Request $request) public function mediaUploadV2(Request $request)
{ {
abort_if(!$request->user() || !$request->user()->token(), 403); abort_if(! $request->user() || ! $request->user()->token(), 403);
abort_unless($request->user()->tokenCan('write'), 403); abort_unless($request->user()->tokenCan('write'), 403);
$this->validate($request, [ $this->validate($request, [
'file.*' => [ 'file.*' => [
'required_without:file', 'required_without:file',
'mimetypes:' . config_cache('pixelfed.media_types'), 'mimetypes:'.config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'), 'max:'.config_cache('pixelfed.max_photo_size'),
], ],
'file' => [ 'file' => [
'required_without:file.*', 'required_without:file.*',
'mimetypes:' . config_cache('pixelfed.media_types'), 'mimetypes:'.config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'), 'max:'.config_cache('pixelfed.max_photo_size'),
], ],
'filter_name' => 'nullable|string|max:24', 'filter_name' => 'nullable|string|max:24',
'filter_class' => 'nullable|alpha_dash|max:24', 'filter_class' => 'nullable|alpha_dash|max:24',
'description' => 'nullable|string|max:' . config_cache('pixelfed.max_altext_length'), 'description' => 'nullable|string|max:'.config_cache('pixelfed.max_altext_length'),
'replace_id' => 'sometimes' 'replace_id' => 'sometimes',
]); ]);
$user = $request->user(); $user = $request->user();
if($user->last_active_at == null) { if ($user->last_active_at == null) {
return []; return [];
} }
if(empty($request->file('file'))) { if (empty($request->file('file'))) {
return response('', 422); return response('', 422);
} }
$limitKey = 'compose:rate-limit:media-upload:' . $user->id; $limitKey = 'compose:rate-limit:media-upload:'.$user->id;
$limitTtl = now()->addMinutes(15); $limitTtl = now()->addMinutes(15);
$limitReached = Cache::remember($limitKey, $limitTtl, function() use($user) { $limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count(); $dailyLimit = Media::whereUserId($user->id)->where('created_at', '>', now()->subDays(1))->count();
return $dailyLimit >= 1250; return $dailyLimit >= 1250;
@ -246,12 +238,16 @@ class ApiV2Controller extends Controller
$profile = $user->profile; $profile = $user->profile;
if(config_cache('pixelfed.enforce_account_limit') == true) { $accountSize = UserStorageService::get($user->id);
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) { abort_if($accountSize === -1, 403, 'Invalid request.');
return Media::whereUserId($user->id)->sum('size') / 1000; $photo = $request->file('file');
}); $fileSize = $photo->getSize();
$sizeInKbs = (int) ceil($fileSize / 1000);
$updatedAccountSize = (int) $accountSize + (int) $sizeInKbs;
if ((bool) config_cache('pixelfed.enforce_account_limit') == true) {
$limit = (int) config_cache('pixelfed.max_account_size'); $limit = (int) config_cache('pixelfed.max_account_size');
if ($size >= $limit) { if ($updatedAccountSize >= $limit) {
abort(403, 'Account size limit reached.'); abort(403, 'Account size limit reached.');
} }
} }
@ -259,10 +255,8 @@ class ApiV2Controller extends Controller
$filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null; $filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
$filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null; $filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
$photo = $request->file('file');
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if(in_array($photo->getMimeType(), $mimes) == false) { if (in_array($photo->getMimeType(), $mimes) == false) {
abort(403, 'Invalid or unsupported mime type.'); abort(403, 'Invalid or unsupported mime type.');
} }
@ -274,24 +268,24 @@ class ApiV2Controller extends Controller
$settings = UserSetting::whereUserId($user->id)->first(); $settings = UserSetting::whereUserId($user->id)->first();
if($settings && !empty($settings->compose_settings)) { if ($settings && ! empty($settings->compose_settings)) {
$compose = $settings->compose_settings; $compose = $settings->compose_settings;
if(isset($compose['default_license']) && $compose['default_license'] != 1) { if (isset($compose['default_license']) && $compose['default_license'] != 1) {
$license = $compose['default_license']; $license = $compose['default_license'];
} }
} }
abort_if(MediaBlocklistService::exists($hash) == true, 451); abort_if(MediaBlocklistService::exists($hash) == true, 451);
if($request->has('replace_id')) { if ($request->has('replace_id')) {
$rpid = $request->input('replace_id'); $rpid = $request->input('replace_id');
$removeMedia = Media::whereNull('status_id') $removeMedia = Media::whereNull('status_id')
->whereUserId($user->id) ->whereUserId($user->id)
->whereProfileId($profile->id) ->whereProfileId($profile->id)
->where('created_at', '>', now()->subHours(2)) ->where('created_at', '>', now()->subHours(2))
->find($rpid); ->find($rpid);
if($removeMedia) { if ($removeMedia) {
MediaDeletePipeline::dispatch($removeMedia) MediaDeletePipeline::dispatch($removeMedia)
->onQueue('mmo') ->onQueue('mmo')
->delay(now()->addMinutes(15)); ->delay(now()->addMinutes(15));
@ -309,7 +303,7 @@ class ApiV2Controller extends Controller
$media->caption = $request->input('description'); $media->caption = $request->input('description');
$media->filter_class = $filterClass; $media->filter_class = $filterClass;
$media->filter_name = $filterName; $media->filter_name = $filterName;
if($license) { if ($license) {
$media->license = $license; $media->license = $license;
} }
$media->save(); $media->save();
@ -327,13 +321,18 @@ class ApiV2Controller extends Controller
break; break;
} }
$user->storage_used = (int) $updatedAccountSize;
$user->storage_used_updated_at = now();
$user->save();
Cache::forget($limitKey); Cache::forget($limitKey);
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer()); $fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($media, new MediaTransformer()); $resource = new Fractal\Resource\Item($media, new MediaTransformer());
$res = $fractal->createData($resource)->toArray(); $res = $fractal->createData($resource)->toArray();
$res['preview_url'] = $media->url(). '?v=' . time(); $res['preview_url'] = $media->url().'?v='.time();
$res['url'] = null; $res['url'] = null;
return $this->json($res, 202); return $this->json($res, 202);
} }
} }

View file

@ -4,8 +4,9 @@ namespace App\Http\Controllers\Api;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\{Profile, Status, User}; use App\{Profile, Instance, Status, User};
use Cache; use Cache;
use App\Services\StatusService;
class InstanceApiController extends Controller { class InstanceApiController extends Controller {
@ -40,11 +41,8 @@ class InstanceApiController extends Controller {
'urls' => [], 'urls' => [],
'stats' => [ 'stats' => [
'user_count' => User::count(), 'user_count' => User::count(),
'status_count' => Status::whereNull('uri')->count(), 'status_count' => StatusService::totalLocalStatuses(),
'domain_count' => Profile::whereNotNull('domain') 'domain_count' => Instance::count()
->groupBy('domain')
->pluck('domain')
->count()
], ],
'thumbnail' => '', 'thumbnail' => '',
'languages' => [], 'languages' => [],

View file

@ -72,7 +72,7 @@ class DomainBlockController extends Controller
abort_if(config_cache('pixelfed.domain.app') == $domain, 400, 'Cannot ban your own server'); abort_if(config_cache('pixelfed.domain.app') == $domain, 400, 'Cannot ban your own server');
$existingCount = UserDomainBlock::whereProfileId($pid)->count(); $existingCount = UserDomainBlock::whereProfileId($pid)->count();
$maxLimit = config('instance.user_filters.max_domain_blocks'); $maxLimit = (int) config_cache('instance.user_filters.max_domain_blocks');
$errorMsg = __('profile.block.domain.max', ['max' => $maxLimit]); $errorMsg = __('profile.block.domain.max', ['max' => $maxLimit]);
abort_if($existingCount >= $maxLimit, 400, $errorMsg); abort_if($existingCount >= $maxLimit, 400, $errorMsg);

View file

@ -62,7 +62,7 @@ class ForgotPasswordController extends Controller
usleep(random_int(100000, 3000000)); usleep(random_int(100000, 3000000));
if(config('captcha.enabled')) { if((bool) config_cache('captcha.enabled')) {
$rules = [ $rules = [
'email' => 'required|email', 'email' => 'required|email',
'h-captcha-response' => 'required|captcha' 'h-captcha-response' => 'required|captcha'

View file

@ -74,10 +74,10 @@ class LoginController extends Controller
$messages = []; $messages = [];
if( if(
config('captcha.enabled') || (bool) config_cache('captcha.enabled') &&
config('captcha.active.login') || (bool) config_cache('captcha.active.login') ||
( (
config('captcha.triggers.login.enabled') && (bool) config_cache('captcha.triggers.login.enabled') &&
request()->session()->has('login_attempts') && request()->session()->has('login_attempts') &&
request()->session()->get('login_attempts') >= config('captcha.triggers.login.attempts') request()->session()->get('login_attempts') >= config('captcha.triggers.login.attempts')
) )

View file

@ -3,16 +3,16 @@
namespace App\Http\Controllers\Auth; namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use App\Services\BouncerService;
use App\Services\EmailService;
use App\User; use App\User;
use Purify;
use App\Util\Lexer\RestrictedNames; use App\Util\Lexer\RestrictedNames;
use Illuminate\Auth\Events\Registered;
use Illuminate\Foundation\Auth\RegistersUsers; use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Illuminate\Auth\Events\Registered; use Purify;
use Illuminate\Http\Request;
use App\Services\EmailService;
use App\Services\BouncerService;
class RegisterController extends Controller class RegisterController extends Controller
{ {
@ -48,7 +48,7 @@ class RegisterController extends Controller
public function getRegisterToken() public function getRegisterToken()
{ {
return \Cache::remember('pf:register:rt', 900, function() { return \Cache::remember('pf:register:rt', 900, function () {
return str_random(40); return str_random(40);
}); });
} }
@ -56,13 +56,12 @@ class RegisterController extends Controller
/** /**
* Get a validator for an incoming registration request. * Get a validator for an incoming registration request.
* *
* @param array $data
* *
* @return \Illuminate\Contracts\Validation\Validator * @return \Illuminate\Contracts\Validation\Validator
*/ */
public function validator(array $data) public function validator(array $data)
{ {
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
$data['username'] = strtolower($data['username']); $data['username'] = strtolower($data['username']);
$data['email'] = strtolower($data['email']); $data['email'] = strtolower($data['email']);
} }
@ -77,27 +76,31 @@ class RegisterController extends Controller
$underscore = substr_count($value, '_'); $underscore = substr_count($value, '_');
$period = substr_count($value, '.'); $period = substr_count($value, '.');
if(ends_with($value, ['.php', '.js', '.css'])) { if (ends_with($value, ['.php', '.js', '.css'])) {
return $fail('Username is invalid.'); return $fail('Username is invalid.');
} }
if(($dash + $underscore + $period) > 1) { if (($dash + $underscore + $period) > 1) {
return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).'); return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
} }
if (!ctype_alnum($value[0])) { if (! ctype_alnum($value[0])) {
return $fail('Username is invalid. Must start with a letter or number.'); return $fail('Username is invalid. Must start with a letter or number.');
} }
if (!ctype_alnum($value[strlen($value) - 1])) { if (! ctype_alnum($value[strlen($value) - 1])) {
return $fail('Username is invalid. Must end with a letter or number.'); return $fail('Username is invalid. Must end with a letter or number.');
} }
$val = str_replace(['_', '.', '-'], '', $value); $val = str_replace(['_', '.', '-'], '', $value);
if(!ctype_alnum($val)) { if (! ctype_alnum($val)) {
return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).'); return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
} }
if (! preg_match('/[a-zA-Z]/', $value)) {
return $fail('Username is invalid. Must contain at least one alphabetical character.');
}
$restricted = RestrictedNames::get(); $restricted = RestrictedNames::get();
if (in_array(strtolower($value), array_map('strtolower', $restricted))) { if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
return $fail('Username cannot be used.'); return $fail('Username cannot be used.');
@ -113,7 +116,7 @@ class RegisterController extends Controller
'unique:users', 'unique:users',
function ($attribute, $value, $fail) { function ($attribute, $value, $fail) {
$banned = EmailService::isBanned($value); $banned = EmailService::isBanned($value);
if($banned) { if ($banned) {
return $fail('Email is invalid.'); return $fail('Email is invalid.');
} }
}, },
@ -122,10 +125,10 @@ class RegisterController extends Controller
$rt = [ $rt = [
'required', 'required',
function ($attribute, $value, $fail) { function ($attribute, $value, $fail) {
if($value !== $this->getRegisterToken()) { if ($value !== $this->getRegisterToken()) {
return $fail('Something went wrong'); return $fail('Something went wrong');
} }
} },
]; ];
$rules = [ $rules = [
@ -137,7 +140,7 @@ class RegisterController extends Controller
'password' => 'required|string|min:'.config('pixelfed.min_password_length').'|confirmed', 'password' => 'required|string|min:'.config('pixelfed.min_password_length').'|confirmed',
]; ];
if(config('captcha.enabled') || config('captcha.active.register')) { if ((bool) config_cache('captcha.enabled') && (bool) config_cache('captcha.active.register')) {
$rules['h-captcha-response'] = 'required|captcha'; $rules['h-captcha-response'] = 'required|captcha';
} }
@ -147,13 +150,12 @@ class RegisterController extends Controller
/** /**
* Create a new user instance after a valid registration. * Create a new user instance after a valid registration.
* *
* @param array $data
* *
* @return \App\User * @return \App\User
*/ */
public function create(array $data) public function create(array $data)
{ {
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
$data['username'] = strtolower($data['username']); $data['username'] = strtolower($data['username']);
$data['email'] = strtolower($data['email']); $data['email'] = strtolower($data['email']);
} }
@ -163,7 +165,7 @@ class RegisterController extends Controller
'username' => $data['username'], 'username' => $data['username'],
'email' => $data['email'], 'email' => $data['email'],
'password' => Hash::make($data['password']), 'password' => Hash::make($data['password']),
'app_register_ip' => request()->ip() 'app_register_ip' => request()->ip(),
]); ]);
} }
@ -174,24 +176,27 @@ class RegisterController extends Controller
*/ */
public function showRegistrationForm() public function showRegistrationForm()
{ {
if((bool) config_cache('pixelfed.open_registration')) { if ((bool) config_cache('pixelfed.open_registration')) {
if(config('pixelfed.bouncer.cloud_ips.ban_signups')) { if (config('pixelfed.bouncer.cloud_ips.ban_signups')) {
abort_if(BouncerService::checkIp(request()->ip()), 404); abort_if(BouncerService::checkIp(request()->ip()), 404);
} }
$hasLimit = config('pixelfed.enforce_max_users'); $hasLimit = config('pixelfed.enforce_max_users');
if($hasLimit) { if ($hasLimit) {
$limit = config('pixelfed.max_users'); $limit = config('pixelfed.max_users');
$count = User::where(function($q){ return $q->whereNull('status')->orWhereNotIn('status', ['deleted','delete']); })->count(); $count = User::where(function ($q) {
if($limit <= $count) { return $q->whereNull('status')->orWhereNotIn('status', ['deleted', 'delete']);
})->count();
if ($limit <= $count) {
return redirect(route('help.instance-max-users-limit')); return redirect(route('help.instance-max-users-limit'));
} }
abort_if($limit <= $count, 404); abort_if($limit <= $count, 404);
return view('auth.register'); return view('auth.register');
} else { } else {
return view('auth.register'); return view('auth.register');
} }
} else { } else {
if((bool) config_cache('instance.curated_registration.enabled') && config('instance.curated_registration.state.fallback_on_closed_reg')) { if ((bool) config_cache('instance.curated_registration.enabled') && config('instance.curated_registration.state.fallback_on_closed_reg')) {
return redirect('/auth/sign_up'); return redirect('/auth/sign_up');
} else { } else {
abort(404); abort(404);
@ -202,28 +207,28 @@ class RegisterController extends Controller
/** /**
* Handle a registration request for the application. * Handle a registration request for the application.
* *
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response * @return \Illuminate\Http\Response
*/ */
public function register(Request $request) public function register(Request $request)
{ {
abort_if(config_cache('pixelfed.open_registration') == false, 400); abort_if(config_cache('pixelfed.open_registration') == false, 400);
if(config('pixelfed.bouncer.cloud_ips.ban_signups')) { if (config('pixelfed.bouncer.cloud_ips.ban_signups')) {
abort_if(BouncerService::checkIp($request->ip()), 404); abort_if(BouncerService::checkIp($request->ip()), 404);
} }
$hasLimit = config('pixelfed.enforce_max_users'); $hasLimit = config('pixelfed.enforce_max_users');
if($hasLimit) { if ($hasLimit) {
$count = User::where(function($q){ return $q->whereNull('status')->orWhereNotIn('status', ['deleted','delete']); })->count(); $count = User::where(function ($q) {
return $q->whereNull('status')->orWhereNotIn('status', ['deleted', 'delete']);
})->count();
$limit = config('pixelfed.max_users'); $limit = config('pixelfed.max_users');
if($limit && $limit <= $count) { if ($limit && $limit <= $count) {
return redirect(route('help.instance-max-users-limit')); return redirect(route('help.instance-max-users-limit'));
} }
} }
$this->validator($request->all())->validate(); $this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all()))); event(new Registered($user = $this->create($request->all())));

View file

@ -50,7 +50,7 @@ class ResetPasswordController extends Controller
{ {
usleep(random_int(100000, 3000000)); usleep(random_int(100000, 3000000));
if(config('captcha.enabled')) { if((bool) config_cache('captcha.enabled')) {
return [ return [
'token' => 'required', 'token' => 'required',
'email' => 'required|email', 'email' => 'required|email',

View file

@ -2,39 +2,30 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Collection;
use Auth; use App\CollectionItem;
use App\{
Collection,
CollectionItem,
Profile,
Status
};
use League\Fractal;
use App\Transformer\Api\{
AccountTransformer,
StatusTransformer,
};
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\CollectionService; use App\Services\CollectionService;
use App\Services\FollowerService; use App\Services\FollowerService;
use App\Services\StatusService; use App\Services\StatusService;
use App\Status;
use Auth;
use Illuminate\Http\Request;
class CollectionController extends Controller class CollectionController extends Controller
{ {
public function create(Request $request) public function create(Request $request)
{ {
abort_if(!Auth::check(), 403); abort_if(! Auth::check(), 403);
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$collection = Collection::firstOrCreate([ $collection = Collection::firstOrCreate([
'profile_id' => $profile->id, 'profile_id' => $profile->id,
'published_at' => null 'published_at' => null,
]); ]);
$collection->visibility = 'draft'; $collection->visibility = 'draft';
$collection->save(); $collection->save();
return view('collection.create', compact('collection')); return view('collection.create', compact('collection'));
} }
@ -42,32 +33,34 @@ class CollectionController extends Controller
{ {
$user = $request->user(); $user = $request->user();
$collection = CollectionService::getCollection($id); $collection = CollectionService::getCollection($id);
abort_if(!$collection, 404); abort_if(! $collection, 404);
if($collection['published_at'] == null || $collection['visibility'] != 'public') { if ($collection['published_at'] == null || $collection['visibility'] != 'public') {
abort_if(!$user, 404); abort_if(! $user, 404);
if($user->profile_id != $collection['pid']) { if ($user->profile_id != $collection['pid']) {
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($collection['visibility'] != 'private', 404); abort_if($collection['visibility'] != 'private', 404);
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404); abort_if(! FollowerService::follows($user->profile_id, $collection['pid']), 404);
} }
} }
} }
return view('collection.show', compact('collection')); return view('collection.show', compact('collection'));
} }
public function index(Request $request) public function index(Request $request)
{ {
abort_if(!Auth::check(), 403); abort_if(! Auth::check(), 403);
return $request->all(); return $request->all();
} }
public function store(Request $request, $id) public function store(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'title' => 'nullable|max:50', 'title' => 'nullable|max:50',
'description' => 'nullable|max:500', 'description' => 'nullable|max:500',
'visibility' => 'nullable|string|in:public,private,draft' 'visibility' => 'nullable|string|in:public,private,draft',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -78,20 +71,21 @@ class CollectionController extends Controller
$collection->save(); $collection->save();
CollectionService::deleteCollection($id); CollectionService::deleteCollection($id);
return CollectionService::setCollection($collection->id, $collection); return CollectionService::setCollection($collection->id, $collection);
} }
public function publish(Request $request, int $id) public function publish(Request $request, int $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'title' => 'nullable|max:50', 'title' => 'nullable|max:50',
'description' => 'nullable|max:500', 'description' => 'nullable|max:500',
'visibility' => 'required|alpha|in:public,private,draft' 'visibility' => 'required|alpha|in:public,private,draft',
]); ]);
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$collection = Collection::whereProfileId($profile->id)->findOrFail($id); $collection = Collection::whereProfileId($profile->id)->findOrFail($id);
if($collection->items()->count() == 0) { if ($collection->items()->count() == 0) {
abort(404); abort(404);
} }
$collection->title = strip_tags($request->input('title')); $collection->title = strip_tags($request->input('title'));
@ -99,12 +93,13 @@ class CollectionController extends Controller
$collection->visibility = $request->input('visibility'); $collection->visibility = $request->input('visibility');
$collection->published_at = now(); $collection->published_at = now();
$collection->save(); $collection->save();
return CollectionService::setCollection($collection->id, $collection); return CollectionService::setCollection($collection->id, $collection);
} }
public function delete(Request $request, int $id) public function delete(Request $request, int $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$user = $request->user(); $user = $request->user();
$collection = Collection::whereProfileId($user->profile_id)->findOrFail($id); $collection = Collection::whereProfileId($user->profile_id)->findOrFail($id);
@ -113,7 +108,7 @@ class CollectionController extends Controller
CollectionService::deleteCollection($id); CollectionService::deleteCollection($id);
if($request->wantsJson()) { if ($request->wantsJson()) {
return 200; return 200;
} }
@ -122,11 +117,11 @@ class CollectionController extends Controller
public function storeId(Request $request) public function storeId(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'collection_id' => 'required|int|min:1|exists:collections,id', 'collection_id' => 'required|int|min:1|exists:collections,id',
'post_id' => 'required|int|min:1' 'post_id' => 'required|int|min:1',
]); ]);
$profileId = $request->user()->profile_id; $profileId = $request->user()->profile_id;
@ -136,20 +131,20 @@ class CollectionController extends Controller
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId); $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
$count = $collection->items()->count(); $count = $collection->items()->count();
if($count) { if ($count) {
CollectionItem::whereCollectionId($collection->id) CollectionItem::whereCollectionId($collection->id)
->get() ->get()
->filter(function($col) { ->filter(function ($col) {
return StatusService::get($col->object_id, false) == null; return StatusService::get($col->object_id, false) == null;
}) })
->each(function($col) use($collectionId) { ->each(function ($col) use ($collectionId) {
CollectionService::removeItem($collectionId, $col->object_id); CollectionService::removeItem($collectionId, $col->object_id);
$col->delete(); $col->delete();
}); });
} }
$max = config('pixelfed.max_collection_length'); $max = config('pixelfed.max_collection_length');
if($count >= $max) { if ($count >= $max) {
abort(400, 'You can only add '.$max.' posts per collection'); abort(400, 'You can only add '.$max.' posts per collection');
} }
@ -161,8 +156,8 @@ class CollectionController extends Controller
$item = CollectionItem::firstOrCreate([ $item = CollectionItem::firstOrCreate([
'collection_id' => $collection->id, 'collection_id' => $collection->id,
'object_type' => 'App\Status', 'object_type' => 'App\Status',
'object_id' => $status->id 'object_id' => $status->id,
],[ ], [
'order' => $count, 'order' => $count,
]); ]);
@ -180,16 +175,16 @@ class CollectionController extends Controller
$user = $request->user(); $user = $request->user();
$collection = CollectionService::getCollection($id); $collection = CollectionService::getCollection($id);
if(!$collection) { if (! $collection) {
return response()->json([], 404); return response()->json([], 404);
} }
if($collection['published_at'] == null || $collection['visibility'] != 'public') { if ($collection['published_at'] == null || $collection['visibility'] != 'public') {
abort_unless($user, 404); abort_unless($user, 404);
if($user->profile_id != $collection['pid']) { if ($user->profile_id != $collection['pid']) {
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($collection['visibility'] != 'private', 404); abort_if($collection['visibility'] != 'private', 404);
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404); abort_if(! FollowerService::follows($user->profile_id, $collection['pid']), 404);
} }
} }
} }
@ -202,16 +197,16 @@ class CollectionController extends Controller
$user = $request->user(); $user = $request->user();
$collection = CollectionService::getCollection($id); $collection = CollectionService::getCollection($id);
if(!$collection) { if (! $collection) {
return response()->json([], 404); return response()->json([], 404);
} }
if($collection['published_at'] == null || $collection['visibility'] != 'public') { if ($collection['published_at'] == null || $collection['visibility'] != 'public') {
abort_unless($user, 404); abort_unless($user, 404);
if($user->profile_id != $collection['pid']) { if ($user->profile_id != $collection['pid']) {
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($collection['visibility'] != 'private', 404); abort_if($collection['visibility'] != 'private', 404);
abort_if(!FollowerService::follows($user->profile_id, $collection['pid']), 404); abort_if(! FollowerService::follows($user->profile_id, $collection['pid']), 404);
} }
} }
} }
@ -221,10 +216,10 @@ class CollectionController extends Controller
$items = CollectionService::getItems($id, $start, $end); $items = CollectionService::getItems($id, $start, $end);
return collect($items) return collect($items)
->map(function($id) { ->map(function ($id) {
return StatusService::get($id, false); return StatusService::get($id, false);
}) })
->filter(function($item) { ->filter(function ($item) {
return $item && ($item['visibility'] == 'public' || $item['visibility'] == 'unlisted') && isset($item['account'], $item['media_attachments']); return $item && ($item['visibility'] == 'public' || $item['visibility'] == 'unlisted') && isset($item['account'], $item['media_attachments']);
}) })
->values(); ->values();
@ -238,49 +233,49 @@ class CollectionController extends Controller
$visibility = ['public']; $visibility = ['public'];
$profile = AccountService::get($id, true); $profile = AccountService::get($id, true);
if(!$profile || !isset($profile['id'])) { if (! $profile || ! isset($profile['id'])) {
return response()->json([], 404); return response()->json([], 404);
} }
if($pid) { if ($pid) {
$follows = FollowerService::follows($pid, $profile['id']); $follows = FollowerService::follows($pid, $profile['id']);
} }
if($profile['locked']) { if ($profile['locked']) {
abort_if(!$pid, 404); abort_if(! $pid, 404);
if(!$user->is_admin) { if (! $user->is_admin) {
abort_if($profile['id'] != $pid && $follows == false, 404); abort_if($profile['id'] != $pid && $follows == false, 404);
} }
} }
$owner = $pid ? $pid == $profile['id'] : false; $owner = $pid ? $pid == $profile['id'] : false;
if($follows) { if ($follows) {
$visibility = ['public', 'private']; $visibility = ['public', 'private'];
} }
if($pid && $pid == $profile['id']) { if ($pid && $pid == $profile['id']) {
$visibility = ['public', 'private', 'draft']; $visibility = ['public', 'private', 'draft'];
} }
return Collection::whereProfileId($profile['id']) return Collection::whereProfileId($profile['id'])
->whereIn('visibility', $visibility) ->whereIn('visibility', $visibility)
->when(!$owner, function($q, $owner) { ->when(! $owner, function ($q, $owner) {
return $q->whereNotNull('published_at'); return $q->whereNotNull('published_at');
}) })
->orderByDesc('id') ->orderByDesc('id')
->paginate(9) ->paginate(9)
->map(function($collection) { ->map(function ($collection) {
return CollectionService::getCollection($collection->id); return CollectionService::getCollection($collection->id);
}); });
} }
public function deleteId(Request $request) public function deleteId(Request $request)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$this->validate($request, [ $this->validate($request, [
'collection_id' => 'required|int|min:1|exists:collections,id', 'collection_id' => 'required|int|min:1|exists:collections,id',
'post_id' => 'required|int|min:1' 'post_id' => 'required|int|min:1',
]); ]);
$profileId = $request->user()->profile_id; $profileId = $request->user()->profile_id;
@ -290,7 +285,7 @@ class CollectionController extends Controller
$collection = Collection::whereProfileId($profileId)->findOrFail($collectionId); $collection = Collection::whereProfileId($profileId)->findOrFail($collectionId);
$count = $collection->items()->count(); $count = $collection->items()->count();
if($count == 1) { if ($count == 1) {
abort(400, 'You cannot delete the only post of a collection!'); abort(400, 'You cannot delete the only post of a collection!');
} }
@ -308,7 +303,7 @@ class CollectionController extends Controller
CollectionItem::whereCollectionId($collection->id) CollectionItem::whereCollectionId($collection->id)
->orderBy('created_at') ->orderBy('created_at')
->get() ->get()
->each(function($item, $index) { ->each(function ($item, $index) {
$item->order = $index; $item->order = $index;
$item->save(); $item->save();
}); });
@ -319,4 +314,31 @@ class CollectionController extends Controller
return 200; return 200;
} }
public function getSelfCollections(Request $request)
{
abort_if(! $request->user(), 404);
$user = $request->user();
$pid = $user->profile_id;
$profile = AccountService::get($pid, true);
if (! $profile || ! isset($profile['id'])) {
return response()->json([], 404);
}
return Collection::whereProfileId($pid)
->orderByDesc('id')
->paginate(9)
->map(function ($collection) {
$c = CollectionService::getCollection($collection->id);
$c['items'] = collect(CollectionService::getItems($collection->id))
->map(function ($id) {
return StatusService::get($id, false);
})->filter()->values();
return $c;
})
->filter()
->values();
}
} }

View file

@ -21,6 +21,7 @@ use App\Services\MediaStorageService;
use App\Services\MediaTagService; use App\Services\MediaTagService;
use App\Services\SnowflakeService; use App\Services\SnowflakeService;
use App\Services\UserRoleService; use App\Services\UserRoleService;
use App\Services\UserStorageService;
use App\Status; use App\Status;
use App\Transformer\Api\MediaTransformer; use App\Transformer\Api\MediaTransformer;
use App\UserFilter; use App\UserFilter;
@ -70,7 +71,7 @@ class ComposeController extends Controller
'filter_class' => 'nullable|alpha_dash|max:24', 'filter_class' => 'nullable|alpha_dash|max:24',
]); ]);
$user = Auth::user(); $user = $request->user();
$profile = $user->profile; $profile = $user->profile;
abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-post', $user->id), 403, 'Invalid permissions for this action');
@ -84,21 +85,22 @@ class ComposeController extends Controller
abort_if($limitReached == true, 429); abort_if($limitReached == true, 429);
if (config_cache('pixelfed.enforce_account_limit') == true) { $filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function () use ($user) { $filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
return Media::whereUserId($user->id)->sum('size') / 1000; $accountSize = UserStorageService::get($user->id);
}); abort_if($accountSize === -1, 403, 'Invalid request.');
$photo = $request->file('file');
$fileSize = $photo->getSize();
$sizeInKbs = (int) ceil($fileSize / 1000);
$updatedAccountSize = (int) $accountSize + (int) $sizeInKbs;
if ((bool) config_cache('pixelfed.enforce_account_limit') == true) {
$limit = (int) config_cache('pixelfed.max_account_size'); $limit = (int) config_cache('pixelfed.max_account_size');
if ($size >= $limit) { if ($updatedAccountSize >= $limit) {
abort(403, 'Account size limit reached.'); abort(403, 'Account size limit reached.');
} }
} }
$filterClass = in_array($request->input('filter_class'), Filter::classes()) ? $request->input('filter_class') : null;
$filterName = in_array($request->input('filter_name'), Filter::names()) ? $request->input('filter_name') : null;
$photo = $request->file('file');
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
abort_if(in_array($photo->getMimeType(), $mimes) == false, 400, 'Invalid media format'); abort_if(in_array($photo->getMimeType(), $mimes) == false, 400, 'Invalid media format');
@ -143,6 +145,10 @@ class ComposeController extends Controller
break; break;
} }
$user->storage_used = (int) $updatedAccountSize;
$user->storage_used_updated_at = now();
$user->save();
Cache::forget($limitKey); Cache::forget($limitKey);
$resource = new Fractal\Resource\Item($media, new MediaTransformer()); $resource = new Fractal\Resource\Item($media, new MediaTransformer());
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
@ -198,6 +204,7 @@ class ComposeController extends Controller
]; ];
ImageOptimize::dispatch($media)->onQueue('mmo'); ImageOptimize::dispatch($media)->onQueue('mmo');
Cache::forget($limitKey); Cache::forget($limitKey);
UserStorageService::recalculateUpdateStorageUsed($request->user()->id);
return $res; return $res;
} }
@ -218,6 +225,8 @@ class ComposeController extends Controller
MediaStorageService::delete($media, true); MediaStorageService::delete($media, true);
UserStorageService::recalculateUpdateStorageUsed($request->user()->id);
return response()->json([ return response()->json([
'msg' => 'Successfully deleted', 'msg' => 'Successfully deleted',
'code' => 200, 'code' => 200,
@ -494,17 +503,17 @@ class ComposeController extends Controller
$limitKey = 'compose:rate-limit:store:'.$user->id; $limitKey = 'compose:rate-limit:store:'.$user->id;
$limitTtl = now()->addMinutes(15); $limitTtl = now()->addMinutes(15);
$limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) { // $limitReached = Cache::remember($limitKey, $limitTtl, function () use ($user) {
$dailyLimit = Status::whereProfileId($user->profile_id) // $dailyLimit = Status::whereProfileId($user->profile_id)
->whereNull('in_reply_to_id') // ->whereNull('in_reply_to_id')
->whereNull('reblog_of_id') // ->whereNull('reblog_of_id')
->where('created_at', '>', now()->subDays(1)) // ->where('created_at', '>', now()->subDays(1))
->count(); // ->count();
return $dailyLimit >= 1000; // return $dailyLimit >= 1000;
}); // });
abort_if($limitReached == true, 429); // abort_if($limitReached == true, 429);
$license = in_array($request->input('license'), License::keys()) ? $request->input('license') : null; $license = in_array($request->input('license'), License::keys()) ? $request->input('license') : null;
@ -626,7 +635,6 @@ class ComposeController extends Controller
Cache::forget('_api:statuses:recent_9:'.$profile->id); Cache::forget('_api:statuses:recent_9:'.$profile->id);
Cache::forget('profile:status_count:'.$profile->id); Cache::forget('profile:status_count:'.$profile->id);
Cache::forget('status:transformer:media:attachments:'.$status->id); Cache::forget('status:transformer:media:attachments:'.$status->id);
Cache::forget($user->storageUsedKey());
Cache::forget('profile:embed:'.$status->profile_id); Cache::forget('profile:embed:'.$status->profile_id);
Cache::forget($limitKey); Cache::forget($limitKey);
@ -741,7 +749,7 @@ class ComposeController extends Controller
case 'image/jpeg': case 'image/jpeg':
case 'image/png': case 'image/png':
case 'video/mp4': case 'video/mp4':
$finished = config_cache('pixelfed.cloud_storage') ? (bool) $media->cdn_url : (bool) $media->processed_at; $finished = (bool) config_cache('pixelfed.cloud_storage') ? (bool) $media->cdn_url : (bool) $media->processed_at;
break; break;
default: default:

View file

@ -2,31 +2,28 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Auth, Cache; use App\DirectMessage;
use Illuminate\Http\Request;
use App\{
DirectMessage,
Media,
Notification,
Profile,
Status,
User,
UserFilter,
UserSetting
};
use App\Services\MediaPathService;
use App\Services\MediaBlocklistService;
use App\Jobs\StatusPipeline\NewStatusPipeline;
use App\Jobs\StatusPipeline\StatusDelete;
use Illuminate\Support\Str;
use App\Util\ActivityPub\Helpers;
use App\Services\AccountService;
use App\Services\StatusService;
use App\Services\WebfingerService;
use App\Models\Conversation;
use App\Jobs\DirectPipeline\DirectDeletePipeline; use App\Jobs\DirectPipeline\DirectDeletePipeline;
use App\Jobs\DirectPipeline\DirectDeliverPipeline; use App\Jobs\DirectPipeline\DirectDeliverPipeline;
use App\Jobs\StatusPipeline\StatusDelete;
use App\Media;
use App\Models\Conversation;
use App\Notification;
use App\Profile;
use App\Services\AccountService;
use App\Services\MediaBlocklistService;
use App\Services\MediaPathService;
use App\Services\MediaService;
use App\Services\StatusService;
use App\Services\UserFilterService;
use App\Services\UserRoleService; use App\Services\UserRoleService;
use App\Services\UserStorageService;
use App\Services\WebfingerService;
use App\Status;
use App\UserFilter;
use App\Util\ActivityPub\Helpers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class DirectMessageController extends Controller class DirectMessageController extends Controller
{ {
@ -39,25 +36,25 @@ class DirectMessageController extends Controller
{ {
$this->validate($request, [ $this->validate($request, [
'a' => 'nullable|string|in:inbox,sent,filtered', 'a' => 'nullable|string|in:inbox,sent,filtered',
'page' => 'nullable|integer|min:1|max:99' 'page' => 'nullable|integer|min:1|max:99',
]); ]);
$user = $request->user(); $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id)) { if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
return []; return [];
} }
$profile = $user->profile_id; $profile = $user->profile_id;
$action = $request->input('a', 'inbox'); $action = $request->input('a', 'inbox');
$page = $request->input('page'); $page = $request->input('page');
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
if($action == 'inbox') { if ($action == 'inbox') {
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at') $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(false) ->whereIsHidden(false)
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
@ -65,232 +62,232 @@ class DirectMessageController extends Controller
->get() ->get()
->unique('from_id') ->unique('from_id')
->take(8) ->take(8)
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
})->values(); })->values();
} }
if($action == 'sent') { if ($action == 'sent') {
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at') $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
->whereFromId($profile) ->whereFromId($profile)
->with(['author','status']) ->with(['author', 'status'])
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->get() ->get()
->unique('to_id') ->unique('to_id')
->take(8) ->take(8)
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
if($action == 'filtered') { if ($action == 'filtered') {
$dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at') $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(true) ->whereIsHidden(true)
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->get() ->get()
->unique('from_id') ->unique('from_id')
->take(8) ->take(8)
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
} elseif(config('database.default') == 'mysql') { } elseif (config('database.default') == 'mysql') {
if($action == 'inbox') { if ($action == 'inbox') {
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt') $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(false) ->whereIsHidden(false)
->groupBy('from_id') ->groupBy('from_id')
->latest() ->latest()
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
if($action == 'sent') { if ($action == 'sent') {
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt') $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
->whereFromId($profile) ->whereFromId($profile)
->with(['author','status']) ->with(['author', 'status'])
->groupBy('to_id') ->groupBy('to_id')
->orderBy('createdAt', 'desc') ->orderBy('createdAt', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
if($action == 'filtered') { if ($action == 'filtered') {
$dms = DirectMessage::selectRaw('*, max(created_at) as createdAt') $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
->whereToId($profile) ->whereToId($profile)
->with(['author','status']) ->with(['author', 'status'])
->whereIsHidden(true) ->whereIsHidden(true)
->groupBy('from_id') ->groupBy('from_id')
->orderBy('createdAt', 'desc') ->orderBy('createdAt', 'desc')
->when($page, function($q, $page) { ->when($page, function ($q, $page) {
if($page > 1) { if ($page > 1) {
return $q->offset($page * 8 - 8); return $q->offset($page * 8 - 8);
} }
}) })
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) use($profile) { ->map(function ($r) use ($profile) {
return $r->from_id !== $profile ? [ return $r->from_id !== $profile ? [
'id' => (string) $r->from_id, 'id' => (string) $r->from_id,
'name' => $r->author->name, 'name' => $r->author->name,
'username' => $r->author->username, 'username' => $r->author->username,
'avatar' => $r->author->avatarUrl(), 'avatar' => $r->author->avatarUrl(),
'url' => $r->author->url(), 'url' => $r->author->url(),
'isLocal' => (bool) !$r->author->domain, 'isLocal' => (bool) ! $r->author->domain,
'domain' => $r->author->domain, 'domain' => $r->author->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
] : [ ] : [
'id' => (string) $r->to_id, 'id' => (string) $r->to_id,
'name' => $r->recipient->name, 'name' => $r->recipient->name,
'username' => $r->recipient->username, 'username' => $r->recipient->username,
'avatar' => $r->recipient->avatarUrl(), 'avatar' => $r->recipient->avatarUrl(),
'url' => $r->recipient->url(), 'url' => $r->recipient->url(),
'isLocal' => (bool) !$r->recipient->domain, 'isLocal' => (bool) ! $r->recipient->domain,
'domain' => $r->recipient->domain, 'domain' => $r->recipient->domain,
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => $r->status->caption, 'lastMessage' => $r->status->caption,
'messages' => [] 'messages' => [],
]; ];
}); });
} }
@ -304,19 +301,20 @@ class DirectMessageController extends Controller
$this->validate($request, [ $this->validate($request, [
'to_id' => 'required', 'to_id' => 'required',
'message' => 'required|string|min:1|max:500', 'message' => 'required|string|min:1|max:500',
'type' => 'required|in:text,emoji' 'type' => 'required|in:text,emoji',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
abort_if($user->created_at->gt(now()->subHours(72)), 400, 'You need to wait a bit before you can DM another account');
$profile = $user->profile; $profile = $user->profile;
$recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id')); $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403); abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
$msg = $request->input('message'); $msg = $request->input('message');
if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) { if ((! $recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
if($recipient->follows($profile) == true) { if ($recipient->follows($profile) == true) {
$hidden = false; $hidden = false;
} else { } else {
$hidden = true; $hidden = true;
@ -345,23 +343,23 @@ class DirectMessageController extends Controller
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $recipient->id, 'to_id' => $recipient->id,
'from_id' => $profile->id 'from_id' => $profile->id,
], ],
[ [
'type' => $dm->type, 'type' => $dm->type,
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => $hidden 'is_hidden' => $hidden,
] ]
); );
if(filter_var($msg, FILTER_VALIDATE_URL)) { if (filter_var($msg, FILTER_VALIDATE_URL)) {
if(Helpers::validateUrl($msg)) { if (Helpers::validateUrl($msg)) {
$dm->type = 'link'; $dm->type = 'link';
$dm->meta = [ $dm->meta = [
'domain' => parse_url($msg, PHP_URL_HOST), 'domain' => parse_url($msg, PHP_URL_HOST),
'local' => parse_url($msg, PHP_URL_HOST) == 'local' => parse_url($msg, PHP_URL_HOST) ==
parse_url(config('app.url'), PHP_URL_HOST) parse_url(config('app.url'), PHP_URL_HOST),
]; ];
$dm->save(); $dm->save();
} }
@ -373,7 +371,7 @@ class DirectMessageController extends Controller
->whereFilterType('dm.mute') ->whereFilterType('dm.mute')
->exists(); ->exists();
if($recipient->domain == null && $hidden == false && !$nf) { if ($recipient->domain == null && $hidden == false && ! $nf) {
$notification = new Notification(); $notification = new Notification();
$notification->profile_id = $recipient->id; $notification->profile_id = $recipient->id;
$notification->actor_id = $profile->id; $notification->actor_id = $profile->id;
@ -383,7 +381,7 @@ class DirectMessageController extends Controller
$notification->save(); $notification->save();
} }
if($recipient->domain) { if ($recipient->domain) {
$this->remoteDeliver($dm); $this->remoteDeliver($dm);
} }
@ -395,9 +393,9 @@ class DirectMessageController extends Controller
'type' => $dm->type, 'type' => $dm->type,
'text' => $dm->status->caption, 'text' => $dm->status->caption,
'media' => null, 'media' => null,
'timeAgo' => $dm->created_at->diffForHumans(null,null,true), 'timeAgo' => $dm->created_at->diffForHumans(null, null, true),
'seen' => $dm->read_at != null, 'seen' => $dm->read_at != null,
'meta' => $dm->meta 'meta' => $dm->meta,
]; ];
return response()->json($res); return response()->json($res);
@ -406,10 +404,10 @@ class DirectMessageController extends Controller
public function thread(Request $request) public function thread(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'pid' => 'required' 'pid' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$uid = $user->profile_id; $uid = $user->profile_id;
$pid = $request->input('pid'); $pid = $request->input('pid');
@ -418,40 +416,40 @@ class DirectMessageController extends Controller
$r = Profile::findOrFail($pid); $r = Profile::findOrFail($pid);
if($min_id) { if ($min_id) {
$res = DirectMessage::select('*') $res = DirectMessage::select('*')
->where('id', '>', $min_id) ->where('id', '>', $min_id)
->where(function($q) use($pid,$uid) { ->where(function ($q) use ($pid, $uid) {
return $q->where([['from_id',$pid],['to_id',$uid] return $q->where([['from_id', $pid], ['to_id', $uid],
])->orWhere([['from_id',$uid],['to_id',$pid]]); ])->orWhere([['from_id', $uid], ['to_id', $pid]]);
}) })
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
} else if ($max_id) { } elseif ($max_id) {
$res = DirectMessage::select('*') $res = DirectMessage::select('*')
->where('id', '<', $max_id) ->where('id', '<', $max_id)
->where(function($q) use($pid,$uid) { ->where(function ($q) use ($pid, $uid) {
return $q->where([['from_id',$pid],['to_id',$uid] return $q->where([['from_id', $pid], ['to_id', $uid],
])->orWhere([['from_id',$uid],['to_id',$pid]]); ])->orWhere([['from_id', $uid], ['to_id', $pid]]);
}) })
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
} else { } else {
$res = DirectMessage::where(function($q) use($pid,$uid) { $res = DirectMessage::where(function ($q) use ($pid, $uid) {
return $q->where([['from_id',$pid],['to_id',$uid] return $q->where([['from_id', $pid], ['to_id', $uid],
])->orWhere([['from_id',$uid],['to_id',$pid]]); ])->orWhere([['from_id', $uid], ['to_id', $pid]]);
}) })
->latest() ->latest()
->take(8) ->take(8)
->get(); ->get();
} }
$res = $res->filter(function($s) { $res = $res->filter(function ($s) {
return $s && $s->status; return $s && $s->status;
}) })
->map(function($s) use ($uid) { ->map(function ($s) use ($uid) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'hidden' => (bool) $s->is_hidden, 'hidden' => (bool) $s->is_hidden,
@ -459,39 +457,41 @@ class DirectMessageController extends Controller
'type' => $s->type, 'type' => $s->type,
'text' => $s->status->caption, 'text' => $s->status->caption,
'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null, 'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null,
'timeAgo' => $s->created_at->diffForHumans(null,null,true), 'carousel' => MediaService::get($s->status_id),
'created_at' => $s->created_at->format('c'),
'timeAgo' => $s->created_at->diffForHumans(null, null, true),
'seen' => $s->read_at != null, 'seen' => $s->read_at != null,
'reportId' => (string) $s->status_id, 'reportId' => (string) $s->status_id,
'meta' => json_decode($s->meta,true) 'meta' => json_decode($s->meta, true),
]; ];
}) })
->values(); ->values();
$filters = UserFilterService::mutes($uid);
$w = [ $w = [
'id' => (string) $r->id, 'id' => (string) $r->id,
'name' => $r->name, 'name' => $r->name,
'username' => $r->username, 'username' => $r->username,
'avatar' => $r->avatarUrl(), 'avatar' => $r->avatarUrl(),
'url' => $r->url(), 'url' => $r->url(),
'muted' => UserFilter::whereUserId($uid) 'muted' => in_array($r->id, $filters),
->whereFilterableId($r->id) 'isLocal' => (bool) ! $r->domain,
->whereFilterableType('App\Profile')
->whereFilterType('dm.mute')
->first() ? true : false,
'isLocal' => (bool) !$r->domain,
'domain' => $r->domain, 'domain' => $r->domain,
'created_at' => $r->created_at->format('c'),
'updated_at' => $r->updated_at->format('c'),
'timeAgo' => $r->created_at->diffForHumans(null, true, true), 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
'lastMessage' => '', 'lastMessage' => '',
'messages' => $res 'messages' => $res,
]; ];
return response()->json($w, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES); return response()->json($w, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function delete(Request $request) public function delete(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'id' => 'required' 'id' => 'required',
]); ]);
$sid = $request->input('id'); $sid = $request->input('id');
@ -506,30 +506,30 @@ class DirectMessageController extends Controller
$recipient = AccountService::get($dm->to_id); $recipient = AccountService::get($dm->to_id);
if(!$recipient) { if (! $recipient) {
return response('', 422); return response('', 422);
} }
if($recipient['local'] == false) { if ($recipient['local'] == false) {
$dmc = $dm; $dmc = $dm;
$this->remoteDelete($dmc); $this->remoteDelete($dmc);
} else { } else {
StatusDelete::dispatch($status)->onQueue('high'); StatusDelete::dispatch($status)->onQueue('high');
} }
if(Conversation::whereStatusId($sid)->count()) { if (Conversation::whereStatusId($sid)->count()) {
$latest = DirectMessage::where(['from_id' => $dm->from_id, 'to_id' => $dm->to_id]) $latest = DirectMessage::where(['from_id' => $dm->from_id, 'to_id' => $dm->to_id])
->orWhere(['to_id' => $dm->from_id, 'from_id' => $dm->to_id]) ->orWhere(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
->latest() ->latest()
->first(); ->first();
if($latest->status_id == $sid) { if ($latest->status_id == $sid) {
Conversation::where(['to_id' => $dm->from_id, 'from_id' => $dm->to_id]) Conversation::where(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
->update([ ->update([
'updated_at' => $latest->updated_at, 'updated_at' => $latest->updated_at,
'status_id' => $latest->status_id, 'status_id' => $latest->status_id,
'type' => $latest->type, 'type' => $latest->type,
'is_hidden' => false 'is_hidden' => false,
]); ]);
Conversation::where(['to_id' => $dm->to_id, 'from_id' => $dm->from_id]) Conversation::where(['to_id' => $dm->to_id, 'from_id' => $dm->from_id])
@ -537,19 +537,19 @@ class DirectMessageController extends Controller
'updated_at' => $latest->updated_at, 'updated_at' => $latest->updated_at,
'status_id' => $latest->status_id, 'status_id' => $latest->status_id,
'type' => $latest->type, 'type' => $latest->type,
'is_hidden' => false 'is_hidden' => false,
]); ]);
} else { } else {
Conversation::where([ Conversation::where([
'status_id' => $sid, 'status_id' => $sid,
'to_id' => $dm->from_id, 'to_id' => $dm->from_id,
'from_id' => $dm->to_id 'from_id' => $dm->to_id,
])->delete(); ])->delete();
Conversation::where([ Conversation::where([
'status_id' => $sid, 'status_id' => $sid,
'from_id' => $dm->from_id, 'from_id' => $dm->from_id,
'to_id' => $dm->to_id 'to_id' => $dm->to_id,
])->delete(); ])->delete();
} }
} }
@ -557,41 +557,43 @@ class DirectMessageController extends Controller
StatusService::del($status->id, true); StatusService::del($status->id, true);
$status->forceDeleteQuietly(); $status->forceDeleteQuietly();
return [200]; return [200];
} }
public function get(Request $request, $id) public function get(Request $request, $id)
{ {
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$dm = DirectMessage::whereStatusId($id)->firstOrFail(); $dm = DirectMessage::whereStatusId($id)->firstOrFail();
abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404); abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404);
return response()->json($dm, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($dm, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function mediaUpload(Request $request) public function mediaUpload(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'file' => function() { 'file' => function () {
return [ return [
'required', 'required',
'mimetypes:' . config_cache('pixelfed.media_types'), 'mimetypes:'.config_cache('pixelfed.media_types'),
'max:' . config_cache('pixelfed.max_photo_size'), 'max:'.config_cache('pixelfed.max_photo_size'),
]; ];
}, },
'to_id' => 'required' 'to_id' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$profile = $user->profile; $profile = $user->profile;
$recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id')); $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403); abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) { if ((! $recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
if($recipient->follows($profile) == true) { if ($recipient->follows($profile) == true) {
$hidden = false; $hidden = false;
} else { } else {
$hidden = true; $hidden = true;
@ -600,23 +602,26 @@ class DirectMessageController extends Controller
$hidden = false; $hidden = false;
} }
if(config_cache('pixelfed.enforce_account_limit') == true) { $accountSize = UserStorageService::get($user->id);
$size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) { abort_if($accountSize === -1, 403, 'Invalid request.');
return Media::whereUserId($user->id)->sum('size') / 1000; $photo = $request->file('file');
}); $fileSize = $photo->getSize();
$sizeInKbs = (int) ceil($fileSize / 1000);
$updatedAccountSize = (int) $accountSize + (int) $sizeInKbs;
if ((bool) config_cache('pixelfed.enforce_account_limit') == true) {
$limit = (int) config_cache('pixelfed.max_account_size'); $limit = (int) config_cache('pixelfed.max_account_size');
if ($size >= $limit) { if ($updatedAccountSize >= $limit) {
abort(403, 'Account size limit reached.'); abort(403, 'Account size limit reached.');
} }
} }
$photo = $request->file('file');
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if(in_array($photo->getMimeType(), $mimes) == false) { if (in_array($photo->getMimeType(), $mimes) == false) {
abort(403, 'Invalid or unsupported mime type.'); abort(403, 'Invalid or unsupported mime type.');
} }
$storagePath = MediaPathService::get($user, 2) . Str::random(8); $storagePath = MediaPathService::get($user, 2).Str::random(8);
$path = $photo->storePublicly($storagePath); $path = $photo->storePublicly($storagePath);
$hash = \hash_file('sha256', $photo); $hash = \hash_file('sha256', $photo);
@ -655,17 +660,21 @@ class DirectMessageController extends Controller
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $recipient->id, 'to_id' => $recipient->id,
'from_id' => $profile->id 'from_id' => $profile->id,
], ],
[ [
'type' => $dm->type, 'type' => $dm->type,
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => $hidden 'is_hidden' => $hidden,
] ]
); );
if($recipient->domain) { $user->storage_used = (int) $updatedAccountSize;
$user->storage_used_updated_at = now();
$user->save();
if ($recipient->domain) {
$this->remoteDeliver($dm); $this->remoteDeliver($dm);
} }
@ -673,7 +682,7 @@ class DirectMessageController extends Controller
'id' => $dm->id, 'id' => $dm->id,
'reportId' => (string) $dm->status_id, 'reportId' => (string) $dm->status_id,
'type' => $dm->type, 'type' => $dm->type,
'url' => $media->url() 'url' => $media->url(),
]; ];
} }
@ -685,26 +694,26 @@ class DirectMessageController extends Controller
]); ]);
$user = $request->user(); $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id)) { if ($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id)) {
return []; return [];
} }
$q = $request->input('q'); $q = $request->input('q');
$r = $request->input('remote', false); $r = $request->input('remote', false);
if($r && !Str::of($q)->contains('.')) { if ($r && ! Str::of($q)->contains('.')) {
return []; return [];
} }
if($r && Helpers::validateUrl($q)) { if ($r && Helpers::validateUrl($q)) {
Helpers::profileFetch($q); Helpers::profileFetch($q);
} }
if(Str::of($q)->startsWith('@')) { if (Str::of($q)->startsWith('@')) {
if(strlen($q) < 3) { if (strlen($q) < 3) {
return []; return [];
} }
if(substr_count($q, '@') == 2) { if (substr_count($q, '@') == 2) {
WebfingerService::lookup($q); WebfingerService::lookup($q);
} }
$q = mb_substr($q, 1); $q = mb_substr($q, 1);
@ -717,21 +726,22 @@ class DirectMessageController extends Controller
$blocked->push($request->user()->profile_id); $blocked->push($request->user()->profile_id);
$results = Profile::select('id','domain','username') $results = Profile::select('id', 'domain', 'username')
->whereNotIn('id', $blocked) ->whereNotIn('id', $blocked)
->where('username','like','%'.$q.'%') ->where('username', 'like', '%'.$q.'%')
->orderBy('domain') ->orderBy('domain')
->limit(8) ->limit(8)
->get() ->get()
->map(function($r) { ->map(function ($r) {
$acct = AccountService::get($r->id); $acct = AccountService::get($r->id);
return [ return [
'local' => (bool) !$r->domain, 'local' => (bool) ! $r->domain,
'id' => (string) $r->id, 'id' => (string) $r->id,
'name' => $r->username, 'name' => $r->username,
'privacy' => true, 'privacy' => true,
'avatar' => $r->avatarUrl(), 'avatar' => $r->avatarUrl(),
'account' => $acct 'account' => $acct,
]; ];
}); });
@ -742,13 +752,13 @@ class DirectMessageController extends Controller
{ {
$this->validate($request, [ $this->validate($request, [
'pid' => 'required', 'pid' => 'required',
'sid' => 'required' 'sid' => 'required',
]); ]);
$pid = $request->input('pid'); $pid = $request->input('pid');
$sid = $request->input('sid'); $sid = $request->input('sid');
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$dms = DirectMessage::whereToId($request->user()->profile_id) $dms = DirectMessage::whereToId($request->user()->profile_id)
->whereFromId($pid) ->whereFromId($pid)
@ -756,7 +766,7 @@ class DirectMessageController extends Controller
->get(); ->get();
$now = now(); $now = now();
foreach($dms as $dm) { foreach ($dms as $dm) {
$dm->read_at = $now; $dm->read_at = $now;
$dm->save(); $dm->save();
} }
@ -767,11 +777,11 @@ class DirectMessageController extends Controller
public function mute(Request $request) public function mute(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'id' => 'required' 'id' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$fid = $request->input('id'); $fid = $request->input('id');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -780,7 +790,7 @@ class DirectMessageController extends Controller
'user_id' => $pid, 'user_id' => $pid,
'filterable_id' => $fid, 'filterable_id' => $fid,
'filterable_type' => 'App\Profile', 'filterable_type' => 'App\Profile',
'filter_type' => 'dm.mute' 'filter_type' => 'dm.mute',
] ]
); );
@ -790,11 +800,11 @@ class DirectMessageController extends Controller
public function unmute(Request $request) public function unmute(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'id' => 'required' 'id' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
$fid = $request->input('id'); $fid = $request->input('id');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
@ -820,7 +830,7 @@ class DirectMessageController extends Controller
'type' => 'Mention', 'type' => 'Mention',
'href' => $dm->recipient->permalink(), 'href' => $dm->recipient->permalink(),
'name' => $dm->recipient->emailUrl(), 'name' => $dm->recipient->emailUrl(),
] ],
]; ];
$body = [ $body = [
@ -855,7 +865,7 @@ class DirectMessageController extends Controller
]; ];
})->toArray(), })->toArray(),
'tag' => $tags, 'tag' => $tags,
] ],
]; ];
DirectDeliverPipeline::dispatch($profile, $url, $body)->onQueue('high'); DirectDeliverPipeline::dispatch($profile, $url, $body)->onQueue('high');
@ -872,14 +882,14 @@ class DirectMessageController extends Controller
], ],
'id' => $dm->status->permalink('#delete'), 'id' => $dm->status->permalink('#delete'),
'to' => [ 'to' => [
'https://www.w3.org/ns/activitystreams#Public' 'https://www.w3.org/ns/activitystreams#Public',
], ],
'type' => 'Delete', 'type' => 'Delete',
'actor' => $dm->status->profile->permalink(), 'actor' => $dm->status->profile->permalink(),
'object' => [ 'object' => [
'id' => $dm->status->url(), 'id' => $dm->status->url(),
'type' => 'Tombstone' 'type' => 'Tombstone',
] ],
]; ];
DirectDeletePipeline::dispatch($profile, $url, $body)->onQueue('high'); DirectDeletePipeline::dispatch($profile, $url, $body)->onQueue('high');
} }

View file

@ -11,6 +11,7 @@ use App\Services\BookmarkService;
use App\Services\ConfigCacheService; use App\Services\ConfigCacheService;
use App\Services\FollowerService; use App\Services\FollowerService;
use App\Services\HashtagService; use App\Services\HashtagService;
use App\Services\Internal\BeagleService;
use App\Services\LikeService; use App\Services\LikeService;
use App\Services\ReblogService; use App\Services\ReblogService;
use App\Services\SnowflakeService; use App\Services\SnowflakeService;
@ -420,4 +421,11 @@ class DiscoverController extends Controller
return response()->json($ids, 200, [], JSON_UNESCAPED_SLASHES); return response()->json($ids, 200, [], JSON_UNESCAPED_SLASHES);
} }
public function discoverNetworkTrending(Request $request)
{
abort_if(! $request->user(), 404);
return BeagleService::getDiscoverPosts();
}
} }

View file

@ -2,57 +2,42 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\Jobs\InboxPipeline\{ use App\Jobs\InboxPipeline\DeleteWorker;
DeleteWorker, use App\Jobs\InboxPipeline\InboxValidator;
InboxWorker, use App\Jobs\InboxPipeline\InboxWorker;
InboxValidator use App\Profile;
};
use App\Jobs\RemoteFollowPipeline\RemoteFollowPipeline;
use App\{
AccountLog,
Like,
Profile,
Status,
User
};
use App\Util\Lexer\Nickname;
use App\Util\Webfinger\Webfinger;
use Auth;
use Cache;
use Carbon\Carbon;
use Illuminate\Http\Request;
use League\Fractal;
use App\Util\Site\Nodeinfo;
use App\Util\ActivityPub\{
Helpers,
HttpSignature,
Outbox
};
use Zttp\Zttp;
use App\Services\InstanceService;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\InstanceService;
use App\Status;
use App\Util\Lexer\Nickname;
use App\Util\Site\Nodeinfo;
use App\Util\Webfinger\Webfinger;
use Cache;
use Illuminate\Http\Request;
class FederationController extends Controller class FederationController extends Controller
{ {
public function nodeinfoWellKnown() public function nodeinfoWellKnown()
{ {
abort_if(!config('federation.nodeinfo.enabled'), 404); abort_if(! config('federation.nodeinfo.enabled'), 404);
return response()->json(Nodeinfo::wellKnown(), 200, [], JSON_UNESCAPED_SLASHES) return response()->json(Nodeinfo::wellKnown(), 200, [], JSON_UNESCAPED_SLASHES)
->header('Access-Control-Allow-Origin','*'); ->header('Access-Control-Allow-Origin', '*');
} }
public function nodeinfo() public function nodeinfo()
{ {
abort_if(!config('federation.nodeinfo.enabled'), 404); abort_if(! config('federation.nodeinfo.enabled'), 404);
return response()->json(Nodeinfo::get(), 200, [], JSON_UNESCAPED_SLASHES) return response()->json(Nodeinfo::get(), 200, [], JSON_UNESCAPED_SLASHES)
->header('Access-Control-Allow-Origin','*'); ->header('Access-Control-Allow-Origin', '*');
} }
public function webfinger(Request $request) public function webfinger(Request $request)
{ {
if (!config('federation.webfinger.enabled') || if (! config('federation.webfinger.enabled') ||
!$request->has('resource') || ! $request->has('resource') ||
!$request->filled('resource') ! $request->filled('resource')
) { ) {
return response('', 400); return response('', 400);
} }
@ -60,55 +45,87 @@ class FederationController extends Controller
$resource = $request->input('resource'); $resource = $request->input('resource');
$domain = config('pixelfed.domain.app'); $domain = config('pixelfed.domain.app');
if(config('federation.activitypub.sharedInbox') && // Instance Actor
$resource == 'acct:' . $domain . '@' . $domain) { if (
config('federation.activitypub.sharedInbox') &&
$resource == 'acct:'.$domain.'@'.$domain
) {
$res = [ $res = [
'subject' => 'acct:' . $domain . '@' . $domain, 'subject' => 'acct:'.$domain.'@'.$domain,
'aliases' => [ 'aliases' => [
'https://' . $domain . '/i/actor' 'https://'.$domain.'/i/actor',
], ],
'links' => [ 'links' => [
[ [
'rel' => 'http://webfinger.net/rel/profile-page', 'rel' => 'http://webfinger.net/rel/profile-page',
'type' => 'text/html', 'type' => 'text/html',
'href' => 'https://' . $domain . '/site/kb/instance-actor' 'href' => 'https://'.$domain.'/site/kb/instance-actor',
], ],
[ [
'rel' => 'self', 'rel' => 'self',
'type' => 'application/activity+json', 'type' => 'application/activity+json',
'href' => 'https://' . $domain . '/i/actor' 'href' => 'https://'.$domain.'/i/actor',
] ],
] ],
]; ];
return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES); return response()->json($res, 200, [], JSON_UNESCAPED_SLASHES);
} }
$hash = hash('sha256', $resource);
$key = 'federation:webfinger:sha256:' . $hash; if (str_starts_with($resource, 'https://')) {
if($cached = Cache::get($key)) { if (str_starts_with($resource, 'https://'.$domain.'/users/')) {
$username = str_replace('https://'.$domain.'/users/', '', $resource);
if (strlen($username) > 15) {
return response('', 400);
}
$stripped = str_replace(['_', '.', '-'], '', $username);
if (! ctype_alnum($stripped)) {
return response('', 400);
}
$key = 'federation:webfinger:sha256:url-username:'.$username;
if ($cached = Cache::get($key)) {
return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES); return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
} }
if(strpos($resource, $domain) == false) { $profile = Profile::whereUsername($username)->first();
return response('', 400); if (! $profile || $profile->status !== null || $profile->domain) {
}
$parsed = Nickname::normalizeProfileUrl($resource);
if(empty($parsed) || $parsed['domain'] !== $domain) {
return response('', 400);
}
$username = $parsed['username'];
$profile = Profile::whereNull('domain')->whereUsername($username)->first();
if(!$profile || $profile->status !== null) {
return response('', 400); return response('', 400);
} }
$webfinger = (new Webfinger($profile))->generate(); $webfinger = (new Webfinger($profile))->generate();
Cache::put($key, $webfinger, 1209600); Cache::put($key, $webfinger, 1209600);
return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES) return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
->header('Access-Control-Allow-Origin','*'); ->header('Access-Control-Allow-Origin', '*');
} else {
return response('', 400);
}
}
$hash = hash('sha256', $resource);
$key = 'federation:webfinger:sha256:'.$hash;
if ($cached = Cache::get($key)) {
return response()->json($cached, 200, [], JSON_UNESCAPED_SLASHES);
}
if (strpos($resource, $domain) == false) {
return response('', 400);
}
$parsed = Nickname::normalizeProfileUrl($resource);
if (empty($parsed) || $parsed['domain'] !== $domain) {
return response('', 400);
}
$username = $parsed['username'];
$profile = Profile::whereUsername($username)->first();
if (! $profile || $profile->status !== null || $profile->domain) {
return response('', 400);
}
$webfinger = (new Webfinger($profile))->generate();
Cache::put($key, $webfinger, 1209600);
return response()->json($webfinger, 200, [], JSON_UNESCAPED_SLASHES)
->header('Access-Control-Allow-Origin', '*');
} }
public function hostMeta(Request $request) public function hostMeta(Request $request)
{ {
abort_if(!config('federation.webfinger.enabled'), 404); abort_if(! config('federation.webfinger.enabled'), 404);
$path = route('well-known.webfinger'); $path = route('well-known.webfinger');
$xml = '<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" type="application/xrd+xml" template="'.$path.'?resource={uri}"/></XRD>'; $xml = '<?xml version="1.0" encoding="UTF-8"?><XRD xmlns="http://docs.oasis-open.org/ns/xri/xrd-1.0"><Link rel="lrdd" type="application/xrd+xml" template="'.$path.'?resource={uri}"/></XRD>';
@ -118,19 +135,19 @@ class FederationController extends Controller
public function userOutbox(Request $request, $username) public function userOutbox(Request $request, $username)
{ {
abort_if(!config_cache('federation.activitypub.enabled'), 404); abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
if(!$request->wantsJson()) { if (! $request->wantsJson()) {
return redirect('/' . $username); return redirect('/'.$username);
} }
$id = AccountService::usernameToId($username); $id = AccountService::usernameToId($username);
abort_if(!$id, 404); abort_if(! $id, 404);
$account = AccountService::get($id); $account = AccountService::get($id);
abort_if(!$account || !isset($account['statuses_count']), 404); abort_if(! $account || ! isset($account['statuses_count']), 404);
$res = [ $res = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => 'https://' . config('pixelfed.domain.app') . '/users/' . $username . '/outbox', 'id' => 'https://'.config('pixelfed.domain.app').'/users/'.$username.'/outbox',
'type' => 'OrderedCollection', 'type' => 'OrderedCollection',
'totalItems' => $account['statuses_count'] ?? 0, 'totalItems' => $account['statuses_count'] ?? 0,
]; ];
@ -140,135 +157,145 @@ class FederationController extends Controller
public function userInbox(Request $request, $username) public function userInbox(Request $request, $username)
{ {
abort_if(!config_cache('federation.activitypub.enabled'), 404); abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
abort_if(!config('federation.activitypub.inbox'), 404); abort_if(! config('federation.activitypub.inbox'), 404);
$headers = $request->headers->all(); $headers = $request->headers->all();
$payload = $request->getContent(); $payload = $request->getContent();
if(!$payload || empty($payload)) { if (! $payload || empty($payload)) {
return; return;
} }
$obj = json_decode($payload, true, 8); $obj = json_decode($payload, true, 8);
if(!isset($obj['id'])) { if (! isset($obj['id'])) {
return; return;
} }
$domain = parse_url($obj['id'], PHP_URL_HOST); $domain = parse_url($obj['id'], PHP_URL_HOST);
if(in_array($domain, InstanceService::getBannedDomains())) { if (in_array($domain, InstanceService::getBannedDomains())) {
return; return;
} }
if(isset($obj['type']) && $obj['type'] === 'Delete') { if (isset($obj['type']) && $obj['type'] === 'Delete') {
if(isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) { if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
if($obj['object']['type'] === 'Person') { if ($obj['object']['type'] === 'Person') {
if(Profile::whereRemoteUrl($obj['object']['id'])->exists()) { if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox'); dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
return; return;
} }
} }
if($obj['object']['type'] === 'Tombstone') { if ($obj['object']['type'] === 'Tombstone') {
if(Status::whereObjectUrl($obj['object']['id'])->exists()) { if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
dispatch(new DeleteWorker($headers, $payload))->onQueue('delete'); dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
return; return;
} }
} }
if($obj['object']['type'] === 'Story') { if ($obj['object']['type'] === 'Story') {
dispatch(new DeleteWorker($headers, $payload))->onQueue('story'); dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
return; return;
} }
} }
return; return;
} else if( isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) { } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
dispatch(new InboxValidator($username, $headers, $payload))->onQueue('follow'); dispatch(new InboxValidator($username, $headers, $payload))->onQueue('follow');
} else { } else {
dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high'); dispatch(new InboxValidator($username, $headers, $payload))->onQueue('high');
} }
return;
} }
public function sharedInbox(Request $request) public function sharedInbox(Request $request)
{ {
abort_if(!config_cache('federation.activitypub.enabled'), 404); abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
abort_if(!config('federation.activitypub.sharedInbox'), 404); abort_if(! config('federation.activitypub.sharedInbox'), 404);
$headers = $request->headers->all(); $headers = $request->headers->all();
$payload = $request->getContent(); $payload = $request->getContent();
if(!$payload || empty($payload)) { if (! $payload || empty($payload)) {
return; return;
} }
$obj = json_decode($payload, true, 8); $obj = json_decode($payload, true, 8);
if(!isset($obj['id'])) { if (! isset($obj['id'])) {
return; return;
} }
$domain = parse_url($obj['id'], PHP_URL_HOST); $domain = parse_url($obj['id'], PHP_URL_HOST);
if(in_array($domain, InstanceService::getBannedDomains())) { if (in_array($domain, InstanceService::getBannedDomains())) {
return; return;
} }
if(isset($obj['type']) && $obj['type'] === 'Delete') { if (isset($obj['type']) && $obj['type'] === 'Delete') {
if(isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) { if (isset($obj['object']) && isset($obj['object']['type']) && isset($obj['object']['id'])) {
if($obj['object']['type'] === 'Person') { if ($obj['object']['type'] === 'Person') {
if(Profile::whereRemoteUrl($obj['object']['id'])->exists()) { if (Profile::whereRemoteUrl($obj['object']['id'])->exists()) {
dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox'); dispatch(new DeleteWorker($headers, $payload))->onQueue('inbox');
return; return;
} }
} }
if($obj['object']['type'] === 'Tombstone') { if ($obj['object']['type'] === 'Tombstone') {
if(Status::whereObjectUrl($obj['object']['id'])->exists()) { if (Status::whereObjectUrl($obj['object']['id'])->exists()) {
dispatch(new DeleteWorker($headers, $payload))->onQueue('delete'); dispatch(new DeleteWorker($headers, $payload))->onQueue('delete');
return; return;
} }
} }
if($obj['object']['type'] === 'Story') { if ($obj['object']['type'] === 'Story') {
dispatch(new DeleteWorker($headers, $payload))->onQueue('story'); dispatch(new DeleteWorker($headers, $payload))->onQueue('story');
return; return;
} }
} }
return; return;
} else if( isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) { } elseif (isset($obj['type']) && in_array($obj['type'], ['Follow', 'Accept'])) {
dispatch(new InboxWorker($headers, $payload))->onQueue('follow'); dispatch(new InboxWorker($headers, $payload))->onQueue('follow');
} else { } else {
dispatch(new InboxWorker($headers, $payload))->onQueue('shared'); dispatch(new InboxWorker($headers, $payload))->onQueue('shared');
} }
return;
} }
public function userFollowing(Request $request, $username) public function userFollowing(Request $request, $username)
{ {
abort_if(!config_cache('federation.activitypub.enabled'), 404); abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
$id = AccountService::usernameToId($username); $id = AccountService::usernameToId($username);
abort_if(!$id, 404); abort_if(! $id, 404);
$account = AccountService::get($id); $account = AccountService::get($id);
abort_if(!$account || !isset($account['following_count']), 404); abort_if(! $account || ! isset($account['following_count']), 404);
$obj = [ $obj = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $request->getUri(), 'id' => $request->getUri(),
'type' => 'OrderedCollection', 'type' => 'OrderedCollection',
'totalItems' => $account['following_count'] ?? 0, 'totalItems' => $account['following_count'] ?? 0,
]; ];
return response()->json($obj)->header('Content-Type', 'application/activity+json'); return response()->json($obj)->header('Content-Type', 'application/activity+json');
} }
public function userFollowers(Request $request, $username) public function userFollowers(Request $request, $username)
{ {
abort_if(!config_cache('federation.activitypub.enabled'), 404); abort_if(! (bool) config_cache('federation.activitypub.enabled'), 404);
$id = AccountService::usernameToId($username); $id = AccountService::usernameToId($username);
abort_if(!$id, 404); abort_if(! $id, 404);
$account = AccountService::get($id); $account = AccountService::get($id);
abort_if(!$account || !isset($account['followers_count']), 404); abort_if(! $account || ! isset($account['followers_count']), 404);
$obj = [ $obj = [
'@context' => 'https://www.w3.org/ns/activitystreams', '@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $request->getUri(), 'id' => $request->getUri(),
'type' => 'OrderedCollection', 'type' => 'OrderedCollection',
'totalItems' => $account['followers_count'] ?? 0, 'totalItems' => $account['followers_count'] ?? 0,
]; ];
return response()->json($obj)->header('Content-Type', 'application/activity+json'); return response()->json($obj)->header('Content-Type', 'application/activity+json');
} }
} }

View file

@ -0,0 +1,671 @@
<?php
namespace App\Http\Controllers;
use App\Instance;
use App\Models\Group;
use App\Models\GroupBlock;
use App\Models\GroupCategory;
use App\Models\GroupInvitation;
use App\Models\GroupLike;
use App\Models\GroupLimit;
use App\Models\GroupMember;
use App\Models\GroupPost;
use App\Models\GroupReport;
use App\Profile;
use App\Services\AccountService;
use App\Services\GroupService;
use App\Services\HashidService;
use App\Services\StatusService;
use App\Status;
use App\User;
use Illuminate\Http\Request;
use Storage;
class GroupController extends GroupFederationController
{
public function __construct()
{
$this->middleware('auth');
abort_unless(config('groups.enabled'), 404);
}
public function index(Request $request)
{
abort_if(! $request->user(), 404);
return view('layouts.spa');
}
public function home(Request $request)
{
abort_if(! $request->user(), 404);
return view('layouts.spa');
}
public function show(Request $request, $id, $path = false)
{
$group = Group::find($id);
if (! $group || $group->status) {
return response()->view('groups.unavailable')->setStatusCode(404);
}
if ($request->wantsJson()) {
return $this->showGroupObject($group);
}
return view('layouts.spa', compact('id', 'path'));
}
public function showStatus(Request $request, $gid, $sid)
{
$group = Group::find($gid);
$pid = optional($request->user())->profile_id ?? false;
if (! $group || $group->status) {
return response()->view('groups.unavailable')->setStatusCode(404);
}
if ($group->is_private) {
abort_if(! $request->user(), 404);
abort_if(! $group->isMember($pid), 404);
}
$gp = GroupPost::whereGroupId($gid)
->findOrFail($sid);
return view('layouts.spa', compact('group', 'gp'));
}
public function getGroup(Request $request, $id)
{
$group = Group::whereNull('status')->findOrFail($id);
$pid = optional($request->user())->profile_id ?? false;
$group = $this->toJson($group, $pid);
return response()->json($group, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
public function showStatusLikes(Request $request, $id, $sid)
{
$group = Group::findOrFail($id);
$user = $request->user();
$pid = $user->profile_id;
abort_if(! $group->isMember($pid), 404);
$status = GroupPost::whereGroupId($id)->findOrFail($sid);
$likes = GroupLike::whereStatusId($sid)
->cursorPaginate(10)
->map(function ($l) use ($group) {
$account = AccountService::get($l->profile_id);
$account['url'] = "/groups/{$group->id}/user/{$account['id']}";
return $account;
})
->filter(function ($l) {
return $l && isset($l['id']);
})
->values();
return $likes;
}
public function groupSettings(Request $request, $id)
{
abort_if(! $request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(! $group->isMember($pid), 404);
abort_if(! in_array($group->selfRole($pid), ['founder', 'admin']), 404);
return view('groups.settings', compact('group'));
}
public function joinGroup(Request $request, $id)
{
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if($group->isMember($pid), 404);
if (! $request->user()->is_admin) {
abort_if(GroupService::getRejoinTimeout($group->id, $pid), 422, 'Cannot re-join this group for 24 hours after leaving or cancelling a request to join');
}
$member = new GroupMember;
$member->group_id = $group->id;
$member->profile_id = $pid;
$member->role = 'member';
$member->local_group = true;
$member->local_profile = true;
$member->join_request = $group->is_private;
$member->save();
GroupService::delSelf($group->id, $pid);
GroupService::log(
$group->id,
$pid,
'group:joined',
null,
GroupMember::class,
$member->id
);
$group = $this->toJson($group, $pid);
return $group;
}
public function updateGroup(Request $request, $id)
{
$this->validate($request, [
'description' => 'nullable|max:500',
'membership' => 'required|in:all,local,private',
'avatar' => 'nullable',
'header' => 'nullable',
'discoverable' => 'required',
'activitypub' => 'required',
'is_nsfw' => 'required',
'category' => 'required|string|in:'.implode(',', GroupService::categories()),
]);
$pid = $request->user()->profile_id;
$group = Group::whereProfileId($pid)->findOrFail($id);
$member = GroupMember::whereGroupId($group->id)->whereProfileId($pid)->firstOrFail();
abort_if($member->role != 'founder', 403, 'Invalid group permission');
$metadata = $group->metadata;
$len = $group->is_private ? 12 : 4;
if ($request->hasFile('avatar')) {
$avatar = $request->file('avatar');
if ($avatar) {
if (isset($metadata['avatar']) &&
isset($metadata['avatar']['path']) &&
Storage::exists($metadata['avatar']['path'])
) {
Storage::delete($metadata['avatar']['path']);
}
$fileName = 'avatar_'.strtolower(str_random($len)).'.'.$avatar->extension();
$path = $avatar->storePubliclyAs('public/g/'.$group->id.'/meta', $fileName);
$url = url(Storage::url($path));
$metadata['avatar'] = [
'path' => $path,
'url' => $url,
'updated_at' => now(),
];
}
}
if ($request->hasFile('header')) {
$header = $request->file('header');
if ($header) {
if (isset($metadata['header']) &&
isset($metadata['header']['path']) &&
Storage::exists($metadata['header']['path'])
) {
Storage::delete($metadata['header']['path']);
}
$fileName = 'header_'.strtolower(str_random($len)).'.'.$header->extension();
$path = $header->storePubliclyAs('public/g/'.$group->id.'/meta', $fileName);
$url = url(Storage::url($path));
$metadata['header'] = [
'path' => $path,
'url' => $url,
'updated_at' => now(),
];
}
}
$cat = GroupService::categoryById($group->category_id);
if ($request->category !== $cat['name']) {
$group->category_id = GroupCategory::whereName($request->category)->first()->id;
}
$changes = null;
$group->description = e($request->input('description', null));
$group->is_private = $request->input('membership') == 'private';
$group->local_only = $request->input('membership') == 'local';
$group->activitypub = $request->input('activitypub') == 'true';
$group->discoverable = $request->input('discoverable') == 'true';
$group->is_nsfw = $request->input('is_nsfw') == 'true';
$group->metadata = $metadata;
if ($group->isDirty()) {
$changes = $group->getDirty();
}
$group->save();
GroupService::log(
$group->id,
$pid,
'group:settings:updated',
$changes
);
GroupService::del($group->id);
$res = $this->toJson($group, $pid);
return $res;
}
protected function toJson($group, $pid = false)
{
return GroupService::get($group->id, $pid);
}
public function groupLeave(Request $request, $id)
{
abort_if(! $request->user(), 404);
$pid = $request->user()->profile_id;
$group = Group::findOrFail($id);
abort_if($pid == $group->profile_id, 422, 'Cannot leave a group you created');
abort_if(! $group->isMember($pid), 403, 'Not a member of group.');
GroupMember::whereGroupId($group->id)->whereProfileId($pid)->delete();
GroupService::del($group->id);
GroupService::delSelf($group->id, $pid);
GroupService::setRejoinTimeout($group->id, $pid);
return [200];
}
public function cancelJoinRequest(Request $request, $id)
{
abort_if(! $request->user(), 404);
$pid = $request->user()->profile_id;
$group = Group::findOrFail($id);
abort_if($pid == $group->profile_id, 422, 'Cannot leave a group you created');
abort_if($group->isMember($pid), 422, 'Cannot cancel approved join request, please leave group instead.');
GroupMember::whereGroupId($group->id)->whereProfileId($pid)->delete();
GroupService::del($group->id);
GroupService::delSelf($group->id, $pid);
GroupService::setRejoinTimeout($group->id, $pid);
return [200];
}
public function metaBlockSearch(Request $request, $id)
{
abort_if(! $request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(! $group->isMember($pid), 404);
abort_if(! in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$type = $request->input('type');
$item = $request->input('item');
switch ($type) {
case 'instance':
$res = Instance::whereDomain($item)->first();
if ($res) {
abort_if(GroupBlock::whereGroupId($group->id)->whereInstanceId($res->id)->exists(), 400);
}
break;
case 'user':
$res = Profile::whereUsername($item)->first();
if ($res) {
abort_if(GroupBlock::whereGroupId($group->id)->whereProfileId($res->id)->exists(), 400);
}
if ($res->user_id != null) {
abort_if(User::whereIsAdmin(true)->whereId($res->user_id)->exists(), 400);
}
break;
}
return response()->json((bool) $res, ($res ? 200 : 404));
}
public function reportCreate(Request $request, $id)
{
abort_if(! $request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(! $group->isMember($pid), 404);
$id = $request->input('id');
$type = $request->input('type');
$types = [
// original 3
'spam',
'sensitive',
'abusive',
// new
'underage',
'violence',
'copyright',
'impersonation',
'scam',
'terrorism',
];
$gp = GroupPost::whereGroupId($group->id)->find($id);
abort_if(! $gp, 422, 'Cannot report an invalid or deleted post');
abort_if(! in_array($type, $types), 422, 'Invalid report type');
abort_if($gp->profile_id === $pid, 422, 'Cannot report your own post');
abort_if(
GroupReport::whereGroupId($group->id)
->whereProfileId($pid)
->whereItemType(GroupPost::class)
->whereItemId($id)
->exists(),
422,
'You already reported this'
);
$report = new GroupReport();
$report->group_id = $group->id;
$report->profile_id = $pid;
$report->type = $type;
$report->item_type = GroupPost::class;
$report->item_id = $id;
$report->open = true;
$report->save();
GroupService::log(
$group->id,
$pid,
'group:report:create',
[
'type' => $type,
'report_id' => $report->id,
'status_id' => $gp->status_id,
'profile_id' => $gp->profile_id,
'username' => optional(AccountService::get($gp->profile_id))['acct'],
'gpid' => $gp->id,
'url' => $gp->url(),
],
GroupReport::class,
$report->id
);
return response([200]);
}
public function reportAction(Request $request, $id)
{
abort_if(! $request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(! $group->isMember($pid), 404);
abort_if(! in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$this->validate($request, [
'action' => 'required|in:cw,delete,ignore',
'id' => 'required|string',
]);
$action = $request->input('action');
$id = $request->input('id');
$report = GroupReport::whereGroupId($group->id)
->findOrFail($id);
$status = Status::findOrFail($report->item_id);
$gp = GroupPost::whereGroupId($group->id)
->whereStatusId($status->id)
->firstOrFail();
switch ($action) {
case 'cw':
$status->is_nsfw = true;
$status->save();
StatusService::del($status->id);
GroupReport::whereGroupId($group->id)
->whereItemType($report->item_type)
->whereItemId($report->item_id)
->update(['open' => false]);
GroupService::log(
$group->id,
$pid,
'group:moderation:action',
[
'type' => 'cw',
'report_id' => $report->id,
'status_id' => $status->id,
'profile_id' => $status->profile_id,
'status_url' => $gp->url(),
],
GroupReport::class,
$report->id
);
return response()->json([200]);
break;
case 'ignore':
GroupReport::whereGroupId($group->id)
->whereItemType($report->item_type)
->whereItemId($report->item_id)
->update(['open' => false]);
GroupService::log(
$group->id,
$pid,
'group:moderation:action',
[
'type' => 'ignore',
'report_id' => $report->id,
'status_id' => $status->id,
'profile_id' => $status->profile_id,
'status_url' => $gp->url(),
],
GroupReport::class,
$report->id
);
return response()->json([200]);
break;
}
}
public function getMemberInteractionLimits(Request $request, $id)
{
abort_if(! $request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(! $group->isMember($pid), 404);
abort_if(! in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$profile_id = $request->input('profile_id');
abort_if(! $group->isMember($profile_id), 404);
$limits = GroupService::getInteractionLimits($group->id, $profile_id);
return response()->json($limits);
}
public function updateMemberInteractionLimits(Request $request, $id)
{
abort_if(! $request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(! $group->isMember($pid), 404);
abort_if(! in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$this->validate($request, [
'profile_id' => 'required|exists:profiles,id',
'can_post' => 'required',
'can_comment' => 'required',
'can_like' => 'required',
]);
$member = $request->input('profile_id');
$can_post = $request->input('can_post');
$can_comment = $request->input('can_comment');
$can_like = $request->input('can_like');
$account = AccountService::get($member);
abort_if(! $account, 422, 'Invalid profile');
abort_if(! $group->isMember($member), 422, 'Invalid profile');
$limit = GroupLimit::firstOrCreate([
'profile_id' => $member,
'group_id' => $group->id,
]);
if ($limit->wasRecentlyCreated) {
abort_if(GroupLimit::whereGroupId($group->id)->count() >= 25, 422, 'limit_reached');
}
$previousLimits = $limit->limits;
$limit->limits = [
'can_post' => $can_post,
'can_comment' => $can_comment,
'can_like' => $can_like,
];
$limit->save();
GroupService::clearInteractionLimits($group->id, $member);
GroupService::log(
$group->id,
$pid,
'group:member-limits:updated',
[
'profile_id' => $account['id'],
'username' => $account['username'],
'previousLimits' => $previousLimits,
'newLimits' => $limit->limits,
],
GroupLimit::class,
$limit->id
);
return $request->all();
}
public function showProfile(Request $request, $id, $pid)
{
$group = Group::find($id);
if (! $group || $group->status) {
return response()->view('groups.unavailable')->setStatusCode(404);
}
return view('layouts.spa');
}
public function showProfileByUsername(Request $request, $id, $pid)
{
abort_if(! $request->user(), 404);
if (! $request->user()) {
return redirect("/{$pid}");
}
$group = Group::find($id);
$cid = $request->user()->profile_id;
if (! $group || $group->status) {
return response()->view('groups.unavailable')->setStatusCode(404);
}
if (! $group->isMember($cid)) {
return redirect("/{$pid}");
}
$profile = Profile::whereUsername($pid)->first();
if (! $group->isMember($profile->id)) {
return redirect("/{$pid}");
}
if ($profile) {
$url = url("/groups/{$id}/user/{$profile->id}");
return redirect($url);
}
abort(404, 'Invalid username');
}
public function groupInviteLanding(Request $request, $id)
{
abort(404, 'Not yet implemented');
$group = Group::findOrFail($id);
return view('groups.invite', compact('group'));
}
public function groupShortLinkRedirect(Request $request, $hid)
{
$gid = HashidService::decode($hid);
$group = Group::findOrFail($gid);
return redirect($group->url());
}
public function groupInviteClaim(Request $request, $id)
{
$group = GroupService::get($id);
abort_if(! $group || empty($group), 404);
return view('groups.invite-claim', compact('group'));
}
public function groupMemberInviteCheck(Request $request, $id)
{
abort_if(! $request->user(), 404);
$pid = $request->user()->profile_id;
$group = Group::findOrFail($id);
abort_if($group->isMember($pid), 422, 'Already a member');
$exists = GroupInvitation::whereGroupId($id)->whereToProfileId($pid)->exists();
return response()->json([
'gid' => $id,
'can_join' => (bool) $exists,
]);
}
public function groupMemberInviteAccept(Request $request, $id)
{
abort_if(! $request->user(), 404);
$pid = $request->user()->profile_id;
$group = Group::findOrFail($id);
abort_if($group->isMember($pid), 422, 'Already a member');
abort_if(! GroupInvitation::whereGroupId($id)->whereToProfileId($pid)->exists(), 422);
$gm = new GroupMember;
$gm->group_id = $id;
$gm->profile_id = $pid;
$gm->role = 'member';
$gm->local_group = $group->local;
$gm->local_profile = true;
$gm->join_request = false;
$gm->save();
GroupInvitation::whereGroupId($id)->whereToProfileId($pid)->delete();
GroupService::del($id);
GroupService::delSelf($id, $pid);
return ['next_url' => $group->url()];
}
public function groupMemberInviteDecline(Request $request, $id)
{
abort_if(! $request->user(), 404);
$pid = $request->user()->profile_id;
$group = Group::findOrFail($id);
abort_if($group->isMember($pid), 422, 'Already a member');
return ['next_url' => '/'];
}
}

View file

@ -0,0 +1,103 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use App\Models\Group;
use App\Models\GroupPost;
use App\Status;
use App\Models\InstanceActor;
use App\Services\MediaService;
class GroupFederationController extends Controller
{
public function getGroupObject(Request $request, $id)
{
$group = Group::whereLocal(true)->whereActivitypub(true)->findOrFail($id);
$res = $this->showGroupObject($group);
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function showGroupObject($group)
{
return Cache::remember('ap:groups:object:' . $group->id, 3600, function() use($group) {
return [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $group->url(),
'inbox' => $group->permalink('/inbox'),
'name' => $group->name,
'outbox' => $group->permalink('/outbox'),
'summary' => $group->description,
'type' => 'Group',
'attributedTo' => [
'type' => 'Person',
'id' => $group->admin->permalink()
],
// 'endpoints' => [
// 'sharedInbox' => config('app.url') . '/f/inbox'
// ],
'preferredUsername' => 'gid_' . $group->id,
'publicKey' => [
'id' => $group->permalink('#main-key'),
'owner' => $group->permalink(),
'publicKeyPem' => InstanceActor::first()->public_key,
],
'url' => $group->permalink()
];
if($group->metadata && isset($group->metadata['avatar'])) {
$res['icon'] = [
'type' => 'Image',
'url' => $group->metadata['avatar']['url']
];
}
if($group->metadata && isset($group->metadata['header'])) {
$res['image'] = [
'type' => 'Image',
'url' => $group->metadata['header']['url']
];
}
ksort($res);
return $res;
});
}
public function getStatusObject(Request $request, $gid, $sid)
{
$group = Group::whereLocal(true)->whereActivitypub(true)->findOrFail($gid);
$gp = GroupPost::whereGroupId($gid)->findOrFail($sid);
$status = Status::findOrFail($gp->status_id);
// permission check
$res = [
'@context' => 'https://www.w3.org/ns/activitystreams',
'id' => $gp->url(),
'type' => 'Note',
'summary' => null,
'content' => $status->rendered ?? $status->caption,
'inReplyTo' => null,
'published' => $status->created_at->toAtomString(),
'url' => $gp->url(),
'attributedTo' => $status->profile->permalink(),
'to' => [
'https://www.w3.org/ns/activitystreams#Public',
$group->permalink('/followers'),
],
'cc' => [],
'sensitive' => (bool) $status->is_nsfw,
'attachment' => MediaService::activitypub($status->id),
'target' => [
'type' => 'Collection',
'id' => $group->permalink('/wall'),
'attributedTo' => $group->permalink()
]
];
// ksort($res);
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
}

View file

@ -0,0 +1,10 @@
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class GroupPostController extends Controller
{
//
}

View file

@ -0,0 +1,83 @@
<?php
namespace App\Http\Controllers\Groups;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\GroupService;
use App\Models\Group;
use App\Models\GroupMember;
class CreateGroupsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function checkCreatePermission(Request $request)
{
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$config = GroupService::config();
if($request->user()->is_admin) {
$allowed = true;
} else {
$max = $config['limits']['user']['create']['max'];
$allowed = Group::whereProfileId($pid)->count() <= $max;
}
return ['permission' => (bool) $allowed];
}
public function storeGroup(Request $request)
{
abort_if(!$request->user(), 404);
$this->validate($request, [
'name' => 'required',
'description' => 'nullable|max:500',
'membership' => 'required|in:public,private,local'
]);
$pid = $request->user()->profile_id;
$config = GroupService::config();
abort_if($config['limits']['user']['create']['new'] == false && $request->user()->is_admin == false, 422, 'Invalid operation');
$max = $config['limits']['user']['create']['max'];
// abort_if(Group::whereProfileId($pid)->count() <= $max, 422, 'Group limit reached');
$group = new Group;
$group->profile_id = $pid;
$group->name = $request->input('name');
$group->description = $request->input('description', null);
$group->is_private = $request->input('membership') == 'private';
$group->local_only = $request->input('membership') == 'local';
$group->metadata = $request->input('configuration');
$group->save();
GroupService::log($group->id, $pid, 'group:created');
$member = new GroupMember;
$member->group_id = $group->id;
$member->profile_id = $pid;
$member->role = 'founder';
$member->local_group = true;
$member->local_profile = true;
$member->save();
GroupService::log(
$group->id,
$pid,
'group:joined',
null,
GroupMember::class,
$member->id
);
return [
'id' => $group->id,
'url' => $group->url()
];
}
}

View file

@ -0,0 +1,353 @@
<?php
namespace App\Http\Controllers\Groups;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\GroupService;
use App\Instance;
use App\Profile;
use App\Models\Group;
use App\Models\GroupBlock;
use App\Models\GroupCategory;
use App\Models\GroupInteraction;
use App\Models\GroupPost;
use App\Models\GroupMember;
use App\Models\GroupReport;
use App\Services\Groups\GroupAccountService;
use App\Services\Groups\GroupPostService;
class GroupsAdminController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getAdminTabs(Request $request, $id)
{
abort_if(!$request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
abort_if($pid !== $group->profile_id, 404);
$reqs = GroupMember::whereGroupId($group->id)->whereJoinRequest(true)->count();
$mods = GroupReport::whereGroupId($group->id)->whereOpen(true)->count();
$tabs = [
'moderation_count' => $mods > 99 ? '99+' : $mods,
'request_count' => $reqs > 99 ? '99+' : $reqs
];
return response()->json($tabs);
}
public function getInteractionLogs(Request $request, $id)
{
abort_if(!$request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$logs = GroupInteraction::whereGroupId($id)
->latest()
->paginate(10)
->map(function($log) use($group) {
return [
'id' => $log->id,
'profile' => GroupAccountService::get($group->id, $log->profile_id),
'type' => $log->type,
'metadata' => $log->metadata,
'created_at' => $log->created_at->format('c')
];
});
return response()->json($logs, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function getBlocks(Request $request, $id)
{
abort_if(!$request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$blocks = [
'instances' => GroupBlock::whereGroupId($group->id)->whereNotNull('instance_id')->whereModerated(false)->latest()->take(3)->pluck('name'),
'users' => GroupBlock::whereGroupId($group->id)->whereNotNull('profile_id')->whereIsUser(true)->latest()->take(3)->pluck('name'),
'moderated' => GroupBlock::whereGroupId($group->id)->whereNotNull('instance_id')->whereModerated(true)->latest()->take(3)->pluck('name')
];
return response()->json($blocks, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function exportBlocks(Request $request, $id)
{
abort_if(!$request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$blocks = [
'instances' => GroupBlock::whereGroupId($group->id)->whereNotNull('instance_id')->whereModerated(false)->latest()->pluck('name'),
'users' => GroupBlock::whereGroupId($group->id)->whereNotNull('profile_id')->whereIsUser(true)->latest()->pluck('name'),
'moderated' => GroupBlock::whereGroupId($group->id)->whereNotNull('instance_id')->whereModerated(true)->latest()->pluck('name')
];
$blocks['_created_at'] = now()->format('c');
$blocks['_version'] = '1.0.0';
ksort($blocks);
return response()->streamDownload(function() use($blocks) {
echo json_encode($blocks, JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
});
}
public function addBlock(Request $request, $id)
{
abort_if(!$request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$this->validate($request, [
'item' => 'required',
'type' => 'required|in:instance,user,moderate'
]);
$item = $request->input('item');
$type = $request->input('type');
switch($type) {
case 'instance':
$instance = Instance::whereDomain($item)->first();
abort_if(!$instance, 422, 'This domain either isn\'nt known or is invalid');
$gb = new GroupBlock;
$gb->group_id = $group->id;
$gb->admin_id = $pid;
$gb->instance_id = $instance->id;
$gb->name = $instance->domain;
$gb->is_user = false;
$gb->moderated = false;
$gb->save();
GroupService::log(
$group->id,
$pid,
'group:admin:block:instance',
[
'domain' => $instance->domain
],
GroupBlock::class,
$gb->id
);
return [200];
break;
case 'user':
$profile = Profile::whereUsername($item)->first();
abort_if(!$profile, 422, 'This user either isn\'nt known or is invalid');
$gb = new GroupBlock;
$gb->group_id = $group->id;
$gb->admin_id = $pid;
$gb->profile_id = $profile->id;
$gb->name = $profile->username;
$gb->is_user = true;
$gb->moderated = false;
$gb->save();
GroupService::log(
$group->id,
$pid,
'group:admin:block:user',
[
'username' => $profile->username,
'domain' => $profile->domain
],
GroupBlock::class,
$gb->id
);
return [200];
break;
case 'moderate':
$instance = Instance::whereDomain($item)->first();
abort_if(!$instance, 422, 'This domain either isn\'nt known or is invalid');
$gb = new GroupBlock;
$gb->group_id = $group->id;
$gb->admin_id = $pid;
$gb->instance_id = $instance->id;
$gb->name = $instance->domain;
$gb->is_user = false;
$gb->moderated = true;
$gb->save();
GroupService::log(
$group->id,
$pid,
'group:admin:moderate:instance',
[
'domain' => $instance->domain
],
GroupBlock::class,
$gb->id
);
return [200];
break;
default:
return response()->json([], 422, []);
break;
}
}
public function undoBlock(Request $request, $id)
{
abort_if(!$request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$this->validate($request, [
'item' => 'required',
'type' => 'required|in:instance,user,moderate'
]);
$item = $request->input('item');
$type = $request->input('type');
switch($type) {
case 'instance':
$instance = Instance::whereDomain($item)->first();
abort_if(!$instance, 422, 'This domain either isn\'nt known or is invalid');
$gb = GroupBlock::whereGroupId($group->id)
->whereInstanceId($instance->id)
->whereModerated(false)
->first();
abort_if(!$gb, 422, 'Invalid group block');
GroupService::log(
$group->id,
$pid,
'group:admin:unblock:instance',
[
'domain' => $instance->domain
],
GroupBlock::class,
$gb->id
);
$gb->delete();
return [200];
break;
case 'user':
$profile = Profile::whereUsername($item)->first();
abort_if(!$profile, 422, 'This user either isn\'nt known or is invalid');
$gb = GroupBlock::whereGroupId($group->id)
->whereProfileId($profile->id)
->whereIsUser(true)
->first();
abort_if(!$gb, 422, 'Invalid group block');
GroupService::log(
$group->id,
$pid,
'group:admin:unblock:user',
[
'username' => $profile->username,
'domain' => $profile->domain
],
GroupBlock::class,
$gb->id
);
$gb->delete();
return [200];
break;
case 'moderate':
$instance = Instance::whereDomain($item)->first();
abort_if(!$instance, 422, 'This domain either isn\'nt known or is invalid');
$gb = GroupBlock::whereGroupId($group->id)
->whereInstanceId($instance->id)
->whereModerated(true)
->first();
abort_if(!$gb, 422, 'Invalid group block');
GroupService::log(
$group->id,
$pid,
'group:admin:moderate:instance',
[
'domain' => $instance->domain
],
GroupBlock::class,
$gb->id
);
$gb->delete();
return [200];
break;
default:
return response()->json([], 422, []);
break;
}
}
public function getReportList(Request $request, $id)
{
abort_if(!$request->user(), 404);
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$scope = $request->input('scope', 'open');
$list = GroupReport::selectRaw('id, profile_id, item_type, item_id, type, created_at, count(*) as total')
->whereGroupId($group->id)
->groupBy('item_id')
->when($scope == 'open', function($query, $scope) {
return $query->whereOpen(true);
})
->latest()
->simplePaginate(10)
->map(function($report) use($group) {
$res = [
'id' => (string) $report->id,
'profile' => GroupAccountService::get($group->id, $report->profile_id),
'type' => $report->type,
'created_at' => $report->created_at->format('c'),
'total_count' => $report->total
];
if($report->item_type === GroupPost::class) {
$res['status'] = GroupPostService::get($group->id, $report->item_id);
}
return $res;
});
return response()->json($list, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
}

View file

@ -0,0 +1,84 @@
<?php
namespace App\Http\Controllers\Groups;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\GroupService;
use App\Models\Group;
use App\Models\GroupCategory;
use App\Models\GroupMember;
use App\Services\Groups\GroupAccountService;
class GroupsApiController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
protected function toJson($group, $pid = false)
{
return GroupService::get($group->id, $pid);
}
public function getConfig(Request $request)
{
return GroupService::config();
}
public function getGroupAccount(Request $request, $gid, $pid)
{
$res = GroupAccountService::get($gid, $pid);
return response()->json($res);
}
public function getGroupCategories(Request $request)
{
$res = GroupService::categories();
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function getGroupsByCategory(Request $request)
{
$name = $request->input('name');
$category = GroupCategory::whereName($name)->firstOrFail();
$groups = Group::whereCategoryId($category->id)
->simplePaginate(6)
->map(function($group) {
return GroupService::get($group->id);
})
->filter(function($group) {
return $group;
})
->values();
return $groups;
}
public function getRecommendedGroups(Request $request)
{
return [];
}
public function getSelfGroups(Request $request)
{
$selfOnly = $request->input('self') == true;
$memberOnly = $request->input('member') == true;
$pid = $request->user()->profile_id;
$res = GroupMember::whereProfileId($request->user()->profile_id)
->when($selfOnly, function($q, $selfOnly) {
return $q->whereRole('founder');
})
->when($memberOnly, function($q, $memberOnly) {
return $q->whereRole('member');
})
->simplePaginate(4)
->map(function($member) use($pid) {
$group = $member->group;
return $this->toJson($group, $pid);
});
return response()->json($res);
}
}

View file

@ -0,0 +1,361 @@
<?php
namespace App\Http\Controllers\Groups;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\GroupService;
use App\Services\Groups\GroupCommentService;
use App\Services\Groups\GroupMediaService;
use App\Services\Groups\GroupPostService;
use App\Services\Groups\GroupsLikeService;
use App\Models\Group;
use App\Models\GroupLike;
use App\Models\GroupMedia;
use App\Models\GroupPost;
use App\Models\GroupComment;
use Purify;
use App\Util\Lexer\Autolink;
use App\Jobs\GroupsPipeline\ImageResizePipeline;
use App\Jobs\GroupsPipeline\ImageS3UploadPipeline;
use App\Jobs\GroupsPipeline\NewPostPipeline;
use App\Jobs\GroupsPipeline\NewCommentPipeline;
use App\Jobs\GroupsPipeline\DeleteCommentPipeline;
class GroupsCommentController extends Controller
{
public function getComments(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'sid' => 'required',
'cid' => 'sometimes',
'limit' => 'nullable|integer|min:3|max:10'
]);
$pid = optional($request->user())->profile_id;
$gid = $request->input('gid');
$sid = $request->input('sid');
$cid = $request->has('cid') && $request->input('cid') == 1;
$limit = $request->input('limit', 3);
$maxId = $request->input('max_id', 0);
$group = Group::findOrFail($gid);
abort_if($group->is_private && !$group->isMember($pid), 403, 'Not a member of group.');
$status = $cid ? GroupComment::findOrFail($sid) : GroupPost::findOrFail($sid);
abort_if($status->group_id != $group->id, 400, 'Invalid group');
$replies = GroupComment::whereGroupId($group->id)
->whereStatusId($status->id)
->orderByDesc('id')
->when($maxId, function($query, $maxId) {
return $query->where('id', '<', $maxId);
})
->take($limit)
->get()
->map(function($gp) use($pid) {
$status = GroupCommentService::get($gp['group_id'], $gp['id']);
$status['reply_count'] = $gp['reply_count'];
$status['url'] = $gp->url();
$status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['id']);
$status['account']['url'] = url("/groups/{$gp['group_id']}/user/{$gp['profile_id']}");
return $status;
});
return $replies->toArray();
}
public function storeComment(Request $request)
{
$this->validate($request, [
'gid' => 'required|exists:groups,id',
'sid' => 'required|exists:group_posts,id',
'cid' => 'sometimes',
'content' => 'required|string|min:1|max:1500'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$sid = $request->input('sid');
$cid = $request->input('cid');
$limit = $request->input('limit', 3);
$caption = e($request->input('content'));
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
abort_if(!GroupService::canComment($gid, $pid), 422, 'You cannot interact with this content at this time');
$parent = $cid == 1 ?
GroupComment::findOrFail($sid) :
GroupPost::whereGroupId($gid)->findOrFail($sid);
// $autolink = Purify::clean(Autolink::create()->autolink($caption));
// $autolink = str_replace('/discover/tags/', '/groups/' . $gid . '/topics/', $autolink);
$status = new GroupComment;
$status->group_id = $group->id;
$status->profile_id = $pid;
$status->status_id = $parent->id;
$status->caption = Purify::clean($caption);
$status->visibility = 'public';
$status->is_nsfw = false;
$status->local = true;
$status->save();
NewCommentPipeline::dispatch($parent, $status)->onQueue('groups');
// todo: perform in job
$parent->reply_count = $parent->reply_count ? $parent->reply_count + $parent->reply_count : 1;
$parent->save();
GroupPostService::del($parent->group_id, $parent->id);
GroupService::log(
$group->id,
$pid,
'group:comment:created',
[
'type' => 'group:post:comment',
'status_id' => $status->id
],
GroupPost::class,
$status->id
);
//GroupCommentPipeline::dispatch($parent, $status, $gp);
//NewStatusPipeline::dispatch($status, $gp);
//GroupPostService::del($group->id, GroupService::sidToGid($group->id, $parent->id));
// todo: perform in job
$s = GroupCommentService::get($status->group_id, $status->id);
$s['pf_type'] = 'text';
$s['visibility'] = 'public';
$s['url'] = $status->url();
return $s;
}
public function storeCommentPhoto(Request $request)
{
$this->validate($request, [
'gid' => 'required|exists:groups,id',
'sid' => 'required|exists:group_posts,id',
'photo' => 'required|image'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$sid = $request->input('sid');
$limit = $request->input('limit', 3);
$caption = $request->input('content');
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
abort_if(!GroupService::canComment($gid, $pid), 422, 'You cannot interact with this content at this time');
$parent = GroupPost::whereGroupId($gid)->findOrFail($sid);
$status = new GroupComment;
$status->status_id = $parent->id;
$status->group_id = $group->id;
$status->profile_id = $pid;
$status->caption = Purify::clean($caption);
$status->visibility = 'draft';
$status->is_nsfw = false;
$status->save();
$photo = $request->file('photo');
$storagePath = GroupMediaService::path($group->id, $pid, $status->id);
$storagePath = 'public/g/' . $group->id . '/p/' . $parent->id;
$path = $photo->storePublicly($storagePath);
$media = new GroupMedia();
$media->group_id = $group->id;
$media->status_id = $status->id;
$media->profile_id = $request->user()->profile_id;
$media->media_path = $path;
$media->size = $photo->getSize();
$media->mime = $photo->getMimeType();
$media->save();
ImageResizePipeline::dispatchSync($media);
ImageS3UploadPipeline::dispatchSync($media);
// $gp = new GroupPost;
// $gp->group_id = $group->id;
// $gp->profile_id = $pid;
// $gp->type = 'reply:photo';
// $gp->status_id = $status->id;
// $gp->in_reply_to_id = $parent->id;
// $gp->save();
// GroupService::log(
// $group->id,
// $pid,
// 'group:comment:created',
// [
// 'type' => $gp->type,
// 'status_id' => $status->id
// ],
// GroupPost::class,
// $gp->id
// );
// todo: perform in job
// $parent->reply_count = Status::whereInReplyToId($parent->id)->count();
// $parent->save();
// StatusService::del($parent->id);
// GroupPostService::del($group->id, GroupService::sidToGid($group->id, $parent->id));
// delay response while background job optimizes media
// sleep(5);
// todo: perform in job
$s = GroupCommentService::get($status->group_id, $status->id);
// $s['pf_type'] = 'text';
// $s['visibility'] = 'public';
// $s['url'] = $gp->url();
return $s;
}
public function deleteComment(Request $request)
{
abort_if(!$request->user(), 403);
$this->validate($request, [
'id' => 'required|integer|min:1',
'gid' => 'required|integer|min:1'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
$gp = GroupComment::whereGroupId($group->id)->findOrFail($request->input('id'));
abort_if($gp->profile_id != $pid && $group->profile_id != $pid, 403);
$parent = GroupPost::find($gp->status_id);
abort_if(!$parent, 422, 'Invalid parent');
DeleteCommentPipeline::dispatch($parent, $gp)->onQueue('groups');
GroupService::log(
$group->id,
$pid,
'group:status:deleted',
[
'type' => $gp->type,
'status_id' => $gp->id,
],
GroupComment::class,
$gp->id
);
$gp->delete();
if($request->wantsJson()) {
return response()->json(['Status successfully deleted.']);
} else {
return redirect('/groups/feed');
}
}
public function likePost(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'sid' => 'required'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$sid = $request->input('sid');
$group = GroupService::get($gid);
abort_if(!$group || $gid != $group['id'], 422, 'Invalid group');
abort_if(!GroupService::canLike($gid, $pid), 422, 'You cannot interact with this content at this time');
abort_if(!GroupService::isMember($gid, $pid), 403, 'Not a member of group');
$gp = GroupCommentService::get($gid, $sid);
abort_if(!$gp, 422, 'Invalid status');
$count = $gp['favourites_count'] ?? 0;
$like = GroupLike::firstOrCreate([
'group_id' => $gid,
'profile_id' => $pid,
'comment_id' => $sid,
]);
if($like->wasRecentlyCreated) {
// update parent post like count
$parent = GroupComment::find($sid);
abort_if(!$parent || $parent->group_id != $gid, 422, 'Invalid status');
$parent->likes_count = $parent->likes_count + 1;
$parent->save();
GroupsLikeService::add($pid, $sid);
// invalidate cache
GroupCommentService::del($gid, $sid);
$count++;
GroupService::log(
$gid,
$pid,
'group:like',
null,
GroupLike::class,
$like->id
);
}
$response = ['code' => 200, 'msg' => 'Like saved', 'count' => $count];
return $response;
}
public function unlikePost(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'sid' => 'required'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$sid = $request->input('sid');
$group = GroupService::get($gid);
abort_if(!$group || $gid != $group['id'], 422, 'Invalid group');
abort_if(!GroupService::canLike($gid, $pid), 422, 'You cannot interact with this content at this time');
abort_if(!GroupService::isMember($gid, $pid), 403, 'Not a member of group');
$gp = GroupCommentService::get($gid, $sid);
abort_if(!$gp, 422, 'Invalid status');
$count = $gp['favourites_count'] ?? 0;
$like = GroupLike::where([
'group_id' => $gid,
'profile_id' => $pid,
'comment_id' => $sid,
])->first();
if($like) {
$like->delete();
$parent = GroupComment::find($sid);
abort_if(!$parent || $parent->group_id != $gid, 422, 'Invalid status');
$parent->likes_count = $parent->likes_count - 1;
$parent->save();
GroupsLikeService::remove($pid, $sid);
// invalidate cache
GroupCommentService::del($gid, $sid);
$count--;
}
$response = ['code' => 200, 'msg' => 'Unliked post', 'count' => $count];
return $response;
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace App\Http\Controllers\Groups;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\GroupService;
use App\Follower;
use App\Profile;
use App\Models\Group;
use App\Models\GroupMember;
use App\Models\GroupInvitation;
class GroupsDiscoverController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getDiscoverPopular(Request $request)
{
abort_if(!$request->user(), 404);
$groups = Group::orderByDesc('member_count')
->take(12)
->pluck('id')
->map(function($id) {
return GroupService::get($id);
})
->filter(function($id) {
return $id;
})
->take(6)
->values();
return $groups;
}
public function getDiscoverNew(Request $request)
{
abort_if(!$request->user(), 404);
$groups = Group::latest()
->take(12)
->pluck('id')
->map(function($id) {
return GroupService::get($id);
})
->filter(function($id) {
return $id;
})
->take(6)
->values();
return $groups;
}
}

View file

@ -0,0 +1,188 @@
<?php
namespace App\Http\Controllers\Groups;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\GroupService;
use App\Services\UserFilterService;
use App\Services\Groups\GroupFeedService;
use App\Services\Groups\GroupPostService;
use App\Services\RelationshipService;
use App\Services\Groups\GroupsLikeService;
use App\Follower;
use App\Profile;
use App\Models\Group;
use App\Models\GroupPost;
use App\Models\GroupInvitation;
class GroupsFeedController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function getSelfFeed(Request $request)
{
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$limit = $request->input('limit', 5);
$page = $request->input('page');
$initial = $request->has('initial');
if($initial) {
$res = Cache::remember('groups:self:feed:' . $pid, 900, function() use($pid) {
return $this->getSelfFeedV0($pid, 5, null);
});
} else {
abort_if($page && $page > 5, 422);
$res = $this->getSelfFeedV0($pid, $limit, $page);
}
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
protected function getSelfFeedV0($pid, $limit, $page)
{
return GroupPost::join('group_members', 'group_posts.group_id', 'group_members.group_id')
->select('group_posts.*', 'group_members.group_id', 'group_members.profile_id')
->where('group_members.profile_id', $pid)
->whereIn('group_posts.type', ['text', 'photo', 'video'])
->orderByDesc('group_posts.id')
->limit($limit)
// ->pluck('group_posts.status_id')
->simplePaginate($limit)
->map(function($gp) use($pid) {
$status = GroupPostService::get($gp['group_id'], $gp['id']);
if(!$status) {
return false;
}
$status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['id']);
$status['favourites_count'] = GroupsLikeService::count($gp['id']);
$status['pf_type'] = $gp['type'];
$status['visibility'] = 'public';
$status['url'] = url("/groups/{$gp['group_id']}/p/{$gp['id']}");
$status['group'] = GroupService::get($gp['group_id']);
$status['account']['url'] = url("/groups/{$gp['group_id']}/user/{$status['account']['id']}");
return $status;
});
}
public function getGroupProfileFeed(Request $request, $id, $pid)
{
abort_if(!$request->user(), 404);
$cid = $request->user()->profile_id;
$group = Group::findOrFail($id);
abort_if(!$group->isMember($pid), 404);
$feed = GroupPost::whereGroupId($id)
->whereProfileId($pid)
->latest()
->paginate(3)
->map(function($gp) use($pid) {
$status = GroupPostService::get($gp['group_id'], $gp['id']);
if(!$status) {
return false;
}
$status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['id']);
$status['favourites_count'] = GroupsLikeService::count($gp['id']);
$status['pf_type'] = $gp['type'];
$status['visibility'] = 'public';
$status['url'] = $gp->url();
// if($gp['type'] == 'poll') {
// $status['poll'] = PollService::get($status['id']);
// }
$status['account']['url'] = "/groups/{$gp['group_id']}/user/{$status['account']['id']}";
return $status;
})
->filter(function($status) {
return $status;
});
return $feed;
}
public function getGroupFeed(Request $request, $id)
{
$group = Group::findOrFail($id);
$user = $request->user();
$pid = optional($user)->profile_id ?? false;
abort_if(!$group->isMember($pid), 404);
$max = $request->input('max_id');
$limit = $request->limit ?? 3;
$filtered = $user ? UserFilterService::filters($user->profile_id) : [];
// $posts = GroupPost::whereGroupId($group->id)
// ->when($maxId, function($q, $maxId) {
// return $q->where('status_id', '<', $maxId);
// })
// ->whereNull('in_reply_to_id')
// ->orderByDesc('status_id')
// ->simplePaginate($limit)
// ->map(function($gp) use($pid) {
// $status = StatusService::get($gp['status_id'], false);
// if(!$status) {
// return false;
// }
// $status['favourited'] = (bool) LikeService::liked($pid, $gp['status_id']);
// $status['favourites_count'] = LikeService::count($gp['status_id']);
// $status['pf_type'] = $gp['type'];
// $status['visibility'] = 'public';
// $status['url'] = $gp->url();
// if($gp['type'] == 'poll') {
// $status['poll'] = PollService::get($status['id']);
// }
// $status['account']['url'] = url("/groups/{$gp['group_id']}/user/{$status['account']['id']}");
// return $status;
// })->filter(function($status) {
// return $status;
// });
// return $posts;
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() use($id) {
if(GroupFeedService::count($id) == 0) {
GroupFeedService::warmCache($id, true, 400);
}
});
if ($max) {
$feed = GroupFeedService::getRankedMaxId($id, $max, $limit);
} else {
$feed = GroupFeedService::get($id, 0, $limit);
}
$res = collect($feed)
->map(function($k) use($user, $id) {
$status = GroupPostService::get($id, $k);
if($status && $user) {
$pid = $user->profile_id;
$sid = $status['account']['id'];
$status['favourited'] = (bool) GroupsLikeService::liked($pid, $status['id']);
$status['favourites_count'] = GroupsLikeService::count($status['id']);
$status['relationship'] = $pid == $sid ? [] : RelationshipService::get($pid, $sid);
}
return $status;
})
->filter(function($s) use($filtered) {
return $s && in_array($s['account']['id'], $filtered) == false;
})
->values()
->toArray();
return $res;
}
}

View file

@ -0,0 +1,214 @@
<?php
namespace App\Http\Controllers\Groups;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\GroupService;
use App\Models\Group;
use App\Models\GroupCategory;
use App\Models\GroupHashtag;
use App\Models\GroupPostHashtag;
use App\Models\GroupMember;
use App\Services\AccountService;
use App\Services\FollowerService;
use App\Services\Groups\GroupAccountService;
use App\Services\Groups\GroupHashtagService;
use App\Jobs\GroupsPipeline\MemberJoinApprovedPipeline;
use App\Jobs\GroupsPipeline\MemberJoinRejectedPipeline;
class GroupsMemberController extends Controller
{
public function getGroupMembers(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'limit' => 'nullable|integer|min:3|max:10'
]);
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
$members = GroupMember::whereGroupId($gid)
->whereJoinRequest(false)
->simplePaginate(10)
->map(function($member) use($pid) {
$account = AccountService::get($member['profile_id']);
$account['role'] = $member['role'];
$account['joined'] = $member['created_at'];
$account['following'] = $pid != $member['profile_id'] ?
FollowerService::follows($pid, $member['profile_id']) :
null;
$account['url'] = url("/groups/{$member->group_id}/user/{$member['profile_id']}");
return $account;
});
return response()->json($members->toArray(), 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function getGroupMemberJoinRequests(Request $request)
{
abort_if(!$request->user(), 404);
$id = $request->input('gid');
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
return GroupMember::whereGroupId($group->id)
->whereJoinRequest(true)
->whereNull('rejected_at')
->paginate(10)
->map(function($member) {
return AccountService::get($member->profile_id);
});
}
public function handleGroupMemberJoinRequest(Request $request)
{
abort_if(!$request->user(), 404);
$id = $request->input('gid');
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$mid = $request->input('pid');
abort_if($group->isMember($mid), 404);
$this->validate($request, [
'gid' => 'required',
'pid' => 'required',
'action' => 'required|in:approve,reject'
]);
$action = $request->input('action');
$member = GroupMember::whereGroupId($group->id)
->whereProfileId($mid)
->firstOrFail();
if($action == 'approve') {
MemberJoinApprovedPipeline::dispatch($member)->onQueue('groups');
} else if ($action == 'reject') {
MemberJoinRejectedPipeline::dispatch($member)->onQueue('groups');
}
return $request->all();
}
public function getGroupMember(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'pid' => 'required'
]);
abort_if(!$request->user(), 404);
$gid = $request->input('gid');
$group = Group::findOrFail($gid);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$member_id = $request->input('pid');
$member = GroupMember::whereGroupId($gid)
->whereProfileId($member_id)
->firstOrFail();
$account = GroupAccountService::get($group->id, $member['profile_id']);
$account['role'] = $member['role'];
$account['joined'] = $member['created_at'];
$account['following'] = $pid != $member['profile_id'] ?
FollowerService::follows($pid, $member['profile_id']) :
null;
$account['url'] = url("/groups/{$gid}/user/{$member_id}");
return response()->json($account, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function getGroupMemberCommonIntersections(Request $request)
{
abort_if(!$request->user(), 404);
$cid = $request->user()->profile_id;
// $this->validate($request, [
// 'gid' => 'required',
// 'pid' => 'required'
// ]);
$gid = $request->input('gid');
$pid = $request->input('pid');
if($pid === $cid) {
return [];
}
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($cid), 404);
abort_if(!$group->isMember($pid), 404);
$self = GroupPostHashtag::selectRaw('group_post_hashtags.*, count(*) as countr')
->whereProfileId($cid)
->groupBy('hashtag_id')
->orderByDesc('countr')
->take(20)
->pluck('hashtag_id');
$user = GroupPostHashtag::selectRaw('group_post_hashtags.*, count(*) as countr')
->whereProfileId($pid)
->groupBy('hashtag_id')
->orderByDesc('countr')
->take(20)
->pluck('hashtag_id');
$topics = $self->intersect($user)
->values()
->shuffle()
->take(3)
->map(function($id) use($group) {
$tag = GroupHashtagService::get($id);
$tag['url'] = url("/groups/{$group->id}/topics/{$tag['slug']}?src=upt");
return $tag;
});
// $friends = DB::table('followers as u')
// ->join('followers as s', 'u.following_id', '=', 's.following_id')
// ->where('s.profile_id', $cid)
// ->where('u.profile_id', $pid)
// ->inRandomOrder()
// ->take(10)
// ->pluck('s.following_id')
// ->map(function($id) use($gid) {
// $res = AccountService::get($id);
// $res['url'] = url("/groups/{$gid}/user/{$id}");
// return $res;
// });
$mutualGroups = GroupService::mutualGroups($cid, $pid, [$gid]);
$mutualFriends = collect(FollowerService::mutualIds($cid, $pid))
->map(function($id) use($gid) {
$res = AccountService::get($id);
if(GroupService::isMember($gid, $id)) {
$res['url'] = url("/groups/{$gid}/user/{$id}");
} else if(!$res['local']) {
$res['url'] = url("/i/web/profile/_/{$id}");
}
return $res;
});
$mutualFriendsCount = FollowerService::mutualCount($cid, $pid);
$res = [
'groups_count' => $mutualGroups['count'],
'groups' => $mutualGroups['groups'],
'topics' => $topics,
'friends_count' => $mutualFriendsCount,
'friends' => $mutualFriends,
];
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
}

View file

@ -0,0 +1,31 @@
<?php
namespace App\Http\Controllers\Groups;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\GroupService;
use App\Models\Group;
class GroupsMetaController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function deleteGroup(Request $request)
{
abort_if(!$request->user(), 404);
$id = $request->input('gid');
$group = Group::findOrFail($id);
$pid = $request->user()->profile_id;
abort_if(!$group->isMember($pid), 404);
abort_if(!in_array($group->selfRole($pid), ['founder', 'admin']), 404);
$group->status = "delete";
$group->save();
GroupService::del($group->id);
return [200];
}
}

View file

@ -0,0 +1,55 @@
<?php
namespace App\Http\Controllers\Groups;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\StatusService;
use App\Services\GroupService;
use App\Models\Group;
use App\Notification;
class GroupsNotificationsController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function selfGlobalNotifications(Request $request)
{
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$res = Notification::whereProfileId($pid)
->where('action', 'like', 'group%')
->latest()
->paginate(10)
->map(function($n) {
$res = [
'id' => $n->id,
'type' => $n->action,
'account' => AccountService::get($n->actor_id),
'object' => [
'id' => $n->item_id,
'type' => last(explode('\\', $n->item_type)),
],
'created_at' => $n->created_at->format('c')
];
if($res['object']['type'] == 'Status' || in_array($n->action, ['group:comment'])) {
$res['status'] = StatusService::get($n->item_id, false);
$res['group'] = GroupService::get($res['status']['gid']);
}
if($res['object']['type'] == 'Group') {
$res['group'] = GroupService::get($n->item_id);
}
return $res;
});
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
}

View file

@ -0,0 +1,420 @@
<?php
namespace App\Http\Controllers\Groups;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\RateLimiter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\GroupService;
use App\Services\Groups\GroupFeedService;
use App\Services\Groups\GroupPostService;
use App\Services\Groups\GroupMediaService;
use App\Services\Groups\GroupsLikeService;
use App\Follower;
use App\Profile;
use App\Models\Group;
use App\Models\GroupHashtag;
use App\Models\GroupPost;
use App\Models\GroupLike;
use App\Models\GroupMember;
use App\Models\GroupInvitation;
use App\Models\GroupMedia;
use App\Jobs\GroupsPipeline\ImageResizePipeline;
use App\Jobs\GroupsPipeline\ImageS3UploadPipeline;
use App\Jobs\GroupsPipeline\NewPostPipeline;
class GroupsPostController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function storePost(Request $request)
{
$this->validate($request, [
'group_id' => 'required|exists:groups,id',
'caption' => 'sometimes|string|max:'.config_cache('pixelfed.max_caption_length', 500),
'pollOptions' => 'sometimes|array|min:1|max:4'
]);
$group = Group::findOrFail($request->input('group_id'));
$pid = $request->user()->profile_id;
$caption = $request->input('caption');
$type = $request->input('type', 'text');
abort_if(!GroupService::canPost($group->id, $pid), 422, 'You cannot create new posts at this time');
if($type == 'text') {
abort_if(strlen(e($caption)) == 0, 403);
}
$gp = new GroupPost;
$gp->group_id = $group->id;
$gp->profile_id = $pid;
$gp->caption = e($caption);
$gp->type = $type;
$gp->visibility = 'draft';
$gp->save();
$status = $gp;
NewPostPipeline::dispatchSync($gp);
// NewStatusPipeline::dispatch($status, $gp);
if($type == 'poll') {
// Polls not supported yet
// $poll = new Poll;
// $poll->status_id = $status->id;
// $poll->profile_id = $status->profile_id;
// $poll->poll_options = $request->input('pollOptions');
// $poll->expires_at = now()->addMinutes($request->input('expiry'));
// $poll->cached_tallies = collect($poll->poll_options)->map(function($o) {
// return 0;
// })->toArray();
// $poll->save();
// sleep(5);
}
if($type == 'photo') {
$photo = $request->file('photo');
$storagePath = GroupMediaService::path($group->id, $pid, $status->id);
// $storagePath = 'public/g/' . $group->id . '/p/' . $status->id;
$path = $photo->storePublicly($storagePath);
// $hash = \hash_file('sha256', $photo);
$media = new GroupMedia();
$media->group_id = $group->id;
$media->status_id = $status->id;
$media->profile_id = $request->user()->profile_id;
$media->media_path = $path;
$media->size = $photo->getSize();
$media->mime = $photo->getMimeType();
$media->save();
// Bus::chain([
// new ImageResizePipeline($media),
// new ImageS3UploadPipeline($media),
// ])->dispatch($media);
ImageResizePipeline::dispatchSync($media);
ImageS3UploadPipeline::dispatchSync($media);
// ImageOptimize::dispatch($media);
// delay response while background job optimizes media
// sleep(5);
}
if($type == 'video') {
$video = $request->file('video');
$storagePath = 'public/g/' . $group->id . '/p/' . $status->id;
$path = $video->storePublicly($storagePath);
$hash = \hash_file('sha256', $video);
$media = new Media();
$media->status_id = $status->id;
$media->profile_id = $request->user()->profile_id;
$media->user_id = $request->user()->id;
$media->media_path = $path;
$media->original_sha256 = $hash;
$media->size = $video->getSize();
$media->mime = $video->getMimeType();
$media->save();
VideoThumbnail::dispatch($media);
sleep(15);
}
GroupService::log(
$group->id,
$pid,
'group:status:created',
[
'type' => $gp->type,
'status_id' => $status->id
],
GroupPost::class,
$gp->id
);
$s = GroupPostService::get($status->group_id, $status->id);
GroupFeedService::add($group->id, $gp->id);
Cache::forget('groups:self:feed:' . $pid);
$s['pf_type'] = $type;
$s['visibility'] = 'public';
$s['url'] = $gp->url();
if($type == 'poll') {
$s['poll'] = PollService::get($status->id);
}
$group->last_active_at = now();
$group->save();
return $s;
}
public function deletePost(Request $request)
{
abort_if(!$request->user(), 403);
$this->validate($request, [
'id' => 'required|integer|min:1',
'gid' => 'required|integer|min:1'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
$gp = GroupPost::whereGroupId($status->group_id)->findOrFail($request->input('id'));
abort_if($gp->profile_id != $pid && $group->profile_id != $pid, 403);
$cached = GroupPostService::get($status->group_id, $status->id);
if($cached) {
$cached = collect($cached)->filter(function($r, $k) {
return in_array($k, [
'id',
'sensitive',
'pf_type',
'media_attachments',
'content_text',
'created_at'
]);
});
}
GroupService::log(
$status->group_id,
$request->user()->profile_id,
'group:status:deleted',
[
'type' => $gp->type,
'status_id' => $status->id,
'original' => $cached
],
GroupPost::class,
$gp->id
);
$user = $request->user();
// if($status->profile_id != $user->profile->id &&
// $user->is_admin == true &&
// $status->uri == null
// ) {
// $media = $status->media;
// $ai = new AccountInterstitial;
// $ai->user_id = $status->profile->user_id;
// $ai->type = 'post.removed';
// $ai->view = 'account.moderation.post.removed';
// $ai->item_type = 'App\Status';
// $ai->item_id = $status->id;
// $ai->has_media = (bool) $media->count();
// $ai->blurhash = $media->count() ? $media->first()->blurhash : null;
// $ai->meta = json_encode([
// 'caption' => $status->caption,
// 'created_at' => $status->created_at,
// 'type' => $status->type,
// 'url' => $status->url(),
// 'is_nsfw' => $status->is_nsfw,
// 'scope' => $status->scope,
// 'reblog' => $status->reblog_of_id,
// 'likes_count' => $status->likes_count,
// 'reblogs_count' => $status->reblogs_count,
// ]);
// $ai->save();
// $u = $status->profile->user;
// $u->has_interstitial = true;
// $u->save();
// }
if($status->in_reply_to_id) {
$parent = GroupPost::find($status->in_reply_to_id);
if($parent) {
$parent->reply_count = GroupPost::whereInReplyToId($parent->id)->count();
$parent->save();
GroupPostService::del($group->id, GroupService::sidToGid($group->id, $parent->id));
}
}
GroupPostService::del($group->id, $gp->id);
GroupFeedService::del($group->id, $gp->id);
if ($status->profile_id == $user->profile->id || $user->is_admin == true) {
// Cache::forget('profile:status_count:'.$status->profile_id);
StatusDelete::dispatch($status);
}
if($request->wantsJson()) {
return response()->json(['Status successfully deleted.']);
} else {
return redirect($user->url());
}
}
public function likePost(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'sid' => 'required'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$sid = $request->input('sid');
$group = GroupService::get($gid);
abort_if(!$group, 422, 'Invalid group');
abort_if(!GroupService::canLike($gid, $pid), 422, 'You cannot interact with this content at this time');
abort_if(!GroupService::isMember($gid, $pid), 403, 'Not a member of group');
$gp = GroupPostService::get($gid, $sid);
abort_if(!$gp, 422, 'Invalid status');
$count = $gp['favourites_count'] ?? 0;
$like = GroupLike::firstOrCreate([
'group_id' => $gid,
'profile_id' => $pid,
'status_id' => $sid,
]);
if($like->wasRecentlyCreated) {
// update parent post like count
$parent = GroupPost::whereGroupId($gid)->find($sid);
abort_if(!$parent, 422, 'Invalid status');
$parent->likes_count = $parent->likes_count + 1;
$parent->save();
GroupsLikeService::add($pid, $sid);
// invalidate cache
GroupPostService::del($gid, $sid);
$count++;
GroupService::log(
$gid,
$pid,
'group:like',
null,
GroupLike::class,
$like->id
);
}
// if (GroupLike::whereGroupId($gid)->whereStatusId($sid)->whereProfileId($pid)->exists()) {
// $like = GroupLike::whereProfileId($pid)->whereStatusId($sid)->firstOrFail();
// // UnlikePipeline::dispatch($like);
// $count = $gp->likes_count - 1;
// $action = 'group:unlike';
// } else {
// $count = $gp->likes_count;
// $like = GroupLike::firstOrCreate([
// 'group_id' => $gid,
// 'profile_id' => $pid,
// 'status_id' => $sid
// ]);
// if($like->wasRecentlyCreated == true) {
// $count++;
// $gp->likes_count = $count;
// $like->save();
// $gp->save();
// // LikePipeline::dispatch($like);
// $action = 'group:like';
// }
// }
// Cache::forget('status:'.$status->id.':likedby:userid:'.$request->user()->id);
// StatusService::del($status->id);
$response = ['code' => 200, 'msg' => 'Like saved', 'count' => $count];
return $response;
}
public function unlikePost(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'sid' => 'required'
]);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$sid = $request->input('sid');
$group = GroupService::get($gid);
abort_if(!$group, 422, 'Invalid group');
abort_if(!GroupService::canLike($gid, $pid), 422, 'You cannot interact with this content at this time');
abort_if(!GroupService::isMember($gid, $pid), 403, 'Not a member of group');
$gp = GroupPostService::get($gid, $sid);
abort_if(!$gp, 422, 'Invalid status');
$count = $gp['favourites_count'] ?? 0;
$like = GroupLike::where([
'group_id' => $gid,
'profile_id' => $pid,
'status_id' => $sid,
])->first();
if($like) {
$like->delete();
$parent = GroupPost::whereGroupId($gid)->find($sid);
abort_if(!$parent, 422, 'Invalid status');
$parent->likes_count = $parent->likes_count - 1;
$parent->save();
GroupsLikeService::remove($pid, $sid);
// invalidate cache
GroupPostService::del($gid, $sid);
$count--;
}
$response = ['code' => 200, 'msg' => 'Unliked post', 'count' => $count];
return $response;
}
public function getGroupMedia(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'type' => 'required|in:photo,video'
]);
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$type = $request->input('type');
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
$media = GroupPost::whereGroupId($gid)
->whereType($type)
->latest()
->simplePaginate(20)
->map(function($gp) use($pid) {
$status = GroupPostService::get($gp['group_id'], $gp['id']);
if(!$status) {
return false;
}
$status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['id']);
$status['favourites_count'] = GroupsLikeService::count($gp['id']);
$status['pf_type'] = $gp['type'];
$status['visibility'] = 'public';
$status['url'] = $gp->url();
// if($gp['type'] == 'poll') {
// $status['poll'] = PollService::get($status['id']);
// }
return $status;
})->filter(function($status) {
return $status;
});
return response()->json($media->toArray(), 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
}

View file

@ -0,0 +1,221 @@
<?php
namespace App\Http\Controllers\Groups;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\GroupService;
use App\Follower;
use App\Profile;
use App\Models\Group;
use App\Models\GroupMember;
use App\Models\GroupInvitation;
use App\Util\ActivityPub\Helpers;
use App\Services\Groups\GroupActivityPubService;
class GroupsSearchController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function inviteFriendsToGroup(Request $request)
{
abort_if(!$request->user(), 404);
$this->validate($request, [
'uids' => 'required',
'g' => 'required',
]);
$uid = $request->input('uids');
$gid = $request->input('g');
$pid = $request->user()->profile_id;
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 404);
abort_if(
GroupInvitation::whereGroupId($group->id)
->whereFromProfileId($pid)
->count() >= 20,
422,
'Invite limit reached'
);
$profiles = collect($uid)
->map(function($u) {
return Profile::find($u);
})
->filter(function($u) use($pid) {
return $u &&
$u->id != $pid &&
isset($u->id) &&
Follower::whereFollowingId($pid)
->whereProfileId($u->id)
->exists();
})
->filter(function($u) use($group, $pid) {
return GroupInvitation::whereGroupId($group->id)
->whereFromProfileId($pid)
->whereToProfileId($u->id)
->exists() == false;
})
->each(function($u) use($gid, $pid) {
$gi = new GroupInvitation;
$gi->group_id = $gid;
$gi->from_profile_id = $pid;
$gi->to_profile_id = $u->id;
$gi->to_local = true;
$gi->from_local = $u->domain == null;
$gi->save();
// GroupMemberInvite::dispatch($gi);
});
return [200];
}
public function searchFriendsToInvite(Request $request)
{
abort_if(!$request->user(), 404);
$this->validate($request, [
'q' => 'required|min:2|max:40',
'g' => 'required',
'v' => 'required|in:0.2'
]);
$q = $request->input('q');
$gid = $request->input('g');
$pid = $request->user()->profile_id;
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 404);
$res = Profile::where('username', 'like', "%{$q}%")
->whereNull('profiles.domain')
->join('followers', 'profiles.id', '=', 'followers.profile_id')
->where('followers.following_id', $pid)
->take(10)
->get()
->filter(function($p) use($group) {
return $group->isMember($p->profile_id) == false;
})
->filter(function($p) use($group, $pid) {
return GroupInvitation::whereGroupId($group->id)
->whereFromProfileId($pid)
->whereToProfileId($p->profile_id)
->exists() == false;
})
->map(function($gm) use ($gid) {
$a = AccountService::get($gm->profile_id);
return [
'id' => (string) $gm->profile_id,
'username' => $a['acct'],
'url' => url("/groups/{$gid}/user/{$a['id']}?rf=group_search")
];
})
->values();
return $res;
}
public function searchGlobalResults(Request $request)
{
abort_if(!$request->user(), 404);
$this->validate($request, [
'q' => 'required|min:2|max:140',
'v' => 'required|in:0.2'
]);
$q = $request->input('q');
if(str_starts_with($q, 'https://')) {
$res = Helpers::getSignedFetch($q);
if($res && $res = json_decode($res, true)) {
}
if($res && isset($res['type']) && in_array($res['type'], ['Group', 'Note', 'Page'])) {
if($res['type'] === 'Group') {
return GroupActivityPubService::fetchGroup($q, true);
}
$resp = GroupActivityPubService::fetchGroupPost($q, true);
$resp['name'] = 'Group Post';
$resp['url'] = '/groups/' . $resp['group_id'] . '/p/' . $resp['id'];
return [$resp];
}
}
return Group::whereNull('status')
->where('name', 'like', '%' . $q . '%')
->orderBy('id')
->take(10)
->pluck('id')
->map(function($group) {
return GroupService::get($group);
});
}
public function searchLocalAutocomplete(Request $request)
{
abort_if(!$request->user(), 404);
$this->validate($request, [
'q' => 'required|min:2|max:40',
'g' => 'required',
'v' => 'required|in:0.2'
]);
$q = $request->input('q');
$gid = $request->input('g');
$pid = $request->user()->profile_id;
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 404);
$res = GroupMember::whereGroupId($gid)
->join('profiles', 'group_members.profile_id', '=', 'profiles.id')
->where('profiles.username', 'like', "%{$q}%")
->take(10)
->get()
->map(function($gm) use ($gid) {
$a = AccountService::get($gm->profile_id);
return [
'username' => $a['username'],
'url' => url("/groups/{$gid}/user/{$a['id']}?rf=group_search")
];
});
return $res;
}
public function searchAddRecent(Request $request)
{
$this->validate($request, [
'q' => 'required|min:2|max:40',
'g' => 'required',
]);
$q = $request->input('q');
$gid = $request->input('g');
$pid = $request->user()->profile_id;
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 404);
$key = 'groups:search:recent:'.$gid.':pid:'.$pid;
$ttl = now()->addDays(14);
$res = Cache::get($key);
if(!$res) {
$val = json_encode([$q]);
} else {
$ex = collect(json_decode($res))
->prepend($q)
->unique('value')
->slice(0, 3)
->values()
->all();
$val = json_encode($ex);
}
Cache::put($key, $val, $ttl);
return 200;
}
public function searchGetRecent(Request $request)
{
$gid = $request->input('g');
$pid = $request->user()->profile_id;
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 404);
$key = 'groups:search:recent:'.$gid.':pid:'.$pid;
return Cache::get($key);
}
}

View file

@ -0,0 +1,133 @@
<?php
namespace App\Http\Controllers\Groups;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\RateLimiter;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\GroupService;
use App\Services\Groups\GroupPostService;
use App\Services\Groups\GroupsLikeService;
use App\Follower;
use App\Profile;
use App\Models\Group;
use App\Models\GroupHashtag;
use App\Models\GroupInvitation;
use App\Models\GroupMember;
use App\Models\GroupPostHashtag;
use App\Models\GroupPost;
class GroupsTopicController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function groupTopics(Request $request)
{
$this->validate($request, [
'gid' => 'required',
]);
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
$posts = GroupPostHashtag::join('group_hashtags', 'group_hashtags.id', '=', 'group_post_hashtags.hashtag_id')
->selectRaw('group_hashtags.*, group_post_hashtags.*, count(group_post_hashtags.hashtag_id) as ht_count')
->where('group_post_hashtags.group_id', $gid)
->orderByDesc('ht_count')
->limit(10)
->pluck('group_post_hashtags.hashtag_id', 'ht_count')
->map(function($id, $key) use ($gid) {
$tag = GroupHashtag::find($id);
return [
'hid' => $id,
'name' => $tag->name,
'url' => url("/groups/{$gid}/topics/{$tag->slug}"),
'count' => $key
];
})->values();
return response()->json($posts, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function groupTopicTag(Request $request)
{
$this->validate($request, [
'gid' => 'required',
'name' => 'required'
]);
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$gid = $request->input('gid');
$limit = $request->input('limit', 3);
$group = Group::findOrFail($gid);
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
$name = $request->input('name');
$hashtag = GroupHashtag::whereName($name)->first();
if(!$hashtag) {
return [];
}
// $posts = GroupPost::whereGroupId($gid)
// ->select('status_hashtags.*', 'group_posts.*')
// ->where('status_hashtags.hashtag_id', $hashtag->id)
// ->join('status_hashtags', 'group_posts.status_id', '=', 'status_hashtags.status_id')
// ->orderByDesc('group_posts.status_id')
// ->simplePaginate($limit)
// ->map(function($gp) use($pid) {
// $status = StatusService::get($gp['status_id'], false);
// if(!$status) {
// return false;
// }
// $status['favourited'] = (bool) LikeService::liked($pid, $gp['status_id']);
// $status['favourites_count'] = LikeService::count($gp['status_id']);
// $status['pf_type'] = $gp['type'];
// $status['visibility'] = 'public';
// $status['url'] = $gp->url();
// return $status;
// });
$posts = GroupPostHashtag::whereGroupId($gid)
->whereHashtagId($hashtag->id)
->orderByDesc('id')
->simplePaginate($limit)
->map(function($gp) use($pid) {
$status = GroupPostService::get($gp['group_id'], $gp['status_id']);
if(!$status) {
return false;
}
$status['favourited'] = (bool) GroupsLikeService::liked($pid, $gp['status_id']);
$status['favourites_count'] = GroupsLikeService::count($gp['status_id']);
$status['pf_type'] = $status['pf_type'];
$status['visibility'] = 'public';
return $status;
});
return response()->json($posts, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
}
public function showTopicFeed(Request $request, $gid, $tag)
{
abort_if(!$request->user(), 404);
$pid = $request->user()->profile_id;
$group = Group::findOrFail($gid);
$gid = $group->id;
abort_if(!$group->isMember($pid), 403, 'Not a member of group.');
return view('groups.topic-feed', compact('gid', 'tag'));
}
}

View file

@ -17,7 +17,7 @@ trait Instagram
{ {
public function instagram() public function instagram()
{ {
if(config_cache('pixelfed.import.instagram.enabled') != true) { if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled'); abort(404, 'Feature not enabled');
} }
return view('settings.import.instagram.home'); return view('settings.import.instagram.home');
@ -25,6 +25,9 @@ trait Instagram
public function instagramStart(Request $request) public function instagramStart(Request $request)
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$completed = ImportJob::whereProfileId(Auth::user()->profile->id) $completed = ImportJob::whereProfileId(Auth::user()->profile->id)
->whereService('instagram') ->whereService('instagram')
->whereNotNull('completed_at') ->whereNotNull('completed_at')
@ -38,6 +41,9 @@ trait Instagram
protected function instagramRedirectOrNew() protected function instagramRedirectOrNew()
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$exists = ImportJob::whereProfileId($profile->id) $exists = ImportJob::whereProfileId($profile->id)
->whereService('instagram') ->whereService('instagram')
@ -61,6 +67,9 @@ trait Instagram
public function instagramStepOne(Request $request, $uuid) public function instagramStepOne(Request $request, $uuid)
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$job = ImportJob::whereProfileId($profile->id) $job = ImportJob::whereProfileId($profile->id)
->whereNull('completed_at') ->whereNull('completed_at')
@ -72,6 +81,9 @@ trait Instagram
public function instagramStepOneStore(Request $request, $uuid) public function instagramStepOneStore(Request $request, $uuid)
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$max = 'max:' . config('pixelfed.import.instagram.limits.size'); $max = 'max:' . config('pixelfed.import.instagram.limits.size');
$this->validate($request, [ $this->validate($request, [
'media.*' => 'required|mimes:bin,jpeg,png,gif|'.$max, 'media.*' => 'required|mimes:bin,jpeg,png,gif|'.$max,
@ -114,6 +126,9 @@ trait Instagram
public function instagramStepTwo(Request $request, $uuid) public function instagramStepTwo(Request $request, $uuid)
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$job = ImportJob::whereProfileId($profile->id) $job = ImportJob::whereProfileId($profile->id)
->whereNull('completed_at') ->whereNull('completed_at')
@ -125,6 +140,9 @@ trait Instagram
public function instagramStepTwoStore(Request $request, $uuid) public function instagramStepTwoStore(Request $request, $uuid)
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$this->validate($request, [ $this->validate($request, [
'media' => 'required|file|max:1000' 'media' => 'required|file|max:1000'
]); ]);
@ -150,6 +168,9 @@ trait Instagram
public function instagramStepThree(Request $request, $uuid) public function instagramStepThree(Request $request, $uuid)
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$job = ImportJob::whereProfileId($profile->id) $job = ImportJob::whereProfileId($profile->id)
->whereService('instagram') ->whereService('instagram')
@ -162,6 +183,9 @@ trait Instagram
public function instagramStepThreeStore(Request $request, $uuid) public function instagramStepThreeStore(Request $request, $uuid)
{ {
if((bool) config_cache('pixelfed.import.instagram.enabled') != true) {
abort(404, 'Feature not enabled');
}
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
try { try {

View file

@ -2,38 +2,37 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Profile;
use App\Services\AccountService;
use App\Http\Resources\DirectoryProfile; use App\Http\Resources\DirectoryProfile;
use App\Profile;
use Illuminate\Http\Request;
class LandingController extends Controller class LandingController extends Controller
{ {
public function directoryRedirect(Request $request) public function directoryRedirect(Request $request)
{ {
if($request->user()) { if ($request->user()) {
return redirect('/'); return redirect('/');
} }
abort_if(config_cache('instance.landing.show_directory') == false, 404); abort_if((bool) config_cache('instance.landing.show_directory') == false, 404);
return view('site.index'); return view('site.index');
} }
public function exploreRedirect(Request $request) public function exploreRedirect(Request $request)
{ {
if($request->user()) { if ($request->user()) {
return redirect('/'); return redirect('/');
} }
abort_if(config_cache('instance.landing.show_explore') == false, 404); abort_if((bool) config_cache('instance.landing.show_explore') == false, 404);
return view('site.index'); return view('site.index');
} }
public function getDirectoryApi(Request $request) public function getDirectoryApi(Request $request)
{ {
abort_if(config_cache('instance.landing.show_directory') == false, 404); abort_if((bool) config_cache('instance.landing.show_directory') == false, 404);
return DirectoryProfile::collection( return DirectoryProfile::collection(
Profile::whereNull('domain') Profile::whereNull('domain')

View file

@ -2,14 +2,15 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Media; use App\Media;
use Illuminate\Http\Request;
class MediaController extends Controller class MediaController extends Controller
{ {
public function index(Request $request) public function index(Request $request)
{ {
//return view('settings.drive.index'); //return view('settings.drive.index');
abort(404);
} }
public function composeUpdate(Request $request, $id) public function composeUpdate(Request $request, $id)
@ -19,8 +20,8 @@ class MediaController extends Controller
public function fallbackRedirect(Request $request, $pid, $mhash, $uhash, $f) public function fallbackRedirect(Request $request, $pid, $mhash, $uhash, $f)
{ {
abort_if(!config_cache('pixelfed.cloud_storage'), 404); abort_if(! (bool) config_cache('pixelfed.cloud_storage'), 404);
$path = 'public/m/_v2/' . $pid . '/' . $mhash . '/' . $uhash . '/' . $f; $path = 'public/m/_v2/'.$pid.'/'.$mhash.'/'.$uhash.'/'.$f;
$media = Media::whereProfileId($pid) $media = Media::whereProfileId($pid)
->whereMediaPath($path) ->whereMediaPath($path)
->whereNotNull('cdn_url') ->whereNotNull('cdn_url')

View file

@ -2,37 +2,41 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\ConfigCache; use App\Models\ConfigCache;
use Storage;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\InstanceService;
use App\Services\StatusService; use App\Services\StatusService;
use App\User;
use Cache;
use Illuminate\Http\Request;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Storage;
class PixelfedDirectoryController extends Controller class PixelfedDirectoryController extends Controller
{ {
public function get(Request $request) public function get(Request $request)
{ {
if(!$request->filled('sk')) { if (! $request->filled('sk')) {
abort(404); abort(404);
} }
if(!config_cache('pixelfed.directory.submission-key')) { if (! config_cache('pixelfed.directory.submission-key')) {
abort(404); abort(404);
} }
if(!hash_equals(config_cache('pixelfed.directory.submission-key'), $request->input('sk'))) { if (! hash_equals(config_cache('pixelfed.directory.submission-key'), $request->input('sk'))) {
abort(403); abort(403);
} }
$res = $this->buildListing(); $res = $this->buildListing();
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function buildListing() public function buildListing()
{ {
$res = config_cache('pixelfed.directory'); $res = config_cache('pixelfed.directory');
if($res) { if ($res) {
$res = is_string($res) ? json_decode($res, true) : $res; $res = is_string($res) ? json_decode($res, true) : $res;
} }
@ -41,40 +45,40 @@ class PixelfedDirectoryController extends Controller
$res['_ts'] = config_cache('pixelfed.directory.submission-ts'); $res['_ts'] = config_cache('pixelfed.directory.submission-ts');
$res['version'] = config_cache('pixelfed.version'); $res['version'] = config_cache('pixelfed.version');
if(empty($res['summary'])) { if (empty($res['summary'])) {
$summary = ConfigCache::whereK('app.short_description')->pluck('v'); $summary = ConfigCache::whereK('app.short_description')->pluck('v');
$res['summary'] = $summary ? $summary[0] : null; $res['summary'] = $summary ? $summary[0] : null;
} }
if(isset($res['admin'])) { if (isset($res['admin'])) {
$res['admin'] = AccountService::get($res['admin'], true); $res['admin'] = AccountService::get($res['admin'], true);
} }
if(isset($res['banner_image']) && !empty($res['banner_image'])) { if (isset($res['banner_image']) && ! empty($res['banner_image'])) {
$res['banner_image'] = url(Storage::url($res['banner_image'])); $res['banner_image'] = url(Storage::url($res['banner_image']));
} }
if(isset($res['favourite_posts'])) { if (isset($res['favourite_posts'])) {
$res['favourite_posts'] = collect($res['favourite_posts'])->map(function($id) { $res['favourite_posts'] = collect($res['favourite_posts'])->map(function ($id) {
return StatusService::get($id); return StatusService::get($id);
}) })
->filter(function($post) { ->filter(function ($post) {
return $post && isset($post['account']); return $post && isset($post['account']);
}) })
->map(function($post) { ->map(function ($post) {
return [ return [
'avatar' => $post['account']['avatar'], 'avatar' => $post['account']['avatar'],
'display_name' => $post['account']['display_name'], 'display_name' => $post['account']['display_name'],
'username' => $post['account']['username'], 'username' => $post['account']['username'],
'media' => $post['media_attachments'][0]['url'], 'media' => $post['media_attachments'][0]['url'],
'url' => $post['url'] 'url' => $post['url'],
]; ];
}) })
->values(); ->values();
} }
$guidelines = ConfigCache::whereK('app.rules')->first(); $guidelines = ConfigCache::whereK('app.rules')->first();
if($guidelines) { if ($guidelines) {
$res['community_guidelines'] = json_decode($guidelines->v, true); $res['community_guidelines'] = json_decode($guidelines->v, true);
} }
@ -85,27 +89,27 @@ class PixelfedDirectoryController extends Controller
$res['curated_onboarding'] = $curatedOnboarding; $res['curated_onboarding'] = $curatedOnboarding;
$oauthEnabled = ConfigCache::whereK('pixelfed.oauth_enabled')->first(); $oauthEnabled = ConfigCache::whereK('pixelfed.oauth_enabled')->first();
if($oauthEnabled) { if ($oauthEnabled) {
$keys = file_exists(storage_path('oauth-public.key')) && file_exists(storage_path('oauth-private.key')); $keys = file_exists(storage_path('oauth-public.key')) && file_exists(storage_path('oauth-private.key'));
$res['oauth_enabled'] = (bool) $oauthEnabled && $keys; $res['oauth_enabled'] = (bool) $oauthEnabled && $keys;
} }
$activityPubEnabled = ConfigCache::whereK('federation.activitypub.enabled')->first(); $activityPubEnabled = ConfigCache::whereK('federation.activitypub.enabled')->first();
if($activityPubEnabled) { if ($activityPubEnabled) {
$res['activitypub_enabled'] = (bool) $activityPubEnabled; $res['activitypub_enabled'] = (bool) $activityPubEnabled;
} }
$res['feature_config'] = [ $res['feature_config'] = [
'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','), 'media_types' => Str::of(config_cache('pixelfed.media_types'))->explode(','),
'image_quality' => config_cache('pixelfed.image_quality'), 'image_quality' => config_cache('pixelfed.image_quality'),
'optimize_image' => config_cache('pixelfed.optimize_image'), 'optimize_image' => (bool) config_cache('pixelfed.optimize_image'),
'max_photo_size' => config_cache('pixelfed.max_photo_size'), 'max_photo_size' => config_cache('pixelfed.max_photo_size'),
'max_caption_length' => config_cache('pixelfed.max_caption_length'), 'max_caption_length' => config_cache('pixelfed.max_caption_length'),
'max_altext_length' => config_cache('pixelfed.max_altext_length'), 'max_altext_length' => config_cache('pixelfed.max_altext_length'),
'enforce_account_limit' => config_cache('pixelfed.enforce_account_limit'), 'enforce_account_limit' => (bool) config_cache('pixelfed.enforce_account_limit'),
'max_account_size' => config_cache('pixelfed.max_account_size'), 'max_account_size' => config_cache('pixelfed.max_account_size'),
'max_album_length' => config_cache('pixelfed.max_album_length'), 'max_album_length' => config_cache('pixelfed.max_album_length'),
'account_deletion' => config_cache('pixelfed.account_deletion'), 'account_deletion' => (bool) config_cache('pixelfed.account_deletion'),
]; ];
$res['is_eligible'] = $this->validVal($res, 'admin') && $res['is_eligible'] = $this->validVal($res, 'admin') &&
@ -115,29 +119,34 @@ class PixelfedDirectoryController extends Controller
$this->validVal($res, 'privacy_pledge') && $this->validVal($res, 'privacy_pledge') &&
$this->validVal($res, 'location'); $this->validVal($res, 'location');
if(config_cache('pixelfed.directory.testimonials')) { if (config_cache('pixelfed.directory.testimonials')) {
$res['testimonials'] = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true)) $res['testimonials'] = collect(json_decode(config_cache('pixelfed.directory.testimonials'), true))
->map(function($testimonial) { ->map(function ($testimonial) {
$profile = AccountService::get($testimonial['profile_id']); $profile = AccountService::get($testimonial['profile_id']);
return [ return [
'profile' => [ 'profile' => [
'username' => $profile['username'], 'username' => $profile['username'],
'display_name' => $profile['display_name'], 'display_name' => $profile['display_name'],
'avatar' => $profile['avatar'], 'avatar' => $profile['avatar'],
'created_at' => $profile['created_at'] 'created_at' => $profile['created_at'],
], ],
'body' => $testimonial['body'] 'body' => $testimonial['body'],
]; ];
}); });
} }
$res['features_enabled'] = [ $res['features_enabled'] = [
'stories' => (bool) config_cache('instance.stories.enabled') 'stories' => (bool) config_cache('instance.stories.enabled'),
]; ];
$statusesCount = InstanceService::totalLocalStatuses();
$usersCount = Cache::remember('api:nodeinfo:users', 43200, function () {
return User::count();
});
$res['stats'] = [ $res['stats'] = [
'user_count' => \App\User::count(), 'user_count' => (int) $usersCount,
'post_count' => \App\Status::whereNull('uri')->count(), 'post_count' => (int) $statusesCount,
]; ];
$res['primary_locale'] = config('app.locale'); $res['primary_locale'] = config('app.locale');
@ -150,19 +159,18 @@ class PixelfedDirectoryController extends Controller
protected function validVal($res, $val, $count = false, $minLen = false) protected function validVal($res, $val, $count = false, $minLen = false)
{ {
if(!isset($res[$val])) { if (! isset($res[$val])) {
return false; return false;
} }
if($count) { if ($count) {
return count($res[$val]) >= $count; return count($res[$val]) >= $count;
} }
if($minLen) { if ($minLen) {
return strlen($res[$val]) >= $minLen; return strlen($res[$val]) >= $minLen;
} }
return $res[$val]; return $res[$val];
} }
} }

View file

@ -172,6 +172,8 @@ class ProfileController extends Controller
$user = $this->getCachedUser($username); $user = $this->getCachedUser($username);
abort_if(! $user, 404);
return redirect($user->url()); return redirect($user->url());
} }
@ -252,7 +254,7 @@ class ProfileController extends Controller
abort_if(! $profile || $profile['locked'] || ! $profile['local'], 404); abort_if(! $profile || $profile['locked'] || ! $profile['local'], 404);
$aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile['id'], 86400, function () use ($profile) { $aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile['id'], 3600, function () use ($profile) {
$uid = User::whereProfileId($profile['id'])->first(); $uid = User::whereProfileId($profile['id'])->first();
if (! $uid) { if (! $uid) {
return true; return true;
@ -267,7 +269,7 @@ class ProfileController extends Controller
abort_if($aiCheck, 404); abort_if($aiCheck, 404);
$enabled = Cache::remember('profile:atom:enabled:'.$profile['id'], 84600, function () use ($profile) { $enabled = Cache::remember('profile:atom:enabled:'.$profile['id'], 86400, function () use ($profile) {
$uid = User::whereProfileId($profile['id'])->first(); $uid = User::whereProfileId($profile['id'])->first();
if (! $uid) { if (! $uid) {
return false; return false;
@ -346,7 +348,7 @@ class ProfileController extends Controller
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
$aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile->id, 86400, function () use ($profile) { $aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile->id, 3600, function () use ($profile) {
$exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count(); $exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count();
if ($exists) { if ($exists) {
return true; return true;
@ -359,7 +361,7 @@ class ProfileController extends Controller
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
if (AccountService::canEmbed($profile->user_id) == false) { if (AccountService::canEmbed($profile->id) == false) {
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
@ -371,7 +373,7 @@ class ProfileController extends Controller
public function stories(Request $request, $username) public function stories(Request $request, $username)
{ {
abort_if(! config_cache('instance.stories.enabled') || ! $request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail(); $profile = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
$pid = $profile->id; $pid = $profile->id;
$authed = Auth::user()->profile_id; $authed = Auth::user()->profile_id;

View file

@ -2,47 +2,28 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request; use App\Follower;
use App\{ use App\Profile;
Hashtag, use App\Services\AccountService;
Follower, use App\Services\BookmarkService;
Like, use App\Services\FollowerService;
Media,
Notification,
Profile,
StatusHashtag,
Status,
StatusView,
UserFilter
};
use Auth, Cache, DB;
use Illuminate\Support\Facades\Redis;
use Carbon\Carbon;
use League\Fractal;
use App\Transformer\Api\{
AccountTransformer,
RelationshipTransformer,
StatusTransformer,
StatusStatelessTransformer
};
use App\Services\{
AccountService,
BookmarkService,
FollowerService,
LikeService,
PublicTimelineService,
ProfileService,
NetworkTimelineService,
ReblogService,
RelationshipService,
StatusService,
SnowflakeService,
UserFilterService
};
use App\Jobs\StatusPipeline\NewStatusPipeline;
use League\Fractal\Serializer\ArraySerializer;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use App\Services\InstanceService; use App\Services\InstanceService;
use App\Services\LikeService;
use App\Services\NetworkTimelineService;
use App\Services\PublicTimelineService;
use App\Services\ReblogService;
use App\Services\RelationshipService;
use App\Services\SnowflakeService;
use App\Services\StatusService;
use App\Services\UserFilterService;
use App\Status;
use App\Transformer\Api\StatusStatelessTransformer;
use Auth;
use Cache;
use Illuminate\Http\Request;
use League\Fractal;
use League\Fractal\Pagination\IlluminatePaginatorAdapter;
use League\Fractal\Serializer\ArraySerializer;
class PublicApiController extends Controller class PublicApiController extends Controller
{ {
@ -56,7 +37,7 @@ class PublicApiController extends Controller
protected function getUserData($user) protected function getUserData($user)
{ {
if(!$user) { if (! $user) {
return []; return [];
} else { } else {
return AccountService::get($user->profile_id); return AccountService::get($user->profile_id);
@ -65,18 +46,18 @@ class PublicApiController extends Controller
public function getStatus(Request $request, $id) public function getStatus(Request $request, $id)
{ {
abort_if(!$request->user(), 403); abort_if(! $request->user(), 403);
$status = StatusService::get($id, false); $status = StatusService::get($id, false);
abort_if(!$status, 404); abort_if(! $status, 404);
if(in_array($status['visibility'], ['public', 'unlisted'])) { if (in_array($status['visibility'], ['public', 'unlisted'])) {
return $status; return $status;
} }
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
if($status['account']['id'] == $pid) { if ($status['account']['id'] == $pid) {
return $status; return $status;
} }
if($status['visibility'] == 'private') { if ($status['visibility'] == 'private') {
if(FollowerService::follows($pid, $status['account']['id'])) { if (FollowerService::follows($pid, $status['account']['id'])) {
return $status; return $status;
} }
} }
@ -88,9 +69,9 @@ class PublicApiController extends Controller
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail(); $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($postid); $status = Status::whereProfileId($profile->id)->findOrFail($postid);
$this->scopeCheck($profile, $status); $this->scopeCheck($profile, $status);
if(!$request->user()) { if (! $request->user()) {
$cached = StatusService::get($status->id, false); $cached = StatusService::get($status->id, false);
abort_if(!in_array($cached['visibility'], ['public', 'unlisted']), 403); abort_if(! in_array($cached['visibility'], ['public', 'unlisted']), 403);
$res = ['status' => $cached]; $res = ['status' => $cached];
} else { } else {
$item = new Fractal\Resource\Item($status, new StatusStatelessTransformer()); $item = new Fractal\Resource\Item($status, new StatusStatelessTransformer());
@ -107,7 +88,7 @@ class PublicApiController extends Controller
$profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail(); $profile = Profile::whereUsername($username)->whereNull('status')->firstOrFail();
$status = Status::whereProfileId($profile->id)->findOrFail($postid); $status = Status::whereProfileId($profile->id)->findOrFail($postid);
$this->scopeCheck($profile, $status); $this->scopeCheck($profile, $status);
if(!Auth::check()) { if (! Auth::check()) {
$res = [ $res = [
'user' => [], 'user' => [],
'likes' => [], 'likes' => [],
@ -118,6 +99,7 @@ class PublicApiController extends Controller
'bookmarked' => false, 'bookmarked' => false,
], ],
]; ];
return response()->json($res); return response()->json($res);
} }
$res = [ $res = [
@ -130,6 +112,7 @@ class PublicApiController extends Controller
'bookmarked' => (bool) $status->bookmarked(), 'bookmarked' => (bool) $status->bookmarked(),
], ],
]; ];
return response()->json($res); return response()->json($res);
} }
@ -138,7 +121,7 @@ class PublicApiController extends Controller
$this->validate($request, [ $this->validate($request, [
'min_id' => 'nullable|integer|min:1', 'min_id' => 'nullable|integer|min:1',
'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX, 'max_id' => 'nullable|integer|min:1|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|min:5|max:50' 'limit' => 'nullable|integer|min:5|max:50',
]); ]);
$limit = $request->limit ?? 10; $limit = $request->limit ?? 10;
@ -146,15 +129,15 @@ class PublicApiController extends Controller
$status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId); $status = Status::whereProfileId($profile->id)->whereCommentsDisabled(false)->findOrFail($postId);
$this->scopeCheck($profile, $status); $this->scopeCheck($profile, $status);
if(Auth::check()) { if (Auth::check()) {
$p = Auth::user()->profile; $p = Auth::user()->profile;
$scope = $p->id == $status->profile_id || FollowerService::follows($p->id, $profile->id) ? ['public', 'private', 'unlisted'] : ['public','unlisted']; $scope = $p->id == $status->profile_id || FollowerService::follows($p->id, $profile->id) ? ['public', 'private', 'unlisted'] : ['public', 'unlisted'];
} else { } else {
$scope = ['public', 'unlisted']; $scope = ['public', 'unlisted'];
} }
if($request->filled('min_id') || $request->filled('max_id')) { if ($request->filled('min_id') || $request->filled('max_id')) {
if($request->filled('min_id')) { if ($request->filled('min_id')) {
$replies = $status->comments() $replies = $status->comments()
->whereNull('reblog_of_id') ->whereNull('reblog_of_id')
->whereIn('scope', $scope) ->whereIn('scope', $scope)
@ -163,7 +146,7 @@ class PublicApiController extends Controller
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->paginate($limit); ->paginate($limit);
} }
if($request->filled('max_id')) { if ($request->filled('max_id')) {
$replies = $status->comments() $replies = $status->comments()
->whereNull('reblog_of_id') ->whereNull('reblog_of_id')
->whereIn('scope', $scope) ->whereIn('scope', $scope)
@ -184,12 +167,13 @@ class PublicApiController extends Controller
$resource = new Fractal\Resource\Collection($replies, new StatusStatelessTransformer(), 'data'); $resource = new Fractal\Resource\Collection($replies, new StatusStatelessTransformer(), 'data');
$resource->setPaginator(new IlluminatePaginatorAdapter($replies)); $resource->setPaginator(new IlluminatePaginatorAdapter($replies));
$res = $this->fractal->createData($resource)->toArray(); $res = $this->fractal->createData($resource)->toArray();
return response()->json($res, 200, [], JSON_PRETTY_PRINT); return response()->json($res, 200, [], JSON_PRETTY_PRINT);
} }
protected function scopeCheck(Profile $profile, Status $status) protected function scopeCheck(Profile $profile, Status $status)
{ {
if($profile->is_private == true && Auth::check() == false) { if ($profile->is_private == true && Auth::check() == false) {
abort(404); abort(404);
} }
@ -199,11 +183,11 @@ class PublicApiController extends Controller
break; break;
case 'private': case 'private':
$user = Auth::check() ? Auth::user() : false; $user = Auth::check() ? Auth::user() : false;
if(!$user) { if (! $user) {
abort(403); abort(403);
} else { } else {
$follows = $profile->followedBy($user->profile); $follows = FollowerService::follows($profile->id, $user->profile_id);
if($follows == false && $profile->id !== $user->profile->id && $user->is_admin == false) { if ($follows == false && $profile->id !== $user->profile_id && $user->is_admin == false) {
abort(404); abort(404);
} }
} }
@ -225,14 +209,14 @@ class PublicApiController extends Controller
public function publicTimelineApi(Request $request) public function publicTimelineApi(Request $request)
{ {
$this->validate($request,[ $this->validate($request, [
'page' => 'nullable|integer|max:40', 'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|max:30' 'limit' => 'nullable|integer|max:30',
]); ]);
if(!$request->user()) { if (! $request->user()) {
return response('', 403); return response('', 403);
} }
@ -244,8 +228,8 @@ class PublicApiController extends Controller
$filtered = $user ? UserFilterService::filters($user->profile_id) : []; $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
$hideNsfw = config('instance.hide_nsfw_on_public_feeds'); $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
if(config('exp.cached_public_timeline') == false) { if (config('exp.cached_public_timeline') == false) {
if($min || $max) { if ($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
$timeline = Status::select( $timeline = Status::select(
@ -259,24 +243,25 @@ class PublicApiController extends Controller
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereLocal(true) ->whereLocal(true)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereScope('public') ->whereScope('public')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::getFull($s->id, $user->profile_id); $status = StatusService::getFull($s->id, $user->profile_id);
if(!$status) { if (! $status) {
return false; return false;
} }
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function($s) use($filtered) { ->filter(function ($s) use ($filtered) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values(); ->values();
@ -305,24 +290,25 @@ class PublicApiController extends Controller
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
->whereLocal(true) ->whereLocal(true)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereScope('public') ->whereScope('public')
->orderBy('id', 'desc') ->orderBy('id', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::getFull($s->id, $user->profile_id); $status = StatusService::getFull($s->id, $user->profile_id);
if(!$status) { if (! $status) {
return false; return false;
} }
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function($s) use($filtered) { ->filter(function ($s) use ($filtered) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values(); ->values();
@ -330,15 +316,15 @@ class PublicApiController extends Controller
$res = $timeline->toArray(); $res = $timeline->toArray();
} }
} else { } else {
Cache::remember('api:v1:timelines:public:cache_check', 10368000, function() { Cache::remember('api:v1:timelines:public:cache_check', 10368000, function () {
if(PublicTimelineService::count() == 0) { if (PublicTimelineService::count() == 0) {
PublicTimelineService::warmCache(true, 400); PublicTimelineService::warmCache(true, 400);
} }
}); });
if ($max) { if ($max) {
$feed = PublicTimelineService::getRankedMaxId($max, $limit); $feed = PublicTimelineService::getRankedMaxId($max, $limit);
} else if ($min) { } elseif ($min) {
$feed = PublicTimelineService::getRankedMinId($min, $limit); $feed = PublicTimelineService::getRankedMinId($min, $limit);
} else { } else {
$feed = PublicTimelineService::get(0, $limit); $feed = PublicTimelineService::get(0, $limit);
@ -346,17 +332,18 @@ class PublicApiController extends Controller
$res = collect($feed) $res = collect($feed)
->take($limit) ->take($limit)
->map(function($k) use($user) { ->map(function ($k) use ($user) {
$status = StatusService::get($k); $status = StatusService::get($k);
if($status && isset($status['account']) && $user) { if ($status && isset($status['account']) && $user) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']); $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
} }
return $status; return $status;
}) })
->filter(function($s) use($filtered) { ->filter(function ($s) use ($filtered) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -368,17 +355,17 @@ class PublicApiController extends Controller
public function homeTimelineApi(Request $request) public function homeTimelineApi(Request $request)
{ {
if(!$request->user()) { if (! $request->user()) {
return response('', 403); return response('', 403);
} }
$this->validate($request,[ $this->validate($request, [
'page' => 'nullable|integer|max:40', 'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|max:40', 'limit' => 'nullable|integer|max:40',
'recent_feed' => 'nullable', 'recent_feed' => 'nullable',
'recent_min' => 'nullable|integer' 'recent_min' => 'nullable|integer',
]); ]);
$recentFeed = $request->input('recent_feed') == 'true'; $recentFeed = $request->input('recent_feed') == 'true';
@ -390,7 +377,7 @@ class PublicApiController extends Controller
$user = $request->user(); $user = $request->user();
$key = 'user:last_active_at:id:'.$user->id; $key = 'user:last_active_at:id:'.$user->id;
if(Cache::get($key) == null) { if (Cache::get($key) == null) {
$user->last_active_at = now(); $user->last_active_at = now();
$user->save(); $user->save();
Cache::put($key, true, 43200); Cache::put($key, true, 43200);
@ -398,8 +385,9 @@ class PublicApiController extends Controller
$pid = $user->profile_id; $pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, 1209600, function() use($pid) { $following = Cache::remember('profile:following:'.$pid, 1209600, function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
}); });
@ -409,9 +397,10 @@ class PublicApiController extends Controller
$textOnlyReplies = false; $textOnlyReplies = false;
if($min || $max) { if ($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
return Status::select( return Status::select(
'id', 'id',
'uri', 'uri',
@ -433,30 +422,31 @@ class PublicApiController extends Controller
'updated_at' 'updated_at'
) )
->whereIn('type', $types) ->whereIn('type', $types)
->when(!$textOnlyReplies, function($q, $textOnlyReplies) { ->when(! $textOnlyReplies, function ($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id'); return $q->whereNull('in_reply_to_id');
}) })
->where('id', $dir, $id) ->where('id', $dir, $id)
->whereIn('profile_id', $following) ->whereIn('profile_id', $following)
->whereIn('visibility',['public', 'unlisted', 'private']) ->whereIn('visibility', ['public', 'unlisted', 'private'])
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
try { try {
$status = StatusService::get($s->id, false); $status = StatusService::get($s->id, false);
if(!$status) { if (! $status) {
return false; return false;
} }
} catch(\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function($s) use($filtered) { ->filter(function ($s) use ($filtered) {
return $s && in_array($s['account']['id'], $filtered) == false; return $s && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -483,29 +473,30 @@ class PublicApiController extends Controller
'updated_at' 'updated_at'
) )
->whereIn('type', $types) ->whereIn('type', $types)
->when(!$textOnlyReplies, function($q, $textOnlyReplies) { ->when(! $textOnlyReplies, function ($q, $textOnlyReplies) {
return $q->whereNull('in_reply_to_id'); return $q->whereNull('in_reply_to_id');
}) })
->whereIn('profile_id', $following) ->whereIn('profile_id', $following)
->whereIn('visibility',['public', 'unlisted', 'private']) ->whereIn('visibility', ['public', 'unlisted', 'private'])
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
try { try {
$status = StatusService::get($s->id, false); $status = StatusService::get($s->id, false);
if(!$status) { if (! $status) {
return false; return false;
} }
} catch(\Exception $e) { } catch (\Exception $e) {
return false; return false;
} }
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}) })
->filter(function($s) use($filtered) { ->filter(function ($s) use ($filtered) {
return $s && in_array($s['account']['id'], $filtered) == false; return $s && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -515,17 +506,17 @@ class PublicApiController extends Controller
public function networkTimelineApi(Request $request) public function networkTimelineApi(Request $request)
{ {
if(!$request->user()) { if (! $request->user()) {
return response('', 403); return response('', 403);
} }
abort_if(config('federation.network_timeline') == false, 404); abort_if(config('federation.network_timeline') == false, 404);
$this->validate($request,[ $this->validate($request, [
'page' => 'nullable|integer|max:40', 'page' => 'nullable|integer|max:40',
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|max:30' 'limit' => 'nullable|integer|max:30',
]); ]);
$page = $request->input('page'); $page = $request->input('page');
@ -538,8 +529,8 @@ class PublicApiController extends Controller
$filtered = $user ? UserFilterService::filters($user->profile_id) : []; $filtered = $user ? UserFilterService::filters($user->profile_id) : [];
$hideNsfw = config('instance.hide_nsfw_on_public_feeds'); $hideNsfw = config('instance.hide_nsfw_on_public_feeds');
if(config('instance.timeline.network.cached') == false) { if (config('instance.timeline.network.cached') == false) {
if($min || $max) { if ($min || $max) {
$dir = $min ? '>' : '<'; $dir = $min ? '>' : '<';
$id = $min ?? $max; $id = $min ?? $max;
$timeline = Status::select( $timeline = Status::select(
@ -550,7 +541,7 @@ class PublicApiController extends Controller
'created_at', 'created_at',
) )
->where('id', $dir, $id) ->where('id', $dir, $id)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
@ -562,11 +553,12 @@ class PublicApiController extends Controller
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::get($s->id); $status = StatusService::get($s->id);
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}); });
$res = $timeline->toArray(); $res = $timeline->toArray();
@ -580,7 +572,7 @@ class PublicApiController extends Controller
) )
->whereNull(['in_reply_to_id', 'reblog_of_id']) ->whereNull(['in_reply_to_id', 'reblog_of_id'])
->whereNotIn('profile_id', $filtered) ->whereNotIn('profile_id', $filtered)
->when($hideNsfw, function($q, $hideNsfw) { ->when($hideNsfw, function ($q, $hideNsfw) {
return $q->where('is_nsfw', false); return $q->where('is_nsfw', false);
}) })
->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album']) ->whereIn('type', ['photo', 'photo:album', 'video', 'video:album', 'photo:video:album'])
@ -590,25 +582,26 @@ class PublicApiController extends Controller
->orderBy('created_at', 'desc') ->orderBy('created_at', 'desc')
->limit($limit) ->limit($limit)
->get() ->get()
->map(function($s) use ($user) { ->map(function ($s) use ($user) {
$status = StatusService::get($s->id); $status = StatusService::get($s->id);
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $s->id);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $s->id);
return $status; return $status;
}); });
$res = $timeline->toArray(); $res = $timeline->toArray();
} }
} else { } else {
Cache::remember('api:v1:timelines:network:cache_check', 10368000, function() { Cache::remember('api:v1:timelines:network:cache_check', 10368000, function () {
if(NetworkTimelineService::count() == 0) { if (NetworkTimelineService::count() == 0) {
NetworkTimelineService::warmCache(true, 400); NetworkTimelineService::warmCache(true, 400);
} }
}); });
if ($max) { if ($max) {
$feed = NetworkTimelineService::getRankedMaxId($max, $limit); $feed = NetworkTimelineService::getRankedMaxId($max, $limit);
} else if ($min) { } elseif ($min) {
$feed = NetworkTimelineService::getRankedMinId($min, $limit); $feed = NetworkTimelineService::getRankedMinId($min, $limit);
} else { } else {
$feed = NetworkTimelineService::get(0, $limit); $feed = NetworkTimelineService::get(0, $limit);
@ -616,17 +609,18 @@ class PublicApiController extends Controller
$res = collect($feed) $res = collect($feed)
->take($limit) ->take($limit)
->map(function($k) use($user) { ->map(function ($k) use ($user) {
$status = StatusService::get($k); $status = StatusService::get($k);
if($status && isset($status['account']) && $user) { if ($status && isset($status['account']) && $user) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $k); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $k);
$status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k); $status['bookmarked'] = (bool) BookmarkService::get($user->profile_id, $k);
$status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k); $status['reblogged'] = (bool) ReblogService::get($user->profile_id, $k);
$status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']); $status['relationship'] = RelationshipService::get($user->profile_id, $status['account']['id']);
} }
return $status; return $status;
}) })
->filter(function($s) use($filtered) { ->filter(function ($s) use ($filtered) {
return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false; return $s && isset($s['account']) && in_array($s['account']['id'], $filtered) == false;
}) })
->values() ->values()
@ -638,7 +632,7 @@ class PublicApiController extends Controller
public function relationships(Request $request) public function relationships(Request $request)
{ {
if(!Auth::check()) { if (! Auth::check()) {
return response()->json([]); return response()->json([]);
} }
@ -646,13 +640,13 @@ class PublicApiController extends Controller
$this->validate($request, [ $this->validate($request, [
'id' => 'required|array|min:1|max:20', 'id' => 'required|array|min:1|max:20',
'id.*' => 'required|integer' 'id.*' => 'required|integer',
]); ]);
$ids = collect($request->input('id')); $ids = collect($request->input('id'));
$res = $ids->filter(function($v) use($pid) { $res = $ids->filter(function ($v) use ($pid) {
return $v != $pid; return $v != $pid;
}) })
->map(function($id) use($pid) { ->map(function ($id) use ($pid) {
return RelationshipService::get($pid, $id); return RelationshipService::get($pid, $id);
}); });
@ -662,10 +656,11 @@ class PublicApiController extends Controller
public function account(Request $request, $id) public function account(Request $request, $id)
{ {
$res = AccountService::get($id); $res = AccountService::get($id);
if($res && isset($res['local'], $res['url']) && !$res['local']) { if ($res && isset($res['local'], $res['url']) && ! $res['local']) {
$domain = parse_url($res['url'], PHP_URL_HOST); $domain = parse_url($res['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404); abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
} }
return response()->json($res); return response()->json($res);
} }
@ -675,17 +670,17 @@ class PublicApiController extends Controller
'only_media' => 'nullable', 'only_media' => 'nullable',
'pinned' => 'nullable', 'pinned' => 'nullable',
'exclude_replies' => 'nullable', 'exclude_replies' => 'nullable',
'max_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'max_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'since_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'since_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'min_id' => 'nullable|integer|min:0|max:' . PHP_INT_MAX, 'min_id' => 'nullable|integer|min:0|max:'.PHP_INT_MAX,
'limit' => 'nullable|integer|min:1|max:24' 'limit' => 'nullable|integer|min:1|max:24',
]); ]);
$user = $request->user(); $user = $request->user();
$profile = AccountService::get($id); $profile = AccountService::get($id);
abort_if(!$profile, 404); abort_if(! $profile, 404);
if($profile && isset($profile['local'], $profile['url']) && !$profile['local']) { if ($profile && isset($profile['local'], $profile['url']) && ! $profile['local']) {
$domain = parse_url($profile['url'], PHP_URL_HOST); $domain = parse_url($profile['url'], PHP_URL_HOST);
abort_if(in_array($domain, InstanceService::getBannedDomains()), 404); abort_if(in_array($domain, InstanceService::getBannedDomains()), 404);
} }
@ -696,28 +691,30 @@ class PublicApiController extends Controller
$scope = ['photo', 'photo:album', 'video', 'video:album']; $scope = ['photo', 'photo:album', 'video', 'video:album'];
$onlyMedia = $request->input('only_media', true); $onlyMedia = $request->input('only_media', true);
if(!$min_id && !$max_id) { if (! $min_id && ! $max_id) {
$min_id = 1; $min_id = 1;
} }
if($profile['locked']) { if ($profile['locked']) {
if(!$user) { if (! $user) {
return response()->json([]); return response()->json([]);
} }
$pid = $user->profile_id; $pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) { $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
}); });
$visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : []; $visibility = in_array($profile['id'], $following) == true ? ['public', 'unlisted', 'private'] : [];
} else { } else {
if($user) { if ($user) {
$pid = $user->profile_id; $pid = $user->profile_id;
$following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function() use($pid) { $following = Cache::remember('profile:following:'.$pid, now()->addMinutes(1440), function () use ($pid) {
$following = Follower::whereProfileId($pid)->pluck('following_id'); $following = Follower::whereProfileId($pid)->pluck('following_id');
return $following->push($pid)->toArray(); return $following->push($pid)->toArray();
}); });
$visibility = true == in_array($profile['id'], $following) ? ['public', 'unlisted', 'private'] : ['public', 'unlisted']; $visibility = in_array($profile['id'], $following) == true ? ['public', 'unlisted', 'private'] : ['public', 'unlisted'];
} else { } else {
$visibility = ['public', 'unlisted']; $visibility = ['public', 'unlisted'];
} }
@ -733,27 +730,29 @@ class PublicApiController extends Controller
->limit($limit) ->limit($limit)
->orderByDesc('id') ->orderByDesc('id')
->get() ->get()
->map(function($s) use($user) { ->map(function ($s) use ($user) {
try { try {
$status = StatusService::get($s->id, false); $status = StatusService::get($s->id, false);
} catch (\Exception $e) { } catch (\Exception $e) {
$status = false; $status = false;
} }
if($user && $status) { if ($user && $status) {
$status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id); $status['favourited'] = (bool) LikeService::liked($user->profile_id, $s->id);
} }
return $status; return $status;
}) })
->filter(function($s) use($onlyMedia) { ->filter(function ($s) use ($onlyMedia) {
if($onlyMedia) { if ($onlyMedia) {
if( if (
!isset($s['media_attachments']) || ! isset($s['media_attachments']) ||
!is_array($s['media_attachments']) || ! is_array($s['media_attachments']) ||
empty($s['media_attachments']) empty($s['media_attachments'])
) { ) {
return false; return false;
} }
} }
return $s; return $s;
}) })
->values(); ->values();

View file

@ -2,22 +2,20 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use App\Services\Account\RemoteAuthService;
use App\Models\RemoteAuth; use App\Models\RemoteAuth;
use App\Profile; use App\Services\Account\RemoteAuthService;
use App\Instance;
use App\User;
use Purify;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Auth\Events\Registered;
use App\Util\Lexer\RestrictedNames;
use App\Services\EmailService; use App\Services\EmailService;
use App\Services\MediaStorageService; use App\Services\MediaStorageService;
use App\User;
use App\Util\ActivityPub\Helpers; use App\Util\ActivityPub\Helpers;
use App\Util\Lexer\RestrictedNames;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
use InvalidArgumentException; use InvalidArgumentException;
use Purify;
class RemoteAuthController extends Controller class RemoteAuthController extends Controller
{ {
@ -30,9 +28,10 @@ class RemoteAuthController extends Controller
config('remote-auth.mastodon.ignore_closed_state') && config('remote-auth.mastodon.ignore_closed_state') &&
config('remote-auth.mastodon.enabled') config('remote-auth.mastodon.enabled')
), 404); ), 404);
if($request->user()) { if ($request->user()) {
return redirect('/'); return redirect('/');
} }
return view('auth.remote.start'); return view('auth.remote.start');
} }
@ -51,25 +50,27 @@ class RemoteAuthController extends Controller
config('remote-auth.mastodon.enabled') config('remote-auth.mastodon.enabled')
), 404); ), 404);
if(config('remote-auth.mastodon.domains.only_custom')) { if (config('remote-auth.mastodon.domains.only_custom')) {
$res = config('remote-auth.mastodon.domains.custom'); $res = config('remote-auth.mastodon.domains.custom');
if(!$res || !strlen($res)) { if (! $res || ! strlen($res)) {
return []; return [];
} }
$res = explode(',', $res); $res = explode(',', $res);
return response()->json($res); return response()->json($res);
} }
if( config('remote-auth.mastodon.domains.custom') && if (config('remote-auth.mastodon.domains.custom') &&
!config('remote-auth.mastodon.domains.only_default') && ! config('remote-auth.mastodon.domains.only_default') &&
strlen(config('remote-auth.mastodon.domains.custom')) > 3 && strlen(config('remote-auth.mastodon.domains.custom')) > 3 &&
strpos(config('remote-auth.mastodon.domains.custom'), '.') > -1 strpos(config('remote-auth.mastodon.domains.custom'), '.') > -1
) { ) {
$res = config('remote-auth.mastodon.domains.custom'); $res = config('remote-auth.mastodon.domains.custom');
if(!$res || !strlen($res)) { if (! $res || ! strlen($res)) {
return []; return [];
} }
$res = explode(',', $res); $res = explode(',', $res);
return response()->json($res); return response()->json($res);
} }
@ -93,57 +94,62 @@ class RemoteAuthController extends Controller
$domain = $request->input('domain'); $domain = $request->input('domain');
if(str_starts_with(strtolower($domain), 'http')) { if (str_starts_with(strtolower($domain), 'http')) {
$res = [ $res = [
'domain' => $domain, 'domain' => $domain,
'ready' => false, 'ready' => false,
'action' => 'incompatible_domain' 'action' => 'incompatible_domain',
]; ];
return response()->json($res); return response()->json($res);
} }
$validateInstance = Helpers::validateUrl('https://' . $domain . '/?block-check=' . time()); $validateInstance = Helpers::validateUrl('https://'.$domain.'/?block-check='.time());
if(!$validateInstance) { if (! $validateInstance) {
$res = [ $res = [
'domain' => $domain, 'domain' => $domain,
'ready' => false, 'ready' => false,
'action' => 'blocked_domain' 'action' => 'blocked_domain',
]; ];
return response()->json($res); return response()->json($res);
} }
$compatible = RemoteAuthService::isDomainCompatible($domain); $compatible = RemoteAuthService::isDomainCompatible($domain);
if(!$compatible) { if (! $compatible) {
$res = [ $res = [
'domain' => $domain, 'domain' => $domain,
'ready' => false, 'ready' => false,
'action' => 'incompatible_domain' 'action' => 'incompatible_domain',
]; ];
return response()->json($res); return response()->json($res);
} }
if(config('remote-auth.mastodon.domains.only_default')) { if (config('remote-auth.mastodon.domains.only_default')) {
$defaultDomains = explode(',', config('remote-auth.mastodon.domains.default')); $defaultDomains = explode(',', config('remote-auth.mastodon.domains.default'));
if(!in_array($domain, $defaultDomains)) { if (! in_array($domain, $defaultDomains)) {
$res = [ $res = [
'domain' => $domain, 'domain' => $domain,
'ready' => false, 'ready' => false,
'action' => 'incompatible_domain' 'action' => 'incompatible_domain',
]; ];
return response()->json($res); return response()->json($res);
} }
} }
if(config('remote-auth.mastodon.domains.only_custom') && config('remote-auth.mastodon.domains.custom')) { if (config('remote-auth.mastodon.domains.only_custom') && config('remote-auth.mastodon.domains.custom')) {
$customDomains = explode(',', config('remote-auth.mastodon.domains.custom')); $customDomains = explode(',', config('remote-auth.mastodon.domains.custom'));
if(!in_array($domain, $customDomains)) { if (! in_array($domain, $customDomains)) {
$res = [ $res = [
'domain' => $domain, 'domain' => $domain,
'ready' => false, 'ready' => false,
'action' => 'incompatible_domain' 'action' => 'incompatible_domain',
]; ];
return response()->json($res); return response()->json($res);
} }
} }
@ -163,13 +169,13 @@ class RemoteAuthController extends Controller
'state' => $state, 'state' => $state,
]); ]);
$request->session()->put('oauth_redirect_to', 'https://' . $domain . '/oauth/authorize?' . $query); $request->session()->put('oauth_redirect_to', 'https://'.$domain.'/oauth/authorize?'.$query);
$dsh = Str::random(17); $dsh = Str::random(17);
$res = [ $res = [
'domain' => $domain, 'domain' => $domain,
'ready' => true, 'ready' => true,
'dsh' => $dsh 'dsh' => $dsh,
]; ];
return response()->json($res); return response()->json($res);
@ -185,7 +191,7 @@ class RemoteAuthController extends Controller
config('remote-auth.mastodon.enabled') config('remote-auth.mastodon.enabled')
), 404); ), 404);
if(!$request->filled('d') || !$request->filled('dsh') || !$request->session()->exists('oauth_redirect_to')) { if (! $request->filled('d') || ! $request->filled('dsh') || ! $request->session()->exists('oauth_redirect_to')) {
return redirect('/login'); return redirect('/login');
} }
@ -204,7 +210,7 @@ class RemoteAuthController extends Controller
$domain = $request->session()->get('oauth_domain'); $domain = $request->session()->get('oauth_domain');
if($request->filled('code')) { if ($request->filled('code')) {
$code = $request->input('code'); $code = $request->input('code');
$state = $request->session()->pull('state'); $state = $request->session()->pull('state');
@ -216,12 +222,14 @@ class RemoteAuthController extends Controller
$res = RemoteAuthService::getToken($domain, $code); $res = RemoteAuthService::getToken($domain, $code);
if(!$res || !isset($res['access_token'])) { if (! $res || ! isset($res['access_token'])) {
$request->session()->regenerate(); $request->session()->regenerate();
return redirect('/login'); return redirect('/login');
} }
$request->session()->put('oauth_remote_session_token', $res['access_token']); $request->session()->put('oauth_remote_session_token', $res['access_token']);
return redirect('/auth/mastodon/getting-started'); return redirect('/auth/mastodon/getting-started');
} }
@ -237,9 +245,10 @@ class RemoteAuthController extends Controller
config('remote-auth.mastodon.ignore_closed_state') && config('remote-auth.mastodon.ignore_closed_state') &&
config('remote-auth.mastodon.enabled') config('remote-auth.mastodon.enabled')
), 404); ), 404);
if($request->user()) { if ($request->user()) {
return redirect('/'); return redirect('/');
} }
return view('auth.remote.onboarding'); return view('auth.remote.onboarding');
} }
@ -261,36 +270,36 @@ class RemoteAuthController extends Controller
$res = RemoteAuthService::getVerifyCredentials($domain, $token); $res = RemoteAuthService::getVerifyCredentials($domain, $token);
abort_if(!$res || !isset($res['acct']), 403, 'Invalid credentials'); abort_if(! $res || ! isset($res['acct']), 403, 'Invalid credentials');
$webfinger = strtolower('@' . $res['acct'] . '@' . $domain); $webfinger = strtolower('@'.$res['acct'].'@'.$domain);
$request->session()->put('oauth_masto_webfinger', $webfinger); $request->session()->put('oauth_masto_webfinger', $webfinger);
if(config('remote-auth.mastodon.max_uses.enabled')) { if (config('remote-auth.mastodon.max_uses.enabled')) {
$limit = config('remote-auth.mastodon.max_uses.limit'); $limit = config('remote-auth.mastodon.max_uses.limit');
$uses = RemoteAuthService::lookupWebfingerUses($webfinger); $uses = RemoteAuthService::lookupWebfingerUses($webfinger);
if($uses >= $limit) { if ($uses >= $limit) {
return response()->json([ return response()->json([
'code' => 200, 'code' => 200,
'msg' => 'Success!', 'msg' => 'Success!',
'action' => 'max_uses_reached' 'action' => 'max_uses_reached',
]); ]);
} }
} }
$exists = RemoteAuth::whereDomain($domain)->where('webfinger', $webfinger)->whereNotNull('user_id')->first(); $exists = RemoteAuth::whereDomain($domain)->where('webfinger', $webfinger)->whereNotNull('user_id')->first();
if($exists && $exists->user_id) { if ($exists && $exists->user_id) {
return response()->json([ return response()->json([
'code' => 200, 'code' => 200,
'msg' => 'Success!', 'msg' => 'Success!',
'action' => 'redirect_existing_user' 'action' => 'redirect_existing_user',
]); ]);
} }
return response()->json([ return response()->json([
'code' => 200, 'code' => 200,
'msg' => 'Success!', 'msg' => 'Success!',
'action' => 'onboard' 'action' => 'onboard',
]); ]);
} }
@ -311,7 +320,7 @@ class RemoteAuthController extends Controller
$token = $request->session()->get('oauth_remote_session_token'); $token = $request->session()->get('oauth_remote_session_token');
$res = RemoteAuthService::getVerifyCredentials($domain, $token); $res = RemoteAuthService::getVerifyCredentials($domain, $token);
$res['_webfinger'] = strtolower('@' . $res['acct'] . '@' . $domain); $res['_webfinger'] = strtolower('@'.$res['acct'].'@'.$domain);
$res['_domain'] = strtolower($domain); $res['_domain'] = strtolower($domain);
$request->session()->put('oauth_remasto_id', $res['id']); $request->session()->put('oauth_remasto_id', $res['id']);
@ -324,7 +333,7 @@ class RemoteAuthController extends Controller
'bearer_token' => $token, 'bearer_token' => $token,
'verify_credentials' => $res, 'verify_credentials' => $res,
'last_verify_credentials_at' => now(), 'last_verify_credentials_at' => now(),
'last_successful_login_at' => now() 'last_successful_login_at' => now(),
]); ]);
$request->session()->put('oauth_masto_raid', $ra->id); $request->session()->put('oauth_masto_raid', $ra->id);
@ -355,24 +364,24 @@ class RemoteAuthController extends Controller
$underscore = substr_count($value, '_'); $underscore = substr_count($value, '_');
$period = substr_count($value, '.'); $period = substr_count($value, '.');
if(ends_with($value, ['.php', '.js', '.css'])) { if (ends_with($value, ['.php', '.js', '.css'])) {
return $fail('Username is invalid.'); return $fail('Username is invalid.');
} }
if(($dash + $underscore + $period) > 1) { if (($dash + $underscore + $period) > 1) {
return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).'); return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
} }
if (!ctype_alnum($value[0])) { if (! ctype_alnum($value[0])) {
return $fail('Username is invalid. Must start with a letter or number.'); return $fail('Username is invalid. Must start with a letter or number.');
} }
if (!ctype_alnum($value[strlen($value) - 1])) { if (! ctype_alnum($value[strlen($value) - 1])) {
return $fail('Username is invalid. Must end with a letter or number.'); return $fail('Username is invalid. Must end with a letter or number.');
} }
$val = str_replace(['_', '.', '-'], '', $value); $val = str_replace(['_', '.', '-'], '', $value);
if(!ctype_alnum($val)) { if (! ctype_alnum($val)) {
return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).'); return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
} }
@ -380,8 +389,8 @@ class RemoteAuthController extends Controller
if (in_array(strtolower($value), array_map('strtolower', $restricted))) { if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
return $fail('Username cannot be used.'); return $fail('Username cannot be used.');
} }
} },
] ],
]); ]);
$username = strtolower($request->input('username')); $username = strtolower($request->input('username'));
@ -390,7 +399,7 @@ class RemoteAuthController extends Controller
return response()->json([ return response()->json([
'code' => 200, 'code' => 200,
'username' => $username, 'username' => $username,
'exists' => $exists 'exists' => $exists,
]); ]);
} }
@ -411,7 +420,7 @@ class RemoteAuthController extends Controller
'email' => [ 'email' => [
'required', 'required',
'email:strict,filter_unicode,dns,spoof', 'email:strict,filter_unicode,dns,spoof',
] ],
]); ]);
$email = $request->input('email'); $email = $request->input('email');
@ -422,7 +431,7 @@ class RemoteAuthController extends Controller
'code' => 200, 'code' => 200,
'email' => $email, 'email' => $email,
'exists' => $exists, 'exists' => $exists,
'banned' => $banned 'banned' => $banned,
]); ]);
} }
@ -445,18 +454,18 @@ class RemoteAuthController extends Controller
$res = RemoteAuthService::getFollowing($domain, $token, $id); $res = RemoteAuthService::getFollowing($domain, $token, $id);
if(!$res) { if (! $res) {
return response()->json([ return response()->json([
'code' => 200, 'code' => 200,
'following' => [] 'following' => [],
]); ]);
} }
$res = collect($res)->filter(fn($acct) => Helpers::validateUrl($acct['url']))->values()->toArray(); $res = collect($res)->filter(fn ($acct) => Helpers::validateUrl($acct['url']))->values()->toArray();
return response()->json([ return response()->json([
'code' => 200, 'code' => 200,
'following' => $res 'following' => $res,
]); ]);
} }
@ -487,24 +496,24 @@ class RemoteAuthController extends Controller
$underscore = substr_count($value, '_'); $underscore = substr_count($value, '_');
$period = substr_count($value, '.'); $period = substr_count($value, '.');
if(ends_with($value, ['.php', '.js', '.css'])) { if (ends_with($value, ['.php', '.js', '.css'])) {
return $fail('Username is invalid.'); return $fail('Username is invalid.');
} }
if(($dash + $underscore + $period) > 1) { if (($dash + $underscore + $period) > 1) {
return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).'); return $fail('Username is invalid. Can only contain one dash (-), period (.) or underscore (_).');
} }
if (!ctype_alnum($value[0])) { if (! ctype_alnum($value[0])) {
return $fail('Username is invalid. Must start with a letter or number.'); return $fail('Username is invalid. Must start with a letter or number.');
} }
if (!ctype_alnum($value[strlen($value) - 1])) { if (! ctype_alnum($value[strlen($value) - 1])) {
return $fail('Username is invalid. Must end with a letter or number.'); return $fail('Username is invalid. Must end with a letter or number.');
} }
$val = str_replace(['_', '.', '-'], '', $value); $val = str_replace(['_', '.', '-'], '', $value);
if(!ctype_alnum($val)) { if (! ctype_alnum($val)) {
return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).'); return $fail('Username is invalid. Username must be alpha-numeric and may contain dashes (-), periods (.) and underscores (_).');
} }
@ -512,10 +521,10 @@ class RemoteAuthController extends Controller
if (in_array(strtolower($value), array_map('strtolower', $restricted))) { if (in_array(strtolower($value), array_map('strtolower', $restricted))) {
return $fail('Username cannot be used.'); return $fail('Username cannot be used.');
} }
} },
], ],
'password' => 'required|string|min:8|confirmed', 'password' => 'required|string|min:8|confirmed',
'name' => 'nullable|max:30' 'name' => 'nullable|max:30',
]); ]);
$email = $request->input('email'); $email = $request->input('email');
@ -527,7 +536,7 @@ class RemoteAuthController extends Controller
'name' => $name, 'name' => $name,
'username' => $username, 'username' => $username,
'password' => $password, 'password' => $password,
'email' => $email 'email' => $email,
]); ]);
$raid = $request->session()->pull('oauth_masto_raid'); $raid = $request->session()->pull('oauth_masto_raid');
@ -541,7 +550,7 @@ class RemoteAuthController extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Success', 'msg' => 'Success',
'token' => $token 'token' => $token,
]; ];
} }
@ -585,7 +594,7 @@ class RemoteAuthController extends Controller
abort_unless($request->session()->exists('oauth_remasto_id'), 403); abort_unless($request->session()->exists('oauth_remasto_id'), 403);
$this->validate($request, [ $this->validate($request, [
'account' => 'required|url' 'account' => 'required|url',
]); ]);
$account = $request->input('account'); $account = $request->input('account');
@ -594,10 +603,10 @@ class RemoteAuthController extends Controller
$host = strtolower(config('pixelfed.domain.app')); $host = strtolower(config('pixelfed.domain.app'));
$domain = strtolower(parse_url($account, PHP_URL_HOST)); $domain = strtolower(parse_url($account, PHP_URL_HOST));
if($domain == $host) { if ($domain == $host) {
$username = Str::of($account)->explode('/')->last(); $username = Str::of($account)->explode('/')->last();
$user = User::where('username', $username)->first(); $user = User::where('username', $username)->first();
if($user) { if ($user) {
return ['id' => (string) $user->profile_id]; return ['id' => (string) $user->profile_id];
} else { } else {
return []; return [];
@ -605,7 +614,7 @@ class RemoteAuthController extends Controller
} else { } else {
try { try {
$profile = Helpers::profileFetch($account); $profile = Helpers::profileFetch($account);
if($profile) { if ($profile) {
return ['id' => (string) $profile->id]; return ['id' => (string) $profile->id];
} else { } else {
return []; return [];
@ -635,13 +644,13 @@ class RemoteAuthController extends Controller
$user = $request->user(); $user = $request->user();
$profile = $user->profile; $profile = $user->profile;
abort_if(!$profile->avatar, 404, 'Missing avatar'); abort_if(! $profile->avatar, 404, 'Missing avatar');
$avatar = $profile->avatar; $avatar = $profile->avatar;
$avatar->remote_url = $request->input('avatar_url'); $avatar->remote_url = $request->input('avatar_url');
$avatar->save(); $avatar->save();
MediaStorageService::avatar($avatar, config_cache('pixelfed.cloud_storage') == false); MediaStorageService::avatar($avatar, (bool) config_cache('pixelfed.cloud_storage') == false);
return [200]; return [200];
} }
@ -657,7 +666,7 @@ class RemoteAuthController extends Controller
), 404); ), 404);
abort_unless($request->user(), 404); abort_unless($request->user(), 404);
$currentWebfinger = '@' . $request->user()->username . '@' . config('pixelfed.domain.app'); $currentWebfinger = '@'.$request->user()->username.'@'.config('pixelfed.domain.app');
$ra = RemoteAuth::where('user_id', $request->user()->id)->firstOrFail(); $ra = RemoteAuth::where('user_id', $request->user()->id)->firstOrFail();
RemoteAuthService::submitToBeagle( RemoteAuthService::submitToBeagle(
$ra->webfinger, $ra->webfinger,
@ -691,6 +700,7 @@ class RemoteAuthController extends Controller
$user = User::findOrFail($ra->user_id); $user = User::findOrFail($ra->user_id);
abort_if($user->is_admin || $user->status != null, 422, 'Invalid auth action'); abort_if($user->is_admin || $user->status != null, 422, 'Invalid auth action');
Auth::loginUsingId($ra->user_id); Auth::loginUsingId($ra->user_id);
return [200]; return [200];
} }
@ -703,7 +713,7 @@ class RemoteAuthController extends Controller
'password' => Hash::make($data['password']), 'password' => Hash::make($data['password']),
'email_verified_at' => config('remote-auth.mastodon.contraints.skip_email_verification') ? now() : null, 'email_verified_at' => config('remote-auth.mastodon.contraints.skip_email_verification') ? now() : null,
'app_register_ip' => request()->ip(), 'app_register_ip' => request()->ip(),
'register_source' => 'mastodon' 'register_source' => 'mastodon',
]))); ])));
$this->guarder()->login($user); $this->guarder()->login($user);

View file

@ -2,27 +2,25 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Auth;
use App\Hashtag; use App\Hashtag;
use App\Place; use App\Place;
use App\Profile; use App\Profile;
use App\Services\WebfingerService;
use App\Status; use App\Status;
use Illuminate\Http\Request;
use App\Util\ActivityPub\Helpers; use App\Util\ActivityPub\Helpers;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use App\Transformer\Api\{
AccountTransformer,
HashtagTransformer,
StatusTransformer,
};
use App\Services\WebfingerService;
class SearchController extends Controller class SearchController extends Controller
{ {
public $tokens = []; public $tokens = [];
public $term = ''; public $term = '';
public $hash = ''; public $hash = '';
public $cacheKey = 'api:search:tag:'; public $cacheKey = 'api:search:tag:';
public function __construct() public function __construct()
@ -36,7 +34,7 @@ class SearchController extends Controller
'q' => 'required|string|min:3|max:120', 'q' => 'required|string|min:3|max:120',
'src' => 'required|string|in:metro', 'src' => 'required|string|in:metro',
'v' => 'required|integer|in:2', 'v' => 'required|integer|in:2',
'scope' => 'required|in:all,hashtag,profile,remote,webfinger' 'scope' => 'required|in:all,hashtag,profile,remote,webfinger',
]); ]);
$scope = $request->input('scope') ?? 'all'; $scope = $request->input('scope') ?? 'all';
@ -82,14 +80,15 @@ class SearchController extends Controller
{ {
$tag = $this->term; $tag = $this->term;
$hash = hash('sha256', $tag); $hash = hash('sha256', $tag);
if( Helpers::validateUrl($tag) != false && if (Helpers::validateUrl($tag) != false &&
Helpers::validateLocalUrl($tag) != true && Helpers::validateLocalUrl($tag) != true &&
config_cache('federation.activitypub.enabled') == true && (bool) config_cache('federation.activitypub.enabled') == true &&
config('federation.activitypub.remoteFollow') == true config('federation.activitypub.remoteFollow') == true
) { ) {
$remote = Helpers::fetchFromUrl($tag); $remote = Helpers::fetchFromUrl($tag);
if( isset($remote['type']) && if (isset($remote['type']) &&
$remote['type'] == 'Note') { in_array($remote['type'], ['Note', 'Question'])
) {
$item = Helpers::statusFetch($tag); $item = Helpers::statusFetch($tag);
$this->tokens['posts'] = [[ $this->tokens['posts'] = [[
'count' => 0, 'count' => 0,
@ -112,8 +111,8 @@ class SearchController extends Controller
->limit(10) ->limit(10)
->get(); ->get();
if($posts->count() > 0) { if ($posts->count() > 0) {
$posts = $posts->map(function($item, $key) { $posts = $posts->map(function ($item, $key) {
return [ return [
'count' => 0, 'count' => 0,
'url' => $item->url(), 'url' => $item->url(),
@ -122,7 +121,7 @@ class SearchController extends Controller
'tokens' => [$item->caption], 'tokens' => [$item->caption],
'name' => $item->caption, 'name' => $item->caption,
'thumb' => $item->thumb(), 'thumb' => $item->thumb(),
'filter' => $item->firstMedia()->filter_class 'filter' => $item->firstMedia()->filter_class,
]; ];
}); });
$this->tokens['posts'] = $posts; $this->tokens['posts'] = $posts;
@ -133,16 +132,16 @@ class SearchController extends Controller
protected function getHashtags() protected function getHashtags()
{ {
$tag = $this->term; $tag = $this->term;
$key = $this->cacheKey . 'hashtags:' . $this->hash; $key = $this->cacheKey.'hashtags:'.$this->hash;
$ttl = now()->addMinutes(1); $ttl = now()->addMinutes(1);
$tokens = Cache::remember($key, $ttl, function() use($tag) { $tokens = Cache::remember($key, $ttl, function () use ($tag) {
$htag = Str::startsWith($tag, '#') == true ? mb_substr($tag, 1) : $tag; $htag = Str::startsWith($tag, '#') == true ? mb_substr($tag, 1) : $tag;
$hashtags = Hashtag::select('id', 'name', 'slug') $hashtags = Hashtag::select('id', 'name', 'slug')
->where('slug', 'like', '%'.$htag.'%') ->where('slug', 'like', '%'.$htag.'%')
->whereHas('posts') ->whereHas('posts')
->limit(20) ->limit(20)
->get(); ->get();
if($hashtags->count() > 0) { if ($hashtags->count() > 0) {
$tags = $hashtags->map(function ($item, $key) { $tags = $hashtags->map(function ($item, $key) {
return [ return [
'count' => $item->posts()->count(), 'count' => $item->posts()->count(),
@ -153,6 +152,7 @@ class SearchController extends Controller
'name' => null, 'name' => null,
]; ];
}); });
return $tags; return $tags;
} }
}); });
@ -170,17 +170,17 @@ class SearchController extends Controller
->where('name', 'like', '%'.$htag[0].'%') ->where('name', 'like', '%'.$htag[0].'%')
->paginate(20); ->paginate(20);
$tags = []; $tags = [];
if($hashtags->count() > 0) { if ($hashtags->count() > 0) {
$tags = $hashtags->map(function ($item, $key) { $tags = $hashtags->map(function ($item, $key) {
return [ return [
'count' => null, 'count' => null,
'url' => $item->url(), 'url' => $item->url(),
'type' => 'place', 'type' => 'place',
'value' => $item->name . ', ' . $item->country, 'value' => $item->name.', '.$item->country,
'tokens' => '', 'tokens' => '',
'name' => null, 'name' => null,
'city' => $item->name, 'city' => $item->name,
'country' => $item->country 'country' => $item->country,
]; ];
}); });
// return $tags; // return $tags;
@ -190,27 +190,27 @@ class SearchController extends Controller
$this->tokens['placesPagination'] = [ $this->tokens['placesPagination'] = [
'total' => $hashtags->total(), 'total' => $hashtags->total(),
'current_page' => $hashtags->currentPage(), 'current_page' => $hashtags->currentPage(),
'last_page' => $hashtags->lastPage() 'last_page' => $hashtags->lastPage(),
]; ];
} }
protected function getProfiles() protected function getProfiles()
{ {
$tag = $this->term; $tag = $this->term;
$remoteKey = $this->cacheKey . 'profiles:remote:' . $this->hash; $remoteKey = $this->cacheKey.'profiles:remote:'.$this->hash;
$key = $this->cacheKey . 'profiles:' . $this->hash; $key = $this->cacheKey.'profiles:'.$this->hash;
$remoteTtl = now()->addMinutes(15); $remoteTtl = now()->addMinutes(15);
$ttl = now()->addHours(2); $ttl = now()->addHours(2);
if( Helpers::validateUrl($tag) != false && if (Helpers::validateUrl($tag) != false &&
Helpers::validateLocalUrl($tag) != true && Helpers::validateLocalUrl($tag) != true &&
config_cache('federation.activitypub.enabled') == true && (bool) config_cache('federation.activitypub.enabled') == true &&
config('federation.activitypub.remoteFollow') == true config('federation.activitypub.remoteFollow') == true
) { ) {
$remote = Helpers::fetchFromUrl($tag); $remote = Helpers::fetchFromUrl($tag);
if( isset($remote['type']) && if (isset($remote['type']) &&
$remote['type'] == 'Person' $remote['type'] == 'Person'
) { ) {
$this->tokens['profiles'] = Cache::remember($remoteKey, $remoteTtl, function() use($tag) { $this->tokens['profiles'] = Cache::remember($remoteKey, $remoteTtl, function () use ($tag) {
$item = Helpers::profileFirstOrNew($tag); $item = Helpers::profileFirstOrNew($tag);
$tokens = [[ $tokens = [[
'count' => 1, 'count' => 1,
@ -224,18 +224,17 @@ class SearchController extends Controller
'following' => $item->followedBy(Auth::user()->profile), 'following' => $item->followedBy(Auth::user()->profile),
'follow_request' => $item->hasFollowRequestById(Auth::user()->profile_id), 'follow_request' => $item->hasFollowRequestById(Auth::user()->profile_id),
'thumb' => $item->avatarUrl(), 'thumb' => $item->avatarUrl(),
'local' => (bool) !$item->domain, 'local' => (bool) ! $item->domain,
'post_count' => $item->statuses()->count() 'post_count' => $item->statuses()->count(),
] ],
]]; ]];
return $tokens; return $tokens;
}); });
} }
} } else {
$this->tokens['profiles'] = Cache::remember($key, $ttl, function () use ($tag) {
else { if (Str::startsWith($tag, '@')) {
$this->tokens['profiles'] = Cache::remember($key, $ttl, function() use($tag) {
if(Str::startsWith($tag, '@')) {
$tag = substr($tag, 1); $tag = substr($tag, 1);
} }
$users = Profile::select('status', 'domain', 'username', 'name', 'id') $users = Profile::select('status', 'domain', 'username', 'name', 'id')
@ -245,7 +244,7 @@ class SearchController extends Controller
->orderBy('domain') ->orderBy('domain')
->get(); ->get();
if($users->count() > 0) { if ($users->count() > 0) {
return $users->map(function ($item, $key) { return $users->map(function ($item, $key) {
return [ return [
'count' => 0, 'count' => 0,
@ -261,9 +260,9 @@ class SearchController extends Controller
'following' => $item->followedBy(Auth::user()->profile), 'following' => $item->followedBy(Auth::user()->profile),
'follow_request' => $item->hasFollowRequestById(Auth::user()->profile_id), 'follow_request' => $item->hasFollowRequestById(Auth::user()->profile_id),
'thumb' => $item->avatarUrl(), 'thumb' => $item->avatarUrl(),
'local' => (bool) !$item->domain, 'local' => (bool) ! $item->domain,
'post_count' => $item->statuses()->count() 'post_count' => $item->statuses()->count(),
] ],
]; ];
}); });
} }
@ -284,7 +283,7 @@ class SearchController extends Controller
{ {
$wfs = WebfingerService::lookup($this->term); $wfs = WebfingerService::lookup($this->term);
if(empty($wfs)) { if (empty($wfs)) {
return; return;
} }
@ -301,11 +300,11 @@ class SearchController extends Controller
'following' => null, 'following' => null,
'follow_request' => null, 'follow_request' => null,
'thumb' => $wfs['avatar'], 'thumb' => $wfs['avatar'],
'local' => (bool) $wfs['local'] 'local' => (bool) $wfs['local'],
] ],
] ],
]; ];
return;
} }
protected function remotePostLookup() protected function remotePostLookup()
@ -315,15 +314,15 @@ class SearchController extends Controller
$local = Helpers::validateLocalUrl($tag); $local = Helpers::validateLocalUrl($tag);
$valid = Helpers::validateUrl($tag); $valid = Helpers::validateUrl($tag);
if($valid == false || $local == true) { if ($valid == false || $local == true) {
return; return;
} }
if(Status::whereUri($tag)->whereLocal(false)->exists()) { if (Status::whereUri($tag)->whereLocal(false)->exists()) {
$item = Status::whereUri($tag)->first(); $item = Status::whereUri($tag)->first();
$media = $item->firstMedia(); $media = $item->firstMedia();
$url = null; $url = null;
if($media) { if ($media) {
$url = $media->remote_url; $url = $media->remote_url;
} }
$this->tokens['posts'] = [[ $this->tokens['posts'] = [[
@ -333,17 +332,17 @@ class SearchController extends Controller
'username' => $item->profile->username, 'username' => $item->profile->username,
'caption' => $item->rendered ?? $item->caption, 'caption' => $item->rendered ?? $item->caption,
'thumb' => $url, 'thumb' => $url,
'timestamp' => $item->created_at->diffForHumans() 'timestamp' => $item->created_at->diffForHumans(),
]]; ]];
} }
$remote = Helpers::fetchFromUrl($tag); $remote = Helpers::fetchFromUrl($tag);
if(isset($remote['type']) && $remote['type'] == 'Note') { if (isset($remote['type']) && $remote['type'] == 'Note') {
$item = Helpers::statusFetch($tag); $item = Helpers::statusFetch($tag);
$media = $item->firstMedia(); $media = $item->firstMedia();
$url = null; $url = null;
if($media) { if ($media) {
$url = $media->remote_url; $url = $media->remote_url;
} }
$this->tokens['posts'] = [[ $this->tokens['posts'] = [[
@ -353,14 +352,14 @@ class SearchController extends Controller
'username' => $item->profile->username, 'username' => $item->profile->username,
'caption' => $item->rendered ?? $item->caption, 'caption' => $item->rendered ?? $item->caption,
'thumb' => $url, 'thumb' => $url,
'timestamp' => $item->created_at->diffForHumans() 'timestamp' => $item->created_at->diffForHumans(),
]]; ]];
} }
} }
protected function remoteLookupSearch() protected function remoteLookupSearch()
{ {
if(!Helpers::validateUrl($this->term)) { if (! Helpers::validateUrl($this->term)) {
return; return;
} }
$this->getProfiles(); $this->getProfiles();

View file

@ -4,21 +4,17 @@ namespace App\Http\Controllers\Settings;
use App\AccountLog; use App\AccountLog;
use App\EmailVerification; use App\EmailVerification;
use App\Mail\PasswordChange;
use App\Media; use App\Media;
use App\Profile; use App\Services\AccountService;
use App\User; use App\Services\PronounService;
use App\UserFilter;
use App\Util\Lexer\Autolink; use App\Util\Lexer\Autolink;
use App\Util\Lexer\PrettyNumber; use App\Util\Lexer\PrettyNumber;
use Auth; use Auth;
use Cache; use Cache;
use DB; use Illuminate\Http\Request;
use Mail; use Mail;
use Purify; use Purify;
use App\Mail\PasswordChange;
use Illuminate\Http\Request;
use App\Services\AccountService;
use App\Services\PronounService;
trait HomeSettings trait HomeSettings
{ {
@ -44,7 +40,7 @@ trait HomeSettings
'bio' => 'nullable|string|max:'.config('pixelfed.max_bio_length'), 'bio' => 'nullable|string|max:'.config('pixelfed.max_bio_length'),
'website' => 'nullable|url', 'website' => 'nullable|url',
'language' => 'nullable|string|min:2|max:5', 'language' => 'nullable|string|min:2|max:5',
'pronouns' => 'nullable|array|max:4' 'pronouns' => 'nullable|array|max:4',
]); ]);
$changes = false; $changes = false;
@ -57,14 +53,14 @@ trait HomeSettings
$pronouns = $request->input('pronouns'); $pronouns = $request->input('pronouns');
$existingPronouns = PronounService::get($profile->id); $existingPronouns = PronounService::get($profile->id);
$layout = $request->input('profile_layout'); $layout = $request->input('profile_layout');
if($layout) { if ($layout) {
$layout = !in_array($layout, ['metro', 'moment']) ? 'metro' : $layout; $layout = ! in_array($layout, ['metro', 'moment']) ? 'metro' : $layout;
} }
$enforceEmailVerification = config_cache('pixelfed.enforce_email_verification'); $enforceEmailVerification = config_cache('pixelfed.enforce_email_verification');
// Only allow email to be updated if not yet verified // Only allow email to be updated if not yet verified
if (!$enforceEmailVerification || !$changes && $user->email_verified_at) { if (! $enforceEmailVerification || ! $changes && $user->email_verified_at) {
if ($profile->name != $name) { if ($profile->name != $name) {
$changes = true; $changes = true;
$user->name = $name; $user->name = $name;
@ -81,7 +77,7 @@ trait HomeSettings
$profile->bio = Autolink::create()->autolink($bio); $profile->bio = Autolink::create()->autolink($bio);
} }
if($user->language != $language && if ($user->language != $language &&
in_array($language, \App\Util\Localization\Localization::languages()) in_array($language, \App\Util\Localization\Localization::languages())
) { ) {
$changes = true; $changes = true;
@ -89,8 +85,8 @@ trait HomeSettings
session()->put('locale', $language); session()->put('locale', $language);
} }
if($existingPronouns != $pronouns) { if ($existingPronouns != $pronouns) {
if($pronouns && in_array('Select Pronoun(s)', $pronouns)) { if ($pronouns && in_array('Select Pronoun(s)', $pronouns)) {
PronounService::clear($profile->id); PronounService::clear($profile->id);
} else { } else {
PronounService::put($profile->id, $pronouns); PronounService::put($profile->id, $pronouns);
@ -102,7 +98,9 @@ trait HomeSettings
$user->save(); $user->save();
$profile->save(); $profile->save();
Cache::forget('user:account:id:'.$user->id); Cache::forget('user:account:id:'.$user->id);
AccountService::forgetAccountSettings($profile->id);
AccountService::del($profile->id); AccountService::del($profile->id);
return redirect('/settings/home')->with('status', 'Profile successfully updated!'); return redirect('/settings/home')->with('status', 'Profile successfully updated!');
} }
@ -144,6 +142,7 @@ trait HomeSettings
$log->save(); $log->save();
Mail::to($request->user())->send(new PasswordChange($user)); Mail::to($request->user())->send(new PasswordChange($user));
return redirect('/settings/home')->with('status', 'Password successfully updated!'); return redirect('/settings/home')->with('status', 'Password successfully updated!');
} else { } else {
return redirect()->back()->with('error', 'There was an error with your request! Please try again.'); return redirect()->back()->with('error', 'There was an error with your request! Please try again.');

View file

@ -2,30 +2,31 @@
namespace App\Http\Controllers\Settings; namespace App\Http\Controllers\Settings;
use App\AccountLog;
use App\EmailVerification;
use App\Instance;
use App\Follower; use App\Follower;
use App\Media;
use App\Profile; use App\Profile;
use App\User; use App\Services\AccountService;
use App\Services\RelationshipService;
use App\UserFilter; use App\UserFilter;
use App\Util\Lexer\PrettyNumber; use Auth;
use App\Util\ActivityPub\Helpers; use Cache;
use Auth, Cache, DB; use DB;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use App\Models\UserDomainBlock;
trait PrivacySettings trait PrivacySettings
{ {
public function privacy() public function privacy()
{ {
$user = Auth::user(); $user = Auth::user();
$settings = $user->settings; $settings = $user->settings;
$profile = $user->profile; $profile = $user->profile;
$is_private = $profile->is_private; $is_private = $profile->is_private;
$cachedSettings = AccountService::getAccountSettings($profile->id);
$settings['is_private'] = (bool) $is_private; $settings['is_private'] = (bool) $is_private;
if ($cachedSettings && isset($cachedSettings['disable_embeds'])) {
$settings['disable_embeds'] = (bool) $cachedSettings['disable_embeds'];
} else {
$settings['disable_embeds'] = false;
}
return view('settings.privacy', compact('settings', 'profile')); return view('settings.privacy', compact('settings', 'profile'));
} }
@ -34,6 +35,7 @@ trait PrivacySettings
{ {
$settings = $request->user()->settings; $settings = $request->user()->settings;
$profile = $request->user()->profile; $profile = $request->user()->profile;
$other = $settings->other;
$fields = [ $fields = [
'is_private', 'is_private',
'crawlable', 'crawlable',
@ -48,6 +50,16 @@ trait PrivacySettings
$profile->is_suggestable = $request->input('is_suggestable') == 'on'; $profile->is_suggestable = $request->input('is_suggestable') == 'on';
$profile->save(); $profile->save();
if ($request->has('disable_embeds')) {
$other['disable_embeds'] = true;
$settings->other = $other;
$settings->save();
} else {
$other['disable_embeds'] = false;
$settings->other = $other;
$settings->save();
}
foreach ($fields as $field) { foreach ($fields as $field) {
$form = $request->input($field); $form = $request->input($field);
if ($field == 'is_private') { if ($field == 'is_private') {
@ -85,18 +97,20 @@ trait PrivacySettings
$settings->save(); $settings->save();
} }
$pid = $profile->id; $pid = $profile->id;
Cache::forget('profile:settings:' . $pid); Cache::forget('profile:settings:'.$pid);
Cache::forget('user:account:id:' . $profile->user_id); Cache::forget('user:account:id:'.$profile->user_id);
Cache::forget('profile:follower_count:' . $pid); Cache::forget('profile:follower_count:'.$pid);
Cache::forget('profile:following_count:' . $pid); Cache::forget('profile:following_count:'.$pid);
Cache::forget('profile:atom:enabled:' . $pid); Cache::forget('profile:atom:enabled:'.$pid);
Cache::forget('profile:embed:' . $pid); Cache::forget('profile:embed:'.$pid);
Cache::forget('pf:acct:settings:hidden-followers:' . $pid); Cache::forget('pf:acct:settings:hidden-followers:'.$pid);
Cache::forget('pf:acct:settings:hidden-following:' . $pid); Cache::forget('pf:acct:settings:hidden-following:'.$pid);
Cache::forget('pf:acct-trans:hideFollowing:' . $pid); Cache::forget('pf:acct-trans:hideFollowing:'.$pid);
Cache::forget('pf:acct-trans:hideFollowers:' . $pid); Cache::forget('pf:acct-trans:hideFollowers:'.$pid);
Cache::forget('pfc:cached-user:wt:' . strtolower($profile->username)); Cache::forget('pfc:cached-user:wt:'.strtolower($profile->username));
Cache::forget('pfc:cached-user:wot:' . strtolower($profile->username)); Cache::forget('pfc:cached-user:wot:'.strtolower($profile->username));
AccountService::forgetAccountSettings($profile->id);
return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!'); return redirect(route('settings.privacy'))->with('status', 'Settings successfully updated!');
} }
@ -105,13 +119,14 @@ trait PrivacySettings
$pid = Auth::user()->profile->id; $pid = Auth::user()->profile->id;
$ids = (new UserFilter())->mutedUserIds($pid); $ids = (new UserFilter())->mutedUserIds($pid);
$users = Profile::whereIn('id', $ids)->simplePaginate(15); $users = Profile::whereIn('id', $ids)->simplePaginate(15);
return view('settings.privacy.muted', compact('users')); return view('settings.privacy.muted', compact('users'));
} }
public function mutedUsersUpdate(Request $request) public function mutedUsersUpdate(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'profile_id' => 'required|integer|min:1' 'profile_id' => 'required|integer|min:1',
]); ]);
$fid = $request->input('profile_id'); $fid = $request->input('profile_id');
$pid = Auth::user()->profile->id; $pid = Auth::user()->profile->id;
@ -123,6 +138,8 @@ trait PrivacySettings
->firstOrFail(); ->firstOrFail();
$filter->delete(); $filter->delete();
}); });
RelationshipService::refresh($pid, $fid);
return redirect()->back(); return redirect()->back();
} }
@ -131,14 +148,14 @@ trait PrivacySettings
$pid = Auth::user()->profile->id; $pid = Auth::user()->profile->id;
$ids = (new UserFilter())->blockedUserIds($pid); $ids = (new UserFilter())->blockedUserIds($pid);
$users = Profile::whereIn('id', $ids)->simplePaginate(15); $users = Profile::whereIn('id', $ids)->simplePaginate(15);
return view('settings.privacy.blocked', compact('users')); return view('settings.privacy.blocked', compact('users'));
} }
public function blockedUsersUpdate(Request $request) public function blockedUsersUpdate(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'profile_id' => 'required|integer|min:1' 'profile_id' => 'required|integer|min:1',
]); ]);
$fid = $request->input('profile_id'); $fid = $request->input('profile_id');
$pid = Auth::user()->profile->id; $pid = Auth::user()->profile->id;
@ -150,6 +167,8 @@ trait PrivacySettings
->firstOrFail(); ->firstOrFail();
$filter->delete(); $filter->delete();
}); });
RelationshipService::refresh($pid, $fid);
return redirect()->back(); return redirect()->back();
} }
@ -194,7 +213,7 @@ trait PrivacySettings
$profile = Auth::user()->profile; $profile = Auth::user()->profile;
$settings = Auth::user()->settings; $settings = Auth::user()->settings;
if($mode !== 'keep-all') { if ($mode !== 'keep-all') {
switch ($mode) { switch ($mode) {
case 'mutual-only': case 'mutual-only':
$following = $profile->following()->pluck('profiles.id'); $following = $profile->following()->pluck('profiles.id');
@ -211,7 +230,7 @@ trait PrivacySettings
break; break;
default: default:
# code... // code...
break; break;
} }
} }
@ -221,6 +240,7 @@ trait PrivacySettings
$settings->save(); $settings->save();
$profile->save(); $profile->save();
Cache::forget('profiles:private'); Cache::forget('profiles:private');
return [200]; return [200];
} }
} }

View file

@ -2,34 +2,30 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use App\AccountLog; use App\Http\Controllers\Settings\ExportSettings;
use App\Following; use App\Http\Controllers\Settings\HomeSettings;
use App\ProfileSponsor; use App\Http\Controllers\Settings\LabsSettings;
use App\Report; use App\Http\Controllers\Settings\PrivacySettings;
use App\UserFilter; use App\Http\Controllers\Settings\RelationshipSettings;
use App\UserSetting; use App\Http\Controllers\Settings\SecuritySettings;
use Auth, Cookie, DB, Cache, Purify;
use Illuminate\Support\Facades\Redis;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Http\Controllers\Settings\{
ExportSettings,
LabsSettings,
HomeSettings,
PrivacySettings,
RelationshipSettings,
SecuritySettings
};
use App\Jobs\DeletePipeline\DeleteAccountPipeline; use App\Jobs\DeletePipeline\DeleteAccountPipeline;
use App\Jobs\MediaPipeline\MediaSyncLicensePipeline; use App\Jobs\MediaPipeline\MediaSyncLicensePipeline;
use App\ProfileSponsor;
use App\Services\AccountService; use App\Services\AccountService;
use App\UserSetting;
use Auth;
use Cache;
use Carbon\Carbon;
use Cookie;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Str;
class SettingsController extends Controller class SettingsController extends Controller
{ {
use ExportSettings, use ExportSettings,
LabsSettings,
HomeSettings, HomeSettings,
LabsSettings,
PrivacySettings, PrivacySettings,
RelationshipSettings, RelationshipSettings,
SecuritySettings; SecuritySettings;
@ -48,7 +44,8 @@ class SettingsController extends Controller
public function accessibilityStore(Request $request) public function accessibilityStore(Request $request)
{ {
$settings = Auth::user()->settings; $user = $request->user();
$settings = $user->settings;
$fields = [ $fields = [
'compose_media_descriptions', 'compose_media_descriptions',
'reduce_motion', 'reduce_motion',
@ -65,6 +62,7 @@ class SettingsController extends Controller
} }
$settings->save(); $settings->save();
} }
AccountService::forgetAccountSettings($user->profile_id);
return redirect(route('settings.accessibility'))->with('status', 'Settings successfully updated!'); return redirect(route('settings.accessibility'))->with('status', 'Settings successfully updated!');
} }
@ -97,7 +95,7 @@ class SettingsController extends Controller
public function removeAccountTemporary(Request $request) public function removeAccountTemporary(Request $request)
{ {
$user = Auth::user(); $user = Auth::user();
abort_if(!config('pixelfed.account_deletion'), 403); abort_if(! config('pixelfed.account_deletion'), 403);
abort_if($user->is_admin, 403); abort_if($user->is_admin, 403);
return view('settings.remove.temporary'); return view('settings.remove.temporary');
@ -106,7 +104,7 @@ class SettingsController extends Controller
public function removeAccountTemporarySubmit(Request $request) public function removeAccountTemporarySubmit(Request $request)
{ {
$user = Auth::user(); $user = Auth::user();
abort_if(!config('pixelfed.account_deletion'), 403); abort_if(! config('pixelfed.account_deletion'), 403);
abort_if($user->is_admin, 403); abort_if($user->is_admin, 403);
$profile = $user->profile; $profile = $user->profile;
$user->status = 'disabled'; $user->status = 'disabled';
@ -115,6 +113,7 @@ class SettingsController extends Controller
$profile->save(); $profile->save();
Auth::logout(); Auth::logout();
Cache::forget('profiles:private'); Cache::forget('profiles:private');
return redirect('/'); return redirect('/');
} }
@ -122,16 +121,17 @@ class SettingsController extends Controller
{ {
$user = Auth::user(); $user = Auth::user();
abort_if($user->is_admin, 403); abort_if($user->is_admin, 403);
return view('settings.remove.permanent'); return view('settings.remove.permanent');
} }
public function removeAccountPermanentSubmit(Request $request) public function removeAccountPermanentSubmit(Request $request)
{ {
if(config('pixelfed.account_deletion') == false) { if (config('pixelfed.account_deletion') == false) {
abort(404); abort(404);
} }
$user = Auth::user(); $user = Auth::user();
abort_if(!config('pixelfed.account_deletion'), 403); abort_if(! config('pixelfed.account_deletion'), 403);
abort_if($user->is_admin, 403); abort_if($user->is_admin, 403);
$profile = $user->profile; $profile = $user->profile;
$ts = Carbon::now()->addMonth(); $ts = Carbon::now()->addMonth();
@ -147,24 +147,26 @@ class SettingsController extends Controller
AccountService::del($profile->id); AccountService::del($profile->id);
Auth::logout(); Auth::logout();
DeleteAccountPipeline::dispatch($user)->onQueue('low'); DeleteAccountPipeline::dispatch($user)->onQueue('low');
return redirect('/'); return redirect('/');
} }
public function requestFullExport(Request $request) public function requestFullExport(Request $request)
{ {
$user = Auth::user(); $user = Auth::user();
return view('settings.export.show'); return view('settings.export.show');
} }
public function metroDarkMode(Request $request) public function metroDarkMode(Request $request)
{ {
$this->validate($request, [ $this->validate($request, [
'mode' => 'required|string|in:light,dark' 'mode' => 'required|string|in:light,dark',
]); ]);
$mode = $request->input('mode'); $mode = $request->input('mode');
if($mode == 'dark') { if ($mode == 'dark') {
$cookie = Cookie::make('dark-mode', 'true', 43800); $cookie = Cookie::make('dark-mode', 'true', 43800);
} else { } else {
$cookie = Cookie::forget('dark-mode'); $cookie = Cookie::forget('dark-mode');
@ -178,10 +180,11 @@ class SettingsController extends Controller
$default = [ $default = [
'patreon' => null, 'patreon' => null,
'liberapay' => null, 'liberapay' => null,
'opencollective' => null 'opencollective' => null,
]; ];
$sponsors = ProfileSponsor::whereProfileId(Auth::user()->profile->id)->first(); $sponsors = ProfileSponsor::whereProfileId(Auth::user()->profile->id)->first();
$sponsors = $sponsors ? json_decode($sponsors->sponsors, true) : $default; $sponsors = $sponsors ? json_decode($sponsors->sponsors, true) : $default;
return view('settings.sponsor', compact('sponsors')); return view('settings.sponsor', compact('sponsors'));
} }
@ -190,7 +193,7 @@ class SettingsController extends Controller
$this->validate($request, [ $this->validate($request, [
'patreon' => 'nullable|string', 'patreon' => 'nullable|string',
'liberapay' => 'nullable|string', 'liberapay' => 'nullable|string',
'opencollective' => 'nullable|string' 'opencollective' => 'nullable|string',
]); ]);
$patreon = Str::startsWith($request->input('patreon'), 'https://') ? $patreon = Str::startsWith($request->input('patreon'), 'https://') ?
@ -209,22 +212,23 @@ class SettingsController extends Controller
$liberapay = Str::startsWith($liberapay, 'liberapay.com/') ? e($liberapay) : null; $liberapay = Str::startsWith($liberapay, 'liberapay.com/') ? e($liberapay) : null;
$opencollective = Str::startsWith($opencollective, 'opencollective.com/') ? e($opencollective) : null; $opencollective = Str::startsWith($opencollective, 'opencollective.com/') ? e($opencollective) : null;
if(empty($patreon) && empty($liberapay) && empty($opencollective)) { if (empty($patreon) && empty($liberapay) && empty($opencollective)) {
return redirect(route('settings'))->with('error', 'An error occured. Please try again later.'); return redirect(route('settings'))->with('error', 'An error occured. Please try again later.');
} }
$res = [ $res = [
'patreon' => $patreon, 'patreon' => $patreon,
'liberapay' => $liberapay, 'liberapay' => $liberapay,
'opencollective' => $opencollective 'opencollective' => $opencollective,
]; ];
$sponsors = ProfileSponsor::firstOrCreate([ $sponsors = ProfileSponsor::firstOrCreate([
'profile_id' => Auth::user()->profile_id ?? Auth::user()->profile->id 'profile_id' => Auth::user()->profile_id ?? Auth::user()->profile->id,
]); ]);
$sponsors->sponsors = json_encode($res); $sponsors->sponsors = json_encode($res);
$sponsors->save(); $sponsors->save();
$sponsors = $res; $sponsors = $res;
return redirect(route('settings'))->with('status', 'Sponsor settings successfully updated!'); return redirect(route('settings'))->with('status', 'Sponsor settings successfully updated!');
} }
@ -235,20 +239,21 @@ class SettingsController extends Controller
$top = Redis::zscore('pf:tl:top', $pid) != false; $top = Redis::zscore('pf:tl:top', $pid) != false;
$replies = Redis::zscore('pf:tl:replies', $pid) != false; $replies = Redis::zscore('pf:tl:replies', $pid) != false;
$userSettings = UserSetting::firstOrCreate([ $userSettings = UserSetting::firstOrCreate([
'user_id' => $uid 'user_id' => $uid,
]); ]);
if(!$userSettings || !$userSettings->other) { if (! $userSettings || ! $userSettings->other) {
$userSettings = [ $userSettings = [
'enable_reblogs' => false, 'enable_reblogs' => false,
'photo_reblogs_only' => false 'photo_reblogs_only' => false,
]; ];
} else { } else {
$userSettings = array_merge([ $userSettings = array_merge([
'enable_reblogs' => false, 'enable_reblogs' => false,
'photo_reblogs_only' => false 'photo_reblogs_only' => false,
], ],
$userSettings->other); $userSettings->other);
} }
return view('settings.timeline', compact('top', 'replies', 'userSettings')); return view('settings.timeline', compact('top', 'replies', 'userSettings'));
} }
@ -258,14 +263,14 @@ class SettingsController extends Controller
$uid = $request->user()->id; $uid = $request->user()->id;
$this->validate($request, [ $this->validate($request, [
'enable_reblogs' => 'sometimes', 'enable_reblogs' => 'sometimes',
'photo_reblogs_only' => 'sometimes' 'photo_reblogs_only' => 'sometimes',
]); ]);
Redis::zrem('pf:tl:top', $pid); Redis::zrem('pf:tl:top', $pid);
Redis::zrem('pf:tl:replies', $pid); Redis::zrem('pf:tl:replies', $pid);
$userSettings = UserSetting::firstOrCreate([ $userSettings = UserSetting::firstOrCreate([
'user_id' => $uid 'user_id' => $uid,
]); ]);
if($userSettings->other) { if ($userSettings->other) {
$other = $userSettings->other; $other = $userSettings->other;
$other['enable_reblogs'] = $request->has('enable_reblogs'); $other['enable_reblogs'] = $request->has('enable_reblogs');
$other['photo_reblogs_only'] = $request->has('photo_reblogs_only'); $other['photo_reblogs_only'] = $request->has('photo_reblogs_only');
@ -275,6 +280,7 @@ class SettingsController extends Controller
} }
$userSettings->other = $other; $userSettings->other = $other;
$userSettings->save(); $userSettings->save();
return redirect(route('settings'))->with('status', 'Timeline settings successfully updated!'); return redirect(route('settings'))->with('status', 'Timeline settings successfully updated!');
} }
@ -285,8 +291,9 @@ class SettingsController extends Controller
is_string($setting->compose_settings) ? json_decode($setting->compose_settings, true) : $setting->compose_settings is_string($setting->compose_settings) ? json_decode($setting->compose_settings, true) : $setting->compose_settings
) : [ ) : [
'default_license' => null, 'default_license' => null,
'media_descriptions' => false 'media_descriptions' => false,
]; ];
return view('settings.media', compact('compose')); return view('settings.media', compact('compose'));
} }
@ -295,7 +302,7 @@ class SettingsController extends Controller
$this->validate($request, [ $this->validate($request, [
'default' => 'required|int|min:1|max:16', 'default' => 'required|int|min:1|max:16',
'sync' => 'nullable', 'sync' => 'nullable',
'media_descriptions' => 'nullable' 'media_descriptions' => 'nullable',
]); ]);
$license = $request->input('default'); $license = $request->input('default');
@ -307,40 +314,40 @@ class SettingsController extends Controller
$compose = is_string($setting->compose_settings) ? json_decode($setting->compose_settings, true) : $setting->compose_settings; $compose = is_string($setting->compose_settings) ? json_decode($setting->compose_settings, true) : $setting->compose_settings;
$changed = false; $changed = false;
if($sync) { if ($sync) {
$key = 'pf:settings:mls_recently:'.$uid; $key = 'pf:settings:mls_recently:'.$uid;
if(Cache::get($key) == 2) { if (Cache::get($key) == 2) {
$msg = 'You can only sync licenses twice per 24 hours. Try again later.'; $msg = 'You can only sync licenses twice per 24 hours. Try again later.';
return redirect(route('settings')) return redirect(route('settings'))
->with('error', $msg); ->with('error', $msg);
} }
} }
if(!isset($compose['default_license']) || $compose['default_license'] !== $license) { if (! isset($compose['default_license']) || $compose['default_license'] !== $license) {
$compose['default_license'] = (int) $license; $compose['default_license'] = (int) $license;
$changed = true; $changed = true;
} }
if(!isset($compose['media_descriptions']) || $compose['media_descriptions'] !== $media_descriptions) { if (! isset($compose['media_descriptions']) || $compose['media_descriptions'] !== $media_descriptions) {
$compose['media_descriptions'] = $media_descriptions; $compose['media_descriptions'] = $media_descriptions;
$changed = true; $changed = true;
} }
if($changed) { if ($changed) {
$setting->compose_settings = $compose; $setting->compose_settings = $compose;
$setting->save(); $setting->save();
Cache::forget('profile:compose:settings:' . $request->user()->id); Cache::forget('profile:compose:settings:'.$request->user()->id);
} }
if($sync) { if ($sync) {
$val = Cache::has($key) ? 2 : 1; $val = Cache::has($key) ? 2 : 1;
Cache::put($key, $val, 86400); Cache::put($key, $val, 86400);
MediaSyncLicensePipeline::dispatch($uid, $license); MediaSyncLicensePipeline::dispatch($uid, $license);
return redirect(route('settings'))->with('status', 'Media licenses successfully synced! It may take a few minutes to take effect for every post.'); return redirect(route('settings'))->with('status', 'Media licenses successfully synced! It may take a few minutes to take effect for every post.');
} }
return redirect(route('settings'))->with('status', 'Media settings successfully updated!'); return redirect(route('settings'))->with('status', 'Media settings successfully updated!');
} }
} }

View file

@ -5,7 +5,7 @@ namespace App\Http\Controllers;
use App\Page; use App\Page;
use App\Profile; use App\Profile;
use App\Services\FollowerService; use App\Services\FollowerService;
use App\Status; use App\Services\StatusService;
use App\User; use App\User;
use App\Util\ActivityPub\Helpers; use App\Util\ActivityPub\Helpers;
use App\Util\Localization\Localization; use App\Util\Localization\Localization;
@ -60,7 +60,7 @@ class SiteController extends Controller
{ {
return Cache::remember('site.about_v2', now()->addMinutes(15), function () { return Cache::remember('site.about_v2', now()->addMinutes(15), function () {
$user_count = number_format(User::count()); $user_count = number_format(User::count());
$post_count = number_format(Status::count()); $post_count = number_format(StatusService::totalLocalStatuses());
$rules = config_cache('app.rules') ? json_decode(config_cache('app.rules'), true) : null; $rules = config_cache('app.rules') ? json_decode(config_cache('app.rules'), true) : null;
return view('site.about', compact('rules', 'user_count', 'post_count'))->render(); return view('site.about', compact('rules', 'user_count', 'post_count'))->render();

View file

@ -8,6 +8,7 @@ use App\Jobs\SharePipeline\UndoSharePipeline;
use App\Jobs\StatusPipeline\RemoteStatusDelete; use App\Jobs\StatusPipeline\RemoteStatusDelete;
use App\Jobs\StatusPipeline\StatusDelete; use App\Jobs\StatusPipeline\StatusDelete;
use App\Profile; use App\Profile;
use App\Services\AccountService;
use App\Services\HashidService; use App\Services\HashidService;
use App\Services\ReblogService; use App\Services\ReblogService;
use App\Services\StatusService; use App\Services\StatusService;
@ -34,8 +35,21 @@ class StatusController extends Controller
} }
} }
$user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail(); $status = StatusService::get($id, false);
abort_if(
! $status ||
! isset($status['account'], $status['account']['username']) ||
$status['account']['username'] != $username ||
isset($status['reblog']), 404);
abort_if(! in_array($status['visibility'], ['public', 'unlisted']) && ! $request->user(), 403, 'Invalid permission');
if ($request->wantsJson() && (bool) config_cache('federation.activitypub.enabled')) {
return $this->showActivityPub($request, $status);
}
$user = Profile::whereNull('domain')->whereUsername($username)->firstOrFail();
if ($user->status != null) { if ($user->status != null) {
return ProfileController::accountCheck($user); return ProfileController::accountCheck($user);
} }
@ -70,18 +84,6 @@ class StatusController extends Controller
} }
} }
if ($request->user() && $request->user()->profile_id != $status->profile_id) {
StatusView::firstOrCreate([
'status_id' => $status->id,
'status_profile_id' => $status->profile_id,
'profile_id' => $request->user()->profile_id,
]);
}
if ($request->wantsJson() && config_cache('federation.activitypub.enabled')) {
return $this->showActivityPub($request, $status);
}
$template = $status->in_reply_to_id ? 'status.reply' : 'status.show'; $template = $status->in_reply_to_id ? 'status.reply' : 'status.show';
return view($template, compact('user', 'status')); return view($template, compact('user', 'status'));
@ -113,19 +115,41 @@ class StatusController extends Controller
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
$profile = Profile::whereNull(['domain', 'status']) $status = StatusService::get($id);
->whereIsPrivate(false)
->whereUsername($username)
->first();
if (! $profile) { if (
! $status ||
! isset($status['account'], $status['account']['id'], $status['local']) ||
! $status['local'] ||
strtolower($status['account']['username']) !== strtolower($username)
) {
$content = view('status.embed-removed');
return response($content, 404)->header('X-Frame-Options', 'ALLOWALL');
}
$profile = AccountService::get($status['account']['id'], true);
if (! $profile || $profile['locked'] || ! $profile['local']) {
$content = view('status.embed-removed'); $content = view('status.embed-removed');
return response($content)->header('X-Frame-Options', 'ALLOWALL'); return response($content)->header('X-Frame-Options', 'ALLOWALL');
} }
$aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile->id, 86400, function () use ($profile) { $embedCheck = AccountService::canEmbed($profile['id']);
$exists = AccountInterstitial::whereUserId($profile->user_id)->where('is_spam', 1)->count();
if (! $embedCheck) {
$content = view('status.embed-removed');
return response($content)->header('X-Frame-Options', 'ALLOWALL');
}
$aiCheck = Cache::remember('profile:ai-check:spam-login:'.$profile['id'], 3600, function () use ($profile) {
$user = Profile::find($profile['id']);
if (! $user) {
return true;
}
$exists = AccountInterstitial::whereUserId($user->user_id)->where('is_spam', 1)->count();
if ($exists) { if ($exists) {
return true; return true;
} }
@ -138,17 +162,22 @@ class StatusController extends Controller
return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']); return response($res)->withHeaders(['X-Frame-Options' => 'ALLOWALL']);
} }
$status = Status::whereProfileId($profile->id)
->whereNull('uri') $status = StatusService::get($id);
->whereScope('public')
->whereIsNsfw(false) if (
->whereIn('type', ['photo', 'video', 'photo:album']) ! $status ||
->find($id); ! isset($status['account'], $status['account']['id']) ||
if (! $status) { intval($status['account']['id']) !== intval($profile['id']) ||
$status['sensitive'] ||
$status['visibility'] !== 'public' ||
! in_array($status['pf_type'], ['photo', 'photo:album'])
) {
$content = view('status.embed-removed'); $content = view('status.embed-removed');
return response($content)->header('X-Frame-Options', 'ALLOWALL'); return response($content)->header('X-Frame-Options', 'ALLOWALL');
} }
$showLikes = $request->filled('likes') && $request->likes == true; $showLikes = $request->filled('likes') && $request->likes == true;
$showCaption = $request->filled('caption') && $request->caption !== false; $showCaption = $request->filled('caption') && $request->caption !== false;
$layout = $request->filled('layout') && $request->layout == 'compact' ? 'compact' : 'full'; $layout = $request->filled('layout') && $request->layout == 'compact' ? 'compact' : 'full';
@ -319,12 +348,17 @@ class StatusController extends Controller
public function showActivityPub(Request $request, $status) public function showActivityPub(Request $request, $status)
{ {
$key = 'pf:status:ap:v1:sid:'.$status['id'];
return Cache::remember($key, 3600, function () use ($status) {
$status = Status::findOrFail($status['id']);
$object = $status->type == 'poll' ? new Question() : new Note(); $object = $status->type == 'poll' ? new Question() : new Note();
$fractal = new Fractal\Manager(); $fractal = new Fractal\Manager();
$resource = new Fractal\Resource\Item($status, $object); $resource = new Fractal\Resource\Item($status, $object);
$res = $fractal->createData($resource)->toArray(); $res = $fractal->createData($resource)->toArray();
return response()->json($res['data'], 200, ['Content-Type' => 'application/activity+json'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); return response()->json($res['data'], 200, ['Content-Type' => 'application/activity+json'], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
});
} }
public function edit(Request $request, $username, $id) public function edit(Request $request, $username, $id)

View file

@ -2,54 +2,56 @@
namespace App\Http\Controllers\Stories; namespace App\Http\Controllers\Stories;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use App\Models\Conversation;
use App\DirectMessage; use App\DirectMessage;
use App\Notification; use App\Http\Controllers\Controller;
use App\Story; use App\Http\Resources\StoryView as StoryViewResource;
use App\Status;
use App\StoryView;
use App\Jobs\StoryPipeline\StoryDelete; use App\Jobs\StoryPipeline\StoryDelete;
use App\Jobs\StoryPipeline\StoryFanout; use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryReplyDeliver; use App\Jobs\StoryPipeline\StoryReplyDeliver;
use App\Jobs\StoryPipeline\StoryViewDeliver; use App\Jobs\StoryPipeline\StoryViewDeliver;
use App\Models\Conversation;
use App\Notification;
use App\Services\AccountService; use App\Services\AccountService;
use App\Services\MediaPathService; use App\Services\MediaPathService;
use App\Services\StoryService; use App\Services\StoryService;
use App\Http\Resources\StoryView as StoryViewResource; use App\Status;
use App\Story;
use App\StoryView;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class StoryApiV1Controller extends Controller class StoryApiV1Controller extends Controller
{ {
const RECENT_KEY = 'pf:stories:recent-by-id:'; const RECENT_KEY = 'pf:stories:recent-by-id:';
const RECENT_TTL = 300; const RECENT_TTL = 300;
public function carousel(Request $request) public function carousel(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
->where('stories.active', true) ->where('stories.active', true)
->map(function($s) { ->map(function ($s) {
$r = new \StdClass; $r = new \StdClass;
$r->id = $s->id; $r->id = $s->id;
$r->profile_id = $s->profile_id; $r->profile_id = $s->profile_id;
$r->type = $s->type; $r->type = $s->type;
$r->path = $s->path; $r->path = $s->path;
return $r; return $r;
}) })
->unique('profile_id'); ->unique('profile_id');
}); });
} else { } else {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
@ -59,9 +61,9 @@ class StoryApiV1Controller extends Controller
}); });
} }
$nodes = $s->map(function($s) use($pid) { $nodes = $s->map(function ($s) use ($pid) {
$profile = AccountService::get($s->profile_id, true); $profile = AccountService::get($s->profile_id, true);
if(!$profile || !isset($profile['id'])) { if (! $profile || ! isset($profile['id'])) {
return false; return false;
} }
@ -72,24 +74,25 @@ class StoryApiV1Controller extends Controller
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration ?? 3, 'duration' => $s->duration ?? 3,
'seen' => StoryService::hasSeen($pid, $s->id), 'seen' => StoryService::hasSeen($pid, $s->id),
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->filter() ->filter()
->groupBy('pid') ->groupBy('pid')
->map(function($item) use($pid) { ->map(function ($item) use ($pid) {
$profile = AccountService::get($item[0]['pid'], true); $profile = AccountService::get($item[0]['pid'], true);
$url = $profile['local'] ? url("/stories/{$profile['username']}") : $url = $profile['local'] ? url("/stories/{$profile['username']}") :
url("/i/rs/{$profile['id']}"); url("/i/rs/{$profile['id']}");
return [ return [
'id' => 'pfs:' . $profile['id'], 'id' => 'pfs:'.$profile['id'],
'user' => [ 'user' => [
'id' => (string) $profile['id'], 'id' => (string) $profile['id'],
'username' => $profile['username'], 'username' => $profile['username'],
'username_acct' => $profile['acct'], 'username_acct' => $profile['acct'],
'avatar' => $profile['avatar'], 'avatar' => $profile['avatar'],
'local' => $profile['local'], 'local' => $profile['local'],
'is_author' => $profile['id'] == $pid 'is_author' => $profile['id'] == $pid,
], ],
'nodes' => $item, 'nodes' => $item,
'url' => $url, 'url' => $url,
@ -104,18 +107,18 @@ class StoryApiV1Controller extends Controller
'nodes' => $nodes, 'nodes' => $nodes,
]; ];
if(Story::whereProfileId($pid)->whereActive(true)->exists()) { if (Story::whereProfileId($pid)->whereActive(true)->exists()) {
$selfStories = Story::whereProfileId($pid) $selfStories = Story::whereProfileId($pid)
->whereActive(true) ->whereActive(true)
->get() ->get()
->map(function($s) use($pid) { ->map(function ($s) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'type' => $s->type, 'type' => $s->type,
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration, 'duration' => $s->duration,
'seen' => true, 'seen' => true,
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->sortBy('id') ->sortBy('id')
@ -127,38 +130,40 @@ class StoryApiV1Controller extends Controller
'username' => $selfProfile['acct'], 'username' => $selfProfile['acct'],
'avatar' => $selfProfile['avatar'], 'avatar' => $selfProfile['avatar'],
'local' => $selfProfile['local'], 'local' => $selfProfile['local'],
'is_author' => true 'is_author' => true,
], ],
'nodes' => $selfStories, 'nodes' => $selfStories,
]; ];
} }
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function selfCarousel(Request $request) public function selfCarousel(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
if(config('database.default') == 'pgsql') { if (config('database.default') == 'pgsql') {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
->where('stories.active', true) ->where('stories.active', true)
->map(function($s) { ->map(function ($s) {
$r = new \StdClass; $r = new \StdClass;
$r->id = $s->id; $r->id = $s->id;
$r->profile_id = $s->profile_id; $r->profile_id = $s->profile_id;
$r->type = $s->type; $r->type = $s->type;
$r->path = $s->path; $r->path = $s->path;
return $r; return $r;
}) })
->unique('profile_id'); ->unique('profile_id');
}); });
} else { } else {
$s = Cache::remember(self::RECENT_KEY . $pid, self::RECENT_TTL, function() use($pid) { $s = Cache::remember(self::RECENT_KEY.$pid, self::RECENT_TTL, function () use ($pid) {
return Story::select('stories.*', 'followers.following_id') return Story::select('stories.*', 'followers.following_id')
->leftJoin('followers', 'followers.following_id', 'stories.profile_id') ->leftJoin('followers', 'followers.following_id', 'stories.profile_id')
->where('followers.profile_id', $pid) ->where('followers.profile_id', $pid)
@ -168,9 +173,9 @@ class StoryApiV1Controller extends Controller
}); });
} }
$nodes = $s->map(function($s) use($pid) { $nodes = $s->map(function ($s) use ($pid) {
$profile = AccountService::get($s->profile_id, true); $profile = AccountService::get($s->profile_id, true);
if(!$profile || !isset($profile['id'])) { if (! $profile || ! isset($profile['id'])) {
return false; return false;
} }
@ -181,24 +186,25 @@ class StoryApiV1Controller extends Controller
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration ?? 3, 'duration' => $s->duration ?? 3,
'seen' => StoryService::hasSeen($pid, $s->id), 'seen' => StoryService::hasSeen($pid, $s->id),
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->filter() ->filter()
->groupBy('pid') ->groupBy('pid')
->map(function($item) use($pid) { ->map(function ($item) use ($pid) {
$profile = AccountService::get($item[0]['pid'], true); $profile = AccountService::get($item[0]['pid'], true);
$url = $profile['local'] ? url("/stories/{$profile['username']}") : $url = $profile['local'] ? url("/stories/{$profile['username']}") :
url("/i/rs/{$profile['id']}"); url("/i/rs/{$profile['id']}");
return [ return [
'id' => 'pfs:' . $profile['id'], 'id' => 'pfs:'.$profile['id'],
'user' => [ 'user' => [
'id' => (string) $profile['id'], 'id' => (string) $profile['id'],
'username' => $profile['username'], 'username' => $profile['username'],
'username_acct' => $profile['acct'], 'username_acct' => $profile['acct'],
'avatar' => $profile['avatar'], 'avatar' => $profile['avatar'],
'local' => $profile['local'], 'local' => $profile['local'],
'is_author' => $profile['id'] == $pid 'is_author' => $profile['id'] == $pid,
], ],
'nodes' => $item, 'nodes' => $item,
'url' => $url, 'url' => $url,
@ -216,7 +222,7 @@ class StoryApiV1Controller extends Controller
'username' => $selfProfile['acct'], 'username' => $selfProfile['acct'],
'avatar' => $selfProfile['avatar'], 'avatar' => $selfProfile['avatar'],
'local' => $selfProfile['local'], 'local' => $selfProfile['local'],
'is_author' => true 'is_author' => true,
], ],
'nodes' => [], 'nodes' => [],
@ -224,40 +230,41 @@ class StoryApiV1Controller extends Controller
'nodes' => $nodes, 'nodes' => $nodes,
]; ];
if(Story::whereProfileId($pid)->whereActive(true)->exists()) { if (Story::whereProfileId($pid)->whereActive(true)->exists()) {
$selfStories = Story::whereProfileId($pid) $selfStories = Story::whereProfileId($pid)
->whereActive(true) ->whereActive(true)
->get() ->get()
->map(function($s) use($pid) { ->map(function ($s) {
return [ return [
'id' => (string) $s->id, 'id' => (string) $s->id,
'type' => $s->type, 'type' => $s->type,
'src' => url(Storage::url($s->path)), 'src' => url(Storage::url($s->path)),
'duration' => $s->duration, 'duration' => $s->duration,
'seen' => true, 'seen' => true,
'created_at' => $s->created_at->format('c') 'created_at' => $s->created_at->format('c'),
]; ];
}) })
->sortBy('id') ->sortBy('id')
->values(); ->values();
$res['self']['nodes'] = $selfStories; $res['self']['nodes'] = $selfStories;
} }
return response()->json($res, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
return response()->json($res, 200, [], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} }
public function add(Request $request) public function add(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'file' => function() { 'file' => function () {
return [ return [
'required', 'required',
'mimetypes:image/jpeg,image/png,video/mp4', 'mimetypes:image/jpeg,image/png,video/mp4',
'max:' . config_cache('pixelfed.max_photo_size'), 'max:'.config_cache('pixelfed.max_photo_size'),
]; ];
}, },
'duration' => 'sometimes|integer|min:0|max:30' 'duration' => 'sometimes|integer|min:0|max:30',
]); ]);
$user = $request->user(); $user = $request->user();
@ -267,7 +274,7 @@ class StoryApiV1Controller extends Controller
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
->count(); ->count();
if($count >= Story::MAX_PER_DAY) { if ($count >= Story::MAX_PER_DAY) {
abort(418, 'You have reached your limit for new Stories today.'); abort(418, 'You have reached your limit for new Stories today.');
} }
@ -277,7 +284,7 @@ class StoryApiV1Controller extends Controller
$story = new Story(); $story = new Story();
$story->duration = $request->input('duration', 3); $story->duration = $request->input('duration', 3);
$story->profile_id = $user->profile_id; $story->profile_id = $user->profile_id;
$story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' :'photo'; $story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' : 'photo';
$story->mime = $photo->getMimeType(); $story->mime = $photo->getMimeType();
$story->path = $path; $story->path = $path;
$story->local = true; $story->local = true;
@ -292,8 +299,8 @@ class StoryApiV1Controller extends Controller
'code' => 200, 'code' => 200,
'msg' => 'Successfully added', 'msg' => 'Successfully added',
'media_id' => (string) $story->id, 'media_id' => (string) $story->id,
'media_url' => url(Storage::url($url)) . '?v=' . time(), 'media_url' => url(Storage::url($url)).'?v='.time(),
'media_type' => $story->type 'media_type' => $story->type,
]; ];
return $res; return $res;
@ -301,13 +308,13 @@ class StoryApiV1Controller extends Controller
public function publish(Request $request) public function publish(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'media_id' => 'required', 'media_id' => 'required',
'duration' => 'required|integer|min:0|max:30', 'duration' => 'required|integer|min:0|max:30',
'can_reply' => 'required|boolean', 'can_reply' => 'required|boolean',
'can_react' => 'required|boolean' 'can_react' => 'required|boolean',
]); ]);
$id = $request->input('media_id'); $id = $request->input('media_id');
@ -333,7 +340,7 @@ class StoryApiV1Controller extends Controller
public function delete(Request $request, $id) public function delete(Request $request, $id)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$user = $request->user(); $user = $request->user();
@ -346,13 +353,13 @@ class StoryApiV1Controller extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully deleted' 'msg' => 'Successfully deleted',
]; ];
} }
public function viewed(Request $request) public function viewed(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'id' => 'required|min:1', 'id' => 'required|min:1',
@ -367,44 +374,45 @@ class StoryApiV1Controller extends Controller
$profile = $story->profile; $profile = $story->profile;
if($story->profile_id == $authed->id) { if ($story->profile_id == $authed->id) {
return []; return [];
} }
$publicOnly = (bool) $profile->followedBy($authed); $publicOnly = (bool) $profile->followedBy($authed);
abort_if(!$publicOnly, 403); abort_if(! $publicOnly, 403);
$v = StoryView::firstOrCreate([ $v = StoryView::firstOrCreate([
'story_id' => $id, 'story_id' => $id,
'profile_id' => $authed->id 'profile_id' => $authed->id,
]); ]);
if($v->wasRecentlyCreated) { if ($v->wasRecentlyCreated) {
Story::findOrFail($story->id)->increment('view_count'); Story::findOrFail($story->id)->increment('view_count');
if($story->local == false) { if ($story->local == false) {
StoryViewDeliver::dispatch($story, $authed)->onQueue('story'); StoryViewDeliver::dispatch($story, $authed)->onQueue('story');
} }
} }
Cache::forget('stories:recent:by_id:' . $authed->id); Cache::forget('stories:recent:by_id:'.$authed->id);
StoryService::addSeen($authed->id, $story->id); StoryService::addSeen($authed->id, $story->id);
return ['code' => 200]; return ['code' => 200];
} }
public function comment(Request $request) public function comment(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'caption' => 'required|string' 'caption' => 'required|string',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('caption'); $text = $request->input('caption');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!$story->can_reply, 422); abort_if(! $story->can_reply, 422);
$status = new Status; $status = new Status;
$status->type = 'story:reply'; $status->type = 'story:reply';
@ -415,7 +423,7 @@ class StoryApiV1Controller extends Controller
$status->visibility = 'direct'; $status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id; $status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([ $status->entities = json_encode([
'story_id' => $story->id 'story_id' => $story->id,
]); ]);
$status->save(); $status->save();
@ -429,24 +437,24 @@ class StoryApiV1Controller extends Controller
'story_actor_username' => $request->user()->username, 'story_actor_username' => $request->user()->username,
'story_id' => $story->id, 'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)), 'story_media_url' => url(Storage::url($story->path)),
'caption' => $text 'caption' => $text,
]); ]);
$dm->save(); $dm->save();
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $story->profile_id, 'to_id' => $story->profile_id,
'from_id' => $pid 'from_id' => $pid,
], ],
[ [
'type' => 'story:comment', 'type' => 'story:comment',
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => false 'is_hidden' => false,
] ]
); );
if($story->local) { if ($story->local) {
$n = new Notification; $n = new Notification;
$n->profile_id = $dm->to_id; $n->profile_id = $dm->to_id;
$n->actor_id = $dm->from_id; $n->actor_id = $dm->from_id;
@ -460,33 +468,35 @@ class StoryApiV1Controller extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Sent!' 'msg' => 'Sent!',
]; ];
} }
protected function storeMedia($photo, $user) protected function storeMedia($photo, $user)
{ {
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if(in_array($photo->getMimeType(), [ if (in_array($photo->getMimeType(), [
'image/jpeg', 'image/jpeg',
'image/png', 'image/png',
'video/mp4' 'video/mp4',
]) == false) { ]) == false) {
abort(400, 'Invalid media type'); abort(400, 'Invalid media type');
return; return;
} }
$storagePath = MediaPathService::story($user->profile); $storagePath = MediaPathService::story($user->profile);
$path = $photo->storePubliclyAs($storagePath, Str::random(random_int(2, 12)) . '_' . Str::random(random_int(32, 35)) . '_' . Str::random(random_int(1, 14)) . '.' . $photo->extension()); $path = $photo->storePubliclyAs($storagePath, Str::random(random_int(2, 12)).'_'.Str::random(random_int(32, 35)).'_'.Str::random(random_int(1, 14)).'.'.$photo->extension());
return $path; return $path;
} }
public function viewers(Request $request) public function viewers(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required|string|min:1|max:50' 'sid' => 'required|string|min:1|max:50',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;

View file

@ -2,59 +2,52 @@
namespace App\Http\Controllers; namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Media;
use App\Profile;
use App\Report;
use App\DirectMessage; use App\DirectMessage;
use App\Notification; use App\Jobs\StoryPipeline\StoryDelete;
use App\Status; use App\Jobs\StoryPipeline\StoryFanout;
use App\Story;
use App\StoryView;
use App\Models\Poll;
use App\Models\PollVote;
use App\Services\ProfileService;
use App\Services\StoryService;
use Cache, Storage;
use Image as Intervention;
use App\Services\FollowerService;
use App\Services\MediaPathService;
use FFMpeg;
use FFMpeg\Coordinate\Dimension;
use FFMpeg\Format\Video\X264;
use App\Jobs\StoryPipeline\StoryReactionDeliver; use App\Jobs\StoryPipeline\StoryReactionDeliver;
use App\Jobs\StoryPipeline\StoryReplyDeliver; use App\Jobs\StoryPipeline\StoryReplyDeliver;
use App\Jobs\StoryPipeline\StoryFanout;
use App\Jobs\StoryPipeline\StoryDelete;
use ImageOptimizer;
use App\Models\Conversation; use App\Models\Conversation;
use App\Models\Poll;
use App\Models\PollVote;
use App\Notification;
use App\Report;
use App\Services\FollowerService;
use App\Services\MediaPathService;
use App\Services\StoryService;
use App\Services\UserRoleService; use App\Services\UserRoleService;
use App\Status;
use App\Story;
use FFMpeg;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Image as Intervention;
use Storage;
class StoryComposeController extends Controller class StoryComposeController extends Controller
{ {
public function apiV1Add(Request $request) public function apiV1Add(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'file' => function() { 'file' => function () {
return [ return [
'required', 'required',
'mimetypes:image/jpeg,image/png,video/mp4', 'mimetypes:image/jpeg,image/png,video/mp4',
'max:' . config_cache('pixelfed.max_photo_size'), 'max:'.config_cache('pixelfed.max_photo_size'),
]; ];
}, },
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$count = Story::whereProfileId($user->profile_id) $count = Story::whereProfileId($user->profile_id)
->whereActive(true) ->whereActive(true)
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
->count(); ->count();
if($count >= Story::MAX_PER_DAY) { if ($count >= Story::MAX_PER_DAY) {
abort(418, 'You have reached your limit for new Stories today.'); abort(418, 'You have reached your limit for new Stories today.');
} }
@ -64,7 +57,7 @@ class StoryComposeController extends Controller
$story = new Story(); $story = new Story();
$story->duration = 3; $story->duration = 3;
$story->profile_id = $user->profile_id; $story->profile_id = $user->profile_id;
$story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' :'photo'; $story->type = Str::endsWith($photo->getMimeType(), 'mp4') ? 'video' : 'photo';
$story->mime = $photo->getMimeType(); $story->mime = $photo->getMimeType();
$story->path = $path; $story->path = $path;
$story->local = true; $story->local = true;
@ -79,19 +72,20 @@ class StoryComposeController extends Controller
'code' => 200, 'code' => 200,
'msg' => 'Successfully added', 'msg' => 'Successfully added',
'media_id' => (string) $story->id, 'media_id' => (string) $story->id,
'media_url' => url(Storage::url($url)) . '?v=' . time(), 'media_url' => url(Storage::url($url)).'?v='.time(),
'media_type' => $story->type 'media_type' => $story->type,
]; ];
if($story->type === 'video') { if ($story->type === 'video') {
$video = FFMpeg::open($path); $video = FFMpeg::open($path);
$duration = $video->getDurationInSeconds(); $duration = $video->getDurationInSeconds();
$res['media_duration'] = $duration; $res['media_duration'] = $duration;
if($duration > 500) { if ($duration > 500) {
Storage::delete($story->path); Storage::delete($story->path);
$story->delete(); $story->delete();
return response()->json([ return response()->json([
'message' => 'Video duration cannot exceed 60 seconds' 'message' => 'Video duration cannot exceed 60 seconds',
], 422); ], 422);
} }
} }
@ -102,37 +96,39 @@ class StoryComposeController extends Controller
protected function storePhoto($photo, $user) protected function storePhoto($photo, $user)
{ {
$mimes = explode(',', config_cache('pixelfed.media_types')); $mimes = explode(',', config_cache('pixelfed.media_types'));
if(in_array($photo->getMimeType(), [ if (in_array($photo->getMimeType(), [
'image/jpeg', 'image/jpeg',
'image/png', 'image/png',
'video/mp4' 'video/mp4',
]) == false) { ]) == false) {
abort(400, 'Invalid media type'); abort(400, 'Invalid media type');
return; return;
} }
$storagePath = MediaPathService::story($user->profile); $storagePath = MediaPathService::story($user->profile);
$path = $photo->storePubliclyAs($storagePath, Str::random(random_int(2, 12)) . '_' . Str::random(random_int(32, 35)) . '_' . Str::random(random_int(1, 14)) . '.' . $photo->extension()); $path = $photo->storePubliclyAs($storagePath, Str::random(random_int(2, 12)).'_'.Str::random(random_int(32, 35)).'_'.Str::random(random_int(1, 14)).'.'.$photo->extension());
if(in_array($photo->getMimeType(), ['image/jpeg','image/png'])) { if (in_array($photo->getMimeType(), ['image/jpeg', 'image/png'])) {
$fpath = storage_path('app/' . $path); $fpath = storage_path('app/'.$path);
$img = Intervention::make($fpath); $img = Intervention::make($fpath);
$img->orientate(); $img->orientate();
$img->save($fpath, config_cache('pixelfed.image_quality')); $img->save($fpath, config_cache('pixelfed.image_quality'));
$img->destroy(); $img->destroy();
} }
return $path; return $path;
} }
public function cropPhoto(Request $request) public function cropPhoto(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'media_id' => 'required|integer|min:1', 'media_id' => 'required|integer|min:1',
'width' => 'required', 'width' => 'required',
'height' => 'required', 'height' => 'required',
'x' => 'required', 'x' => 'required',
'y' => 'required' 'y' => 'required',
]); ]);
$user = $request->user(); $user = $request->user();
@ -144,13 +140,13 @@ class StoryComposeController extends Controller
$story = Story::whereProfileId($user->profile_id)->findOrFail($id); $story = Story::whereProfileId($user->profile_id)->findOrFail($id);
$path = storage_path('app/' . $story->path); $path = storage_path('app/'.$story->path);
if(!is_file($path)) { if (! is_file($path)) {
abort(400, 'Invalid or missing media.'); abort(400, 'Invalid or missing media.');
} }
if($story->type === 'photo') { if ($story->type === 'photo') {
$img = Intervention::make($path); $img = Intervention::make($path);
$img->crop($width, $height, $x, $y); $img->crop($width, $height, $x, $y);
$img->resize(1080, 1920, function ($constraint) { $img->resize(1080, 1920, function ($constraint) {
@ -167,18 +163,18 @@ class StoryComposeController extends Controller
public function publishStory(Request $request) public function publishStory(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'media_id' => 'required', 'media_id' => 'required',
'duration' => 'required|integer|min:3|max:120', 'duration' => 'required|integer|min:3|max:120',
'can_reply' => 'required|boolean', 'can_reply' => 'required|boolean',
'can_react' => 'required|boolean' 'can_react' => 'required|boolean',
]); ]);
$id = $request->input('media_id'); $id = $request->input('media_id');
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$story = Story::whereProfileId($user->profile_id) $story = Story::whereProfileId($user->profile_id)
->findOrFail($id); ->findOrFail($id);
@ -200,7 +196,7 @@ class StoryComposeController extends Controller
public function apiV1Delete(Request $request, $id) public function apiV1Delete(Request $request, $id)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$user = $request->user(); $user = $request->user();
@ -213,40 +209,40 @@ class StoryComposeController extends Controller
return [ return [
'code' => 200, 'code' => 200,
'msg' => 'Successfully deleted' 'msg' => 'Successfully deleted',
]; ];
} }
public function compose(Request $request) public function compose(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
return view('stories.compose'); return view('stories.compose');
} }
public function createPoll(Request $request) public function createPoll(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
abort_if(!config_cache('instance.polls.enabled'), 404); abort_if(! config_cache('instance.polls.enabled'), 404);
return $request->all(); return $request->all();
} }
public function publishStoryPoll(Request $request) public function publishStoryPoll(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'question' => 'required|string|min:6|max:140', 'question' => 'required|string|min:6|max:140',
'options' => 'required|array|min:2|max:4', 'options' => 'required|array|min:2|max:4',
'can_reply' => 'required|boolean', 'can_reply' => 'required|boolean',
'can_react' => 'required|boolean' 'can_react' => 'required|boolean',
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$count = Story::whereProfileId($pid) $count = Story::whereProfileId($pid)
@ -254,7 +250,7 @@ class StoryComposeController extends Controller
->where('expires_at', '>', now()) ->where('expires_at', '>', now())
->count(); ->count();
if($count >= Story::MAX_PER_DAY) { if ($count >= Story::MAX_PER_DAY) {
abort(418, 'You have reached your limit for new Stories today.'); abort(418, 'You have reached your limit for new Stories today.');
} }
@ -262,7 +258,7 @@ class StoryComposeController extends Controller
$story->type = 'poll'; $story->type = 'poll';
$story->story = json_encode([ $story->story = json_encode([
'question' => $request->input('question'), 'question' => $request->input('question'),
'options' => $request->input('options') 'options' => $request->input('options'),
]); ]);
$story->public = false; $story->public = false;
$story->local = true; $story->local = true;
@ -278,7 +274,7 @@ class StoryComposeController extends Controller
$poll->profile_id = $pid; $poll->profile_id = $pid;
$poll->poll_options = $request->input('options'); $poll->poll_options = $request->input('options');
$poll->expires_at = $story->expires_at; $poll->expires_at = $story->expires_at;
$poll->cached_tallies = collect($poll->poll_options)->map(function($o) { $poll->cached_tallies = collect($poll->poll_options)->map(function ($o) {
return 0; return 0;
})->toArray(); })->toArray();
$poll->save(); $poll->save();
@ -296,17 +292,17 @@ class StoryComposeController extends Controller
public function storyPollVote(Request $request) public function storyPollVote(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'ci' => 'required|integer|min:0|max:3' 'ci' => 'required|integer|min:0|max:3',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$ci = $request->input('ci'); $ci = $request->input('ci');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!FollowerService::follows($pid, $story->profile_id), 403); abort_if(! FollowerService::follows($pid, $story->profile_id), 403);
$poll = Poll::whereStoryId($story->id)->firstOrFail(); $poll = Poll::whereStoryId($story->id)->firstOrFail();
$vote = new PollVote; $vote = new PollVote;
@ -318,7 +314,7 @@ class StoryComposeController extends Controller
$vote->save(); $vote->save();
$poll->votes_count = $poll->votes_count + 1; $poll->votes_count = $poll->votes_count + 1;
$poll->cached_tallies = collect($poll->getTallies())->map(function($tally, $key) use($ci) { $poll->cached_tallies = collect($poll->getTallies())->map(function ($tally, $key) use ($ci) {
return $ci == $key ? $tally + 1 : $tally; return $ci == $key ? $tally + 1 : $tally;
})->toArray(); })->toArray();
$poll->save(); $poll->save();
@ -328,7 +324,7 @@ class StoryComposeController extends Controller
public function storeReport(Request $request) public function storeReport(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'type' => 'required|alpha_dash', 'type' => 'required|alpha_dash',
@ -336,7 +332,7 @@ class StoryComposeController extends Controller
]); ]);
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$sid = $request->input('id'); $sid = $request->input('id');
@ -353,24 +349,24 @@ class StoryComposeController extends Controller
'copyright', 'copyright',
'impersonation', 'impersonation',
'scam', 'scam',
'terrorism' 'terrorism',
]; ];
abort_if(!in_array($type, $types), 422, 'Invalid story report type'); abort_if(! in_array($type, $types), 422, 'Invalid story report type');
$story = Story::findOrFail($sid); $story = Story::findOrFail($sid);
abort_if($story->profile_id == $pid, 422, 'Cannot report your own story'); abort_if($story->profile_id == $pid, 422, 'Cannot report your own story');
abort_if(!FollowerService::follows($pid, $story->profile_id), 422, 'Cannot report a story from an account you do not follow'); abort_if(! FollowerService::follows($pid, $story->profile_id), 422, 'Cannot report a story from an account you do not follow');
if( Report::whereProfileId($pid) if (Report::whereProfileId($pid)
->whereObjectType('App\Story') ->whereObjectType('App\Story')
->whereObjectId($story->id) ->whereObjectId($story->id)
->exists() ->exists()
) { ) {
return response()->json(['error' => [ return response()->json(['error' => [
'code' => 409, 'code' => 409,
'message' => 'Cannot report the same story again' 'message' => 'Cannot report the same story again',
]], 409); ]], 409);
} }
@ -389,18 +385,18 @@ class StoryComposeController extends Controller
public function react(Request $request) public function react(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'reaction' => 'required|string' 'reaction' => 'required|string',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('reaction'); $text = $request->input('reaction');
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!$story->can_react, 422); abort_if(! $story->can_react, 422);
abort_if(StoryService::reactCounter($story->id, $pid) >= 5, 422, 'You have already reacted to this story'); abort_if(StoryService::reactCounter($story->id, $pid) >= 5, 422, 'You have already reacted to this story');
$status = new Status; $status = new Status;
@ -413,7 +409,7 @@ class StoryComposeController extends Controller
$status->in_reply_to_profile_id = $story->profile_id; $status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([ $status->entities = json_encode([
'story_id' => $story->id, 'story_id' => $story->id,
'reaction' => $text 'reaction' => $text,
]); ]);
$status->save(); $status->save();
@ -427,24 +423,24 @@ class StoryComposeController extends Controller
'story_actor_username' => $request->user()->username, 'story_actor_username' => $request->user()->username,
'story_id' => $story->id, 'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)), 'story_media_url' => url(Storage::url($story->path)),
'reaction' => $text 'reaction' => $text,
]); ]);
$dm->save(); $dm->save();
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $story->profile_id, 'to_id' => $story->profile_id,
'from_id' => $pid 'from_id' => $pid,
], ],
[ [
'type' => 'story:react', 'type' => 'story:react',
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => false 'is_hidden' => false,
] ]
); );
if($story->local) { if ($story->local) {
// generate notification // generate notification
$n = new Notification; $n = new Notification;
$n->profile_id = $dm->to_id; $n->profile_id = $dm->to_id;
@ -464,18 +460,18 @@ class StoryComposeController extends Controller
public function comment(Request $request) public function comment(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(! (bool) config_cache('instance.stories.enabled') || ! $request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required', 'sid' => 'required',
'caption' => 'required|string' 'caption' => 'required|string',
]); ]);
$pid = $request->user()->profile_id; $pid = $request->user()->profile_id;
$text = $request->input('caption'); $text = $request->input('caption');
$user = $request->user(); $user = $request->user();
abort_if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action'); abort_if($user->has_roles && ! UserRoleService::can('can-use-stories', $user->id), 403, 'Invalid permissions for this action');
$story = Story::findOrFail($request->input('sid')); $story = Story::findOrFail($request->input('sid'));
abort_if(!$story->can_reply, 422); abort_if(! $story->can_reply, 422);
$status = new Status; $status = new Status;
$status->type = 'story:reply'; $status->type = 'story:reply';
@ -486,7 +482,7 @@ class StoryComposeController extends Controller
$status->visibility = 'direct'; $status->visibility = 'direct';
$status->in_reply_to_profile_id = $story->profile_id; $status->in_reply_to_profile_id = $story->profile_id;
$status->entities = json_encode([ $status->entities = json_encode([
'story_id' => $story->id 'story_id' => $story->id,
]); ]);
$status->save(); $status->save();
@ -500,24 +496,24 @@ class StoryComposeController extends Controller
'story_actor_username' => $request->user()->username, 'story_actor_username' => $request->user()->username,
'story_id' => $story->id, 'story_id' => $story->id,
'story_media_url' => url(Storage::url($story->path)), 'story_media_url' => url(Storage::url($story->path)),
'caption' => $text 'caption' => $text,
]); ]);
$dm->save(); $dm->save();
Conversation::updateOrInsert( Conversation::updateOrInsert(
[ [
'to_id' => $story->profile_id, 'to_id' => $story->profile_id,
'from_id' => $pid 'from_id' => $pid,
], ],
[ [
'type' => 'story:comment', 'type' => 'story:comment',
'status_id' => $status->id, 'status_id' => $status->id,
'dm_id' => $dm->id, 'dm_id' => $dm->id,
'is_hidden' => false 'is_hidden' => false,
] ]
); );
if($story->local) { if ($story->local) {
// generate notification // generate notification
$n = new Notification; $n = new Notification;
$n->profile_id = $dm->to_id; $n->profile_id = $dm->to_id;

View file

@ -34,7 +34,7 @@ class StoryController extends StoryComposeController
{ {
public function recent(Request $request) public function recent(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$user = $request->user(); $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) { if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return []; return [];
@ -117,7 +117,7 @@ class StoryController extends StoryComposeController
public function profile(Request $request, $id) public function profile(Request $request, $id)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$user = $request->user(); $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) { if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
@ -176,7 +176,7 @@ class StoryController extends StoryComposeController
public function viewed(Request $request) public function viewed(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'id' => 'required|min:1', 'id' => 'required|min:1',
@ -221,7 +221,7 @@ class StoryController extends StoryComposeController
public function exists(Request $request, $id) public function exists(Request $request, $id)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$user = $request->user(); $user = $request->user();
if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) { if($user->has_roles && !UserRoleService::can('can-use-stories', $user->id)) {
return response()->json(false); return response()->json(false);
@ -233,7 +233,7 @@ class StoryController extends StoryComposeController
public function iRedirect(Request $request) public function iRedirect(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$user = $request->user(); $user = $request->user();
abort_if(!$user, 404); abort_if(!$user, 404);
@ -243,7 +243,7 @@ class StoryController extends StoryComposeController
public function viewers(Request $request) public function viewers(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required|string' 'sid' => 'required|string'
@ -274,7 +274,7 @@ class StoryController extends StoryComposeController
public function remoteStory(Request $request, $id) public function remoteStory(Request $request, $id)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$profile = Profile::findOrFail($id); $profile = Profile::findOrFail($id);
if($profile->user_id != null || $profile->domain == null) { if($profile->user_id != null || $profile->domain == null) {
@ -286,7 +286,7 @@ class StoryController extends StoryComposeController
public function pollResults(Request $request) public function pollResults(Request $request)
{ {
abort_if(!config_cache('instance.stories.enabled') || !$request->user(), 404); abort_if(!(bool) config_cache('instance.stories.enabled') || !$request->user(), 404);
$this->validate($request, [ $this->validate($request, [
'sid' => 'required|string' 'sid' => 'required|string'
@ -304,7 +304,7 @@ class StoryController extends StoryComposeController
public function getActivityObject(Request $request, $username, $id) public function getActivityObject(Request $request, $username, $id)
{ {
abort_if(!config_cache('instance.stories.enabled'), 404); abort_if(!(bool) config_cache('instance.stories.enabled'), 404);
if(!$request->wantsJson()) { if(!$request->wantsJson()) {
return redirect('/stories/' . $username); return redirect('/stories/' . $username);

View file

@ -34,7 +34,7 @@ class UserEmailForgotController extends Controller
'username.exists' => 'This username is no longer active or does not exist!' 'username.exists' => 'This username is no longer active or does not exist!'
]; ];
if(config('captcha.enabled') || config('captcha.active.login') || config('captcha.active.register')) { if((bool) config_cache('captcha.enabled')) {
$rules['h-captcha-response'] = 'required|captcha'; $rules['h-captcha-response'] = 'required|captcha';
$messages['h-captcha-response.required'] = 'You need to complete the captcha!'; $messages['h-captcha-response.required'] = 'You need to complete the captcha!';
} }

View file

@ -14,12 +14,12 @@ class Kernel extends HttpKernel
* @var array * @var array
*/ */
protected $middleware = [ protected $middleware = [
\Illuminate\Http\Middleware\HandleCors::class,
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrustProxies::class,
\App\Http\Middleware\TrimStrings::class, \App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
]; ];
/** /**

View file

@ -2,10 +2,10 @@
namespace App\Http\Requests\Status; namespace App\Http\Requests\Status;
use Illuminate\Foundation\Http\FormRequest;
use App\Media; use App\Media;
use App\Status; use App\Status;
use Closure; use Closure;
use Illuminate\Foundation\Http\FormRequest;
class StoreStatusEditRequest extends FormRequest class StoreStatusEditRequest extends FormRequest
{ {
@ -15,23 +15,24 @@ class StoreStatusEditRequest extends FormRequest
public function authorize(): bool public function authorize(): bool
{ {
$profile = $this->user()->profile; $profile = $this->user()->profile;
if($profile->status != null) { if ($profile->status != null) {
return false; return false;
} }
if($profile->unlisted == true && $profile->cw == true) { if ($profile->unlisted == true && $profile->cw == true) {
return false; return false;
} }
$types = [ $types = [
"photo", 'photo',
"photo:album", 'photo:album',
"photo:video:album", 'photo:video:album',
"reply", 'reply',
"text", 'text',
"video", 'video',
"video:album" 'video:album',
]; ];
$scopes = ['public', 'unlisted', 'private']; $scopes = ['public', 'unlisted', 'private'];
$status = Status::whereNull('reblog_of_id')->whereIn('type', $types)->whereIn('scope', $scopes)->find($this->route('id')); $status = Status::whereNull('reblog_of_id')->whereIn('type', $types)->whereIn('scope', $scopes)->find($this->route('id'));
return $status && $this->user()->profile_id === $status->profile_id; return $status && $this->user()->profile_id === $status->profile_id;
} }
@ -50,10 +51,10 @@ class StoreStatusEditRequest extends FormRequest
'nullable', 'nullable',
'required_without:status', 'required_without:status',
'array', 'array',
'max:' . config('pixelfed.max_album_length'), 'max:'.(int) config_cache('pixelfed.max_album_length'),
function (string $attribute, mixed $value, Closure $fail) { function (string $attribute, mixed $value, Closure $fail) {
Media::whereProfileId($this->user()->profile_id) Media::whereProfileId($this->user()->profile_id)
->where(function($query) { ->where(function ($query) {
return $query->whereNull('status_id') return $query->whereNull('status_id')
->orWhere('status_id', '=', $this->route('id')); ->orWhere('status_id', '=', $this->route('id'));
}) })

View file

@ -2,8 +2,8 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Services\AccountService; use App\Services\AccountService;
use Illuminate\Http\Resources\Json\JsonResource;
class AdminUser extends JsonResource class AdminUser extends JsonResource
{ {
@ -18,8 +18,8 @@ class AdminUser extends JsonResource
$account = AccountService::get($this->profile_id, true); $account = AccountService::get($this->profile_id, true);
$res = [ $res = [
'id' => $this->id, 'id' => (string) $this->id,
'profile_id' => $this->profile_id, 'profile_id' => (string) $this->profile_id,
'name' => $this->name, 'name' => $this->name,
'username' => $this->username, 'username' => $this->username,
'is_admin' => (bool) $this->is_admin, 'is_admin' => (bool) $this->is_admin,
@ -28,17 +28,18 @@ class AdminUser extends JsonResource
'two_factor_enabled' => (bool) $this->{'2fa_enabled'}, 'two_factor_enabled' => (bool) $this->{'2fa_enabled'},
'register_source' => $this->register_source, 'register_source' => $this->register_source,
'app_register_ip' => $this->app_register_ip, 'app_register_ip' => $this->app_register_ip,
'has_interstitial' => (bool) $this->has_interstitial,
'last_active_at' => $this->last_active_at, 'last_active_at' => $this->last_active_at,
'created_at' => $this->created_at, 'created_at' => $this->created_at,
]; ];
if($account) { if ($account) {
$res['avatar'] = $account['avatar']; $res['avatar'] = $account['avatar'];
$res['bio'] = $account['note_text']; $res['bio'] = $account['note_text'];
$res['statuses_count'] = $account['statuses_count']; $res['statuses_count'] = (int) $account['statuses_count'];
$res['following_count'] = $account['following_count']; $res['following_count'] = (int) $account['following_count'];
$res['followers_count'] = $account['followers_count']; $res['followers_count'] = (int) $account['followers_count'];
$res['is_private'] = $account['locked']; $res['is_private'] = (bool) $account['locked'];
} }
return $res; return $res;

View file

@ -2,9 +2,9 @@
namespace App\Jobs\AvatarPipeline; namespace App\Jobs\AvatarPipeline;
use Cache;
use App\Avatar; use App\Avatar;
use App\Profile; use App\Profile;
use Cache;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
@ -20,6 +20,7 @@ class AvatarOptimize implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $profile; protected $profile;
protected $current; protected $current;
/** /**
@ -62,10 +63,10 @@ class AvatarOptimize implements ShouldQueue
$avatar->change_count = ++$avatar->change_count; $avatar->change_count = ++$avatar->change_count;
$avatar->last_processed_at = Carbon::now(); $avatar->last_processed_at = Carbon::now();
$avatar->save(); $avatar->save();
Cache::forget('avatar:' . $avatar->profile_id); Cache::forget('avatar:'.$avatar->profile_id);
$this->deleteOldAvatar($avatar->media_path, $this->current); $this->deleteOldAvatar($avatar->media_path, $this->current);
if(config_cache('pixelfed.cloud_storage') && config('instance.avatar.local_to_cloud')) { if ((bool) config_cache('pixelfed.cloud_storage') && (bool) config_cache('instance.avatar.local_to_cloud')) {
$this->uploadToCloud($avatar); $this->uploadToCloud($avatar);
} else { } else {
$avatar->cdn_url = null; $avatar->cdn_url = null;
@ -77,10 +78,9 @@ class AvatarOptimize implements ShouldQueue
protected function deleteOldAvatar($new, $current) protected function deleteOldAvatar($new, $current)
{ {
if ( storage_path('app/'.$new) == $current || if (storage_path('app/'.$new) == $current ||
Str::endsWith($current, 'avatars/default.png') || Str::endsWith($current, 'avatars/default.png') ||
Str::endsWith($current, 'avatars/default.jpg')) Str::endsWith($current, 'avatars/default.jpg')) {
{
return; return;
} }
if (is_file($current)) { if (is_file($current)) {
@ -90,15 +90,15 @@ class AvatarOptimize implements ShouldQueue
protected function uploadToCloud($avatar) protected function uploadToCloud($avatar)
{ {
$base = 'cache/avatars/' . $avatar->profile_id; $base = 'cache/avatars/'.$avatar->profile_id;
$disk = Storage::disk(config('filesystems.cloud')); $disk = Storage::disk(config('filesystems.cloud'));
$disk->deleteDirectory($base); $disk->deleteDirectory($base);
$path = $base . '/' . 'avatar_' . strtolower(Str::random(random_int(3,6))) . $avatar->change_count . '.' . pathinfo($avatar->media_path, PATHINFO_EXTENSION); $path = $base.'/'.'avatar_'.strtolower(Str::random(random_int(3, 6))).$avatar->change_count.'.'.pathinfo($avatar->media_path, PATHINFO_EXTENSION);
$url = $disk->put($path, Storage::get($avatar->media_path)); $url = $disk->put($path, Storage::get($avatar->media_path));
$avatar->media_path = $path; $avatar->media_path = $path;
$avatar->cdn_url = $disk->url($path); $avatar->cdn_url = $disk->url($path);
$avatar->save(); $avatar->save();
Storage::delete($avatar->media_path); Storage::delete($avatar->media_path);
Cache::forget('avatar:' . $avatar->profile_id); Cache::forget('avatar:'.$avatar->profile_id);
} }
} }

View file

@ -4,20 +4,13 @@ namespace App\Jobs\AvatarPipeline;
use App\Avatar; use App\Avatar;
use App\Profile; use App\Profile;
use App\Services\MediaStorageService;
use App\Util\ActivityPub\Helpers;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use App\Util\ActivityPub\Helpers;
use Illuminate\Support\Str;
use Zttp\Zttp;
use App\Http\Controllers\AvatarController;
use Storage;
use Log;
use Illuminate\Http\File;
use App\Services\MediaStorageService;
use App\Services\ActivityPubFetchService;
class RemoteAvatarFetch implements ShouldQueue class RemoteAvatarFetch implements ShouldQueue
{ {
@ -38,7 +31,9 @@ class RemoteAvatarFetch implements ShouldQueue
* @var int * @var int
*/ */
public $tries = 1; public $tries = 1;
public $timeout = 300; public $timeout = 300;
public $maxExceptions = 1; public $maxExceptions = 1;
/** /**
@ -60,23 +55,23 @@ class RemoteAvatarFetch implements ShouldQueue
{ {
$profile = $this->profile; $profile = $this->profile;
if(boolval(config_cache('pixelfed.cloud_storage')) == false && boolval(config_cache('federation.avatars.store_local')) == false) { if ((bool) config_cache('pixelfed.cloud_storage') == false && (bool) config_cache('federation.avatars.store_local') == false) {
return 1; return 1;
} }
if($profile->domain == null || $profile->private_key) { if ($profile->domain == null || $profile->private_key) {
return 1; return 1;
} }
$avatar = Avatar::whereProfileId($profile->id)->first(); $avatar = Avatar::whereProfileId($profile->id)->first();
if(!$avatar) { if (! $avatar) {
$avatar = new Avatar; $avatar = new Avatar;
$avatar->profile_id = $profile->id; $avatar->profile_id = $profile->id;
$avatar->save(); $avatar->save();
} }
if($avatar->media_path == null && $avatar->remote_url == null) { if ($avatar->media_path == null && $avatar->remote_url == null) {
$avatar->media_path = 'public/avatars/default.jpg'; $avatar->media_path = 'public/avatars/default.jpg';
$avatar->is_remote = true; $avatar->is_remote = true;
$avatar->save(); $avatar->save();
@ -84,22 +79,22 @@ class RemoteAvatarFetch implements ShouldQueue
$person = Helpers::fetchFromUrl($profile->remote_url); $person = Helpers::fetchFromUrl($profile->remote_url);
if(!$person || !isset($person['@context'])) { if (! $person || ! isset($person['@context'])) {
return 1; return 1;
} }
if( !isset($person['icon']) || if (! isset($person['icon']) ||
!isset($person['icon']['type']) || ! isset($person['icon']['type']) ||
!isset($person['icon']['url']) ! isset($person['icon']['url'])
) { ) {
return 1; return 1;
} }
if($person['icon']['type'] !== 'Image') { if ($person['icon']['type'] !== 'Image') {
return 1; return 1;
} }
if(!Helpers::validateUrl($person['icon']['url'])) { if (! Helpers::validateUrl($person['icon']['url'])) {
return 1; return 1;
} }
@ -108,7 +103,7 @@ class RemoteAvatarFetch implements ShouldQueue
$avatar->remote_url = $icon['url']; $avatar->remote_url = $icon['url'];
$avatar->save(); $avatar->save();
MediaStorageService::avatar($avatar, boolval(config_cache('pixelfed.cloud_storage')) == false, true); MediaStorageService::avatar($avatar, (bool) config_cache('pixelfed.cloud_storage') == false, true);
return 1; return 1;
} }

View file

@ -4,28 +4,21 @@ namespace App\Jobs\AvatarPipeline;
use App\Avatar; use App\Avatar;
use App\Profile; use App\Profile;
use App\Services\AccountService;
use App\Services\MediaStorageService;
use Cache;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use App\Util\ActivityPub\Helpers;
use Illuminate\Support\Str;
use Zttp\Zttp;
use App\Http\Controllers\AvatarController;
use Cache;
use Storage;
use Log;
use Illuminate\Http\File;
use App\Services\AccountService;
use App\Services\MediaStorageService;
use App\Services\ActivityPubFetchService;
class RemoteAvatarFetchFromUrl implements ShouldQueue class RemoteAvatarFetchFromUrl implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $profile; protected $profile;
protected $url; protected $url;
/** /**
@ -41,7 +34,9 @@ class RemoteAvatarFetchFromUrl implements ShouldQueue
* @var int * @var int
*/ */
public $tries = 1; public $tries = 1;
public $timeout = 300; public $timeout = 300;
public $maxExceptions = 1; public $maxExceptions = 1;
/** /**
@ -64,20 +59,20 @@ class RemoteAvatarFetchFromUrl implements ShouldQueue
{ {
$profile = $this->profile; $profile = $this->profile;
Cache::forget('avatar:' . $profile->id); Cache::forget('avatar:'.$profile->id);
AccountService::del($profile->id); AccountService::del($profile->id);
if(boolval(config_cache('pixelfed.cloud_storage')) == false && boolval(config_cache('federation.avatars.store_local')) == false) { if ((bool) config_cache('pixelfed.cloud_storage') == false && (bool) config_cache('federation.avatars.store_local') == false) {
return 1; return 1;
} }
if($profile->domain == null || $profile->private_key) { if ($profile->domain == null || $profile->private_key) {
return 1; return 1;
} }
$avatar = Avatar::whereProfileId($profile->id)->first(); $avatar = Avatar::whereProfileId($profile->id)->first();
if(!$avatar) { if (! $avatar) {
$avatar = new Avatar; $avatar = new Avatar;
$avatar->profile_id = $profile->id; $avatar->profile_id = $profile->id;
$avatar->is_remote = true; $avatar->is_remote = true;
@ -89,7 +84,7 @@ class RemoteAvatarFetchFromUrl implements ShouldQueue
$avatar->save(); $avatar->save();
} }
MediaStorageService::avatar($avatar, boolval(config_cache('pixelfed.cloud_storage')) == false, true); MediaStorageService::avatar($avatar, (bool) config_cache('pixelfed.cloud_storage') == false, true);
return 1; return 1;
} }

View file

@ -4,26 +4,25 @@ namespace App\Jobs\FollowPipeline;
use App\Follower; use App\Follower;
use App\FollowRequest; use App\FollowRequest;
use App\Jobs\HomeFeedPipeline\FeedUnfollowPipeline;
use App\Notification; use App\Notification;
use App\Profile; use App\Profile;
use App\Services\AccountService;
use App\Services\FollowerService;
use App\Services\NotificationService;
use Cache; use Cache;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Log;
use Illuminate\Support\Facades\Redis;
use App\Services\AccountService;
use App\Services\FollowerService;
use App\Services\NotificationService;
use App\Jobs\HomeFeedPipeline\FeedUnfollowPipeline;
class UnfollowPipeline implements ShouldQueue class UnfollowPipeline implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $actor; protected $actor;
protected $target; protected $target;
/** /**
@ -48,11 +47,11 @@ class UnfollowPipeline implements ShouldQueue
$target = $this->target; $target = $this->target;
$actorProfile = Profile::find($actor); $actorProfile = Profile::find($actor);
if(!$actorProfile) { if (! $actorProfile) {
return; return;
} }
$targetProfile = Profile::find($target); $targetProfile = Profile::find($target);
if(!$targetProfile) { if (! $targetProfile) {
return; return;
} }
@ -60,37 +59,37 @@ class UnfollowPipeline implements ShouldQueue
FollowerService::remove($actor, $target); FollowerService::remove($actor, $target);
$actorProfileSync = Cache::get(FollowerService::FOLLOWING_SYNC_KEY . $actor); $actorProfileSync = Cache::get(FollowerService::FOLLOWING_SYNC_KEY.$actor);
if(!$actorProfileSync) { if (! $actorProfileSync) {
FollowServiceWarmCache::dispatch($actor)->onQueue('low'); FollowServiceWarmCache::dispatch($actor)->onQueue('low');
} else { } else {
if($actorProfile->following_count) { if ($actorProfile->following_count) {
$actorProfile->decrement('following_count'); $actorProfile->decrement('following_count');
} else { } else {
$count = Follower::whereProfileId($actor)->count(); $count = Follower::whereProfileId($actor)->count();
$actorProfile->following_count = $count; $actorProfile->following_count = $count;
$actorProfile->save(); $actorProfile->save();
} }
Cache::put(FollowerService::FOLLOWING_SYNC_KEY . $actor, 1, 604800); Cache::put(FollowerService::FOLLOWING_SYNC_KEY.$actor, 1, 604800);
AccountService::del($actor); AccountService::del($actor);
} }
$targetProfileSync = Cache::get(FollowerService::FOLLOWERS_SYNC_KEY . $target); $targetProfileSync = Cache::get(FollowerService::FOLLOWERS_SYNC_KEY.$target);
if(!$targetProfileSync) { if (! $targetProfileSync) {
FollowServiceWarmCache::dispatch($target)->onQueue('low'); FollowServiceWarmCache::dispatch($target)->onQueue('low');
} else { } else {
if($targetProfile->followers_count) { if ($targetProfile->followers_count) {
$targetProfile->decrement('followers_count'); $targetProfile->decrement('followers_count');
} else { } else {
$count = Follower::whereFollowingId($target)->count(); $count = Follower::whereFollowingId($target)->count();
$targetProfile->followers_count = $count; $targetProfile->followers_count = $count;
$targetProfile->save(); $targetProfile->save();
} }
Cache::put(FollowerService::FOLLOWERS_SYNC_KEY . $target, 1, 604800); Cache::put(FollowerService::FOLLOWERS_SYNC_KEY.$target, 1, 604800);
AccountService::del($target); AccountService::del($target);
} }
if($targetProfile->domain == null) { if ($targetProfile->domain == null) {
Notification::withTrashed() Notification::withTrashed()
->whereProfileId($target) ->whereProfileId($target)
->whereAction('follow') ->whereAction('follow')
@ -98,20 +97,22 @@ class UnfollowPipeline implements ShouldQueue
->whereItemId($target) ->whereItemId($target)
->whereItemType('App\Profile') ->whereItemType('App\Profile')
->get() ->get()
->each(function($n) { ->each(function ($n) {
NotificationService::del($n->profile_id, $n->id); NotificationService::del($n->profile_id, $n->id);
$n->forceDelete(); $n->forceDelete();
}); });
} }
if($actorProfile->domain == null && config('instance.timeline.home.cached')) { if ($actorProfile->domain == null && config('instance.timeline.home.cached')) {
Cache::forget('pf:timelines:home:' . $actor); Cache::forget('pf:timelines:home:'.$actor);
} }
FollowRequest::whereFollowingId($target) FollowRequest::whereFollowingId($target)
->whereFollowerId($actor) ->whereFollowerId($actor)
->delete(); ->delete();
return; AccountService::del($target);
AccountService::del($actor);
} }
} }

View file

@ -0,0 +1,99 @@
<?php
namespace App\Jobs\GroupPipeline;
use App\Notification;
use App\Status;
use App\Models\GroupPost;
use Cache;
use DB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use App\Services\MediaStorageService;
use App\Services\NotificationService;
use App\Services\StatusService;
class GroupCommentPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status;
protected $comment;
protected $groupPost;
public function __construct(Status $status, Status $comment, $groupPost = null)
{
$this->status = $status;
$this->comment = $comment;
$this->groupPost = $groupPost;
}
public function handle()
{
if($this->status->group_id == null || $this->comment->group_id == null) {
return;
}
$this->updateParentReplyCount();
$this->generateNotification();
if($this->groupPost) {
$this->updateChildReplyCount();
}
}
protected function updateParentReplyCount()
{
$parent = $this->status;
$parent->reply_count = Status::whereInReplyToId($parent->id)->count();
$parent->save();
StatusService::del($parent->id);
}
protected function updateChildReplyCount()
{
$gp = $this->groupPost;
if($gp->reply_child_id) {
$parent = GroupPost::whereStatusId($gp->reply_child_id)->first();
if($parent) {
$parent->reply_count++;
$parent->save();
}
}
}
protected function generateNotification()
{
$status = $this->status;
$comment = $this->comment;
$target = $status->profile;
$actor = $comment->profile;
if ($actor->id == $target->id || $status->comments_disabled == true) {
return;
}
$notification = DB::transaction(function() use($target, $actor, $comment) {
$actorName = $actor->username;
$actorUrl = $actor->url();
$text = "{$actorName} commented on your group post.";
$html = "<a href='{$actorUrl}' class='profile-link'>{$actorName}</a> commented on your group post.";
$notification = new Notification();
$notification->profile_id = $target->id;
$notification->actor_id = $actor->id;
$notification->action = 'group:comment';
$notification->item_id = $comment->id;
$notification->item_type = "App\Status";
$notification->save();
return $notification;
});
NotificationService::setNotification($notification);
NotificationService::set($notification->profile_id, $notification->id);
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace App\Jobs\GroupPipeline;
use App\Media;
use Cache;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use App\Services\MediaStorageService;
class GroupMediaPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $media;
public function __construct(Media $media)
{
$this->media = $media;
}
public function handle()
{
MediaStorageService::store($this->media);
}
protected function localToCloud($media)
{
$path = storage_path('app/'.$media->media_path);
$thumb = storage_path('app/'.$media->thumbnail_path);
$p = explode('/', $media->media_path);
$name = array_pop($p);
$pt = explode('/', $media->thumbnail_path);
$thumbname = array_pop($pt);
$storagePath = implode('/', $p);
$disk = Storage::disk(config('filesystems.cloud'));
$file = $disk->putFileAs($storagePath, new File($path), $name, 'public');
$url = $disk->url($file);
$thumbFile = $disk->putFileAs($storagePath, new File($thumb), $thumbname, 'public');
$thumbUrl = $disk->url($thumbFile);
$media->thumbnail_url = $thumbUrl;
$media->cdn_url = $url;
$media->optimized_url = $url;
$media->replicated_at = now();
$media->save();
if($media->status_id) {
Cache::forget('status:transformer:media:attachments:' . $media->status_id);
}
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace App\Jobs\GroupPipeline;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\GroupInvitation;
use App\Notification;
use App\Profile;
class GroupMemberInvite implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $invite;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupInvitation $invite)
{
$this->invite = $invite;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$invite = $this->invite;
$actor = Profile::find($invite->from_profile_id);
$target = Profile::find($invite->to_profile_id);
if(!$actor || !$target) {
return;
}
$notification = new Notification;
$notification->profile_id = $target->id;
$notification->actor_id = $actor->id;
$notification->action = 'group:invite';
$notification->item_id = $invite->group_id;
$notification->item_type = 'App\Models\Group';
$notification->save();
}
}

View file

@ -0,0 +1,54 @@
<?php
namespace App\Jobs\GroupPipeline;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\GroupMember;
use App\Notification;
use App\Services\GroupService;
class JoinApproved implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $member;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupMember $member)
{
$this->member = $member;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$member = $this->member;
$member->approved_at = now();
$member->join_request = false;
$member->role = 'member';
$member->save();
$n = new Notification;
$n->profile_id = $member->profile_id;
$n->actor_id = $member->profile_id;
$n->item_id = $member->group_id;
$n->item_type = 'App\Models\Group';
$n->save();
GroupService::del($member->group_id);
GroupService::delSelf($member->group_id, $member->profile_id);
}
}

View file

@ -0,0 +1,50 @@
<?php
namespace App\Jobs\GroupPipeline;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\GroupMember;
use App\Notification;
use App\Services\GroupService;
class JoinRejected implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $member;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupMember $member)
{
$this->member = $member;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$member = $this->member;
$member->rejected_at = now();
$member->save();
$n = new Notification;
$n->profile_id = $member->profile_id;
$n->actor_id = $member->profile_id;
$n->item_id = $member->group_id;
$n->item_type = 'App\Models\Group';
$n->action = 'group.join.rejected';
$n->save();
}
}

View file

@ -0,0 +1,107 @@
<?php
namespace App\Jobs\GroupPipeline;
use Cache, Log;
use Illuminate\Support\Facades\Redis;
use App\{Like, Notification};
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Util\ActivityPub\Helpers;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use App\Transformer\ActivityPub\Verb\Like as LikeTransformer;
use App\Services\StatusService;
class LikePipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $like;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
public $timeout = 5;
public $tries = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Like $like)
{
$this->like = $like;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$like = $this->like;
$status = $this->like->status;
$actor = $this->like->actor;
if (!$status) {
// Ignore notifications to deleted statuses
return;
}
StatusService::refresh($status->id);
if($status->url && $actor->domain == null) {
return $this->remoteLikeDeliver();
}
$exists = Notification::whereProfileId($status->profile_id)
->whereActorId($actor->id)
->whereAction('group:like')
->whereItemId($status->id)
->whereItemType('App\Status')
->count();
if ($actor->id === $status->profile_id || $exists !== 0) {
return true;
}
try {
$notification = new Notification();
$notification->profile_id = $status->profile_id;
$notification->actor_id = $actor->id;
$notification->action = 'group:like';
$notification->item_id = $status->id;
$notification->item_type = "App\Status";
$notification->save();
} catch (Exception $e) {
}
}
public function remoteLikeDeliver()
{
$like = $this->like;
$status = $this->like->status;
$actor = $this->like->actor;
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($like, new LikeTransformer());
$activity = $fractal->createData($resource)->toArray();
$url = $status->profile->sharedInbox ?? $status->profile->inbox_url;
Helpers::sendSignedObject($actor, $url, $activity);
}
}

View file

@ -0,0 +1,130 @@
<?php
namespace App\Jobs\GroupPipeline;
use App\Notification;
use App\Hashtag;
use App\Mention;
use App\Profile;
use App\Status;
use App\StatusHashtag;
use App\Models\GroupPostHashtag;
use App\Models\GroupPost;
use Cache;
use DB;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use App\Services\MediaStorageService;
use App\Services\NotificationService;
use App\Services\StatusService;
use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor;
class NewStatusPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status;
protected $gp;
protected $tags;
protected $mentions;
public function __construct(Status $status, GroupPost $gp)
{
$this->status = $status;
$this->gp = $gp;
}
public function handle()
{
$status = $this->status;
$autolink = Autolink::create()
->setAutolinkActiveUsersOnly(true)
->setBaseHashPath("/groups/{$status->group_id}/topics/")
->setBaseUserPath("/groups/{$status->group_id}/username/")
->autolink($status->caption);
$entities = Extractor::create()->extract($status->caption);
$autolink = str_replace('/discover/tags/', '/groups/' . $status->group_id . '/topics/', $autolink);
$status->rendered = nl2br($autolink);
$status->entities = null;
$status->save();
$this->tags = array_unique($entities['hashtags']);
$this->mentions = array_unique($entities['mentions']);
if(count($this->tags)) {
$this->storeHashtags();
}
if(count($this->mentions)) {
$this->storeMentions($this->mentions);
}
}
protected function storeHashtags()
{
$tags = $this->tags;
$status = $this->status;
$gp = $this->gp;
foreach ($tags as $tag) {
if(mb_strlen($tag) > 124) {
continue;
}
DB::transaction(function () use ($status, $tag, $gp) {
$slug = str_slug($tag, '-', false);
$hashtag = Hashtag::firstOrCreate(
['name' => $tag, 'slug' => $slug]
);
GroupPostHashtag::firstOrCreate(
[
'group_id' => $status->group_id,
'group_post_id' => $gp->id,
'status_id' => $status->id,
'hashtag_id' => $hashtag->id,
'profile_id' => $status->profile_id,
]
);
});
}
if(count($this->mentions)) {
$this->storeMentions();
}
StatusService::del($status->id);
}
protected function storeMentions()
{
$mentions = $this->mentions;
$status = $this->status;
foreach ($mentions as $mention) {
$mentioned = Profile::whereUsername($mention)->first();
if (empty($mentioned) || !isset($mentioned->id)) {
continue;
}
DB::transaction(function () use ($status, $mentioned) {
$m = new Mention();
$m->status_id = $status->id;
$m->profile_id = $mentioned->id;
$m->save();
MentionPipeline::dispatch($status, $m);
});
}
StatusService::del($status->id);
}
}

View file

@ -0,0 +1,109 @@
<?php
namespace App\Jobs\GroupPipeline;
use Cache, Log;
use Illuminate\Support\Facades\Redis;
use App\{Like, Notification};
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Util\ActivityPub\Helpers;
use League\Fractal;
use League\Fractal\Serializer\ArraySerializer;
use App\Transformer\ActivityPub\Verb\UndoLike as LikeTransformer;
use App\Services\StatusService;
class UnlikePipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $like;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
public $timeout = 5;
public $tries = 1;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(Like $like)
{
$this->like = $like;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$like = $this->like;
$status = $this->like->status;
$actor = $this->like->actor;
if (!$status) {
// Ignore notifications to deleted statuses
return;
}
$count = $status->likes_count > 1 ? $status->likes_count : $status->likes()->count();
$status->likes_count = $count - 1;
$status->save();
StatusService::del($status->id);
if($actor->id !== $status->profile_id && $status->url && $actor->domain == null) {
$this->remoteLikeDeliver();
}
$exists = Notification::whereProfileId($status->profile_id)
->whereActorId($actor->id)
->whereAction('group:like')
->whereItemId($status->id)
->whereItemType('App\Status')
->first();
if($exists) {
$exists->delete();
}
$like = Like::whereProfileId($actor->id)->whereStatusId($status->id)->first();
if(!$like) {
return;
}
$like->forceDelete();
return;
}
public function remoteLikeDeliver()
{
$like = $this->like;
$status = $this->like->status;
$actor = $this->like->actor;
$fractal = new Fractal\Manager();
$fractal->setSerializer(new ArraySerializer());
$resource = new Fractal\Resource\Item($like, new LikeTransformer());
$activity = $fractal->createData($resource)->toArray();
$url = $status->profile->sharedInbox ?? $status->profile->inbox_url;
Helpers::sendSignedObject($actor, $url, $activity);
}
}

View file

@ -0,0 +1,58 @@
<?php
namespace App\Jobs\GroupsPipeline;
use App\Util\Media\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Group;
use App\Models\GroupComment;
use App\Models\GroupPost;
use App\Models\GroupHashtag;
use App\Models\GroupPostHashtag;
use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor;
use DB;
class DeleteCommentPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $parent;
protected $status;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($parent, $status)
{
$this->parent = $parent;
$this->status = $status;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$parent = $this->parent;
$parent->reply_count = GroupComment::whereStatusId($parent->id)->count();
$parent->save();
return;
}
}

View file

@ -0,0 +1,89 @@
<?php
namespace App\Jobs\GroupsPipeline;
use App\Models\GroupMedia;
use App\Util\Media\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;
use Storage;
use Image as Intervention;
class ImageResizePipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $media;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupMedia $media)
{
$this->media = $media;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$media = $this->media;
if(!$media) {
return;
}
if (!Storage::exists($media->media_path) || $media->skip_optimize) {
return;
}
$path = $media->media_path;
$file = storage_path('app/' . $path);
$quality = config_cache('pixelfed.image_quality');
$orientations = [
'square' => [
'width' => 1080,
'height' => 1080,
],
'landscape' => [
'width' => 1920,
'height' => 1080,
],
'portrait' => [
'width' => 1080,
'height' => 1350,
],
];
try {
$img = Intervention::make($file);
$img->orientate();
$width = $img->width();
$height = $img->height();
$aspect = $width / $height;
$orientation = $aspect === 1 ? 'square' : ($aspect > 1 ? 'landscape' : 'portrait');
$ratio = $orientations[$orientation];
$img->resize($ratio['width'], $ratio['height']);
$img->save($file, $quality);
} catch (Exception $e) {
Log::error($e);
}
}
}

View file

@ -0,0 +1,67 @@
<?php
namespace App\Jobs\GroupsPipeline;
use App\Models\GroupMedia;
use App\Util\Media\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Storage;
use Illuminate\Http\File;
use Exception;
use GuzzleHttp\Exception\ClientException;
use Aws\S3\Exception\S3Exception;
use GuzzleHttp\Exception\ConnectException;
use League\Flysystem\UnableToWriteFile;
class ImageS3DeletePipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $media;
static $attempts = 1;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupMedia $media)
{
$this->media = $media;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$media = $this->media;
if(!$media || (bool) config_cache('pixelfed.cloud_storage') === false) {
return;
}
$fs = Storage::disk(config('filesystems.cloud'));
if(!$fs) {
return;
}
if($fs->exists($media->media_path)) {
$fs->delete($media->media_path);
}
}
}

View file

@ -0,0 +1,107 @@
<?php
namespace App\Jobs\GroupsPipeline;
use App\Models\GroupMedia;
use App\Util\Media\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Storage;
use Illuminate\Http\File;
use Exception;
use GuzzleHttp\Exception\ClientException;
use Aws\S3\Exception\S3Exception;
use GuzzleHttp\Exception\ConnectException;
use League\Flysystem\UnableToWriteFile;
class ImageS3UploadPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $media;
static $attempts = 1;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupMedia $media)
{
$this->media = $media;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$media = $this->media;
if(!$media || (bool) config_cache('pixelfed.cloud_storage') === false) {
return;
}
$path = storage_path('app/' . $media->media_path);
$p = explode('/', $media->media_path);
$name = array_pop($p);
$storagePath = implode('/', $p);
$url = (bool) config_cache('pixelfed.cloud_storage') && (bool) config('media.storage.remote.resilient_mode') ?
self::handleResilientStore($storagePath, $path, $name) :
self::handleStore($storagePath, $path, $name);
if($url && strlen($url) && str_starts_with($url, 'https://')) {
$media->cdn_url = $url;
$media->processed_at = now();
$media->version = 11;
$media->save();
Storage::disk('local')->delete($media->media_path);
}
}
protected function handleStore($storagePath, $path, $name)
{
return retry(3, function() use($storagePath, $path, $name) {
$baseDisk = (bool) config_cache('pixelfed.cloud_storage') ? config('filesystems.cloud') : 'local';
$disk = Storage::disk($baseDisk);
$file = $disk->putFileAs($storagePath, new File($path), $name, 'public');
return $disk->url($file);
}, random_int(100, 500));
}
protected function handleResilientStore($storagePath, $path, $name)
{
$attempts = 0;
return retry(4, function() use($storagePath, $path, $name, $attempts) {
self::$attempts++;
usleep(100000);
$baseDisk = self::$attempts > 1 ? $this->getAltDriver() : config('filesystems.cloud');
try {
$disk = Storage::disk($baseDisk);
$file = $disk->putFileAs($storagePath, new File($path), $name, 'public');
} catch (S3Exception | ClientException | ConnectException | UnableToWriteFile | Exception $e) {}
return $disk->url($file);
}, function (int $attempt, Exception $exception) {
return $attempt * 200;
});
}
protected function getAltDriver()
{
return config('filesystems.cloud');
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace App\Jobs\GroupsPipeline;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\GroupMember;
use App\Notification;
use App\Services\GroupService;
class MemberJoinApprovedPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $member;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupMember $member)
{
$this->member = $member;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$member = $this->member;
$member->approved_at = now();
$member->join_request = false;
$member->role = 'member';
$member->save();
GroupService::del($member->group_id);
GroupService::delSelf($member->group_id, $member->profile_id);
}
}

View file

@ -0,0 +1,42 @@
<?php
namespace App\Jobs\GroupsPipeline;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\GroupMember;
use App\Notification;
use App\Services\GroupService;
class MemberJoinRejectedPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $member;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupMember $member)
{
$this->member = $member;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$member = $this->member;
$member->rejected_at = now();
$member->save();
}
}

View file

@ -0,0 +1,115 @@
<?php
namespace App\Jobs\GroupsPipeline;
use App\Util\Media\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Group;
use App\Models\GroupComment;
use App\Models\GroupPost;
use App\Models\GroupHashtag;
use App\Models\GroupPostHashtag;
use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor;
use DB;
class NewCommentPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status;
protected $parent;
protected $entities;
protected $autolink;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($parent, GroupComment $status)
{
$this->parent = $parent;
$this->status = $status;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$profile = $this->status->profile;
$status = $this->status;
$parent = $this->parent;
$parent->reply_count = GroupComment::whereStatusId($parent->id)->count();
$parent->save();
if ($profile->no_autolink == false) {
$this->parseEntities();
}
}
public function parseEntities()
{
$this->extractEntities();
}
public function extractEntities()
{
$this->entities = Extractor::create()->extract($this->status->caption);
$this->autolinkStatus();
}
public function autolinkStatus()
{
$this->autolink = Autolink::create()->autolink($this->status->caption);
$this->storeHashtags();
}
public function storeHashtags()
{
$tags = array_unique($this->entities['hashtags']);
$status = $this->status;
foreach ($tags as $tag) {
if (mb_strlen($tag) > 124) {
continue;
}
DB::transaction(function () use ($status, $tag) {
$hashtag = GroupHashtag::firstOrCreate([
'name' => $tag,
]);
GroupPostHashtag::firstOrCreate(
[
'status_id' => $status->id,
'group_id' => $status->group_id,
'hashtag_id' => $hashtag->id,
'profile_id' => $status->profile_id,
'status_visibility' => $status->visibility,
]
);
});
}
$this->storeMentions();
}
public function storeMentions()
{
// todo
}
}

View file

@ -0,0 +1,108 @@
<?php
namespace App\Jobs\GroupsPipeline;
use App\Util\Media\Image;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use App\Models\Group;
use App\Models\GroupPost;
use App\Models\GroupHashtag;
use App\Models\GroupPostHashtag;
use App\Util\Lexer\Autolink;
use App\Util\Lexer\Extractor;
use DB;
class NewPostPipeline implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $status;
protected $entities;
protected $autolink;
/**
* Delete the job if its models no longer exist.
*
* @var bool
*/
public $deleteWhenMissingModels = true;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct(GroupPost $status)
{
$this->status = $status;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$profile = $this->status->profile;
$status = $this->status;
if ($profile->no_autolink == false) {
$this->parseEntities();
}
}
public function parseEntities()
{
$this->extractEntities();
}
public function extractEntities()
{
$this->entities = Extractor::create()->extract($this->status->caption);
$this->autolinkStatus();
}
public function autolinkStatus()
{
$this->autolink = Autolink::create()->autolink($this->status->caption);
$this->storeHashtags();
}
public function storeHashtags()
{
$tags = array_unique($this->entities['hashtags']);
$status = $this->status;
foreach ($tags as $tag) {
if (mb_strlen($tag) > 124) {
continue;
}
DB::transaction(function () use ($status, $tag) {
$hashtag = GroupHashtag::firstOrCreate([
'name' => $tag,
]);
GroupPostHashtag::firstOrCreate(
[
'status_id' => $status->id,
'group_id' => $status->group_id,
'hashtag_id' => $hashtag->id,
'profile_id' => $status->profile_id,
'status_visibility' => $status->visibility,
]
);
});
}
$this->storeMentions();
}
public function storeMentions()
{
// todo
}
}

View file

@ -45,7 +45,7 @@ class ImageOptimize implements ShouldQueue
return; return;
} }
if(config('pixelfed.optimize_image') == false) { if((bool) config_cache('pixelfed.optimize_image') == false) {
ImageThumbnail::dispatch($media)->onQueue('mmo'); ImageThumbnail::dispatch($media)->onQueue('mmo');
return; return;
} else { } else {

View file

@ -51,7 +51,7 @@ class ImageResize implements ShouldQueue
return; return;
} }
if(!config('pixelfed.optimize_image')) { if((bool) config_cache('pixelfed.optimize_image') === false) {
ImageThumbnail::dispatch($media)->onQueue('mmo'); ImageThumbnail::dispatch($media)->onQueue('mmo');
return; return;
} }

View file

@ -61,7 +61,7 @@ class ImageUpdate implements ShouldQueue
return; return;
} }
if(config('pixelfed.optimize_image')) { if((bool) config_cache('pixelfed.optimize_image')) {
if (in_array($media->mime, $this->protectedMimes) == true) { if (in_array($media->mime, $this->protectedMimes) == true) {
ImageOptimizer::optimize($thumb); ImageOptimizer::optimize($thumb);
if(!$media->skip_optimize) { if(!$media->skip_optimize) {

View file

@ -72,10 +72,12 @@ class FetchNodeinfoPipeline implements ShouldQueue, ShouldBeUniqueUntilProcessin
$instance->software = strtolower(strip_tags($software)); $instance->software = strtolower(strip_tags($software));
$instance->user_count = Profile::whereDomain($instance->domain)->count(); $instance->user_count = Profile::whereDomain($instance->domain)->count();
$instance->nodeinfo_last_fetched = now(); $instance->nodeinfo_last_fetched = now();
$instance->last_crawled_at = now();
$instance->save(); $instance->save();
} }
} else { } else {
$instance->delivery_timeout = 1; $instance->delivery_timeout = 1;
$instance->last_crawled_at = now();
$instance->delivery_next_after = now()->addHours(14); $instance->delivery_next_after = now()->addHours(14);
$instance->save(); $instance->save();
} }

View file

@ -3,27 +3,30 @@
namespace App\Jobs\MediaPipeline; namespace App\Jobs\MediaPipeline;
use App\Media; use App\Media;
use App\Services\Media\MediaHlsService;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage;
use App\Services\Media\MediaHlsService;
use Illuminate\Queue\Middleware\WithoutOverlapping; use Illuminate\Queue\Middleware\WithoutOverlapping;
use Illuminate\Contracts\Queue\ShouldBeUniqueUntilProcessing; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Storage;
class MediaDeletePipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing class MediaDeletePipeline implements ShouldBeUniqueUntilProcessing, ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $media; protected $media;
public $timeout = 300; public $timeout = 300;
public $tries = 3; public $tries = 3;
public $maxExceptions = 1; public $maxExceptions = 1;
public $failOnTimeout = true; public $failOnTimeout = true;
public $deleteWhenMissingModels = true; public $deleteWhenMissingModels = true;
/** /**
@ -38,7 +41,7 @@ class MediaDeletePipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
*/ */
public function uniqueId(): string public function uniqueId(): string
{ {
return 'media:purge-job:id-' . $this->media->id; return 'media:purge-job:id-'.$this->media->id;
} }
/** /**
@ -62,7 +65,7 @@ class MediaDeletePipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
$path = $media->media_path; $path = $media->media_path;
$thumb = $media->thumbnail_path; $thumb = $media->thumbnail_path;
if(!$path) { if (! $path) {
return 1; return 1;
} }
@ -70,32 +73,32 @@ class MediaDeletePipeline implements ShouldQueue, ShouldBeUniqueUntilProcessing
array_pop($e); array_pop($e);
$i = implode('/', $e); $i = implode('/', $e);
if(config_cache('pixelfed.cloud_storage') == true) { if ((bool) config_cache('pixelfed.cloud_storage') == true) {
$disk = Storage::disk(config('filesystems.cloud')); $disk = Storage::disk(config('filesystems.cloud'));
if($path && $disk->exists($path)) { if ($path && $disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
if($thumb && $disk->exists($thumb)) { if ($thumb && $disk->exists($thumb)) {
$disk->delete($thumb); $disk->delete($thumb);
} }
} }
$disk = Storage::disk(config('filesystems.local')); $disk = Storage::disk(config('filesystems.local'));
if($path && $disk->exists($path)) { if ($path && $disk->exists($path)) {
$disk->delete($path); $disk->delete($path);
} }
if($thumb && $disk->exists($thumb)) { if ($thumb && $disk->exists($thumb)) {
$disk->delete($thumb); $disk->delete($thumb);
} }
if($media->hls_path != null) { if ($media->hls_path != null) {
$files = MediaHlsService::allFiles($media); $files = MediaHlsService::allFiles($media);
if($files && count($files)) { if ($files && count($files)) {
foreach($files as $file) { foreach ($files as $file) {
$disk->delete($file); $disk->delete($file);
} }
} }

View file

@ -8,7 +8,6 @@ use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
class MediaFixLocalFilesystemCleanupPipeline implements ShouldQueue class MediaFixLocalFilesystemCleanupPipeline implements ShouldQueue
@ -16,12 +15,14 @@ class MediaFixLocalFilesystemCleanupPipeline implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $timeout = 1800; public $timeout = 1800;
public $tries = 5; public $tries = 5;
public $maxExceptions = 1; public $maxExceptions = 1;
public function handle() public function handle()
{ {
if(config_cache('pixelfed.cloud_storage') == false) { if ((bool) config_cache('pixelfed.cloud_storage') == false) {
// Only run if cloud storage is enabled // Only run if cloud storage is enabled
return; return;
} }
@ -30,40 +31,40 @@ class MediaFixLocalFilesystemCleanupPipeline implements ShouldQueue
$cloud = Storage::disk(config('filesystems.cloud')); $cloud = Storage::disk(config('filesystems.cloud'));
Media::whereNotNull(['status_id', 'cdn_url', 'replicated_at']) Media::whereNotNull(['status_id', 'cdn_url', 'replicated_at'])
->chunk(20, function ($medias) use($disk, $cloud) { ->chunk(20, function ($medias) use ($disk, $cloud) {
foreach($medias as $media) { foreach ($medias as $media) {
if(!str_starts_with($media->media_path, 'public')) { if (! str_starts_with($media->media_path, 'public')) {
continue; continue;
} }
if($disk->exists($media->media_path) && $cloud->exists($media->media_path)) { if ($disk->exists($media->media_path) && $cloud->exists($media->media_path)) {
$disk->delete($media->media_path); $disk->delete($media->media_path);
} }
if($media->thumbnail_path) { if ($media->thumbnail_path) {
if($disk->exists($media->thumbnail_path)) { if ($disk->exists($media->thumbnail_path)) {
$disk->delete($media->thumbnail_path); $disk->delete($media->thumbnail_path);
} }
} }
$paths = explode('/', $media->media_path); $paths = explode('/', $media->media_path);
if(count($paths) === 7) { if (count($paths) === 7) {
array_pop($paths); array_pop($paths);
$baseDir = implode('/', $paths); $baseDir = implode('/', $paths);
if(count($disk->allFiles($baseDir)) === 0) { if (count($disk->allFiles($baseDir)) === 0) {
$disk->deleteDirectory($baseDir); $disk->deleteDirectory($baseDir);
array_pop($paths); array_pop($paths);
$baseDir = implode('/', $paths); $baseDir = implode('/', $paths);
if(count($disk->allFiles($baseDir)) === 0) { if (count($disk->allFiles($baseDir)) === 0) {
$disk->deleteDirectory($baseDir); $disk->deleteDirectory($baseDir);
array_pop($paths); array_pop($paths);
$baseDir = implode('/', $paths); $baseDir = implode('/', $paths);
if(count($disk->allFiles($baseDir)) === 0) { if (count($disk->allFiles($baseDir)) === 0) {
$disk->deleteDirectory($baseDir); $disk->deleteDirectory($baseDir);
} }
} }

Some files were not shown because too many files have changed in this diff Show more