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

[NEW] Adds ability for Rocket.Chat Apps to create discussions #16683

Merged
merged 13 commits into from
Apr 17, 2020
Merged
35 changes: 33 additions & 2 deletions app/apps/server/bridges/rooms.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Meteor } from 'meteor/meteor';
import { RoomType } from '@rocket.chat/apps-engine/definition/rooms';
import { Meteor } from 'meteor/meteor';

import { Rooms, Subscriptions, Users } from '../../../models/server';
import { addUserToRoom } from '../../../lib/server/functions/addUserToRoom';
import { Rooms, Subscriptions, Users } from '../../../models/server';

export class AppRoomBridge {
constructor(orch) {
Expand Down Expand Up @@ -120,4 +120,35 @@ export class AppRoomBridge {
addUserToRoom(rm._id, member);
}
}

async createDiscussion(room, parentMessage = null, reply = '', members = [], appId) {
this.orch.debugLog(`The App ${ appId } is creating a new discussion.`, room);

const rcRoom = this.orch.getConverters().get('rooms').convertAppRoom(room);

let rcMessage;
if (parentMessage) {
rcMessage = this.orch.getConverters().get('messages').convertAppMessage(parentMessage);
}

if (!rcRoom.prid || !Rooms.findOneById(rcRoom.prid)) {
throw new Error('There must be a parent room to create a discussion.');
}

const discussion = {
prid: rcRoom.prid,
t_name: rcRoom.fname,
pmid: rcMessage ? rcMessage._id : undefined,
reply: reply && reply.trim() !== '' ? reply : undefined,
users: members.length > 0 ? members : [],
};

let rid;
Meteor.runAsUser(room.creator.id, () => {
const info = Meteor.call('createDiscussion', discussion);
rid = info.rid;
});

return rid;
}
}
11 changes: 11 additions & 0 deletions app/apps/server/converters/rooms.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export class AppRoomsConverter {
closedAt: room.closedAt,
lm: room.lastModifiedAt,
customFields: room.customFields,
prid: typeof room.parentRoom === 'undefined' ? undefined : room.parentRoom.id,
};

return Object.assign(newRoom, room._unmappedProperties_);
Expand Down Expand Up @@ -195,7 +196,17 @@ export class AppRoomsConverter {

return this.orch.getConverters().get('users').convertById(responseBy._id);
},
parentRoom: (room) => {
const { prid } = room;

if (!prid) {
return undefined;
}

delete room.prid;

return this.orch.getConverters().get('rooms').convertById(prid);
},
};

return transformMappedData(room, map);
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
"@nivo/heatmap": "^0.61.0",
"@nivo/line": "^0.61.1",
"@nivo/pie": "^0.61.1",
"@rocket.chat/apps-engine": "^1.13.0",
"@rocket.chat/apps-engine": "^1.14.0-beta.3119",
"@rocket.chat/fuselage": "^0.7.1",
"@rocket.chat/fuselage-hooks": "^0.7.1",
"@rocket.chat/fuselage-polyfills": "^0.7.1",
Expand Down