diff --git a/KNOWLEDGE_BASE.md b/KNOWLEDGE_BASE.md index a62f58c0d4..1cbba9edc8 100644 --- a/KNOWLEDGE_BASE.md +++ b/KNOWLEDGE_BASE.md @@ -137,7 +137,7 @@ Its admins and members can **post as the organization**: in the composer, and in Every organization has a color, /dev/null's blue until an admin chooses another in the **Profile** tab of its **Settings**, where its admins land: one of the same six lane colors as developers, or a custom one. It rings the organization's avatar, leads its handle, and colors its link preview. -Signed in, you can **Follow** an organization from its page, which shows how many people follow it. Once you follow it, the button opens a menu where you can unfollow. Following puts the organization's own commits in your Following log, and ranks them higher in Trending, like the people you follow. **Watch**, which will also show the commits that mention the organization, is planned and shown as coming soon. The organizations you follow are listed on your Following list, above the people, a few at a time with **Show more organizations**, and count toward your following count. They are in your data export and go when you delete your account. +Signed in, you can **Follow** an organization from its page, which shows how many people follow it, or from the **…** menu on any of its commits, which also unfollows it. Once you follow it, the button opens a menu where you can unfollow. Following puts the organization's own commits in your Following log, and ranks them higher in Trending, like the people you follow. **Watch**, which will also show the commits that mention the organization, is planned and shown as coming soon. The organizations you follow are listed on your Following list, above the people, a few at a time with **Show more organizations**, and count toward your following count. They are in your data export and go when you delete your account. Sources: [organization page](resources/js/pages/organization/show.tsx), [commits](app/Queries/OrganizationCommitsQuery.php), [posting as an organization](app/Rules/OrganizationToPostAs.php), [mentions](app/Queries/OrganizationMentionsQuery.php), [organization lookups](app/Actions/UpdateCommitTopics.php), [claims](app/Http/Controllers/OrganizationAdminController.php), [color](app/Http/Controllers/OrganizationLaneController.php), [members](app/Http/Controllers/OrganizationMemberController.php). diff --git a/app/Http/Controllers/OrganizationFollowController.php b/app/Http/Controllers/OrganizationFollowController.php index 181af6ff8f..07d9beb2fe 100644 --- a/app/Http/Controllers/OrganizationFollowController.php +++ b/app/Http/Controllers/OrganizationFollowController.php @@ -11,10 +11,21 @@ use App\Models\Organization; use App\Models\User; use Illuminate\Container\Attributes\CurrentUser; +use Illuminate\Http\JsonResponse; use Illuminate\Http\RedirectResponse; final readonly class OrganizationFollowController { + /** + * Whether the user follows the organization, which commits from it don't carry, for the commit menu to ask once it opens. + */ + public function show(#[CurrentUser] User $user, Organization $organization): JsonResponse + { + return response()->json([ + 'isFollowing' => $user->followedOrganizations()->newPivotQuery()->where('organization_id', $organization->id)->exists(), + ]); + } + public function store(CreateOrganizationFollowRequest $request, #[CurrentUser] User $user, Organization $organization, CreateOrganizationFollow $action): RedirectResponse { $action->handle($user, $organization); diff --git a/resources/js/components/commit-secondary-actions.tsx b/resources/js/components/commit-secondary-actions.tsx index 1c11f811ee..f9a65af11c 100644 --- a/resources/js/components/commit-secondary-actions.tsx +++ b/resources/js/components/commit-secondary-actions.tsx @@ -12,10 +12,11 @@ import { Volume2, VolumeX, } from 'lucide-react'; -import { useRef, useState } from 'react'; +import { useEffect, useRef, useState } from 'react'; import type { Ref } from 'react'; import CommitController from '@/actions/App/Http/Controllers/CommitController'; import ConversationController from '@/actions/App/Http/Controllers/ConversationController'; +import OrganizationFollowController from '@/actions/App/Http/Controllers/OrganizationFollowController'; import UserBlockController from '@/actions/App/Http/Controllers/UserBlockController'; import UserFollowController from '@/actions/App/Http/Controllers/UserFollowController'; import UserMuteController from '@/actions/App/Http/Controllers/UserMuteController'; @@ -27,8 +28,10 @@ import { useCopyLink } from '@/components/copy-link-button'; import { followVisitOptions, onFollowHttpException, + prepareFollowingUpdate, useIsFollowing, } from '@/components/follow-button'; +import { useOrganizationFollow } from '@/components/organization-follow-button'; import { QuotedCommit } from '@/components/quoted-commit'; import { SearchDialog } from '@/components/search-dialog'; import { Button } from '@/components/ui/button'; @@ -64,8 +67,14 @@ import { } from '@/components/user-safety-menu'; import { signIn } from '@/lib/auth'; import { sendMessage } from '@/lib/chat'; +import { toastWhenThrottled } from '@/lib/throttle'; import { cn } from '@/lib/utils'; -import type { Commit, CursorPaginated, User } from '@/types'; +import type { + Commit, + CursorPaginated, + OrganizationAuthor, + User, +} from '@/types'; type PendingAction = 'send' | 'block' | 'report' | 'delete'; @@ -339,6 +348,12 @@ function CommitMoreMenu({ /> )} + {author.type === 'organization' && ( + <> + + + + )} {(canReport || canDelete) && } {canReport && ( ); } + +/** + * Commits don't say whether you follow the organization they are from, so the item asks once the menu opens, and waits + * for the answer before offering to follow or unfollow it. The answer updates every follow control for the organization, + * unless a follow was clicked while it was out. A failed read leaves the item disabled, and reopening the menu asks again. + */ +function CommitOrganizationItems({ + organization, +}: { + organization: OrganizationAuthor; +}) { + const { + component, + props: { auth, log, commits }, + } = usePage<{ commits?: CursorPaginated }>(); + const isSignedIn = auth.user !== null; + const [isLoaded, setIsLoaded] = useState(false); + const isLoading = isSignedIn && !isLoaded; + const { isFollowing, toggle } = useOrganizationFollow( + organization, + false, + followVisitOptions(component, log, commits), + ); + + const { id } = organization; + const url = OrganizationFollowController.show(organization).url; + + useEffect(() => { + if (!isSignedIn) { + return; + } + + const load = () => { + const update = prepareFollowingUpdate({ id }, load); + + fetch(url, { + headers: { + Accept: 'application/json', + 'X-Requested-With': 'XMLHttpRequest', + }, + }) + .then(async (response) => { + if (!response.ok) { + toastWhenThrottled(response); + + return; + } + + const { isFollowing } = (await response.json()) as { + isFollowing: unknown; + }; + + if (typeof isFollowing === 'boolean') { + update(isFollowing); + setIsLoaded(true); + } + }) + .catch(() => undefined); + }; + + load(); + }, [id, url, isSignedIn]); + + return ( + + {isFollowing ? : } + + {isFollowing ? 'Unfollow' : 'Follow'} @{organization.handle} + + + ); +} diff --git a/resources/js/components/organization-follow-button.tsx b/resources/js/components/organization-follow-button.tsx index 84db0456e1..eb4a2344bf 100644 --- a/resources/js/components/organization-follow-button.tsx +++ b/resources/js/components/organization-follow-button.tsx @@ -1,3 +1,4 @@ +import type { VisitOptions } from '@inertiajs/core'; import { router, usePage } from '@inertiajs/react'; import { ChevronDown } from 'lucide-react'; import type { ComponentProps } from 'react'; @@ -28,11 +29,11 @@ type FollowedOrganization = Pick & { id: string }; * following people. Each follow waits for the one before it to answer, so a double click can't follow and then * unfollow. */ -function useOrganizationFollow( +export function useOrganizationFollow( organization: FollowedOrganization, initialIsFollowing: boolean, - only: string[], - onToggled: () => void, + options: Pick, + onToggled: () => void = () => undefined, ) { const { isFollowing, callbacks } = useFollow( organization, @@ -45,7 +46,7 @@ function useOrganizationFollow( ? OrganizationFollowController.destroy(organization) : OrganizationFollowController.store(organization), { - only, + ...options, preserveState: true, preserveScroll: true, async: true, @@ -90,7 +91,7 @@ export function OrganizationFollowButton({ const { isFollowing, toggle } = useOrganizationFollow( organization, initialIsFollowing, - only, + { only }, () => requestAnimationFrame(() => { if (document.activeElement === document.body) { diff --git a/routes/web.php b/routes/web.php index 13b13548d1..818d71a383 100644 --- a/routes/web.php +++ b/routes/web.php @@ -718,6 +718,10 @@ ->name('pending-follow.destroy'); // Organization Follow... + Route::get('organizations/{organization}/follow', [OrganizationFollowController::class, 'show']) + ->whereUuid('organization') + ->middleware('throttle:people') + ->name('organization-follow.show'); Route::post('organizations/{organization}/follow', [OrganizationFollowController::class, 'store']) ->whereUuid('organization') ->middleware('throttle:follows') diff --git a/tests/Feature/Controllers/OrganizationFollowControllerTest.php b/tests/Feature/Controllers/OrganizationFollowControllerTest.php index 097b6d4394..0077bad542 100644 --- a/tests/Feature/Controllers/OrganizationFollowControllerTest.php +++ b/tests/Feature/Controllers/OrganizationFollowControllerTest.php @@ -56,3 +56,24 @@ expect($organization->followers()->count())->toBe(0); }); + +it('tells the user whether they follow an organization', function (): void { + $user = User::factory()->create(); + $followed = Organization::factory()->create(); + $other = Organization::factory()->create(); + $user->followedOrganizations()->attach($followed); + + $this->actingAs($user) + ->getJson(route('organization-follow.show', $followed)) + ->assertOk() + ->assertExactJson(['isFollowing' => true]); + + $this->actingAs($user) + ->getJson(route('organization-follow.show', $other)) + ->assertOk() + ->assertExactJson(['isFollowing' => false]); +}); + +it('tells guests nothing about follows', function (): void { + $this->getJson(route('organization-follow.show', Organization::factory()->create()))->assertUnauthorized(); +});