Skip to content

Commit

Permalink
fix: update friendships endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
nsmle committed Sep 10, 2024
1 parent 3e4b81f commit 8b84346
Show file tree
Hide file tree
Showing 7 changed files with 194 additions and 51 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "igramapi",
"description": "Instagram private API wrapper for full access to instagram",
"version": "1.47.0",
"version": "1.48.3",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
Expand Down
132 changes: 101 additions & 31 deletions src/repositories/friendship.repository.ts
Original file line number Diff line number Diff line change
@@ -1,77 +1,136 @@
import { Repository } from '../core/repository';
import {
FriendshipRepositoryShowResponseRootObject,
FriendshipRepositoryChangeResponseRootObject,
FriendshipRepositoryRootResponseObject,
FriendshipRepositoryShowResponseObject,
FriendshipRepositorySetBestiesResponseRootObject,
FriendshipRepositoryShowManyResponseObject,
FriendshipRepositoryActionOptions,
FriendshipRepositoryBlockOptions,
FriendshipRepositoryUnBlockOptions,
FriendshipRepositoryCreateDestroyOptions,
FriendshipRepositoryBlockResponse,
FriendshipRepositoryUnBlockResponse,
} from '../responses';
import { SetBestiesInput } from '../types';

export class FriendshipRepository extends Repository {
async show(id: string | number) {
const { body } = await this.client.request.send<FriendshipRepositoryShowResponseRootObject>({
async show(id: string | number, isExternalDeeplinkProfileView: boolean = false) {
const { body } = await this.client.request.send<FriendshipRepositoryShowResponseObject>({
url: `/api/v1/friendships/show/${id}/`,
qs: {
is_external_deeplink_profile_view: isExternalDeeplinkProfileView,
},
});
return body;
}

async showMany(userIds: string[] | number[]) {
const { body } = await this.client.request.send({
async showMany<T extends string | number>(userIds: T[], includeFollowedBy: boolean = true) {
const { body } = await this.client.request.send<{
friendship_statuses: Record<T, FriendshipRepositoryShowManyResponseObject>;
}>({
url: `/api/v1/friendships/show_many/`,
method: 'POST',
form: {
_csrftoken: this.client.state.cookieCsrfToken,
include_followed_by: includeFollowedBy,
user_ids: userIds.join(),
_uuid: this.client.state.uuid,
},
});
return body.friendship_statuses;
}

async block(id: string | number, mediaIdAttribution?: string) {
return this.change('block', id, mediaIdAttribution);
async block(id: string | number, options?: FriendshipRepositoryBlockOptions) {
return this.action<FriendshipRepositoryBlockResponse>('block', id, {
user_id: String(id),
is_auto_block_enabled: String(options?.is_auto_block_enabled || true),
surface: options?.surface || 'profile',
container_module: options?.container_module || 'profile',
});
}

async unblock(id: string | number, mediaIdAttribution?: string) {
return this.change('unblock', id, mediaIdAttribution);
async unblock(id: string | number, options?: FriendshipRepositoryUnBlockOptions) {
return this.action<FriendshipRepositoryUnBlockResponse>('unblock', id, {
user_id: String(id),
container_module: options?.container_module || 'profile',
});
}

async create(id: string | number, mediaIdAttribution?: string) {
return this.change('create', id, mediaIdAttribution);
async create(id: string | number, options?: FriendshipRepositoryCreateDestroyOptions) {
return this.action('create', id, {
include_follow_friction_check: String(Number(options?.include_follow_friction_check || true)) as any,
user_id: String(id),
radio_type: this.client.state.radioType,
device_id: this.client.state.deviceId,
container_module: options?.container_module || 'profile',
...(options?.nav_chain && {
nav_chain: options.nav_chain,
}),
});
}

async destroy(id: string | number, mediaIdAttribution?: string) {
return this.change('destroy', id, mediaIdAttribution);
async destroy(id: string | number, options?: FriendshipRepositoryCreateDestroyOptions) {
return this.action('destroy', id, {
include_follow_friction_check: String(Number(options?.include_follow_friction_check || true)) as any,
user_id: String(id),
radio_type: this.client.state.radioType,
device_id: this.client.state.deviceId,
container_module: options?.container_module || 'profile',
...(options?.nav_chain && {
nav_chain: options.nav_chain,
}),
});
}

async approve(id: string | number, mediaIdAttribution?: string) {
return this.change('approve', id, mediaIdAttribution);
/**
* @todo Test approve requested frindship
* @deprecated This method is un-tested!
*/
async approve(id: string | number, options?: any) {
return this.action('approve', id, options);
}

async deny(id: string | number, mediaIdAttribution?: string) {
return this.change('ignore', id, mediaIdAttribution);
/**
* @todo Test ignore requested frindship
* @deprecated This method is un-tested!
*/
async deny(id: string | number, options?: any) {
return this.action('ignore', id, options);
}

async removeFollower(id: string | number) {
return this.change('remove_follower', id);
async removeFollower(id: string | number, options?: FriendshipRepositoryCreateDestroyOptions) {
return this.action('remove_follower', id, {
include_follow_friction_check: String(Number(options?.include_follow_friction_check || false)) as any,
user_id: String(id),
radio_type: this.client.state.radioType,
device_id: this.client.state.deviceId,
container_module: options?.container_module || 'self_followers',
...(options?.nav_chain && {
nav_chain: options.nav_chain,
}),
});
}

private async change(action: string, id: string | number, mediaIdAttribution?: string) {
const { body } = await this.client.request.send<FriendshipRepositoryChangeResponseRootObject>({
private async action<T = any>(
action: string,
id: string | number,
options?: FriendshipRepositoryActionOptions,
): Promise<T> {
const { body } = await this.client.request.send({
url: `/api/v1/friendships/${action}/${id}/`,
method: 'POST',
form: this.client.request.sign({
_csrftoken: this.client.state.cookieCsrfToken,
user_id: id,
radio_type: this.client.state.radioType,
_uid: this.client.state.cookieUserId,
device_id: this.client.state.deviceId,
_uid: this.client.state.igUserId,
_uuid: this.client.state.uuid,
media_id_attribution: mediaIdAttribution,
...options,
}),
});
return body.friendship_status;
}

/**
* @todo Test set frindship bestie
* @deprecated This method is un-tested!
*/
async setBesties(input: SetBestiesInput = {}) {
const { body } = await this.client.request.send<FriendshipRepositorySetBestiesResponseRootObject>({
url: `/api/v1/friendships/set_besties/`,
Expand All @@ -91,28 +150,39 @@ export class FriendshipRepository extends Repository {
return body.friendship_statuses;
}

/**
* @todo Refactor
* @deprecated This method is un-tested!
*/
mutePostsOrStoryFromFollow(options: {
mediaId?: string;
targetReelAuthorId?: string;
targetPostsAuthorId?: string;
}): Promise<FriendshipRepositoryChangeResponseRootObject> {
}): Promise<FriendshipRepositoryRootResponseObject> {
return this.changeMuteFromFollow('mute', {
media_id: options.mediaId,
target_reel_author_id: options.targetReelAuthorId,
target_posts_author_id: options.targetPostsAuthorId,
});
}

/**
* @todo Refactor
* @deprecated This method is un-tested!
*/
unmutePostsOrStoryFromFollow(options: {
targetReelAuthorId?: string;
targetPostsAuthorId?: string;
}): Promise<FriendshipRepositoryChangeResponseRootObject> {
}): Promise<FriendshipRepositoryRootResponseObject> {
return this.changeMuteFromFollow('unmute', {
target_reel_author_id: options.targetReelAuthorId,
target_posts_author_id: options.targetPostsAuthorId,
});
}

/**
* @todo Refactor
*/
private async changeMuteFromFollow(mode: 'mute' | 'unmute', options: Record<string, any>) {
const { body } = await this.client.request.send({
url: `/api/v1/friendships/${mode}_posts_or_story_from_follow/`,
Expand Down
36 changes: 24 additions & 12 deletions src/responses/friendship.repository.change.response.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
export interface FriendshipRepositoryChangeResponseRootObject {
friendship_status: FriendshipRepositoryChangeResponseFriendship_status;
status: string;
export interface FriendshipRepositoryActionOptions {
source?: 'profile' | 'self_followers' | 'audience_manager';
container_module?: 'profile' | 'self_followers' | 'favorites_home_list';
surface?: 'profile' | 'self_followers';
is_auto_block_enabled?: string; // String of boolean
include_follow_friction_check?: '1' | '0';
nav_chain?: string;
user_id?: string | number;
device_id?: string;
radio_type?: string;
}

export interface FriendshipRepositoryChangeResponseFriendship_status {
following: boolean;
followed_by: boolean;
blocking: boolean;
muting: boolean;
is_private: boolean;
incoming_request: boolean;
outgoing_request: boolean;
is_bestie: boolean;
export interface FriendshipRepositoryBlockOptions {
is_auto_block_enabled?: boolean;
surface?: 'profile' | 'self_followers';
container_module?: 'profile' | 'self_followers' | 'favorites_home_list';
}

export interface FriendshipRepositoryUnBlockOptions {
container_module?: 'profile' | 'self_followers' | 'favorites_home_list';
}

export interface FriendshipRepositoryCreateDestroyOptions {
include_follow_friction_check?: boolean;
container_module?: 'profile' | 'self_followers' | 'favorites_home_list';
nav_chain?: string;
}
22 changes: 22 additions & 0 deletions src/responses/friendship.repository.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export interface FriendshipRepositorFriendshipStatusResponseObject {
blocking: boolean;
followed_by: boolean;
following: boolean;
incoming_request: boolean;
is_bestie: boolean;
is_eligible_to_subscribe: boolean;
is_feed_favorite: boolean;
is_private: boolean;
is_restricted: boolean;
muting: boolean;
outgoing_request: boolean;
subscribed: boolean;
}

export interface FriendshipRepositoryRootResponseObject {
friendship_status: FriendshipRepositorFriendshipStatusResponseObject;
status: string;
}

export interface FriendshipRepositoryBlockResponse extends FriendshipRepositorFriendshipStatusResponseObject {}
export interface FriendshipRepositoryUnBlockResponse extends FriendshipRepositorFriendshipStatusResponseObject {}
18 changes: 18 additions & 0 deletions src/responses/friendship.repository.show-many.response.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export interface FriendshipRepositoryShowManyResponseRootObject {
following: boolean;
incoming_request: boolean;
is_bestie: boolean;
is_private: boolean;
is_restricted: boolean;
outgoing_request: boolean;
is_feed_favorite: boolean;
}

export interface FriendshipRepositoryShowManyResponseFollowedByObject
extends FriendshipRepositoryShowManyResponseRootObject {
followed_by: boolean;
}

export type FriendshipRepositoryShowManyResponseObject =
| FriendshipRepositoryShowManyResponseRootObject
| FriendshipRepositoryShowManyResponseFollowedByObject;
31 changes: 25 additions & 6 deletions src/responses/friendship.repository.show.response.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
export interface FriendshipRepositoryShowResponseRootObject {
following: boolean;
followed_by: boolean;
blocking: boolean;
muting: boolean;
is_private: boolean;
followed_by: boolean;
following: boolean;
incoming_request: boolean;
outgoing_request: boolean;
is_bestie: boolean;
is_blocking_reel: boolean;
is_eligible_to_subscribe: boolean;
is_feed_favorite: boolean;
is_guardian_of_viewer: boolean;
is_muting_media_notes: boolean;
is_muting_notes: boolean;
is_muting_reel: boolean;
is_bestie: boolean;
is_private: boolean;
is_restricted: boolean;
is_supervised_by_viewer: boolean;
is_viewer_unconnected: boolean;
muting: boolean;
outgoing_request: boolean;
status: string;
subscribed: boolean;
}

export interface FriendshipRepositoryShowResponseExternalDeeplinkProfileObject
extends FriendshipRepositoryShowResponseRootObject {
should_show_profile_upsell: boolean;
is_banner_profile_upsell: boolean;
}

export type FriendshipRepositoryShowResponseObject =
| FriendshipRepositoryShowResponseRootObject
| FriendshipRepositoryShowResponseExternalDeeplinkProfileObject;
4 changes: 3 additions & 1 deletion src/responses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export * from './direct-thread.repository.update-title.response';
export * from './direct-thread.repository.approve-participant-request.response';
export * from './fbsearch.repository.places.response';
export * from './fbsearch.repository.topsearch-flat.response';
export * from './friendship.repository.change.response';
export * from './friendship.repository.besties.response';
export * from './friendship.repository.response';
export * from './friendship.repository.change.response';
export * from './friendship.repository.show.response';
export * from './friendship.repository.show-many.response';
export * from './live.comments.response';
export * from './live.create-broadcast.response';
export * from './live.final-viewers.response';
Expand Down

0 comments on commit 8b84346

Please sign in to comment.