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

[GSoC19] Remove the outgoing callbacks #8

Open
wants to merge 18 commits into
base: articles
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion app/api/server/v1/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ API.v1.addRoute('rooms.leave', { authRequired: true }, {

API.v1.addRoute('rooms.createDiscussion', { authRequired: true }, {
post() {
const { prid, pmid, reply, t_name, users } = this.bodyParams;
const { prid, pmid, reply, t_name, users, t } = this.bodyParams;
if (!prid) {
return API.v1.failure('Body parameter "prid" is required.');
}
Expand All @@ -238,6 +238,7 @@ API.v1.addRoute('rooms.createDiscussion', { authRequired: true }, {
pmid,
t_name,
reply,
t,
users: users || [],
}));

Expand Down
1 change: 1 addition & 0 deletions app/articles/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Readme file for articles.
84 changes: 84 additions & 0 deletions app/articles/server/api/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Restivus } from 'meteor/nimble:restivus';
import _ from 'underscore';

import { processWebhookMessage } from '../../../lib';
import { API } from '../../../api';
import { settings } from '../../../settings';
import * as Models from '../../../models';

const Api = new Restivus({
enableCors: true,
apiPath: 'ghooks/',
auth: {
user() {
const payloadKeys = Object.keys(this.bodyParams);
const payloadIsWrapped = (this.bodyParams && this.bodyParams.payload) && payloadKeys.length === 1;
if (payloadIsWrapped && this.request.headers['content-type'] === 'application/x-www-form-urlencoded') {
try {
this.bodyParams = JSON.parse(this.bodyParams.payload);
} catch ({ message }) {
return {
error: {
statusCode: 400,
body: {
success: false,
error: message,
},
},
};
}
}

this.announceToken = settings.get('Announcement_Token');
const token = decodeURIComponent(this.request.params.token);

if (this.announceToken !== token) {
return {
error: {
statusCode: 404,
body: {
success: false,
error: 'Invalid token provided.',
},
},
};
}

const user = Models.Users.findOne({ _id: this.bodyParams.userId });

return { user };
},
},
});

function executeAnnouncementRest() {
const defaultValues = {
channel: this.bodyParams.channel,
alias: this.bodyParams.alias,
avatar: this.bodyParams.avatar,
emoji: this.bodyParams.emoji,
};

// TODO: Turn this into an option on the integrations - no body means a success
// TODO: Temporary fix for https://github.com/RocketChat/Rocket.Chat/issues/7770 until the above is implemented
if (!this.bodyParams || (_.isEmpty(this.bodyParams) && !this.integration.scriptEnabled)) {
// return RocketChat.API.v1.failure('body-empty');
return API.v1.success();
}

try {
const message = processWebhookMessage(this.bodyParams, this.user, defaultValues);
if (_.isEmpty(message)) {
return API.v1.failure('unknown-error');
}

return API.v1.success();
} catch ({ error, message }) {
return API.v1.failure(error || message);
}
}

Api.addRoute(':token', { authRequired: true }, {
post: executeAnnouncementRest,
get: executeAnnouncementRest,
});
6 changes: 6 additions & 0 deletions app/articles/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import './settings';
import './methods/admin';
import './methods/user';
import './api/api';
import './lib/triggerHandler';
import './triggers';
46 changes: 46 additions & 0 deletions app/articles/server/lib/triggerHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ghostAPI } from '../utils/ghostAPI';

export const triggerHandler = new class ArticlesSettingsHandler {
eventNameArgumentsToObject(...args) {
const argObject = {
event: args[0],
};
switch (argObject.event) {
case 'deleteUser':
if (args.length >= 2) {
argObject.user = args[1];
}
break;
default:
argObject.event = undefined;
break;
}
return argObject;
}

mapEventArgsToData(data, { event, user }) {
data.event = event;
switch (event) {
case 'deleteUser':
data.user_id = user._id;
break;
default:
break;
}
}

executeTrigger(...args) {
const argObject = this.eventNameArgumentsToObject(...args);
const { event } = argObject;

if (!event) {
return;
}

const data = {};

this.mapEventArgsToData(data, argObject);

ghostAPI.executeTriggerUrl(data, 0);
}
}();
13 changes: 13 additions & 0 deletions app/articles/server/logoutCleanUp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { settings } from '../../settings';
import { ghostAPI } from './utils/ghostAPI';

export function ghostCleanUp(cookie) {
try {
if (settings.get('Articles_Enabled')) {
ghostAPI.deleteSesseion(cookie);
}
} catch (e) {
// Do nothing if failed to logout from Ghost.
// Error will be because user has not logged in to Ghost.
}
}
51 changes: 51 additions & 0 deletions app/articles/server/methods/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Meteor } from 'meteor/meteor';

import { hasPermission } from '../../../authorization';
import { ghostAPI } from '../utils/ghostAPI';
import { settings } from '../../../settings';

Meteor.methods({
articlesAdminPanel(loginToken) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'articlesAdminPanel',
});
}

const enabled = settings.get('Articles_Enabled');

