Unverified Commit f7fbffbd authored by Bradley Hilton's avatar Bradley Hilton
Browse files

The out of office application now works

parent 8ff8026b
export interface IOutOfOfficeStorage {
out: boolean;
message: string;
}
import { IHttp, IMessageBuilder, IPersistence, IRead } from '@rocket.chat/apps-ts-definition/accessors';
import {
IConfigurationExtend,
IEnvironmentRead,
IHttp,
IMessageBuilder,
IPersistence,
IRead,
} from '@rocket.chat/apps-ts-definition/accessors';
import { App } from '@rocket.chat/apps-ts-definition/App';
import { IMessage, IPostMessageSent, IPreMessageSentModify } from '@rocket.chat/apps-ts-definition/messages';
import { RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-ts-definition/metadata';
import { RoomType } from '@rocket.chat/apps-ts-definition/rooms';
import { IOutOfOfficeStorage } from './IOutOfOfficeStorage';
import { OutOfOfficeCommand} from './OutOfOfficeCommand';
export class OutOfOfficeApp extends App implements IPostMessageSent {
public checkPostMessageSent(message: IMessage, read: IRead, http: IHttp): boolean {
// We don't auto-respond to rooms beside direct messages
......@@ -15,6 +26,35 @@ export class OutOfOfficeApp extends App implements IPostMessageSent {
public executePostMessageSent(message: IMessage, read: IRead,
http: IHttp, persistence: IPersistence): void {
this.getLogger().log(message.room.type, message.sender.username, message.room.usernames);
const otherUsers = message.room.usernames.filter((u) => u !== message.sender.username);
if (otherUsers.length !== 1) {
// We don't care if there isn't one other person in the room
return;
}
const otherUser = read.getUserReader().getByUsername(otherUsers[0]);
const assoc = new RocketChatAssociationRecord(RocketChatAssociationModel.USER, otherUser.id);
const awayDatas = read.getPersistenceReader().readByAssociation(assoc);
if (awayDatas.length === 0) {
// The user is not marked as away
return;
}
const data = awayDatas[0] as IOutOfOfficeStorage;
const msg = read.getNotifier().getMessageBuilder().setText(otherUser.username +
' is currently *out of office*, however they left the following message:\n\n>' +
data.message)
.setUsernameAlias('Out of Office').setEmojiAvatar(':calendar:')
.setRoom(message.room).setSender(message.sender).getMessage();
read.getNotifier().notifyUser(message.sender, msg);
this.getLogger().log(otherUser.username +
' is currently *out of office*, however they left the following message:\n\n>"' + data.message + '"');
}
protected extendConfiguration(configuration: IConfigurationExtend, environmentRead: IEnvironmentRead): void {
configuration.slashCommands.provideSlashCommand(new OutOfOfficeCommand());
}
}
import { IHttp, IModify, IPersistence, IRead } from '@rocket.chat/apps-ts-definition/accessors';
import { IMessage } from '@rocket.chat/apps-ts-definition/messages';
import { RocketChatAssociationModel, RocketChatAssociationRecord } from '@rocket.chat/apps-ts-definition/metadata';
import { ISlashCommand, SlashCommandContext } from '@rocket.chat/apps-ts-definition/slashcommands';
import { IOutOfOfficeStorage } from './IOutOfOfficeStorage';
export class OutOfOfficeCommand implements ISlashCommand {
public command = 'out-of-office';
public paramsExample = 'outOfOfficeParamExample';
public i18nDescription = 'todo2';
// tslint:disable-next-line:max-line-length
public executor(context: SlashCommandContext, read: IRead, modify: IModify, http: IHttp, persis: IPersistence): void {
switch (context.getArguments().length) {
case 0:
return this.invalidUsageHandler(context, modify);
case 1:
return this.handleStatusArgOnly(context, read, modify, persis);
default:
return this.handleWithCustomMessage(context, read, modify, persis);
}
}
private invalidUsageHandler(context: SlashCommandContext, modify: IModify): void {
this.sendNotifyMessage(context, modify, 'Invalid usage of the Out of Office command. ' +
'Please provide whether you are `out` or `in`, with the message optional if you are away.');
}
// tslint:disable-next-line:max-line-length
private handleStatusArgOnly(context: SlashCommandContext, read: IRead, modify: IModify, persis: IPersistence): void {
const assoc = new RocketChatAssociationRecord(RocketChatAssociationModel.USER, context.getSender().id);
const data: IOutOfOfficeStorage = {
out: true,
// tslint:disable-next-line:max-line-length
message: 'I am currently out of office and unable to respond to your message _(this is an automated response via a Rocket.Chat App)_.',
};
switch (context.getArguments()[0].toLowerCase()) {
case 'in':
persis.removeByAssociation(assoc);
// TODO: Maybe say something different if they weren't away to come back lol
return this.sendNotifyMessage(context, modify, `Welcome back, ${ context.getSender().username }!`);
case 'out':
persis.createWithAssociation(data, assoc);
return this.sendNotifyMessage(context, modify,
'You are marked as *Out of Office*, we will see you when you get back.');
case 'status':
if (read.getPersistenceReader().readByAssociation(assoc).length > 0) {
return this.sendNotifyMessage(context, modify, 'You are currently *out of office*.');
} else {
return this.sendNotifyMessage(context, modify, 'You are currently *in office*.');
}
default:
return this.sendNotifyMessage(context, modify,
'No idea what you are talking about. ' +
'Only `out`, `in` and `status` are accepted options for the first argument.');
}
}
// tslint:disable-next-line:max-line-length
private handleWithCustomMessage(context: SlashCommandContext, read: IRead, modify: IModify, persis: IPersistence): void {
const action = context.getArguments()[0].toLowerCase();
if (action === 'in' || action === 'status') {
return this.handleStatusArgOnly(context, read, modify, persis);
} else if (action !== 'out') {
return this.sendNotifyMessage(context, modify,
'No idea what you are talking about. ' +
'Only `out`, `in` and `status` are accepted options for the first argument.');
}
const args = Array.from(context.getArguments());
args.splice(0, 1); // Removing the action
const assoc = new RocketChatAssociationRecord(RocketChatAssociationModel.USER, context.getSender().id);
const data: IOutOfOfficeStorage = {
out: true,
message: args.join(' '),
};
// Allow setting their status again if they're currently marked as away
if (read.getPersistenceReader().readByAssociation(assoc).length > 0) {
persis.removeByAssociation(assoc);
}
persis.createWithAssociation(data, assoc);
return this.sendNotifyMessage(context, modify,
'You are marked as *Out of Office*, we will see you when you get back. ' +
'The message being sent to others when they contact you is: "' +
data.message + '"');
}
private sendNotifyMessage(context: SlashCommandContext, modify: IModify, text: string): void {
const msg = modify.getCreator().startMessage().setText(text)
.setUsernameAlias('Out of Office').setEmojiAvatar(':calendar:')
.setRoom(context.getRoom()).setSender(context.getSender()).getMessage();
return modify.getNotifer().notifyUser(context.getSender(), msg);
}
}
......@@ -3,8 +3,8 @@
"name": "Out of Office Responder",
"nameSlug": "out-of-office",
"description": "Provides a command and auto responder for being out of office",
"version": "0.0.5",
"requiredApiVersion": ">=0.7.8",
"version": "0.0.19",
"requiredApiVersion": ">=0.7.12",
"author": {
"name": "Bradley Hilton",
"homepage": "https://github.com/RocketChat/Rocket.Chat.Apps-dev-environment",
......
{
"outOfOfficeParamExample": "out|in|status <Your away message goes here (optional)>"
}
......@@ -28,11 +28,11 @@
}
},
"@rocket.chat/apps-engine": {
"version": "0.4.0",
"resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-0.4.0.tgz",
"integrity": "sha512-TKnBnPWBI8isse7Xxbkxy8IgKRfn8aBbF0jYKP6CiKjYC05Thq4ms7RMnANXXiIaeH49mddn1orBBq77Obj0Wg==",
"version": "0.4.8",
"resolved": "https://registry.npmjs.org/@rocket.chat/apps-engine/-/apps-engine-0.4.8.tgz",
"integrity": "sha512-QZ+VNeMf4RDWKTd8O6t8/enwOSNVla6hcpvZYAb9nd8D2Syi6RstcLCYCFjaM+Vc6EX5Dscv4nUjXJtsY/2KOw==",
"requires": {
"@rocket.chat/apps-ts-definition": "0.7.9",
"@rocket.chat/apps-ts-definition": "0.7.15",
"adm-zip": "0.4.7",
"semver": "5.5.0",
"stack-trace": "0.0.10",
......@@ -48,9 +48,9 @@
}
},
"@rocket.chat/apps-ts-definition": {
"version": "0.7.9",
"resolved": "https://registry.npmjs.org/@rocket.chat/apps-ts-definition/-/apps-ts-definition-0.7.9.tgz",
"integrity": "sha512-8ZPDr5xLskcMTipKtdQB/Bs4pxMN4YKDDbzK4DnI2rX1nQhKqULuF58uIuwg+/75eQv0Zb3HkJ+ZcjKXUgH5og=="
"version": "0.7.15",
"resolved": "https://registry.npmjs.org/@rocket.chat/apps-ts-definition/-/apps-ts-definition-0.7.15.tgz",
"integrity": "sha512-mOD1C/bTfeOY77/Rnlq8nLuCQSUTq8qeHH604DiehJJGD3gToEt2yIAbKOdFrkoOCV2T5z6eXzyBifMjsreAbQ=="
},
"@types/body-parser": {
"version": "1.16.4",
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment