Skip to content
Merged
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
4 changes: 4 additions & 0 deletions packages/social-controllers/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- Add `unblockProfile` to `SocialService` (`SocialService:unblockProfile`). Calls `DELETE /v1/moderation/blocks/profiles/:profileId` to remove the current user's block on a trader; unblocking a trader who is not blocked is a no-op. Export `UnblockProfileOptions` and `SocialServiceUnblockProfileAction` ([#11159](https://github.com/MetaMask/core/pull/11159))

## [3.13.0]

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,22 @@ export type SocialServiceBlockAction = {
handler: SocialService['block'];
};

/**
* Removes the current user's block on a trader.
*
* Calls `DELETE ${baseUrl}/moderation/blocks/profiles/${profileId}`.
* Unblocking a trader who is not blocked is a no-op. The caller is
* identified server-side from the JWT sub claim carried in the
* Authorization header.
*
* @param options - Options bag.
* @param options.profileId - Profile id (UUID) of the trader to unblock.
*/
export type SocialServiceUnblockProfileAction = {
type: `SocialService:unblockProfile`;
handler: SocialService['unblockProfile'];
};

/**
* Fetches traders the current user has blocked.
*
Expand Down Expand Up @@ -491,5 +507,6 @@ export type SocialServiceMethodActions =
| SocialServiceOptInToLeaderboardAction
| SocialServiceRefreshNotificationPreferencesCacheAction
| SocialServiceBlockAction
| SocialServiceUnblockProfileAction
| SocialServiceFetchBlockedProfilesAction
| SocialServiceFetchBlockedContentAction;
68 changes: 68 additions & 0 deletions packages/social-controllers/src/SocialService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2932,6 +2932,74 @@ describe('SocialService', () => {
});
});

describe('unblockProfile', () => {
it('sends DELETE to /moderation/blocks/profiles/:profileId', async () => {
mockFetch.mockResolvedValue({ ok: true, status: 204 });

const service = createService();
const result = await service.unblockProfile({
profileId: mockProfileSummary.profileId,
});

expect(result).toBeUndefined();
expect(mockFetch).toHaveBeenCalledWith(
`${V1_URL}/moderation/blocks/profiles/${mockProfileSummary.profileId}`,
{
method: 'DELETE',
headers: { Authorization: `Bearer ${MOCK_TOKEN}` },
},
);
});

it('encodes the profile id in the URL path', async () => {
mockFetch.mockResolvedValue({ ok: true, status: 204 });

const service = createService();
await service.unblockProfile({ profileId: 'a/b c' });

expect(mockFetch).toHaveBeenCalledWith(
`${V1_URL}/moderation/blocks/profiles/a%2Fb%20c`,
expect.anything(),
);
});

it('throws HttpError on non-ok response', async () => {
mockFetch.mockResolvedValue({ ok: false, status: 400 });

const service = createService();

await expect(
service.unblockProfile({ profileId: mockProfileSummary.profileId }),
).rejects.toThrow(
`${SocialServiceErrorMessage.UNBLOCK_PROFILE_FAILED}: 400`,
);
});

it('always sends the request on repeated calls (staleTime: 0)', async () => {
mockFetch.mockResolvedValue({ ok: true, status: 204 });

const service = createService();
await service.unblockProfile({ profileId: mockProfileSummary.profileId });
await service.unblockProfile({ profileId: mockProfileSummary.profileId });

expect(mockFetch).toHaveBeenCalledTimes(2);
});

it('is callable via messenger action', async () => {
mockFetch.mockResolvedValue({ ok: true, status: 204 });

const messenger = createMessenger();
// eslint-disable-next-line no-new
new SocialService({ messenger, baseUrl: BASE_URL });

const result = await messenger.call('SocialService:unblockProfile', {
profileId: mockProfileSummary.profileId,
});

expect(result).toBeUndefined();
});
});

describe('fetchBlockedProfiles', () => {
const mockBlockedProfilesResponse = {
items: [
Expand Down
33 changes: 33 additions & 0 deletions packages/social-controllers/src/SocialService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import type {
SwapCommentResponse,
TranslateCommentOptions,
TraderProfileResponse,
UnblockProfileOptions,
UnfollowOptions,
UnfollowResponse,
} from './social-types.js';
Expand Down Expand Up @@ -95,6 +96,7 @@ const MESSENGER_EXPOSED_METHODS = [
'optInToLeaderboard',
'refreshNotificationPreferencesCache',
'block',
'unblockProfile',
'fetchBlockedProfiles',
'fetchBlockedContent',
] as const;
Expand Down Expand Up @@ -1176,6 +1178,37 @@ export class SocialService extends BaseDataService<
});
}

/**
* Removes the current user's block on a trader.
*
* Calls `DELETE ${baseUrl}/moderation/blocks/profiles/${profileId}`.
* Unblocking a trader who is not blocked is a no-op. The caller is
* identified server-side from the JWT sub claim carried in the
* Authorization header.
*
* @param options - Options bag.
* @param options.profileId - Profile id (UUID) of the trader to unblock.
*/
async unblockProfile({ profileId }: UnblockProfileOptions): Promise<void> {
await this.fetchQuery({
queryKey: [`${this.name}:unblockProfile`, profileId],
staleTime: 0,
queryFn: async () => {
const url = `${this.#v1Url}/moderation/blocks/profiles/${encodeURIComponent(profileId)}`;
const authHeaders = await this.#getAuthHeaders();
const response = await fetch(url, {
method: 'DELETE',
headers: authHeaders,
});
SocialService.#throwIfNotOk(
response,
SocialServiceErrorMessage.UNBLOCK_PROFILE_FAILED,
);
return null;
},
});
}

/**
* Fetches traders the current user has blocked.
*
Expand Down
2 changes: 2 additions & 0 deletions packages/social-controllers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export type {
SocialServiceTranslateCommentAction,
SocialServiceRefreshNotificationPreferencesCacheAction,
SocialServiceRemoveCommentReactionAction,
SocialServiceUnblockProfileAction,
SocialServiceUnfollowAction,
} from './SocialService-method-action-types.js';

Expand Down Expand Up @@ -138,6 +139,7 @@ export type {
TraderProfileResponse,
TraderRankingTag,
TraderStats,
UnblockProfileOptions,
UnfollowOptions,
UnfollowResponse,
} from './social-types.js';
Expand Down
1 change: 1 addition & 0 deletions packages/social-controllers/src/social-constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const SocialServiceErrorMessage = {
NOTIFICATION_PREFERENCES_CACHE_REFRESH_FAILED:
'SocialService: Notification preferences cache refresh request failed',
BLOCK_FAILED: 'SocialService: Block request failed',
UNBLOCK_PROFILE_FAILED: 'SocialService: Unblock profile request failed',
FETCH_BLOCKED_PROFILES_FAILED:
'SocialService: Blocked profiles request failed',
FETCH_BLOCKED_PROFILES_INVALID_RESPONSE:
Expand Down
8 changes: 8 additions & 0 deletions packages/social-controllers/src/social-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,14 @@ export type BlockOptions =
reason?: string;
};

/**
* Options for `DELETE /v1/moderation/blocks/profiles/:profileId`.
*/
export type UnblockProfileOptions = {
/** Profile id (UUID) of the trader to unblock. */
profileId: string;
};

/**
* Options for the cursor-paginated moderation list endpoints. Omit `cursor`
* for the first page.
Expand Down
Loading