if (!enabled) {
throw new Meteor.Error('error-articles-disabled', 'Articles are disabled', {
method: 'articlesAdminPanel',
});
}
let errMsg = 'Unable to connect to Ghost. Make sure Ghost is running';

try {
let response = ghostAPI.isSetup();

if (response.data.setup[0].status) { // Ghost site is already setup
return ghostAPI.redirectToGhostLink();
}

if (!hasPermission(Meteor.userId(), 'setup-ghost')) {
throw new Meteor.Error('error-action-not-allowed', 'Setting up Ghost is not allowed', {
method: 'articlesAdminPanel',
});
}

// Setup Ghost Site and set title
response = ghostAPI.setupGhost(loginToken);

errMsg = 'Unable to setup. Make sure Ghost is running';

if (response.statusCode === 201 && response.content) {
return ghostAPI.redirectToGhostLink();
}

throw new Meteor.Error(errMsg);
} catch (e) {
throw new Meteor.Error(e.error || errMsg);
}
},
});
79 changes: 79 additions & 0 deletions app/articles/server/methods/user.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { Meteor } from 'meteor/meteor';

import { ghostAPI } from '../utils/ghostAPI';
import { settings } from '../../../settings';

Meteor.methods({
redirectUserToArticles(loginToken) {
if (!Meteor.userId()) {
throw new Meteor.Error('error-invalid-user', 'Invalid user', {
method: 'redirectUserToArticles',
});
}

const enabled = settings.get('Articles_Enabled');

if (!enabled) {
throw new Meteor.Error('error-articles-disabled', 'Articles are disabled', {
method: 'redirectUserToArticles',
});
}

let errMsg = 'Ghost is not set up. Setup can be done from Admin Panel';

try {
const response = ghostAPI.isSetup();

if (response.data.setup[0].status) { // Ghost site is already setup
const u = ghostAPI.userExistInGhost(Meteor.userId()).users[0];

if (u.exist && u.status === 'active') {
return ghostAPI.redirectToGhostLink();
}

if (u.exist) { // user exist but suspended
throw new Meteor.Error('error-articles-user-suspended', 'You are suspended from Ghost', {
method: 'redirectUserToArticles',
});
}

const inviteOnly = ghostAPI.inviteSettingInGhost();

// create user account in ghost
if (!inviteOnly && ghostAPI.createUserAccount(loginToken).statusCode === 200) {
return ghostAPI.redirectToGhostLink();
}

errMsg = inviteOnly ? 'You are not a member of Ghost. Ask admin to add' : 'Unable to setup your account';
}

// Cannot setup Ghost from sidenav
throw new Meteor.Error(errMsg);
} catch (e) {
throw new Meteor.Error(e.error || 'Unable to connect to Ghost.');
}
},

redirectToUsersArticles(_id) {
const enabled = settings.get('Articles_Enabled');

if (!enabled) {
throw new Meteor.Error('error-articles-disabled', 'Articles are disabled', {
method: 'redirectToUsersArticles',
});
}
const errMsg = 'User is not a member of Ghost';

try {
const response = ghostAPI.userExistInGhost(_id).users[0];

if (response.exist) {
return ghostAPI.redirectToPublicLink(response.slug);
}

throw new Meteor.Error(errMsg);
} catch (e) {
throw new Meteor.Error(e.error || errMsg);
}
},
});
54 changes: 54 additions & 0 deletions app/articles/server/settings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { Meteor } from 'meteor/meteor';

import { settings } from '../../settings';

Meteor.startup(() => {
settings.addGroup('Articles', function() {
this.add('Articles_Enabled', false, {
type: 'boolean',
i18nLabel: 'Enable',
public: true,
});

this.add('Article_Site_Url', 'http://localhost:2368', {
type: 'string',
enableQuery: {
_id: 'Articles_Enabled',
value: true,
},
i18nLabel: 'Article_Site_Url',
public: true,
});

this.add('Announcement_Token', 'announcement_token', {
type: 'string',
enableQuery: {
_id: 'Articles_Enabled',
value: true,
},
i18nLabel: 'Announcement_Token',
public: true,
});

this.add('Settings_Token', 'articles_settings_token', {
type: 'string',
enableQuery: {
_id: 'Articles_Enabled',
value: true,
},
i18nLabel: 'Settings_Token',
public: true,
});

this.add('Articles_Admin_Panel', 'articlesAdminPanel', {
type: 'link',
enableQuery: {
_id: 'Articles_Enabled',
value: true,
},
linkText: 'Article_Admin_Panel',
i18nLabel: 'Article_Admin_Panel',
i18nDescription: 'Article_Admin_Panel_Description',
});
});
});
13 changes: 13 additions & 0 deletions app/articles/server/triggers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { callbacks } from '../../callbacks';
import { triggerHandler } from './lib/triggerHandler';
import { settings } from '../../settings';

const callbackHandler = function _callbackHandler(eventType) {
return function _wrapperFunction(...args) {
return triggerHandler.executeTrigger(eventType, ...args);
};
};

const priority = settings.get('Articles_Enabled') ? callbacks.priority.HIGH : callbacks.priority.LOW;

callbacks.add('afterDeleteUser', callbackHandler('deleteUser'), priority);
Loading