forked from matrix-hacks/matrix-puppet-hangouts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
148 lines (141 loc) · 4.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
const {
MatrixAppServiceBridge: {
Cli, AppServiceRegistration
},
Puppet,
MatrixPuppetBridgeBase
} = require("matrix-puppet-bridge");
const HangoutsClient = require('./client');
const config = require('./config.json');
const path = require('path');
const puppet = new Puppet(path.join(__dirname, './config.json' ));
const debug = require('debug');
const debugVerbose = debug('verbose:matrix-puppet:hangouts:index');
class App extends MatrixPuppetBridgeBase {
getServicePrefix() {
return "hangouts";
}
getServiceName() {
return "Hangouts";
}
defaultDeduplicationTag() {
return " \u200b"; // Unicode Character 'ZERO WIDTH SPACE'
}
defaultDeduplicationTagPattern() {
return "\\u200b$"; // Unicode Character 'ZERO WIDTH SPACE'
}
initThirdPartyClient() {
// We used to call this here, but can't do so anymore because the bridge isn't ready yet at this point.
// Fixes error: "Cannot read property 'getClient' of null"
//this.sendStatusMsg({fixedWidthOutput:false}, "Starting hangouts bridge!");
this.threadInfo = {};
this.userId = null;
this.thirdPartyClient = new HangoutsClient();
this.thirdPartyClient.connect();
this.thirdPartyClient.on('status', (statusTxt)=> {
this.sendStatusMsg({}, statusTxt);
});
this.thirdPartyClient.on('message', (data)=> {
if(data && data.type === 'message')
{
try {
this.threadInfo[data.conversation_id] = {
conversation_name: data.conversation_name,
};
debugVerbose("incoming message data:", data);
const isMe = data.user_id.chat_id === data.self_user_id;
// normal message
if (!data.attachments.length) {
const payload = {
roomId: data.conversation_id,
senderName: data.user,
senderId: isMe ? undefined : data.user_id.chat_id,
text: data.content,
avatarUrl: data.photo_url,
};
return this.handleThirdPartyRoomMessage(payload).catch(err => {
console.log("handleThirdPartyRoomMessage error", err);
sendStatusMsg({}, "handleThirdPartyRoomMessage error", err);
});
}
// image message
return Promise.all(data.attachments.map((attachment)=> {
const payload = {
roomId: data.conversation_id,
senderName: data.user,
senderId: isMe ? undefined : data.user_id.chat_id,
text: data.content,
url: attachment,
avatarUrl: data.photo_url,
};
return this.handleThirdPartyRoomImageMessage(payload).catch(err => {
console.log("handleThirdPartyRoomMessage error", err);
sendStatusMsg({}, "handleThirdPartyRoomMessage error", err);
});
}));
} catch(er) {
console.log("incoming message handling error:", er);
sendStatusMsg({}, "incoming message handling error:", err);
}
}
// Message data format:
/*{ user_id:
{ chat_id: '10xxxxxxxxxxxxxxxxxxx',
gaia_id: '10xxxxxxxxxxxxxxxxxxx' },
conversation_id: 'Ugxxxxxxxxxxxxxxxxxxxxxxxx',
conversation_name: '+1nnnnnnnnnn',
user: 'John Doe',
content: 'a message!',
type: 'message',
status: 'success' }*/
});
return this.thirdPartyClient;
}
getThirdPartyRoomDataById(id) {
let roomData = {
name: this.threadInfo[id].conversation_name,
topic: "Hangouts Chat"
};
return roomData;
}
sendReadReceiptAsPuppetToThirdPartyRoomWithId() {
// not available for now
}
sendMessageAsPuppetToThirdPartyRoomWithId(id, text) {
return this.thirdPartyClient.send(id, text);
}
sendImageMessageAsPuppetToThirdPartyRoomWithId(id, data) {
return this.thirdPartyClient.sendImage(id, data);
}
}
new Cli({
port: config.port,
registrationPath: config.registrationPath,
generateRegistration: function(reg, callback) {
puppet.associate().then(()=>{
reg.setId(AppServiceRegistration.generateToken());
reg.setHomeserverToken(AppServiceRegistration.generateToken());
reg.setAppServiceToken(AppServiceRegistration.generateToken());
reg.setSenderLocalpart("hangoutsbot");
reg.addRegexPattern("users", "@hangouts_.*", true);
reg.addRegexPattern("aliases", "#hangouts_.*", true);
callback(reg);
}).catch(err=>{
console.error(err.message);
process.exit(-1);
});
},
run: function(port) {
const app = new App(config, puppet);
return puppet.startClient().then(()=>{
return app.initThirdPartyClient();
}).then(() => {
return app.bridge.run(port, config);
}).then(()=>{
console.log('Matrix-side listening on port %s', port);
}).catch(err=>{
console.error(err.message);
process.exit(-1);
});
}
}).run();