Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#941): collection and collections for saved feed #1684

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
10 changes: 10 additions & 0 deletions src/core/feed.factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
ReelsMediaFeed,
ReelsTrayFeed,
SavedFeed,
SavedCollectionsFeed,
StoriesInsightsFeed,
TagFeed,
TagsFeed,
Expand All @@ -36,6 +37,7 @@ import { plainToClassFromExist } from 'class-transformer';
import * as Chance from 'chance';
import { PostsInsightsFeedOptions, TimelineFeedReason, IgAppModule } from '../types';
import { UserStoryFeed } from '../feeds/user-story.feed';
import { SavedCollectionFeed } from '../feeds';
import { ListReelMediaViewerFeed } from '../feeds/list-reel-media-viewer.feed';
import { MediaInlineChildCommentsFeed } from '../feeds/media.inline-child-comments.feed';
import { MediaStickerResponsesFeed } from '../feeds/media.sticker-responses.feed';
Expand Down Expand Up @@ -219,6 +221,14 @@ export class FeedFactory {
return new SavedFeed(this.client);
}

public savedCollections(): SavedCollectionsFeed {
return new SavedCollectionsFeed(this.client);
}

public savedCollection(collectionId: string | number) {
return plainToClassFromExist(new SavedCollectionFeed(this.client), { collectionId });
}

