diff --git a/app/discussion/server/methods/createDiscussion.js b/app/discussion/server/methods/createDiscussion.js index b15a057adedb..e4287b86a45e 100644 --- a/app/discussion/server/methods/createDiscussion.js +++ b/app/discussion/server/methods/createDiscussion.js @@ -5,6 +5,7 @@ import { hasAtLeastOnePermission, canAccessRoom } from '../../../authorization/s import { Messages, Rooms } from '../../../models/server'; import { createRoom, addUserToRoom, sendMessage, attachMessage } from '../../../lib/server'; import { settings } from '../../../settings/server'; +import { roomTypes } from '../../../utils/server'; const getParentRoom = (rid) => { const room = Rooms.findOne(rid); @@ -86,7 +87,8 @@ const create = ({ prid, pmid, t_name, reply, users }) => { // auto invite the replied message owner const invitedUsers = message ? [message.u.username, ...users] : users; - const discussion = createRoom(p_room.t, name, user.username, [...new Set(invitedUsers)], false, { + const type = roomTypes.getConfig(p_room.t).getDiscussionType(); + const discussion = createRoom(type, name, user.username, [...new Set(invitedUsers)], false, { fname: t_name, description: message.msg, // TODO discussions remove topic: p_room.name, // TODO discussions remove diff --git a/app/lib/lib/roomTypes/public.js b/app/lib/lib/roomTypes/public.js index 11d688731eff..8732e6d13d9c 100644 --- a/app/lib/lib/roomTypes/public.js +++ b/app/lib/lib/roomTypes/public.js @@ -133,4 +133,8 @@ export class PublicRoomType extends RoomTypeConfig { return getAvatarURL({ username: `@${ this.roomName(roomData) }` }); } + + getDiscussionType() { + return 'c'; + } } diff --git a/app/utils/lib/RoomTypeConfig.js b/app/utils/lib/RoomTypeConfig.js index da578a82402e..bc40944c6b3a 100644 --- a/app/utils/lib/RoomTypeConfig.js +++ b/app/utils/lib/RoomTypeConfig.js @@ -299,4 +299,8 @@ export class RoomTypeConfig { openCustomProfileTab() { return false; } + + getDiscussionType() { + return 'p'; + } } diff --git a/server/startup/migrations/index.js b/server/startup/migrations/index.js index 60ee4afe9979..948bc590af93 100644 --- a/server/startup/migrations/index.js +++ b/server/startup/migrations/index.js @@ -180,4 +180,5 @@ import './v179'; import './v180'; import './v181'; import './v182'; +import './v183'; import './xrun'; diff --git a/server/startup/migrations/v183.js b/server/startup/migrations/v183.js new file mode 100644 index 000000000000..fd242bd4d9ef --- /dev/null +++ b/server/startup/migrations/v183.js @@ -0,0 +1,94 @@ +import { Random } from 'meteor/random'; + +import { Migrations } from '../../../app/migrations'; +import { Rooms, Messages, Subscriptions, Uploads } from '../../../app/models/server'; + +const unifyRooms = (room) => { + // verify if other DM already exists + const other = Rooms.findOne({ + _id: { $ne: room._id }, + t: 'd', + uids: room.uids, + }); + + // we need to at least change the _id of the current room, so remove it + Rooms.remove({ _id: room._id }); + + const newId = (other && other._id) || Random.id(); + + if (!other) { + // create the same room with different _id + Rooms.insert({ + ...room, + _id: newId, + }); + + // update subscription to point to new room _id + Subscriptions.update({ rid: room._id }, { + $set: { + rid: newId, + }, + }, { multi: true }); + + return newId; + } + + // the other room exists already, so just remove the subscription of the wrong room + Subscriptions.remove({ rid: room._id }); + + return newId; +}; + +const fixSelfDMs = () => { + Rooms.find({ + t: 'd', + uids: { $size: 1 }, + }).forEach((room) => { + if (!Array.isArray(room.uids) || room._id !== room.uids[0]) { + return; + } + + const correctId = unifyRooms(room); + + // move things to correct room + Messages.update({ rid: room._id }, { + $set: { + rid: correctId, + }, + }, { multi: true }); + Uploads.update({ rid: room._id }, { + $set: { + rid: correctId, + }, + }, { multi: true }); + }); +}; + +const fixDiscussions = () => { + Rooms.find({ t: 'd', prid: { $exists: true } }, { fields: { _id: 1 } }).forEach(({ _id }) => { + const { u } = Messages.findOne({ drid: _id }, { fields: { u: 1 } }) || {}; + + Rooms.update({ _id }, { + $set: { + t: 'p', + name: Random.id(), + u, + ro: false, + default: false, + sysMes: true, + }, + $unset: { + usernames: 1, + uids: 1, + }, + }); + }); +}; + +Migrations.add({ + version: 183, + up() { + fixDiscussions(); + fixSelfDMs(); + }, +});