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

Service account tests #15

Open
wants to merge 21 commits into
base: service-accounts
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
8e87c83
Unread counter added in popver
bhardwajaditya Jun 29, 2019
5a28d41
Get linked service account method added
bhardwajaditya Jul 1, 2019
b3fc0e4
Merge branch 'service-accounts' of https://github.com/bhardwajaditya/…
bhardwajaditya Jul 1, 2019
a0ca489
Added unread counter
bhardwajaditya Jul 8, 2019
38c2630
Fixed CLI errors
bhardwajaditya Jul 10, 2019
fab9cb0
Service account message alert added
bhardwajaditya Jul 17, 2019
6b7082c
Lint errors fixed
bhardwajaditya Jul 17, 2019
5e121d7
Service Account Notification fixed
bhardwajaditya Jul 17, 2019
0135dc2
Username exists error test for service account added
bhardwajaditya Jul 23, 2019
fe7709c
Merged with base branch
bhardwajaditya Aug 5, 2019
c5dadc8
Merge branch 'service-accounts' of https://github.com/bhardwajaditya/…
bhardwajaditya Aug 17, 2019
dede47c
Merge branch 'service-account-notification' of https://github.com/bha…
bhardwajaditya Aug 19, 2019
5e2a763
Service Accounts Server tests added
bhardwajaditya Aug 19, 2019
0495313
Lint errors fixed
bhardwajaditya Aug 19, 2019
c090234
lint errors fixed
bhardwajaditya Aug 20, 2019
c75c284
Test error fixed
bhardwajaditya Aug 21, 2019
b133c36
Sidebar header condition modified
bhardwajaditya Aug 21, 2019
0e90341
Merge branch 'service-account-notification' of https://github.com/bha…
bhardwajaditya Aug 21, 2019
1dfde53
Test errors fixed
bhardwajaditya Aug 24, 2019
06ea7a4
Test error fixed
bhardwajaditya Aug 24, 2019
94cddcc
Token error fixed
bhardwajaditya Aug 26, 2019
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
27 changes: 27 additions & 0 deletions app/lib/server/functions/notifications/serviceAccount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { metrics } from '../../../../metrics';
import { Notifications } from '../../../../notifications';

export function shouldNotifyServiceAccountOwner({
hasMentionToAll,
hasMentionToHere,
isHighlighted,
hasMentionToUser,
hasReplyToThread,
roomType,
}) {
return roomType === 'd' || hasMentionToAll || hasMentionToHere || isHighlighted || hasMentionToUser || hasReplyToThread;
}

export function notifyServiceAccountOwner(receiver, ownerId, message, room) {
metrics.notificationsSent.inc({ notification_type: 'sa' });
Notifications.notifyUser(ownerId, 'sa-notification', {
payload: {
_id: message._id,
rid: message.rid,
sender: message.u,
receiver: receiver.username,
type: room.t,
name: room.name,
},
});
}
14 changes: 14 additions & 0 deletions app/lib/server/lib/sendNotificationsOnMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { sendEmail, shouldNotifyEmail } from '../functions/notifications/email';
import { sendSinglePush, shouldNotifyMobile } from '../functions/notifications/mobile';
import { notifyDesktopUser, shouldNotifyDesktop } from '../functions/notifications/desktop';
import { notifyAudioUser, shouldNotifyAudio } from '../functions/notifications/audio';
import { notifyServiceAccountOwner, shouldNotifyServiceAccountOwner } from '../functions/notifications/serviceAccount';

export const sendNotification = async ({
subscription,
Expand Down Expand Up @@ -46,6 +47,7 @@ export const sendNotification = async ({
status: 1,
statusConnection: 1,
username: 1,
u: 1,
},
}),
];
Expand Down Expand Up @@ -149,6 +151,17 @@ export const sendNotification = async ({
return false;
});
}

if (receiver.u && shouldNotifyServiceAccountOwner({
hasMentionToAll,
hasMentionToHere,
isHighlighted,
hasMentionToUser,
hasReplyToThread,
roomType,
})) {
notifyServiceAccountOwner(receiver, receiver.u._id, message, room);
}
};

const project = {
Expand All @@ -168,6 +181,7 @@ const project = {
'receiver.status': 1,
'receiver.statusConnection': 1,
'receiver.username': 1,
'receiver.u': 1,
},
};

