Skip to content

Commit 1bb0385

Browse files
authored
Merge pull request #37 from cvincent/fix-group-replies
Find group chats by chat id
2 parents 337c700 + a68499e commit 1bb0385

File tree

3 files changed

+32
-57
lines changed

3 files changed

+32
-57
lines changed

index.js

+6-17
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ class App extends MatrixPuppetBridgeBase {
144144
ghostIntent.client.sendReadReceipt (event);
145145
return this.receiptHistory.delete(roomId);
146146
} catch (err) {
147-
debug('could not send read event', err.message);
147+
console.log('could not send read event', err.message);
148148
}
149149
}
150150

@@ -159,18 +159,7 @@ class App extends MatrixPuppetBridgeBase {
159159
}
160160
prepareToSend(id, matrixEvent) {
161161
if ( id.match(/^chat/) ) {
162-
// it's a multi party chat... we need to send to participants list
163-
// luckily we can find out about all the ghosts (they get preloaded)
164-
// and pull their handles down and use that to chat with the group
165-
const roomMembers = this.puppet.getMatrixRoomMembers(matrixEvent.room_id);
166-
const handles = roomMembers.reduce((acc, gid) => {
167-
let tpid = this.getThirdPartyUserIdFromMatrixGhostId(gid);
168-
return tpid ? [...acc, tpid] : acc;
169-
},[]);
170-
return Promise.resolve({
171-
isGroup: true,
172-
handles
173-
});
162+
return Promise.resolve({ isGroup: true });
174163
} else {
175164
return this.getRoomService(id).then(service => ({
176165
isGroup: false,
@@ -183,17 +172,17 @@ class App extends MatrixPuppetBridgeBase {
183172
matrixEvent.getRoomId = () => matrixEvent.room_id;
184173
matrixEvent.getId = () => matrixEvent.event_id;
185174
this.receiptHistory.set(id, matrixEvent);
186-
return this.prepareToSend(id, matrixEvent).then(({isGroup, handles, service})=>{
187-
return isGroup ? sendGroupMessage(handles, text) : sendMessage(id, service, text);
175+
return this.prepareToSend(id, matrixEvent).then(({isGroup, service})=>{
176+
return isGroup ? sendGroupMessage(id, text) : sendMessage(id, service, text);
188177
});
189178
}
190179

191180
sendImageMessageAsPuppetToThirdPartyRoomWithId(id, { url, text }, matrixEvent) {
192181
const { sendGroupMessage, sendMessage } = this.client;
193182
return download.getTempfile(url, { tagFilename: true }).then(({path}) => {
194183
const img = path;
195-
return this.prepareToSend(id, matrixEvent).then(({isGroup, handles, service})=>{
196-
return isGroup ? sendGroupMessage(handles, text, img) : sendMessage(id, service, text, img);
184+
return this.prepareToSend(id, matrixEvent).then(({isGroup, service})=>{
185+
return isGroup ? sendGroupMessage(id, text, img) : sendMessage(id, service, text, img);
197186
});
198187
});
199188
}

src/client.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class Client extends EventEmitter {
2323
sendMessage (id, service, text, file) {
2424
return iMessageSend(id, service != "iMessage" ? "sms" : "iMessage", text, file);
2525
}
26-
sendGroupMessage (handles, text, file) {
27-
return iMessageSendGroup(handles, text, file);
26+
sendGroupMessage (id, text, file) {
27+
return iMessageSendGroup(id, text, file);
2828
}
2929
init (ichatArchives) {
3030
const storage = nodePersist.create({

src/imessage-send-group.js

+24-38
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,40 @@ const escapeString = (str) => {
44
return str.replace(/[\\"]/g, '\\$&');
55
};
66

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

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

19-
const tellBody = () => {
20-
if ( file ){
21-
return `
22-
set theAttachment1 to POSIX file "${file}"
23-
send theAttachment1 to thisChat
24-
`;
25-
} else {
26-
return `send "${message}" to thisChat`;
27-
}
28-
};
18+
const sendAttachment = `
19+
send theAttachment to thisChat
20+
`;
21+
22+
const sendMessage = `
23+
send "${message}" to thisChat
24+
`;
2925

30-
args.push(`tell application "Messages"
31-
activate
32-
${buddySetters.join('\n\t')}
33-
set thisChat to make new text chat with properties {participants:{${buddyVars.join(',')}}}
34-
${tellBody()}
35-
end tell`);
26+
args.push(`
27+
${file ? setAttachment : ''}
28+
29+
tell application "Messages"
30+
activate
31+
set thisChat to a reference to chat id "iMessage;+;${id}"
32+
${file ? sendAttachment : sendMessage}
33+
end tell
34+
`);
3635

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

3938
return new Promise(function(resolve, reject) {
4039
// Check user input
41-
if (!handles) reject(new Error('You didn\'t enter a recipient!'));
40+
if (!id) reject(new Error('You didn\'t enter a chat id!'));
4241
else if (!message) reject(new Error('You didn\'t enter a message!'));
4342
else {
4443
var proc = spawn('/usr/bin/osascript', args );
@@ -54,16 +53,3 @@ module.exports = function(handles, _message, file) {
5453
}
5554
});
5655
};
57-
58-
if (!module.parent) {
59-
60-
const handles = [
61-
process.env.PHONE1,
62-
process.env.PHONE2
63-
];
64-
65-
module.exports(handles, "testing group send from javascript (no image)..");
66-
module.exports(handles, "testing group send from javascript (with image)..", __dirname+"/../mr-goldenfold.png");
67-
}
68-
69-

0 commit comments

Comments
 (0)