Skip to content

Commit

Permalink
feat(comments): add 'from' and 'parent_id' fields to comment data (#395)
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoGrosso authored Aug 6, 2024
1 parent 8815969 commit 1a0cb14
Show file tree
Hide file tree
Showing 9 changed files with 660 additions and 772 deletions.
1,197 changes: 428 additions & 769 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,16 @@
"@semantic-release/git": "^10.0.1",
"@types/jest": "^29.5.12",
"@types/retry": "^0.12.2",
"@types/uuid": "^10.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"axios-mock-adapter": "^1.19.0",
"create-ts-index": "^1.14.0",
"dotenv": "^16.0.3",
"eslint": "^8.27.0",
"eslint-config-google": "^0.14.0",
"jest": "^29.7.0",
"husky": "^9.0.11",
"jest": "^29.7.0",
"prettier": "3.3.3",
"retry": "^0.13.1",
"rollup": "^2.52.2",
Expand All @@ -58,7 +59,8 @@
"ts-jest": "^29.1.4",
"ts-node": "^10.9.1",
"typedoc": "^0.26.3",
"typescript": "^5.5.3"
"typescript": "^5.5.3",
"uuid": "^10.0.0"
},
"directories": {
"test": "test"
Expand Down
50 changes: 49 additions & 1 deletion src/it/TestEnv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as dotenv from 'dotenv';
import { Client } from '../main/Client';
import { ApiVersion, MediaTypeInResponses } from '../main/Enums';
import { ApiVersion, CommentField, MediaTypeInResponses, PublicMediaField } from '../main/Enums';

dotenv.config();

Expand Down Expand Up @@ -69,3 +69,51 @@ export function getRandomPhoto(): Media {
export function getRandomVideo(): Media {
return randomInArray(videos);
}

let publishedMedia: string | undefined;
export async function getPublishedMedia(): Promise<string> {
if (!publishedMedia) {
const ids = await getClient()
.newGetPageMediaRequest(PublicMediaField.ID)
.execute()
.then((res) => res.getIds());
if (ids.length === 0) {
throw new Error('No published media found');
}
publishedMedia = ids[0];
}
return publishedMedia;
}

let publishedComment: string | undefined;
export async function getPublishedComment(): Promise<string> {
const media = await getPublishedMedia();
if (!publishedComment) {
const ids = await getClient()
.newGetMediaCommentsRequest(media, CommentField.ID)
.execute()
.then((res) => res.getIds());
if (ids.length === 0) {
throw new Error('No published comment found');
}
publishedComment = ids[0];
}
return publishedComment;
}

let replyComment: string | undefined;
export async function getReplyComment(): Promise<string> {
const comment = await getPublishedComment();
if (!replyComment) {
const replies = await getClient()
.newGetCommentRequest(comment, CommentField.REPLIES)
.execute()
.then((res) => res.getReplies());

if (!replies || replies.length === 0) {
throw new Error('No reply found');
}
replyComment = replies[0].id;
}
return replyComment;
}
122 changes: 122 additions & 0 deletions src/it/page/Comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { CommentField } from '../../main/Enums';
import { CommentData } from '../../main/requests/data/CommentData';
import { getClient, getPublishedComment, getPublishedMedia, getReplyComment } from '../TestEnv';
import { v4 as uuidv4 } from 'uuid';

/*
* Tests comment related requests.
* These tests are very coupled (😔) and to ensure they work they have to be all run in sequence (😔😔)
* Running the "post" ones once should allow others to then be run until other media gets published.
*
* It kinda sucks but the alternatives are:
* - These tests not covering as much as they do
* - A harder setup for devs picking up this project for the first time
*/
describe('Comments', () => {
it('Posts a comment', async () => {
const selectedMedia = await getPublishedMedia();
const comment = uuidv4();
const request = getClient().newPostMediaCommentRequest(selectedMedia, comment);

const result = await request.execute();
expect(result.getId()).toBeDefined();
});

it('Posts a reply', async () => {
const parentComment = await getPublishedComment();
const reply = uuidv4();
const request = getClient().newPostReplyRequest(parentComment, reply);

const result = await request.execute();
expect(result.getId()).toBeDefined();
});

it('Gets page media comments with all the fields', async () => {
const selectedMedia = await getPublishedMedia();
const request = getClient().newGetMediaCommentsRequest(selectedMedia);

const result = await request.execute();
const first: CommentData = result.getData()[0];

expect(first).toBeDefined();

Object.values(CommentField)
.filter((field) => field != CommentField.PARENT_ID)
.forEach((field) => {
expect(first[field]).toBeDefined();
});
expect(first.id).toBeDefined();
expect(result.getIds()).toContainEqual(await getPublishedComment());
});

const fields = Object.values(CommentField).filter(
(field) => ![CommentField.ID, CommentField.PARENT_ID].includes(field)
);
it.each(fields)(`Gets page media comments with only $s`, async (field) => {
const selectedMedia = await getPublishedMedia();
const request = getClient().newGetMediaCommentsRequest(selectedMedia, field);

const result = await request.execute();
const first: CommentData = result.getData()[0];

Object.values(CommentField)
.filter(
(expectedField) =>
![
field,
CommentField.ID, // Always returned
].includes(expectedField)
)
.forEach((expectedField) => expect(first[expectedField]).toBeUndefined());

expect(first.id).toBeDefined();
expect(first[field]).toBeDefined();
});

it('Gets comment with all the fields', async () => {
const comment = await getPublishedComment();
const request = getClient().newGetCommentRequest(comment);

const result = await request.execute().then((res) => res.getData());

expect(result).toBeDefined();
Object.values(CommentField)
.filter((field) => field != CommentField.PARENT_ID)

.forEach((field) => {
expect(result[field]).toBeDefined();
});
expect(result.id).toEqual(comment);
expect(result.replies!.data.map((reply) => reply.id)).toContain(await getReplyComment());
});

it.each(fields)(`Gets comment with only $s`, async (field) => {
const comment = await getPublishedComment();
const request = getClient().newGetCommentRequest(comment, field);

const result = await request.execute().then((res) => res.getData());

Object.values(CommentField)
.filter(
(expectedField) =>
![
field,
CommentField.ID, // Always returned
].includes(expectedField)
)
.forEach((expectedField) => expect(result[expectedField]).toBeUndefined());

expect(result.id).toBeDefined();
expect(result[field]).toBeDefined();
});

it('Gets reply parent id', async () => {
const reply = await getReplyComment();
const request = getClient().newGetCommentRequest(reply, CommentField.PARENT_ID);

const result = await request.execute();

expect(result).toBeDefined();
expect(result.getParentId()).toEqual(await getPublishedComment());
});
});
10 changes: 10 additions & 0 deletions src/main/Enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,16 @@ export enum CommentField {
* The username of the user who made the comment.
*/
USERNAME = 'username',

/**
* The information about the comment owner.
*/
FROM = 'from',

/**
* The ID of the parent IG Comment if this comment was created on another IG Comment (i.e. a reply to another comment).
*/
PARENT_ID = 'parent_id',
}

/**
Expand Down
18 changes: 18 additions & 0 deletions src/main/requests/comment/GetCommentResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,22 @@ export class GetCommentResponse extends AbstractResponse<CommentData> {
public getUsername(): string | undefined {
return this.data.username;
}

/**
* Gets the "from" object of the comment.
*
* @returns the "from" object of the comment.
*/
public getFrom(): { id: string; username: string } | undefined {
return this.data.from;
}

/**
* Gets the id of the parent of the comment (if any).
*
* @returns the id of the parent of the comment (if any).
*/
public getParentId(): string | undefined {
return this.data.parent_id;
}
}
20 changes: 20 additions & 0 deletions src/main/requests/data/CommentData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ export interface CommentData {
* The username of the user who made the comment.
*/
username?: string;

/**
* The information about the comment creator
*/
from?: {
/**
* The IGSID of the comment creator.
*/
id: string;

/**
* The username of the comment creator.
*/
username: string;
};

/**
* The ID of the parent IG Comment if this comment was created on another IG Comment (i.e. a reply to another comment).
*/
parent_id?: string;
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/test/TestConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ export class TestConstants {
id: 'user_id',
},
username: 'username',
from: {
id: 'from_user_id',
username: 'from_username',
},
},
];

Expand Down
5 changes: 5 additions & 0 deletions src/test/requests/comment/GetCommentResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,9 @@ describe('GetCommentResponse', () => {
expect(bareResponse.getUsername()).toBeUndefined();
expect(response.getUsername()).toEqual(TestConstants.COMMENTS_DATA[1].username);
});

it('Gets the "from" object', () => {
expect(bareResponse.getFrom()).toBeUndefined();
expect(response.getFrom()).toEqual(TestConstants.COMMENTS_DATA[1].from);
});
});

0 comments on commit 1a0cb14

Please sign in to comment.