Expand Down
43 changes: 43 additions & 0 deletions app/service-accounts/client/stylesheets/serviceAccounts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.rc-service-account-list {
&__user {

display: flex;

padding: 8px 0;

cursor: pointer;
align-items: center;

&.active,
&:hover {
background-color: #eeeeee;

& .rc-member-list__menu {
opacity: 1;
}
}

& > .avatar {
width: var(--sidebar-account-thumb-size);
height: var(--sidebar-account-thumb-size);
}
}

&__username {
display: flex;
flex: 1 1 auto;

margin: 0 12px 0 8px;

font-size: 16px;
align-items: center;
}

&__username-alert {
font-weight: bold;
}
}

.service-account-notification {
color: var(--rc-color-alert-message-primary-background);
}
49 changes: 31 additions & 18 deletions app/service-accounts/client/views/serviceAccountSidebarLogin.html
Original file line number Diff line number Diff line change
@@ -1,26 +1,39 @@
<template name="serviceAccountSidebarLogin">
<div class="rc-popover__column">
<ul class="rc-popover__list">
{{#if showOwnerAccountLink}}
<p>Go back to main account</p>
<li class='rc-member-list__user js-login'>
{{> avatar username=owner.username}}
<div class="rc-member-list__username">
{{owner.username}}
</div>
</li>
{{#if loading}}
<p>Loading ... </p>
{{else}}
{{#if hasServiceAccounts}}
{{#each users}}
<li class='rc-member-list__user js-login'>
{{> avatar username=username}}
<div class="rc-member-list__username">
{{username}}
</div>
</li>
{{/each}}
{{#if showOwnerAccountLink}}
<p>Go back to main account</p>
<li class='rc-service-account-list__user js-login'>
{{> avatar username=owner.username}}
<div class="rc-service-account-list__username">
{{owner.username}}
</div>
</li>
{{else}}
<p>{{_ "You_have_no_service_accounts"}}</p>
{{#if hasServiceAccounts}}
{{#each users}}
<li class='rc-service-account-list__user js-login'>
{{> avatar username=username}}
<div class="rc-service-account-list__username">
{{#if receivedNewMessage username}}
<span class="rc-service-account-list__username-alert">
{{username}}
</span>
{{else}}
{{username}}
{{/if}}
</div>
<div class="badge">
{{unread}}
</div>
</li>
{{/each}}
{{else}}
<p>{{_ "You_have_no_service_accounts"}}</p>
{{/if}}
{{/if}}
{{/if}}
</ul>
Expand Down
43 changes: 27 additions & 16 deletions app/service-accounts/client/views/serviceAccountSidebarLogin.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { Template } from 'meteor/templating';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Session } from 'meteor/session';

import { handleError } from '../../../utils';
import { callbacks } from '../../../callbacks';
import FullUser from '../../../models/client/models/FullUser';
import './serviceAccountSidebarLogin.html';
import { popover } from '../../../ui-utils/client';

Template.serviceAccountSidebarLogin.helpers({
isReady() {
const instance = Template.instance();
return instance.ready && instance.ready.get();
loading() {
return Template.instance().loading.get();
},
users() {
return Template.instance().users();
return Template.instance().users.get();
},
hasServiceAccounts() {
return Template.instance().users() && Template.instance().users().length > 0;
return Template.instance().users.get() && Template.instance().users.get().length > 0;
},
owner() {
return Meteor.user() && Meteor.user().u;
},
showOwnerAccountLink() {
return localStorage.getItem('serviceAccountForceLogin') && Meteor.user() && !!Meteor.user().u;
},
receivedNewMessage(username) {
if (Template.instance().notifiedServiceAccount) {
return username === Template.instance().notifiedServiceAccount.get();
}
return false;
},
});

Template.serviceAccountSidebarLogin.events({
Expand Down Expand Up @@ -62,16 +68,21 @@ Template.serviceAccountSidebarLogin.events({
Template.serviceAccountSidebarLogin.onCreated(function() {
const instance = this;
this.ready = new ReactiveVar(true);
this.users = new ReactiveVar([]);
this.loading = new ReactiveVar(true);
this.notifiedServiceAccount = new ReactiveVar('');
instance.notifiedServiceAccount.set(Session.get('saMessageReceiver'));
Session.delete('saMessageReceiver');
Session.delete('saNotification');
this.autorun(() => {
const subscription = instance.subscribe('userServiceAccounts');
instance.ready.set(subscription.ready());
instance.loading.set(true);
Meteor.call('getLinkedServiceAccounts', function(err, serviceAccounts) {
if (err) {
this.loading.set(false);
return handleError(err);
}
instance.users.set(serviceAccounts);
instance.loading.set(false);
});
});
this.users = function() {
const query = {
'u._id': Meteor.userId(),
active: true,
};
const limit = instance.limit && instance.limit.get();
return FullUser.find(query, { limit, sort: { username: 1, name: 1 } }).fetch();
};
});
29 changes: 29 additions & 0 deletions app/service-accounts/server/api/v1/serviceAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,32 @@ API.v1.addRoute('serviceAccounts.create', { authRequired: true }, {
return API.v1.success({ user: Users.findOneByUsername(this.bodyParams.username, { fields: API.v1.defaultFieldsToExclude }) });
},
});

API.v1.addRoute('serviceAccounts.getLinkedAccounts', { authRequired: true }, {
get() {
let serviceAccounts;
Meteor.runAsUser(this.userId, () => {
serviceAccounts = Meteor.call('getLinkedServiceAccounts');
});

if (!serviceAccounts) {
return API.v1.failure();
}
return API.v1.success({ serviceAccounts });
},
});

API.v1.addRoute('serviceAccounts.getToken', { authRequired: true }, {
get() {
const { username } = this.queryParams;
let token;
Meteor.runAsUser(this.userId, () => {
token = Meteor.call('getLoginToken', username);
});

if (!token) {
return API.v1.failure();
}
return API.v1.success({ token });
},
});
2 changes: 1 addition & 1 deletion app/service-accounts/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import './api/rest';
import './methods/usernameExists';
import './methods/addServiceAccount';
import './methods/getLoginToken';
import './methods/getLinkedServiceAccounts';

import './hooks/serviceAccountCallback';
import './hooks/serviceAccountBroadcast';

import './publications/fullServiceAccountData';
import './publications/userServiceAccounts';

import '../lib/serviceAccountRoomType';
22 changes: 22 additions & 0 deletions app/service-accounts/server/methods/getLinkedServiceAccounts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Meteor } from 'meteor/meteor';

import { Users, Subscriptions } from '../../../models';
import { getDefaultUserFields } from '../../../utils/server/functions/getDefaultUserFields';

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

const query = {
'u._id': Meteor.userId(),
active: true,
};
const result = Users.find(query, { fields: getDefaultUserFields() }).fetch();
result.forEach((serviceAccount) => {
serviceAccount.unread = Subscriptions.findUnreadByUserId(serviceAccount._id).count();
});
return result;
},
});
25 changes: 0 additions & 25 deletions app/service-accounts/server/publications/userServiceAccounts.js

This file was deleted.

10 changes: 8 additions & 2 deletions app/ui-sidenav/client/sidebarHeader.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
</div>
<div class="sidebar__toolbar">
{{#each toolbarButtons}}
<button class="sidebar__toolbar-button rc-tooltip rc-tooltip--down js-button" aria-label="{{name}}">
{{> icon block="sidebar__toolbar-button-icon" icon=icon }}
<button class=" sidebar__toolbar-button rc-tooltip rc-tooltip--down js-button" aria-label="{{name}}">
{{#if active}}
<span class="service-account-notification">
{{> icon block="sidebar__toolbar-button-icon" icon=icon }}
</span>
{{else}}
{{> icon block="sidebar__toolbar-button-icon" icon=icon }}
{{/if}}
</button>
{{/each}}
</div>
Expand Down
2 changes: 2 additions & 0 deletions app/ui-sidenav/client/sidebarHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Meteor } from 'meteor/meteor';
import { ReactiveVar } from 'meteor/reactive-var';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Template } from 'meteor/templating';
import { Session } from 'meteor/session';

import { popover, AccountBox, menu, SideNav, modal } from '../../ui-utils';
import { t, getUserPreference, handleError } from '../../utils';
Expand Down Expand Up @@ -74,6 +75,7 @@ const toolbarButtons = (user) => [{
{
name: t('Service_account_login'),
icon: 'reload',
active: Session.get('saNotification'),
condition: () => (Meteor.user() && !Meteor.user().u) || (Meteor.user() && Meteor.user().u && localStorage.getItem('serviceAccountForceLogin')),
action: (e) => {
const options = [];
Expand Down
1 change: 1 addition & 0 deletions client/importsCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ import '../app/ui-vrecord/client/vrecord.css';
import '../app/videobridge/client/stylesheets/video.less';
import '../app/wordpress/client/wordpress-login-button.css';
import '../app/katex/katex.min.css';
import '../app/service-accounts/client/stylesheets/serviceAccounts.css';
5 changes: 5 additions & 0 deletions client/notifications/notification.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ Meteor.startup(function() {
Notifications.onUser('subscriptions-changed', (action, sub) => {
notifyNewRoom(sub);
});

Notifications.onUser('sa-notification', (notification) => {
Session.set('saNotification', true);
Session.set('saMessageReceiver', notification.payload.receiver);
});
}
});
});
Loading