Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export async function getSingleton() {
const server = http.createServer(_app.callback());
const listen = util.promisify(server.listen).bind(server);

_app.context.pubsub = new PubsubListener(server, _app);
_app.context.pubsub = new PubsubListener(_app.context.modelRegistry, server, _app);

const port = process.env.PEPYATKA_SERVER_PORT || process.env.PORT || _app.context.config.port;
await listen(port);
Expand Down
9 changes: 4 additions & 5 deletions app/controllers/api/v1/AttachmentsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { reportError, BadRequestException, ValidationException } from '../../../
import { serializeAttachment } from '../../../serializers/v2/post';
import { serializeUsersByIds } from '../../../serializers/v2/user';
import { authRequired } from '../../middlewares';
import { dbAdapter } from '../../../models';
import { startAttachmentsSanitizeJob } from '../../../jobs/attachments-sanitize';

export default class AttachmentsController {
Expand Down Expand Up @@ -90,7 +89,7 @@ export default class AttachmentsController {
page = Number.parseInt(qPage, 10);
}

const attachments = await dbAdapter.listAttachments({
const attachments = await ctx.modelRegistry.dbAdapter.listAttachments({
userId: user.id,
limit: limit + 1,
offset: limit * (page - 1),
Expand All @@ -115,8 +114,8 @@ export default class AttachmentsController {
async (ctx) => {
const { user } = ctx.state;
const [stats, task] = await Promise.all([
dbAdapter.getAttachmentsStats(user.id),
dbAdapter.getAttachmentsSanitizeTask(user.id),
ctx.modelRegistry.dbAdapter.getAttachmentsStats(user.id),
ctx.modelRegistry.dbAdapter.getAttachmentsSanitizeTask(user.id),
]);
ctx.body = {
attachments: stats,
Expand All @@ -129,7 +128,7 @@ export default class AttachmentsController {
authRequired(),
async (ctx) => {
const { user } = ctx.state;
const task = await startAttachmentsSanitizeJob(user);
const task = await startAttachmentsSanitizeJob(ctx.modelRegistry.dbAdapter, user);
ctx.body = {
sanitizeTask: { createdAt: task.createdAt },
};
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/api/v1/BookmarkletController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import compose from 'koa-compose';

import { Post, Comment, AppTokenV1 } from '../../../models';
import { AppTokenV1 } from '../../../models/auth-tokens/AppTokenV1';
import { ForbiddenException } from '../../../support/exceptions';
import { authRequired, monitored, inputSchemaRequired } from '../../middlewares';
import { show as showPost } from '../v2/PostsController';
Expand Down Expand Up @@ -29,7 +29,7 @@ export const create = compose([
destNames.push(author.username);
}

const timelineIds = await checkDestNames(destNames, author);
const timelineIds = await checkDestNames(destNames, author, ctx.modelRegistry.dbAdapter);

// Attachments
if (images.length === 0 && image !== '') {
Expand All @@ -47,7 +47,7 @@ export const create = compose([
}),
);

const post = new Post({
const post = new ctx.modelRegistry.Post({
userId: author.id,
body,
attachments,
Expand All @@ -56,7 +56,7 @@ export const create = compose([
await post.create();

if (commentBody !== '') {
const comment = new Comment({
const comment = new ctx.modelRegistry.Comment({
body: commentBody,
postId: post.id,
userId: author.id,
Expand Down
16 changes: 9 additions & 7 deletions app/controllers/api/v1/CommentsController.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import compose from 'koa-compose';
import monitor from 'monitor-dog';

import { dbAdapter, Comment, AppTokenV1 } from '../../../models';
import { AppTokenV1 } from '../../../models/auth-tokens/AppTokenV1';
import {
ForbiddenException,
NotFoundException,
Expand Down Expand Up @@ -37,7 +37,7 @@ export const create = compose([
throw new ForbiddenException('Comments disabled');
}

const comment = new Comment({ body, postId, userId: author.id });
const comment = new ctx.modelRegistry.Comment({ body, postId, userId: author.id });

try {
await comment.create();
Expand All @@ -58,13 +58,13 @@ export const update = compose([
const { user } = ctx.state;
const { commentId } = ctx.params;

const comment = await dbAdapter.getCommentById(commentId);
const comment = await ctx.modelRegistry.dbAdapter.getCommentById(commentId);

if (!comment) {
throw new NotFoundException('Can not find comment');
}

const post = await dbAdapter.getPostById(comment.postId);
const post = await ctx.modelRegistry.dbAdapter.getPostById(comment.postId);

if (!post) {
// Should not be possible
Expand Down Expand Up @@ -98,13 +98,13 @@ export const destroy = compose([
const { user } = ctx.state;
const { commentId } = ctx.params;

const comment = await dbAdapter.getCommentById(commentId);
const comment = await ctx.modelRegistry.dbAdapter.getCommentById(commentId);

if (!comment) {
throw new NotFoundException('Can not find comment');
}

const post = await dbAdapter.getPostById(comment.postId);
const post = await ctx.modelRegistry.dbAdapter.getPostById(comment.postId);

if (!post) {
// Should not be possible
Expand Down Expand Up @@ -136,6 +136,8 @@ export async function getById(ctx) {
const { user } = ctx.state;
const { commentId } = ctx.params;

const { dbAdapter, Comment } = ctx.modelRegistry;

const comment = await dbAdapter.getCommentById(commentId);

if (!comment) {
Expand Down Expand Up @@ -182,7 +184,7 @@ export async function getBySeqNumber(ctx) {
const { postId, seqNumber } = ctx.params;

const number = Number.parseInt(seqNumber, 10);
const comment = await dbAdapter.getCommentBySeqNumber(
const comment = await ctx.modelRegistry.dbAdapter.getCommentBySeqNumber(
postId,
Number.isFinite(number) ? number : -1,
);
Expand Down
3 changes: 1 addition & 2 deletions app/controllers/api/v1/FeedFactoriesController.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { dbAdapter } from '../../../models';
import { UsersController, GroupsController } from '../../../controllers';
import { NotFoundException } from '../../../support/exceptions';

export default class FeedFactoriesController {
static async update(ctx) {
const feed = await dbAdapter.getFeedOwnerById(ctx.params.userId);
const feed = await ctx.modelRegistry.dbAdapter.getFeedOwnerById(ctx.params.userId);

if (!feed) {
throw new NotFoundException(`Feed ${ctx.params.userId} is not found`);
Expand Down
48 changes: 31 additions & 17 deletions app/controllers/api/v1/GroupsController.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import _ from 'lodash';
import compose from 'koa-compose';

import { dbAdapter, Group, AppTokenV1 } from '../../../models';
import { EventService } from '../../../support/EventService';
import { AppTokenV1 } from '../../../models/auth-tokens/AppTokenV1';
import {
BadRequestException,
NotFoundException,
Expand Down Expand Up @@ -36,9 +35,9 @@ export default class GroupsController {
'isRestricted',
]);

const group = new Group(params);
const group = new ctx.modelRegistry.Group(params);
await group.create(ctx.state.user.id, false);
await EventService.onGroupCreated(ctx.state.user.intId, group.intId);
await ctx.modelRegistry.eventService.onGroupCreated(ctx.state.user.intId, group.intId);

// The same output as of the UsersController.show with 'users' -> 'groups' replacing
ctx.params['username'] = group.username;
Expand All @@ -63,13 +62,13 @@ export default class GroupsController {
}

const adminPromises = ctx.request.body.admins.map(async (username) => {
const admin = await dbAdapter.getUserByUsername(username);
const admin = await ctx.modelRegistry.dbAdapter.getUserByUsername(username);
return null === admin ? false : admin;
});
let admins = await Promise.all(adminPromises);
admins = admins.filter(Boolean);

const group = new Group(params);
const group = new ctx.modelRegistry.Group(params);
await group.create(admins[0].id, true);

// starting iteration from the second admin
Expand Down Expand Up @@ -106,7 +105,7 @@ export default class GroupsController {
'isRestricted',
]);

const group = await dbAdapter.getGroupById(ctx.params.userId);
const group = await ctx.modelRegistry.dbAdapter.getGroupById(ctx.params.userId);

if (null === group) {
throw new NotFoundException("Can't find group");
Expand Down Expand Up @@ -134,7 +133,7 @@ export default class GroupsController {
return;
}

const group = await dbAdapter.getGroupByUsername(ctx.params.groupName);
const group = await ctx.modelRegistry.dbAdapter.getGroupByUsername(ctx.params.groupName);

if (null === group) {
throw new NotFoundException(`Group "${ctx.params.groupName}" is not found`);
Expand All @@ -146,7 +145,7 @@ export default class GroupsController {
throw new ForbiddenException("You aren't an administrator of this group");
}

const newAdmin = await dbAdapter.getUserByUsername(ctx.params.adminName);
const newAdmin = await ctx.modelRegistry.dbAdapter.getUserByUsername(ctx.params.adminName);

if (null === newAdmin) {
throw new NotFoundException(`User "${ctx.params.adminName}" is not found`);
Expand All @@ -158,10 +157,18 @@ export default class GroupsController {

if (newStatus) {
await group.addAdministrator(newAdmin.id);
await EventService.onGroupAdminPromoted(ctx.state.user.intId, group, newAdmin.intId);
await ctx.modelRegistry.eventService.onGroupAdminPromoted(
ctx.state.user.intId,
group,
newAdmin.intId,
);
} else {
await group.removeAdministrator(newAdmin.id);
await EventService.onGroupAdminDemoted(ctx.state.user.intId, group, newAdmin.intId);
await ctx.modelRegistry.eventService.onGroupAdminDemoted(
ctx.state.user.intId,
group,
newAdmin.intId,
);
}

ctx.body = { err: null, status: 'success' };
Expand Down Expand Up @@ -251,7 +258,7 @@ export default class GroupsController {
}

const { groupName, userName } = ctx.params;
const group = await dbAdapter.getGroupByUsername(groupName);
const group = await ctx.modelRegistry.dbAdapter.getGroupByUsername(groupName);

if (null === group) {
throw new NotFoundException(`Group "${groupName}" is not found`);
Expand All @@ -263,20 +270,27 @@ export default class GroupsController {
throw new ForbiddenException("You aren't an administrator of this group");
}

const user = await dbAdapter.getUserByUsername(userName);
const user = await ctx.modelRegistry.dbAdapter.getUserByUsername(userName);

if (null === user) {
throw new NotFoundException(`User "${userName}" is not found`);
}

const hasRequest = await dbAdapter.isSubscriptionRequestPresent(user.id, group.id);
const hasRequest = await ctx.modelRegistry.dbAdapter.isSubscriptionRequestPresent(
user.id,
group.id,
);

if (!hasRequest) {
throw new Error('Invalid');
}

await group.rejectSubscriptionRequest(user.id);
await EventService.onGroupSubscriptionRequestRejected(ctx.state.user.intId, group, user.intId);
await ctx.modelRegistry.eventService.onGroupSubscriptionRequestRejected(
ctx.state.user.intId,
group,
user.intId,
);

ctx.body = { err: null, status: 'success' };
}
Expand All @@ -289,7 +303,7 @@ export default class GroupsController {
}

const { groupName, userName } = ctx.params;
const group = await dbAdapter.getGroupByUsername(groupName);
const group = await ctx.modelRegistry.dbAdapter.getGroupByUsername(groupName);

if (null === group) {
throw new NotFoundException(`Group "${groupName}" is not found`);
Expand All @@ -301,7 +315,7 @@ export default class GroupsController {
throw new ForbiddenException("You aren't an administrator of this group");
}

const user = await dbAdapter.getUserByUsername(userName);
const user = await ctx.modelRegistry.dbAdapter.getUserByUsername(userName);

if (null === user) {
throw new NotFoundException(`User "${userName}" is not found`);
Expand Down
5 changes: 2 additions & 3 deletions app/controllers/api/v1/PasswordsController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { dbAdapter } from '../../../models';
import { UserMailer } from '../../../mailers';
import { NotFoundException } from '../../../support/exceptions';

Expand All @@ -12,7 +11,7 @@ export default class PasswordsController {
return;
}

const user = await dbAdapter.getUserByEmail(email);
const user = await ctx.modelRegistry.dbAdapter.getUserByEmail(email);

if (!user?.isActive && !user?.isResumable) {
throw new NotFoundException(`Invalid email address or user not found`);
Expand All @@ -33,7 +32,7 @@ export default class PasswordsController {
return;
}

const user = await dbAdapter.getUserByResetToken(token);
const user = await ctx.modelRegistry.dbAdapter.getUserByResetToken(token);

if (!user?.isActive && !user?.isResumable) {
throw new NotFoundException(`Password reset token not found or has expired`);
Expand Down
Loading