Skip to content

Commit

Permalink
feat: vordr on posts (#2108)
Browse files Browse the repository at this point in the history
  • Loading branch information
omBratteng authored Aug 13, 2024
1 parent c6466de commit 357151c
Show file tree
Hide file tree
Showing 21 changed files with 699 additions and 99 deletions.
72 changes: 69 additions & 3 deletions __tests__/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -978,7 +978,23 @@ describe('mutation commentOnPost', () => {
expect(comment.flags).toEqual({ vordr: false });
});

it('should set correct vordr flags on bad user', async () => {
it('should set correct vordr flags on comment by good user if vordr filter catches it', async () => {
loggedUser = '1';

const res = await client.mutate(MUTATION, {
variables: { postId: 'p1', content: 'VordrWillCatchYou' },
});

expect(res.errors).toBeFalsy();

const comment = await con.getRepository(Comment).findOneByOrFail({
id: res.data.commentOnPost.id,
});

expect(comment.flags).toEqual({ vordr: true });
});

it('should set correct vordr flags on comment by bad user', async () => {
loggedUser = 'vordr';

const res = await client.mutate(MUTATION, {
Expand Down Expand Up @@ -1192,7 +1208,7 @@ describe('mutation commentOnComment', () => {
});

describe('vordr', () => {
it('should set correct vordr flags on good user', async () => {
it('should set correct vordr flags on comment reply by good user', async () => {
loggedUser = '1';

const res = await client.mutate(MUTATION, {
Expand All @@ -1208,7 +1224,23 @@ describe('mutation commentOnComment', () => {
expect(comment.flags).toEqual({ vordr: false });
});

it('should set correct vordr flags on bad user', async () => {
it('should set correct vordr flags on comment reply by good user if vordr filter catches it', async () => {
loggedUser = '1';

const res = await client.mutate(MUTATION, {
variables: { commentId: 'c1', content: 'VordrWillCatchYou' },
});

expect(res.errors).toBeFalsy();

const comment = await con.getRepository(Comment).findOneByOrFail({
id: res.data.commentOnComment.id,
});

expect(comment.flags).toEqual({ vordr: true });
});

it('should set correct vordr flags on comment reply by bad user', async () => {
loggedUser = 'vordr';

const res = await client.mutate(MUTATION, {
Expand Down Expand Up @@ -1440,6 +1472,40 @@ describe('mutation editComment', () => {
content: 'Edit',
});
});

describe('vordr', () => {
it('should set correct vordr flags on edited comment by good user', async () => {
loggedUser = '1';

const res = await client.mutate(MUTATION, {
variables: { id: 'c2', content: 'comment' },
});

expect(res.errors).toBeFalsy();

const comment = await con.getRepository(Comment).findOneByOrFail({
id: res.data.editComment.id,
});

expect(comment.flags).toEqual({ vordr: false });
});

it('should set correct vordr flags on edited comment by good user if vordr filter catches it', async () => {
loggedUser = '1';

const res = await client.mutate(MUTATION, {
variables: { id: 'c2', content: 'VordrWillCatchYou' },
});

expect(res.errors).toBeFalsy();

const comment = await con.getRepository(Comment).findOneByOrFail({
id: res.data.editComment.id,
});

expect(comment.flags).toEqual({ vordr: true });
});
});
});

describe('mutation reportComment', () => {
Expand Down
122 changes: 85 additions & 37 deletions __tests__/common/vordr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { DataSource } from 'typeorm';
import createOrGetConnection from '../../src/db';
import { Comment, Post, Source, User } from '../../src/entity';
import { badUsersFixture, sourcesFixture, usersFixture } from '../fixture';
import { checkWithVordr } from '../../src/common/vordr';
import { Context } from '../../src/Context';
import { checkWithVordr, VordrFilterType } from '../../src/common/vordr';
import { postsFixture } from '../fixture/post';
import { saveFixtures } from '../helpers';

Expand Down Expand Up @@ -50,11 +49,18 @@ describe('commmon/vordr', () => {
.getRepository(Comment)
.findOneByOrFail({ id: 'c1' });

const result = await checkWithVordr(comment, {
req: { ip: '127.0.0.1' },
userId: 'vordr',
con,
} as Context);
const result = await checkWithVordr(
{
id: comment.id,
type: VordrFilterType.Comment,
content: comment.content,
},
{
req: { ip: '127.0.0.1' },
userId: 'vordr',
con,
},
);

expect(result).toBeTruthy();
});
Expand All @@ -64,11 +70,18 @@ describe('commmon/vordr', () => {
.getRepository(Comment)
.findOneByOrFail({ id: 'c1' });

const result = await checkWithVordr(comment, {
req: { ip: '127.0.0.1' },
userId: 'low-score',
con,
} as Context);
const result = await checkWithVordr(
{
id: comment.id,
type: VordrFilterType.Comment,
content: comment.content,
},
{
req: { ip: '127.0.0.1' },
userId: 'low-score',
con,
},
);

expect(result).toBeTruthy();
});
Expand All @@ -78,11 +91,18 @@ describe('commmon/vordr', () => {
.getRepository(Comment)
.findOneByOrFail({ id: 'c1' });

const result = await checkWithVordr(comment, {
req: { ip: '192.0.2.1' },
userId: '1',
con,
} as Context);
const result = await checkWithVordr(
{
id: comment.id,
type: VordrFilterType.Comment,
content: comment.content,
},
{
req: { ip: '192.0.2.1' },
userId: '1',
con,
},
);

expect(result).toBeTruthy();
});
Expand All @@ -92,11 +112,18 @@ describe('commmon/vordr', () => {
.getRepository(Comment)
.findOneByOrFail({ id: 'c2' });

const result = await checkWithVordr(comment, {
req: { ip: '127.0.0.1' },
userId: '1',
con,
} as Context);
const result = await checkWithVordr(
{
id: comment.id,
type: VordrFilterType.Comment,
content: comment.content,
},
{
req: { ip: '127.0.0.1' },
userId: '1',
con,
},
);

expect(result).toBeTruthy();
});
Expand All @@ -106,11 +133,18 @@ describe('commmon/vordr', () => {
.getRepository(Comment)
.findOneByOrFail({ id: 'c1' });

const result = await checkWithVordr(comment, {
req: { ip: '127.0.0.1' },
userId: 'low-reputation',
con,
} as Context);
const result = await checkWithVordr(
{
id: comment.id,
type: VordrFilterType.Comment,
content: comment.content,
},
{
req: { ip: '127.0.0.1' },
userId: 'low-reputation',
con,
},
);

expect(result).toBeTruthy();
});
Expand All @@ -120,11 +154,18 @@ describe('commmon/vordr', () => {
.getRepository(Comment)
.findOneByOrFail({ id: 'c1' });

const result = await checkWithVordr(comment, {
req: { ip: '127.0.0.1' },
userId: '1',
con,
} as Context);
const result = await checkWithVordr(
{
id: comment.id,
type: VordrFilterType.Comment,
content: comment.content,
},
{
req: { ip: '127.0.0.1' },
userId: '1',
con,
},
);

expect(result).toBeFalsy();
});
Expand All @@ -134,11 +175,18 @@ describe('commmon/vordr', () => {
.getRepository(Comment)
.findOneByOrFail({ id: 'c1' });

const result = await checkWithVordr(comment, {
req: { ip: '127.0.0.1' },
userId: '1',
con,
} as Context);
const result = await checkWithVordr(
{
id: comment.id,
type: VordrFilterType.Comment,
content: comment.content,
},
{
req: { ip: '127.0.0.1' },
userId: '1',
con,
},
);

expect(result).toBeFalsy();
});
Expand Down
52 changes: 52 additions & 0 deletions __tests__/feeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,6 +1142,58 @@ describe('query sourceFeed', () => {
'FORBIDDEN',
);
});

describe('vordr', () => {
beforeEach(async () => {
await saveFixtures(con, ArticlePost, [
{
id: 'vordr1',
shortId: 'svordr1',
title: 'vordr1',
url: 'http://vordr1.com',
image: 'https://daily.dev/image.jpg',
score: 10,
sourceId: 'b',
createdAt: new Date(new Date().getTime() - 4000),
tagsStr: 'html,javascript',
type: PostType.Article,
contentCuration: ['c1', 'c2'],
authorId: '2',
flags: { vordr: true },
},
{
id: 'vordr2',
shortId: 'svordr2',
title: 'vordr2',
url: 'http://vordr2.com',
image: 'https://daily.dev/image.jpg',
score: 10,
sourceId: 'b',
createdAt: new Date(new Date().getTime() - 4000),
tagsStr: 'html,javascript',
type: PostType.Article,
contentCuration: ['c1', 'c2'],
authorId: '2',
flags: { vordr: true },
},
]);
});
it('should filter out posts that vordr has prevented', async () => {
loggedUser = '1';

const res = await client.query(QUERY('b'));

expect(res.data.sourceFeed.edges.length).toBe(2);
});

it('should not filter out posts that vordr has prevented when author is viewer', async () => {
loggedUser = '2';

const res = await client.query(QUERY('b'));

expect(res.data.sourceFeed.edges.length).toBe(4);
});
});
});

describe('query tagFeed', () => {
Expand Down
Loading

0 comments on commit 357151c

Please sign in to comment.