Skip to content

[WIP] Start conversations with Facebook users #60

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 89 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,19 +151,97 @@ class App extends MatrixPuppetBridgeBase {
}
});

this.thirdPartyClient.on('friendsList', (friends) => {
let thirdPartyUsers = [];
this.thirdPartyClient.on('friendsList', async (friends) => {
const statusRoomId = await this.getStatusRoomId();
debug(`Join ${friends.length} users to the status room (${statusRoomId})`);

for (const i in friends) {
const friend = friends[i];
thirdPartyUsers.push({
userId: friend.userID,
name: friend.fullName,
avatarUrl: friend.profilePicture
});
}
// If the server is not yet ready, it will throw "Too Many Requests" errors.
// Thus, we wait some time here.
await new Promise((resolve) => setTimeout(resolve, 2000));

const botIntent = this.getIntentFromApplicationServerBot();
const botClient = botIntent.getClient();
const puppetClient = this.puppet.getClient();
const puppetId = puppetClient.getUserId();

for (let i = 0; i < friends.length; i++) {
const friend = friends[i]
// to test with a single facebook user
//
// if (friend.fullName !== "Yodan Theburgimastr" && friend.fullName !== "Lord Reetveter") {
// continue; // DEBUG
// }

debug(`Getting user client ${i}/${friends.length} for ${friend.fullName} (ID: ${friend.userID})`)

const ghostIntent = await this.getIntentFromThirdPartySenderId(
friend.userID,
friend.fullName,
friend.profilePicture
);
await ghostIntent.join(statusRoomId);
const ghostClient = await ghostIntent.getClient();
ghostClient.setMaxListeners(ghostClient.getMaxListeners() + 1);

ghostClient.on("RoomMember.membership", async (event, member) => {
// https://github.com/matrix-org/matrix-js-sdk/issues/720
const roomIdInvitationRoom = member.roomId;
const invitationRoom = puppetClient.getRoom(roomIdInvitationRoom);
const invitationRoomAllMembers = invitationRoom.currentState.getMembers();
const isDM = (invitationRoom.getDMInviter() || (invitationRoomAllMembers.length == 2 && invitationRoomAllMembers.some((m) => {return m === m.getDMInviter();})));

if (isDM) {
if (member.membership === "invite" && member.userId === ghostClient.getUserId()) {
try {
const roomAlias = ghostClient.getUserId().replace("@", "#");

let roomId;
try {
roomId = (await ghostClient.getRoomIdForAlias(roomAlias)).room_id;
debug(`Found matrix room via alias "${roomAlias}" (ID: ${roomId})`);

await ghostClient.joinRoom(roomIdInvitationRoom);
await ghostClient.sendMessage(roomIdInvitationRoom, {
"msgtype": "m.text",
"body": `Please use matrix room ${roomAlias} (ID: ${roomId}) for your communication with ${friend.fullName}. This room is *NOT* connected to facebook!`
});
await ghostClient.leave(roomIdInvitationRoom);

await ghostClient.invite(roomId, puppetId);
debug(`Invited myself (${puppetId}) in case I had left the conversation and the room already exists`);
} catch(err) {
roomId = roomIdInvitationRoom;
debug(`No matrix room found via alias, using the invite room (ID: ${roomId})`);

await ghostClient.joinRoom(roomId);
debug(`${friend.fullName} joined matrix room ${roomId}`);
await ghostClient.setRoomName(roomId, friend.fullName);
debug(`Name of matrix room ${roomId} set to "${friend.fullName}"`);
// await ghostClient.deleteAlias(roomAlias); // TODO is this necessary?
// debug(`Deleted alias "${roomAlias}"`);
await ghostClient.createAlias(roomAlias, roomId);
debug(`Created alias "${roomAlias}" for matrix room ${roomId}`);
await ghostClient.invite(roomId, puppetId);
debug(`Invited myself (${puppetId})`);
}

debug(`Auto-joined ${ghostClient.getUserId()} and ${botClient.getUserId()} into room ${member.roomId}`);
} catch (err) {
debug(err);
}
}
} else { // !isDM
await ghostClient.joinRoom(member.roomId);
debug(`${friend.fullName} joined matrix room ${member.roomId}`);
}
});

await ghostClient.startClient();

debug(`User client started for ${friend.fullName}`);
}

return this.joinThirdPartyUsersToStatusRoom(thirdPartyUsers);
debug("Contact list synced");
});

return this.thirdPartyClient.login();
Expand Down