-
Notifications
You must be signed in to change notification settings - Fork 10.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract canSendMessage function (#14909)
- Loading branch information
1 parent
7e9ba13
commit 3ce57f4
Showing
5 changed files
with
62 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,38 @@ | ||
import { Meteor } from 'meteor/meteor'; | ||
import { TAPi18n } from 'meteor/tap:i18n'; | ||
import { Random } from 'meteor/random'; | ||
|
||
import { canAccessRoom } from './canAccessRoom'; | ||
import { hasPermission } from './hasPermission'; | ||
import { Notifications } from '../../../notifications'; | ||
import { Rooms, Subscriptions } from '../../../models'; | ||
|
||
import { canAccessRoomAsync } from './canAccessRoom'; | ||
import { hasPermissionAsync } from './hasPermission'; | ||
import { Subscriptions, Rooms } from '../../../models/server/raw'; | ||
|
||
const subscriptionOptions = { | ||
projection: { | ||
blocked: 1, | ||
blocker: 1, | ||
}, | ||
}; | ||
|
||
export const canSendMessage = (rid, { uid, username }, extraData) => { | ||
const room = Rooms.findOneById(rid); | ||
export const canSendMessageAsync = async (rid, { uid, username }, extraData) => { | ||
const room = await Rooms.findOneById(rid); | ||
|
||
if (!canAccessRoom.call(this, room, { _id: uid, username }, extraData)) { | ||
throw new Meteor.Error('error-not-allowed'); | ||
if (!await canAccessRoomAsync(room, { _id: uid, username }, extraData)) { | ||
throw new Error('error-not-allowed'); | ||
} | ||
|
||
const subscription = Subscriptions.findOneByRoomIdAndUserId(rid, uid); | ||
if (subscription && (subscription.blocked || subscription.blocker)) { | ||
throw new Meteor.Error('room_is_blocked'); | ||
const subscription = await Subscriptions.findOneByRoomIdAndUserId(rid, uid, subscriptionOptions); | ||
if (subscription.blocked || subscription.blocker) { | ||
throw new Error('room_is_blocked'); | ||
} | ||
|
||
if (room.ro === true) { | ||
if (!hasPermission(Meteor.userId(), 'post-readonly', room._id)) { | ||
// Unless the user was manually unmuted | ||
if (!(room.unmuted || []).includes(username)) { | ||
Notifications.notifyUser(Meteor.userId(), 'message', { | ||
_id: Random.id(), | ||
rid: room._id, | ||
ts: new Date(), | ||
msg: TAPi18n.__('room_is_read_only'), | ||
}); | ||
|
||
throw new Meteor.Error('You can\'t send messages because the room is readonly.'); | ||
} | ||
if (room.ro === true && !await hasPermissionAsync(uid, 'post-readonly', rid)) { | ||
// Unless the user was manually unmuted | ||
if (!(room.unmuted || []).includes(username)) { | ||
throw new Error('You can\'t send messages because the room is readonly.'); | ||
} | ||
} | ||
|
||
if ((room.muted || []).includes(username)) { | ||
throw new Meteor.Error('You_have_been_muted'); | ||
throw new Error('You_have_been_muted'); | ||
} | ||
|
||
return room; | ||
}; | ||
|
||
export const canSendMessage = (rid, { uid, username }, extraData) => Promise.await(canSendMessageAsync(rid, { uid, username }, extraData)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { BaseRaw } from './BaseRaw'; | ||
|
||
export class RoomsRaw extends BaseRaw { | ||
findOneByRoomIdAndUserId(rid, uid, options) { | ||
const query = { | ||
rid, | ||
'u._id': uid, | ||
}; | ||
|
||
return this.col.findOne(query, options); | ||
} | ||
|
||
isUserInRole(uid, roleName, rid) { | ||
if (rid == null) { | ||
return; | ||
} | ||
|
||
const query = { | ||
'u._id': uid, | ||
rid, | ||
roles: roleName, | ||
}; | ||
|
||
return this.findOne(query, { fields: { roles: 1 } }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters