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: read from content_preference source member #2451

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .infra/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ debezium.source.database.user=%database_user%
debezium.source.database.password=%database_pass%
debezium.source.database.dbname=%database_dbname%
debezium.source.database.server.name=api
debezium.source.table.include.list=public.comment,public.user_comment,public.comment_mention,public.source_request,public.post,public.user,public.post_report,public.source_feed,public.settings,public.reputation_event,public.submission,public.user_state,public.notification_v2,public.source_member,public.feature,public.source,public.post_mention,public.content_image,public.comment_report,public.user_post,public.banner,public.post_relation,public.marketing_cta,public.squad_public_request,public.user_streak,public.bookmark,public.user_company,public.source_report,public.user_top_reader,public.source_post_moderation
debezium.source.table.include.list=public.comment,public.user_comment,public.comment_mention,public.source_request,public.post,public.user,public.post_report,public.source_feed,public.settings,public.reputation_event,public.submission,public.user_state,public.notification_v2,public.feature,public.source,public.post_mention,public.content_image,public.comment_report,public.user_post,public.banner,public.post_relation,public.marketing_cta,public.squad_public_request,public.user_streak,public.bookmark,public.user_company,public.source_report,public.user_top_reader,public.source_post_moderation,public.content_preference
debezium.source.column.exclude.list=public.post.tsv,public.post.placeholder,public.source.flags,public.user_top_reader.image
debezium.source.skip.messages.without.change=true
debezium.source.plugin.name=pgoutput
Expand Down
4 changes: 2 additions & 2 deletions __tests__/__snapshots__/feeds.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ Object {
"excludeTypes": Array [],
"includeTags": Array [],
"sourceIds": Array [
"a",
"b",
"a-sqvf",
"b-sqvf",
],
}
`;
Expand Down
27 changes: 27 additions & 0 deletions __tests__/__snapshots__/sources.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,30 @@ Object {
},
}
`;

exports[`query sourceMembers should return source members of source without members that blocked squad 1`] = `
Object {
"sourceMembers": Object {
"edges": Array [
Object {
"node": Object {
"role": "admin",
"roleRank": 10,
"source": Object {
"id": "a",
},
"user": Object {
"id": "1",
"name": "Ido",
"username": "idoshamun",
},
},
},
],
"pageInfo": Object {
"endCursor": "YXJyYXljb25uZWN0aW9uOjA=",
"hasNextPage": false,
},
},
}
`;
70 changes: 54 additions & 16 deletions __tests__/boot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
Settings,
SETTINGS_DEFAULT,
Source,
SourceMember,
SourceType,
SQUAD_IMAGE_PLACEHOLDER,
SquadSource,
Expand Down Expand Up @@ -63,6 +62,8 @@ import { DEFAULT_TIMEZONE, submitArticleThreshold } from '../src/common';
import { saveReturnAlerts } from '../src/schema/alerts';
import { UserVote } from '../src/types';
import { BootAlerts, excludeProperties } from '../src/routes/boot';
import { ContentPreferenceSource } from '../src/entity/contentPreference/ContentPreferenceSource';
import { ContentPreferenceStatus } from '../src/entity/contentPreference/types';

let app: FastifyInstance;
let con: DataSource;
Expand Down Expand Up @@ -155,12 +156,19 @@ beforeAll(async () => {
});

beforeEach(async () => {
const users = usersFixture.slice(0, 1);

jest.resetAllMocks();
jest.mocked(getEncryptedFeatures).mockReturnValue('enc');
await con.getRepository(User).save(usersFixture[0]);
await con.getRepository(User).save(users);
await con.getRepository(Source).save(sourcesFixture);
await con.getRepository(Post).save(postsFixture);
await ioRedisPool.execute((client) => client.flushall());
await saveFixtures(
con,
Feed,
users.map((u) => ({ id: u.id, userId: u.id })),
);
});

