Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion KNOWLEDGE_BASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
11 changes: 11 additions & 0 deletions app/Http/Controllers/OrganizationFollowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
98 changes: 96 additions & 2 deletions resources/js/components/commit-secondary-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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';

Expand Down Expand Up @@ -339,6 +348,12 @@ function CommitMoreMenu({
/>
</>
)}
{author.type === 'organization' && (
<>
<DropdownMenuSeparator />
<CommitOrganizationItems organization={author} />
</>
)}
{(canReport || canDelete) && <DropdownMenuSeparator />}
{canReport && (
<DropdownMenuItem
Expand Down Expand Up @@ -508,3 +523,82 @@ function CommitAuthorItems({
</>
);
}

/**
* 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<Commit> }>();
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 (
<DropdownMenuItem
disabled={isLoading}
onSelect={isSignedIn ? toggle : signIn}
data-test="follow-commit-organization"
>
{isFollowing ? <UserCheck /> : <UserPlus />}
<span
className="min-w-0 truncate"
title={`@${organization.handle}`}
>
{isFollowing ? 'Unfollow' : 'Follow'} @{organization.handle}
</span>
</DropdownMenuItem>
);
}
11 changes: 6 additions & 5 deletions resources/js/components/organization-follow-button.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -28,11 +29,11 @@ type FollowedOrganization = Pick<Organization, 'handle'> & { 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<VisitOptions, 'only' | 'headers'>,
onToggled: () => void = () => undefined,
) {
const { isFollowing, callbacks } = useFollow(
organization,
Expand All @@ -45,7 +46,7 @@ function useOrganizationFollow(
? OrganizationFollowController.destroy(organization)
: OrganizationFollowController.store(organization),
{
only,
...options,
preserveState: true,
preserveScroll: true,
async: true,
Expand Down Expand Up @@ -90,7 +91,7 @@ export function OrganizationFollowButton({
const { isFollowing, toggle } = useOrganizationFollow(
organization,
initialIsFollowing,
only,
{ only },
() =>
requestAnimationFrame(() => {
if (document.activeElement === document.body) {
Expand Down
4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
21 changes: 21 additions & 0 deletions tests/Feature/Controllers/OrganizationFollowControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});