Skip to content

Commit

Permalink
Merge pull request #395 from TogetherCrew/390-discourse-platform-+-vi…
Browse files Browse the repository at this point in the history
…olation-detection-module

feat: add runDiscourse extraction function
  • Loading branch information
Behzad-rabiei authored Oct 9, 2024
2 parents 394e687 + 863e941 commit f9f9394
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 11 deletions.
4 changes: 4 additions & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const envVarsSchema = Joi.object()
REDIS_HOST: Joi.string().required().description('Redis host'),
REDIS_PORT: Joi.string().required().description('Redis port'),
REDIS_PASSWORD: Joi.string().required().description('Reids password').allow(''),
DISCOURSE_EXTRACTION_URL: Joi.string().required().description('Discourse extraction url'),
})
.unknown();

Expand Down Expand Up @@ -154,4 +155,7 @@ export default {
session: {
secret: envVars.SESSION_SECRET,
},
discourse: {
extractionURL: envVars.DISCOURSE_EXTRACTION_URL,
},
};
2 changes: 1 addition & 1 deletion src/controllers/platform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const logger = parentLogger.child({ module: 'PlatformController' });
const createPlatform = catchAsync(async function (req: IAuthRequest, res: Response) {
const community = req.community;
const platform = await platformService.managePlatformConnection(community?.id, req.body);
// await airflowService.triggerDag(platform);
await platformService.callExtractionApp(platform);
res.status(httpStatus.CREATED).send(platform);
});

Expand Down
21 changes: 11 additions & 10 deletions src/services/discourse/core.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import parentLogger from '../../config/logger';
import { IAuthAndPlatform } from '../../interfaces';
import categoryService from './category.service';
import { ApiError, pick, sort } from '../../utils';

import { Types } from 'mongoose';
import config from '../../config';
const logger = parentLogger.child({ module: 'DiscourseCoreService' });

async function getPropertyHandler(req: IAuthAndPlatform) {
Expand All @@ -14,20 +15,20 @@ async function getPropertyHandler(req: IAuthAndPlatform) {
}

/**
* create discourse forum
* @param {String} endpoint
* run discourse extraction
* @param {Strin} platformId
* @returns {Promise<IDiscordUser>}
*/
async function createDiscourseForum(endpoint: string): Promise<void> {
async function runDiscourseExtraction(platformId: string): Promise<void> {
try {
const data = {
endpoint,
platformId,
};
console.log(data);
const response = await fetch('http://discourse/forums', {
const response = await fetch(config.discourse.extractionURL, {
method: 'POST',
body: new URLSearchParams(data),
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
headers: { 'Content-Type': 'application/json' },
});
if (response.ok) {
console.log(await response.json());
Expand All @@ -37,12 +38,12 @@ async function createDiscourseForum(endpoint: string): Promise<void> {
logger.error({ error: errorResponse });
}
} catch (error) {
logger.error(error, 'Failed to create discourse forum');
throw new ApiError(590, 'Failed to create discourse forum');
logger.error(error, 'Failed to run discourse extraction discourse');
throw new ApiError(590, 'Failed to run discourse extraction discourse');
}
}

export default {
getPropertyHandler,
createDiscourseForum,
runDiscourseExtraction,
};
18 changes: 18 additions & 0 deletions src/services/platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ const getPlatformById = async (id: Types.ObjectId): Promise<HydratedDocument<IPl
return Platform.findById(id);
};

/**
* Call extraction app for the given platform
* @param {HydratedDocument<IPlatform>} platform
* @returns {Promise<Void>}
*/
const callExtractionApp = async (platform: HydratedDocument<IPlatform>): Promise<void> => {
switch (platform.name) {
case PlatformNames.Discourse: {
await discourseService.coreService.runDiscourseExtraction(platform.id as string);
return;
}
default: {
return;
}
}
};

/**
* Update Platform by filter
* @param {Object} filter - Mongo filter
Expand Down Expand Up @@ -248,4 +265,5 @@ export default {
deletePlatform,
deletePlatformByFilter,
managePlatformConnection,
callExtractionApp,
};

0 comments on commit f9f9394

Please sign in to comment.