Skip to content

Commit

Permalink
Use models' methods in mutualSubscriptions and subscribeToAsync helpers
Browse files Browse the repository at this point in the history
These methods are a way faster than HTTP-requests
  • Loading branch information
davidmz committed Apr 26, 2024
1 parent 681c942 commit e043e43
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
13 changes: 10 additions & 3 deletions test/functional/functional_test_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,8 +542,15 @@ export function groupToProtected(group, userContext) {
return updateGroupAsync(group, userContext, { isPrivate: '0', isProtected: '1' });
}

export function subscribeToAsync(subscriber, victim) {
return postJson(`/v1/users/${victim.username}/subscribe`, { authToken: subscriber.authToken });
export async function subscribeToAsync(subscriber, victim) {
let victimObj = victim.user;

if (!victimObj) {
// Group or old-fashion user context
victimObj = await dbAdapter.getFeedOwnerById(victim.id ?? victim.group.id);
}

await subscriber.user.subscribeTo(victimObj);
}

export function unsubscribeFromAsync(unsubscriber, victim) {
Expand Down Expand Up @@ -577,7 +584,7 @@ export async function mutualSubscriptions(userContexts) {
continue;
}

promises.push(subscribeToAsync(ctx1, ctx2));
promises.push(ctx1.user.subscribeTo(ctx2.user));
}
}

Expand Down
10 changes: 5 additions & 5 deletions test/functional/privates.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ describe('Privates', () => {
});

describe('publish private post to public feed', () => {
const group = 'group';
let group;

beforeEach(async () => {
await Promise.all([
[, group] = await Promise.all([
funcTestHelper.mutualSubscriptions([marsContext, lunaContext]),
funcTestHelper.createGroupAsync(lunaContext, group),
funcTestHelper.createGroupAsync(lunaContext, 'group'),
]);

await funcTestHelper.subscribeToAsync(zeusContext, { username: group });
await funcTestHelper.subscribeToAsync(zeusContext, group);
});

it('should send private post to public feed', (done) => {
Expand All @@ -52,7 +52,7 @@ describe('Privates', () => {
.post(`${app.context.config.host}/v1/posts`)
.send({
post: { body: post },
meta: { feeds: [group, lunaContext.user.username] },
meta: { feeds: [group.username, lunaContext.user.username] },
authToken: lunaContext.authToken,
})
.end(() => {
Expand Down
2 changes: 1 addition & 1 deletion test/functional/timelines-rss.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ describe('TimelinesAsRSS', () => {
createUserAsync('mars', 'pw'),
]);
celestials = await createGroupAsync(luna, 'celestials', 'Celestials');
await subscribeToAsync(mars, { username: 'celestials' });
await subscribeToAsync(mars, celestials);
post1 = await createAndReturnPostToFeed(celestials, luna, 'Luna post');
post2 = await createAndReturnPostToFeed(celestials, mars, 'Mars post');
});
Expand Down
16 changes: 8 additions & 8 deletions test/functional/timelinesV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,12 @@ describe('TimelinesControllerV2', () => {
});

describe('Luna subscribed to Selenites group and not subscribed to Celestials group', () => {
let selenitesPost, celestialsPost;
let selenitesPost, celestialsPost, celestials;

beforeEach(async () => {
await createGroupAsync(venus, 'selenites');
await createGroupAsync(venus, 'celestials');
await subscribeToAsync(luna, { username: 'selenites' });
const selenites = await createGroupAsync(venus, 'selenites');
celestials = await createGroupAsync(venus, 'celestials');
await subscribeToAsync(luna, selenites);

selenitesPost = await createAndReturnPostToFeed(
{ username: 'selenites' },
Expand Down Expand Up @@ -348,7 +348,7 @@ describe('TimelinesControllerV2', () => {
});

it('should return timeline with Mars posts from Celestials group in "friends-all-activity" mode', async () => {
await subscribeToAsync(mars, { username: 'celestials' });
await subscribeToAsync(mars, celestials);
const marsCelestialsPost = await createAndReturnPostToFeed(
{ username: 'celestials' },
mars,
Expand All @@ -367,9 +367,9 @@ describe('TimelinesControllerV2', () => {
beforeEach(async () => {
mars = await createUserAsync('mars', 'pw');
venus = await createUserAsync('venus', 'pw');
await createGroupAsync(venus, 'selenites');
await subscribeToAsync(luna, { username: 'selenites' });
await subscribeToAsync(mars, { username: 'selenites' });
const selenites = await createGroupAsync(venus, 'selenites');
await subscribeToAsync(luna, selenites);
await subscribeToAsync(mars, selenites);
await banUser(mars, luna);
});

Expand Down

0 comments on commit e043e43

Please sign in to comment.