Skip to content

Commit

Permalink
add article in user info and openRoom msg action
Browse files Browse the repository at this point in the history
  • Loading branch information
shubhsherl committed Jul 17, 2019
1 parent 1febcae commit ff9eea8
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 15 deletions.
9 changes: 0 additions & 9 deletions app/articles/server/methods/admin.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http';
import _ from 'underscore';
import { Random } from 'meteor/random';

import { API } from '../utils/url';
import { settings } from '../../../settings';

const api = new API();

// Try to get a verified email, if available.
function getVerifiedEmail(emails) {
const email = _.find(emails, (e) => e.verified);
return email || emails[0].address;
}

function setupGhost(user, token) {
const rcUrl = Meteor.absoluteUrl().replace(/\/$/, '');
const blogTitle = settings.get('Article_Site_title');
Expand All @@ -27,8 +20,6 @@ function setupGhost(user, token) {
rc_url: rcUrl,
rc_id: user._id,
rc_token: token,
name: user.name,
email: getVerifiedEmail(user.emails),
announce_token: announceToken,
settings_token: settingsToken,
blogTitle,
Expand Down
44 changes: 38 additions & 6 deletions app/articles/server/methods/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ function addUser(user, accessToken) {
return HTTP.call('POST', api.createAccount(), { data, headers: { 'Content-Type': 'application/json' } });
}

function userExist(user, accessToken) {
function userExist(id) {
const data = {
user: [{
rc_uid: user._id,
rc_token: accessToken,
rc_uid: id,
}],
};
const response = HTTP.call('GET', api.userExist(), { data, headers: { 'Content-Type': 'application/json' } });
return response.data && response.data.users[0] && response.data.users[0].exist;
return response.data;
}

function inviteSetting() {
Expand All @@ -47,6 +46,13 @@ function redirectGhost() {
};
}

function redirectUser(slug) {
return {
link: api.authorUrl(slug),
message: 'Redirecting.',
};
}

Meteor.methods({
redirectUserToArticles(accessToken) {
const enabled = settings.get('Articles_enabled');
Expand All @@ -61,11 +67,16 @@ Meteor.methods({
const response = HTTP.call('GET', api.setup());

if (response.data.setup[0].status) { // Ghost site is already setup
// user exist in ghost
if (userExist(user, accessToken)) {
const u = userExist(user._id).users[0];

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

if (u.exist) { // user exist but suspended
throw new Meteor.Error('You are suspended from Ghost');
}

const inviteOnly = inviteSetting();

// create user account in ghost
Expand All @@ -82,4 +93,25 @@ Meteor.methods({
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('Articles are disabled');
}
const errMsg = 'User is not a member of Ghost';

try {
const u = userExist(_id).users[0];

if (u.exist) {
return redirectUser(u.slug);
}

throw new Meteor.Error(errMsg);
} catch (e) {
throw new Meteor.Error(e.error || errMsg);
}
},
});
5 changes: 5 additions & 0 deletions app/articles/server/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export class API {
return `${ base }/ghost`;
}

authorUrl(slug) {
const base = settings.get('Article_Site_Url').replace(/\/$/, '');
return `${ base }/author/${ slug }`;
}

setup() {
return this.buildAPIUrl('authentication', 'setup');
}
Expand Down
2 changes: 2 additions & 0 deletions app/lib/server/functions/insertMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ const validateAttachmentsActions = (attachmentActions) => {
webview_height_ratio: String,
msg: String,
msg_in_chat_window: Boolean,
open_room_by_id: Boolean,
rid: String,
}));
};

Expand Down
2 changes: 2 additions & 0 deletions app/lib/server/functions/sendMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ const validateAttachmentsActions = (attachmentActions) => {
webview_height_ratio: String,
msg: String,
msg_in_chat_window: Boolean,
open_room_by_id: Boolean,
rid: String,
}));
};

Expand Down
10 changes: 10 additions & 0 deletions app/message-action/client/messageAction.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
<img class="image-button" src="{{image_url}}" />
</button>
{{/if}}
{{#if open_room_by_id}}
<button class="{{jsActionButtonClassname msg_processing_type}}" value="{{rid}}">
<img class="image-button" src="{{image_url}}" />
</button>
{{/if}}
{{/if}}
{{#if text}}
{{#if url}}
Expand All @@ -25,6 +30,11 @@
<span class="overflow-ellipsis">{{text}}</span>
</button>
{{/if}}
{{#if open_room_by_id}}
<button class="text-button {{jsActionButtonClassname msg_processing_type}}" value="{{rid}}">
<span class="overflow-ellipsis">{{text}}</span>
</button>
{{/if}}
{{/if}}
{{/if}}
</div>
Expand Down
18 changes: 18 additions & 0 deletions app/ui-flextab/client/tabs/userActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ export const getActions = ({ user, directActions, hideAdminControls }) => {
},
},

{
icon: 'articles',
name: t('Articles'),
action: prevent(getUser, ({ _id }) =>
Meteor.call('redirectToUsersArticles', _id, (error, result) => {
if (error) {
return handleError(error);
}
const redirectWindow = window.open(result.link, '_blank');
toastr.success(result.message, 'Success');
redirectWindow.location;
})
),
condition() {
return settings.get('Articles_enabled');
},
},

function() {
if (isSelf(this.username) || !directActions) {
return;
Expand Down
8 changes: 8 additions & 0 deletions app/ui/client/views/app/room.js
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,14 @@ Template.room.events({

await call('sendMessage', msgObject);
},
'click .js-actionButton-openRoom'(event) {
const rid = event.currentTarget.value;
if (!rid) {
return;
}

FlowRouter.goToRoomById(rid);
},
'click .js-actionButton-respondWithMessage'(event, instance) {
const rid = instance.data._id;
const msg = event.currentTarget.value;
Expand Down

0 comments on commit ff9eea8

Please sign in to comment.