const BASE_PATH = '/boot';
Expand Down Expand Up @@ -883,30 +891,50 @@ describe('boot misc', () => {
active: false,
},
]);
await con.getRepository(SourceMember).save([
await con.getRepository(ContentPreferenceSource).save([
{
sourceId: 's1',
referenceId: 's1',
userId: '1',
referralToken: 'rt',
role: SourceMemberRoles.Member,
flags: {
role: SourceMemberRoles.Member,
referralToken: 'rt',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
{
sourceId: 's2',
referenceId: 's2',
userId: '1',
referralToken: 'rt2',
role: SourceMemberRoles.Member,
flags: {
role: SourceMemberRoles.Member,
referralToken: 'rt2',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
{
sourceId: 's4',
referenceId: 's4',
userId: '1',
referralToken: 'rt3',
role: SourceMemberRoles.Member,
flags: {
role: SourceMemberRoles.Member,
referralToken: 'rt3',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
{
sourceId: 's5',
referenceId: 's5',
userId: '1',
referralToken: 'rt5',
role: SourceMemberRoles.Member,
flags: {
role: SourceMemberRoles.Member,
referralToken: 'rt5',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
]);
const res = await request(app.server)
Expand Down Expand Up @@ -986,18 +1014,28 @@ describe('boot misc', () => {
active: false,
},
]);
await con.getRepository(SourceMember).save([
await con.getRepository(ContentPreferenceSource).save([
{
sourceId: 's1',
referenceId: 's1',
userId: '1',
referralToken: 'rt',
role: SourceMemberRoles.Member,
flags: {
role: SourceMemberRoles.Member,
referralToken: 'rt',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
{
sourceId: 's3',
referenceId: 's3',
userId: '1',
referralToken: 'rt3',
role: SourceMemberRoles.Blocked,
flags: {
role: SourceMemberRoles.Blocked,
referralToken: 'rt3',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
]);
const res = await request(app.server)
Expand Down
81 changes: 59 additions & 22 deletions __tests__/comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import {
Comment,
User,
ArticlePost,
SourceMember,
SourceType,
CommentMention,
Feed,
} from '../src/entity';
import { SourceMemberRoles } from '../src/roles';
import { sourcesFixture } from './fixture/source';
Expand All @@ -44,6 +44,8 @@ import {
getRedisObjectExpiry,
} from '../src/redis';
import { badUsersFixture } from './fixture';
import { ContentPreferenceStatus } from '../src/entity/contentPreference/types';
import { ContentPreferenceSource } from '../src/entity/contentPreference/ContentPreferenceSource';

let con: DataSource;
let state: GraphQLTestingState;
Expand Down Expand Up @@ -77,6 +79,11 @@ beforeEach(async () => {
await saveFixtures(con, ArticlePost, sharedPostsFixture);
await saveFixtures(con, PostTag, postTagsFixture);
await con.getRepository(User).save(usersFixture);
await saveFixtures(
con,
Feed,
usersFixture.map((item) => ({ id: item.id, userId: item.id })),
);
await con.getRepository(User).save(badUsersFixture);
await con.getRepository(Comment).save([
{
Expand Down Expand Up @@ -190,20 +197,30 @@ const saveSquadFixture = async (sourceId: string) => {
},
]);
await con.getRepository(Source).update({ id: sourceId }, { private: true });
await con.getRepository(SourceMember).save([
await con.getRepository(ContentPreferenceSource).save([
{
userId: '1',
sourceId,
role: SourceMemberRoles.Admin,
referralToken: 'rt1',
referenceId: sourceId,
userId: '1',
createdAt: new Date(2022, 11, 19),
flags: {
role: SourceMemberRoles.Member,
referralToken: 'rt',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
{
userId: 'sample1',
sourceId,
role: SourceMemberRoles.Member,
referralToken: 'rt2',
referenceId: sourceId,
userId: 'sample1',
createdAt: new Date(2022, 11, 19),
flags: {
role: SourceMemberRoles.Member,
referralToken: 'rt2',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
},
]);
};
Expand Down Expand Up @@ -913,11 +930,16 @@ describe('mutation commentOnPost', () => {
await con
.getRepository(Post)
.update({ id: 'p1' }, { private: false, sourceId: 'a' });
await con.getRepository(SourceMember).save({
await con.getRepository(ContentPreferenceSource).save({
sourceId: 'a',
referenceId: 'a',
userId: '1',
referralToken: 'rt2',
role: SourceMemberRoles.Blocked,
flags: {
role: SourceMemberRoles.Blocked,
referralToken: 'rt2',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
});
return testMutationErrorCode(
client,
Expand Down Expand Up @@ -1152,11 +1174,16 @@ describe('mutation commentOnComment', () => {
await con
.getRepository(Post)
.update({ id: 'p1' }, { private: false, sourceId: 'a' });
await con.getRepository(SourceMember).save({
await con.getRepository(ContentPreferenceSource).save({
sourceId: 'a',
referenceId: 'a',
userId: '1',
referralToken: 'rt2',
role: SourceMemberRoles.Blocked,
flags: {
role: SourceMemberRoles.Blocked,
referralToken: 'rt2',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
});

return testMutationErrorCode(
Expand Down Expand Up @@ -1349,11 +1376,16 @@ describe('mutation deleteComment', () => {

it("should forbidden when other user doesn't have the right permissions", async () => {
loggedUser = '1';
await con.getRepository(SourceMember).insert({
userId: '1',
await con.getRepository(ContentPreferenceSource).insert({
sourceId: 'squad',
role: SourceMemberRoles.Member,
referralToken: 's1',
referenceId: 'squad',
userId: '1',
flags: {
role: SourceMemberRoles.Member,
referralToken: 's1',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
});
return testMutationErrorCode(
client,
Expand All @@ -1367,11 +1399,16 @@ describe('mutation deleteComment', () => {

it('should delete a comment if source admin has permissions', async () => {
loggedUser = '1';
await con.getRepository(SourceMember).insert({
userId: '1',
await con.getRepository(ContentPreferenceSource).insert({
sourceId: 'squad',
role: SourceMemberRoles.Admin,
referralToken: 's1',
referenceId: 'squad',
userId: '1',
flags: {
role: SourceMemberRoles.Admin,
referralToken: 's1',
},
status: ContentPreferenceStatus.Subscribed,
feedId: '1',
});
const before = await con.getRepository(Comment).findBy({ postId: 'p1' });
expect(before.length).toEqual(5);
Expand Down
Loading
Loading