Skip to content

Commit

Permalink
Merge pull request #37 from cvincent/fix-group-replies
Browse files Browse the repository at this point in the history
Find group chats by chat id
  • Loading branch information
nr23730 authored Mar 26, 2020
2 parents 337c700 + a68499e commit 1bb0385
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 57 deletions.
23 changes: 6 additions & 17 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class App extends MatrixPuppetBridgeBase {
ghostIntent.client.sendReadReceipt (event);
return this.receiptHistory.delete(roomId);
} catch (err) {
debug('could not send read event', err.message);
console.log('could not send read event', err.message);
}
}

Expand All @@ -159,18 +159,7 @@ class App extends MatrixPuppetBridgeBase {
}
prepareToSend(id, matrixEvent) {
if ( id.match(/^chat/) ) {
// it's a multi party chat... we need to send to participants list
// luckily we can find out about all the ghosts (they get preloaded)
// and pull their handles down and use that to chat with the group
const roomMembers = this.puppet.getMatrixRoomMembers(matrixEvent.room_id);
const handles = roomMembers.reduce((acc, gid) => {
let tpid = this.getThirdPartyUserIdFromMatrixGhostId(gid);
return tpid ? [...acc, tpid] : acc;
},[]);
return Promise.resolve({
isGroup: true,
handles
});
return Promise.resolve({ isGroup: true });
} else {
return this.getRoomService(id).then(service => ({
isGroup: false,
Expand All @@ -183,17 +172,17 @@ class App extends MatrixPuppetBridgeBase {
matrixEvent.getRoomId = () => matrixEvent.room_id;
matrixEvent.getId = () => matrixEvent.event_id;
this.receiptHistory.set(id, matrixEvent);
return this.prepareToSend(id, matrixEvent).then(({isGroup, handles, service})=>{
return isGroup ? sendGroupMessage(handles, text) : sendMessage(id, service, text);
return this.prepareToSend(id, matrixEvent).then(({isGroup, service})=>{
return isGroup ? sendGroupMessage(id, text) : sendMessage(id, service, text);
});
}

sendImageMessageAsPuppetToThirdPartyRoomWithId(id, { url, text }, matrixEvent) {
const { sendGroupMessage, sendMessage } = this.client;
return download.getTempfile(url, { tagFilename: true }).then(({path}) => {
const img = path;
return this.prepareToSend(id, matrixEvent).then(({isGroup, handles, service})=>{
return isGroup ? sendGroupMessage(handles, text, img) : sendMessage(id, service, text, img);
return this.prepareToSend(id, matrixEvent).then(({isGroup, service})=>{
return isGroup ? sendGroupMessage(id, text, img) : sendMessage(id, service, text, img);
});
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ class Client extends EventEmitter {
sendMessage (id, service, text, file) {
return iMessageSend(id, service != "iMessage" ? "sms" : "iMessage", text, file);
}
sendGroupMessage (handles, text, file) {
return iMessageSendGroup(handles, text, file);
sendGroupMessage (id, text, file) {
return iMessageSendGroup(id, text, file);
}
init (ichatArchives) {
const storage = nodePersist.create({
Expand Down
62 changes: 24 additions & 38 deletions src/imessage-send-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,40 @@ const escapeString = (str) => {
return str.replace(/[\\"]/g, '\\$&');
};

module.exports = function(handles, _message, file) {
module.exports = function(id, _message, file) {
console.log(id);
let message = escapeString(_message);
let args = ['-e'];

let buddyVars = [];
let buddySetters = [];
handles.forEach((handle, i)=>{
const buddyVar = 'buddy'+i;
buddyVars.push(buddyVar);
buddySetters.push(`set ${buddyVar} to first buddy whose handle is "${handle}"`);
});
const setAttachment = `
tell application "System Events"
set theAttachment to POSIX file "${file}"
end tell
`;

const tellBody = () => {
if ( file ){
return `
set theAttachment1 to POSIX file "${file}"
send theAttachment1 to thisChat
`;
} else {
return `send "${message}" to thisChat`;
}
};
const sendAttachment = `
send theAttachment to thisChat
`;

const sendMessage = `
send "${message}" to thisChat
`;

args.push(`tell application "Messages"
activate
${buddySetters.join('\n\t')}
set thisChat to make new text chat with properties {participants:{${buddyVars.join(',')}}}
${tellBody()}
end tell`);
args.push(`
${file ? setAttachment : ''}
tell application "Messages"
activate
set thisChat to a reference to chat id "iMessage;+;${id}"
${file ? sendAttachment : sendMessage}
end tell
`);

console.log('full applescript', args[1]);

return new Promise(function(resolve, reject) {
// Check user input
if (!handles) reject(new Error('You didn\'t enter a recipient!'));
if (!id) reject(new Error('You didn\'t enter a chat id!'));
else if (!message) reject(new Error('You didn\'t enter a message!'));
else {
var proc = spawn('/usr/bin/osascript', args );
Expand All @@ -54,16 +53,3 @@ module.exports = function(handles, _message, file) {
}
});
};

if (!module.parent) {

const handles = [
process.env.PHONE1,
process.env.PHONE2
];

module.exports(handles, "testing group send from javascript (no image)..");
module.exports(handles, "testing group send from javascript (with image)..", __dirname+"/../mr-goldenfold.png");
}


0 comments on commit 1bb0385

Please sign in to comment.