@ -1,4 +1,5 @@
import { Meteor } from 'meteor/meteor' ;
import { check } from 'meteor/check' ;
import { settings } from '../../settings' ;
import { callbacks } from '../../callbacks' ;
@ -28,6 +29,8 @@ const shouldAdd = (attachments, attachment) => !attachments.some(({ message_link
Meteor . methods ( {
pinMessage ( message , pinnedAt ) {
check ( message . _id , String ) ;
const userId = Meteor . userId ( ) ;
if ( ! userId ) {
throw new Meteor . Error ( 'error-invalid-user' , 'Invalid user' , {
@ -42,30 +45,34 @@ Meteor.methods({
} ) ;
}
if ( ! hasPermission ( Meteor . userId ( ) , 'pin-message' , message . rid ) ) {
throw new Meteor . Error ( 'not-authorized' , 'Not Authorized' , { method : 'pinMessage' } ) ;
let originalMessage = Messages . findOneById ( message . _id ) ;
if ( originalMessage == null || originalMessage . _id == null ) {
throw new Meteor . Error ( 'error-invalid-message' , 'Message you are pinning was not found' , {
method : 'pinMessage' ,
action : 'Message_pinning' ,
} ) ;
}
const subscription = Subscriptions . findOneByRoomIdAndUserId ( message . rid , Meteor . userId ( ) , { fields : { _id : 1 } } ) ;
const subscription = Subscriptions . findOneByRoomIdAndUserId ( originalM essage. rid , Meteor . userId ( ) , { fields : { _id : 1 } } ) ;
if ( ! subscription ) {
return false ;
}
let originalMessage = Messages . findOneById ( message . _id ) ;
if ( originalMessage == null || originalMessage . _id == null ) {
// If it's a valid message but on a room that the user is not subscribed to, report that the message was not found.
throw new Meteor . Error ( 'error-invalid-message' , 'Message you are pinning was not found' , {
method : 'pinMessage' ,
action : 'Message_pinning' ,
} ) ;
}
if ( ! hasPermission ( Meteor . userId ( ) , 'pin-message' , originalMessage . rid ) ) {
throw new Meteor . Error ( 'not-authorized' , 'Not Authorized' , { method : 'pinMessage' } ) ;
}
const me = Users . findOneById ( userId ) ;
// If we keep history of edits, insert a new message to store history information
if ( settings . get ( 'Message_KeepHistory' ) ) {
Messages . cloneAndSaveAsHistoryById ( message . _id , me ) ;
}
const room = Meteor . call ( 'canAccessRoom' , m essage. rid , Meteor . userId ( ) ) ;
const room = Meteor . call ( 'canAccessRoom' , originalM essage. rid , Meteor . userId ( ) ) ;
originalMessage . pinned = true ;
originalMessage . pinnedAt = pinnedAt || Date . now ;
@ -110,6 +117,8 @@ Meteor.methods({
) ;
} ,
unpinMessage ( message ) {
check ( message . _id , String ) ;
if ( ! Meteor . userId ( ) ) {
throw new Meteor . Error ( 'error-invalid-user' , 'Invalid user' , {
method : 'unpinMessage' ,
@ -123,24 +132,27 @@ Meteor.methods({
} ) ;
}
if ( ! hasPermission ( Meteor . userId ( ) , 'pin-message' , message . rid ) ) {
throw new Meteor . Error ( 'not-authorized' , 'Not Authorized' , { method : 'pinMessage' } ) ;
let originalMessage = Messages . findOneById ( message . _id ) ;
if ( originalMessage == null || originalMessage . _id == null ) {
throw new Meteor . Error ( 'error-invalid-message' , 'Message you are unpinning was not found' , {
method : 'unpinMessage' ,
action : 'Message_pinning' ,
} ) ;
}
const subscription = Subscriptions . findOneByRoomIdAndUserId ( message . rid , Meteor . userId ( ) , { fields : { _id : 1 } } ) ;
const subscription = Subscriptions . findOneByRoomIdAndUserId ( originalM essage. rid , Meteor . userId ( ) , { fields : { _id : 1 } } ) ;
if ( ! subscription ) {
return false ;
}
let originalMessage = Messages . findOneById ( message . _id ) ;
if ( originalMessage == null || originalMessage . _id == null ) {
// If it's a valid message but on a room that the user is not subscribed to, report that the message was not found.
throw new Meteor . Error ( 'error-invalid-message' , 'Message you are unpinning was not found' , {
method : 'unpinMessage' ,
action : 'Message_pinning' ,
} ) ;
}
if ( ! hasPermission ( Meteor . userId ( ) , 'pin-message' , originalMessage . rid ) ) {
throw new Meteor . Error ( 'not-authorized' , 'Not Authorized' , { method : 'unpinMessage' } ) ;
}
const me = Users . findOneById ( Meteor . userId ( ) ) ;
// If we keep history of edits, insert a new message to store history information
@ -154,7 +166,7 @@ Meteor.methods({
username : me . username ,
} ;
originalMessage = callbacks . run ( 'beforeSaveMessage' , originalMessage ) ;
const room = Meteor . call ( 'canAccessRoom' , m essage. rid , Meteor . userId ( ) ) ;
const room = Meteor . call ( 'canAccessRoom' , originalM essage. rid , Meteor . userId ( ) ) ;
if ( isTheLastMessage ( room , message ) ) {
Rooms . setLastMessagePinned ( room . _id , originalMessage . pinnedBy , originalMessage . pinned ) ;
}