public listReelMediaViewers(mediaId: string): ListReelMediaViewerFeed {
return plainToClassFromExist(new ListReelMediaViewerFeed(this.client), { mediaId });
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ export class State {
const obj = typeof state === 'string' ? JSON.parse(state) : state;
if (typeof obj !== 'object') {
State.stateDebug(`State deserialization failed, obj is of type ${typeof obj} (object expected)`);
throw new TypeError('State isn\'t an object or serialized JSON');
throw new TypeError("State isn't an object or serialized JSON");
}
State.stateDebug(`Deserializing ${Object.keys(obj).join(', ')}`);
if (obj.constants) {
Expand Down
2 changes: 2 additions & 0 deletions src/feeds/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export * from './media-comments.feed';
export * from './news.feed';
export * from './reels-media.feed';
export * from './saved.feed';
export * from './saved-collections.feed';
export * from './saved-collection.feed';
export * from './tag.feed';
export * from './tags.feed';
export * from './timeline.feed';
Expand Down
31 changes: 31 additions & 0 deletions src/feeds/saved-collection.feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Expose } from 'class-transformer';
import { Feed } from '../core/feed';
import { SavedCollectionFeedResponseRootObject, SavedFeedResponseMedia } from '../responses';

export class SavedCollectionFeed extends Feed<SavedCollectionFeedResponseRootObject, SavedFeedResponseMedia> {
collectionId: number | string;
@Expose()
private nextMaxId: string;

set state(body: SavedCollectionFeedResponseRootObject) {
this.moreAvailable = body.more_available;
this.nextMaxId = body.next_max_id;
}

async request(): Promise<SavedCollectionFeedResponseRootObject> {
const { body } = await this.client.request.send({
url: `api/v1/feed/collection/${this.collectionId}/posts`,
qs: {
max_id: this.nextMaxId,
include_igtv_preview: false,
},
});
this.state = body;
return body;
}

async items(): Promise<SavedFeedResponseMedia[]> {
const { items } = await this.request();
return items.map(item => item.media);
}
}
41 changes: 41 additions & 0 deletions src/feeds/saved-collections.feed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Expose } from 'class-transformer';
import { Feed } from '../core/feed';
import {
SavedCollectionsFeedResponseRootObject,
SavedCollectionsFeedResponse,
SavedCollectionCollectionType,
} from '../responses';

export class SavedCollectionsFeed extends Feed<SavedCollectionsFeedResponseRootObject, SavedCollectionsFeedResponse> {
@Expose()
private nextMaxId: string;

set state(body: SavedCollectionsFeedResponseRootObject) {
this.moreAvailable = body.more_available;
this.nextMaxId = body.next_max_id;
}

async request(): Promise<SavedCollectionsFeedResponseRootObject> {
const { body } = await this.client.request.send({
url: 'api/v1/collections/list',
qs: {
max_id: this.nextMaxId,
include_igtv_preview: false,
include_public_only: 0,
collection_types: JSON.stringify([
SavedCollectionCollectionType.AllMediaAutoCollection,
SavedCollectionCollectionType.AudioAutoCollection,
SavedCollectionCollectionType.Media,
]),
get_cover_media_lists: true,
},
});
this.state = body;
return body;
}

async items(): Promise<SavedCollectionsFeedResponse[]> {
const { items } = await this.request();
return items.map(i => i);
}
}
5 changes: 4 additions & 1 deletion src/feeds/topical-explore.feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import * as Chance from 'chance';
import { TopicalExploreFeedResponseRootObject, TopicalExploreFeedResponseSectionalItemsItem } from '../responses';
const chance = new Chance();

export class TopicalExploreFeed extends Feed<TopicalExploreFeedResponseRootObject, TopicalExploreFeedResponseSectionalItemsItem> {
export class TopicalExploreFeed extends Feed<
TopicalExploreFeedResponseRootObject,
TopicalExploreFeedResponseSectionalItemsItem
> {
module: IgAppModule = 'explore_popular';
lat?: string | number;
lng?: string | number;
Expand Down
23 changes: 14 additions & 9 deletions src/repositories/account.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class AccountRepository extends Repository {
if (!this.client.state.passwordEncryptionPubKey) {
await this.client.qe.syncLoginExperiments();
}
const {encrypted, time} = this.encryptPassword(password);
const { encrypted, time } = this.encryptPassword(password);
const response = await Bluebird.try(() =>
this.client.request.send<AccountRepositoryLoginResponseRootObject>({
method: 'POST',
Expand Down Expand Up @@ -76,14 +76,17 @@ export class AccountRepository extends Repository {
return `2${sum}`;
}

public encryptPassword(password: string): { time: string, encrypted: string } {
public encryptPassword(password: string): { time: string; encrypted: string } {
const randKey = crypto.randomBytes(32);
const iv = crypto.randomBytes(12);
const rsaEncrypted = crypto.publicEncrypt({
key: Buffer.from(this.client.state.passwordEncryptionPubKey, 'base64').toString(),
// @ts-ignore
padding: crypto.constants.RSA_PKCS1_PADDING,
}, randKey);
const rsaEncrypted = crypto.publicEncrypt(
{
key: Buffer.from(this.client.state.passwordEncryptionPubKey, 'base64').toString(),
// @ts-ignore
padding: crypto.constants.RSA_PKCS1_PADDING,
},
randKey,
);
const cipher = crypto.createCipheriv('aes-256-gcm', randKey, iv);
const time = Math.floor(Date.now() / 1000).toString();
cipher.setAAD(Buffer.from(time));
Expand All @@ -97,8 +100,10 @@ export class AccountRepository extends Repository {
Buffer.from([1, this.client.state.passwordEncryptionKeyId]),
iv,
sizeBuffer,
rsaEncrypted, authTag, aesEncrypted])
.toString('base64'),
rsaEncrypted,
authTag,
aesEncrypted,
]).toString('base64'),
};
}

Expand Down
17 changes: 7 additions & 10 deletions src/repositories/media.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -682,10 +682,10 @@ export class MediaRepository extends Repository {
* save a media, or save it to collection if you pass the collection ids in array
* @param {string} mediaId - The mediaId of the post
* @param {string[]} [collection_ids] - Optional, The array of collection ids if you want to save the media to a specific collection
* Example:
* Example:
* save("2524149952724070925_1829855275") save media
* save("2524149952724070925_1829855275", ["17865977635619975"]) save media to 1 collection
* save("2524149952724070925_1829855275", ["17865977635619975", "17845997638619928"]) save media to 2 collection
* save("2524149952724070925_1829855275", ["17865977635619975", "17845997638619928"]) save media to 2 collection
*/
public async save(mediaId: string, collection_ids?: string[]) {
const { body } = await this.client.request.send({
Expand Down Expand Up @@ -795,27 +795,24 @@ export class MediaRepository extends Repository {
});
return body;
}

private async storyCountdownAction(
countdownId: string | number,
action: string,
): Promise<StatusResponse> {

private async storyCountdownAction(countdownId: string | number, action: string): Promise<StatusResponse> {
const { body } = await this.client.request.send({
url: `/api/v1/media/${countdownId}/${action}/`,
method: 'POST',
form: this.client.request.sign({
_csrftoken: this.client.state.cookieCsrfToken,
_uid: this.client.state.cookieUserId,
_uuid: this.client.state.uuid
_uuid: this.client.state.uuid,
}),
});
return body;
}

public async storyCountdownFollow(countdownId: string | number) {
return this.storyCountdownAction(countdownId, 'follow_story_countdown');
}

public async storyCountdownUnfollow(countdownId: string | number) {
return this.storyCountdownAction(countdownId, 'unfollow_story_countdown');
}
Expand Down
2 changes: 2 additions & 0 deletions src/responses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ export * from './media.repository.configure.response';
export * from './media.repository.configure-sidecar.response';
export * from './media.repository.configure-video.response';
export * from './saved.feed.response';
export * from './saved-collections.feed.response';
export { SavedCollectionFeedResponseRootObject } from './saved-collection.feed.response';
export * from './status.response';
export * from './reels-tray.feed.response';
export * from './music.repository.moods.response';
Expand Down
Loading