add basic matrix syncing

This commit is contained in:
emerson 2021-12-06 18:33:32 -05:00
parent c6b7488a9c
commit de2fd40feb
No known key found for this signature in database
GPG key ID: 270669502DA603E3
4 changed files with 18 additions and 6 deletions

View file

@ -135,6 +135,7 @@ export class Channel {
return;
this.eventIDsSeen.add(event["event_id"]);
this.handleMatrixMessage(event);
break;
}
case 'm.room.power_levels':
this.handleMatrixPL(event);
@ -149,6 +150,9 @@ export class Channel {
}
joinMatrixUser(matrixUser: MatrixUser, event: any) {
// During initial sync, all past/present members are returned, so we filter out non-joined members
if (event["content"]["membership"] !== "join" && !this.matrixUsers.has(matrixUser.nick))
return;
this.matrixUsers.set(matrixUser.nick, matrixUser);
const prefix = matrixUser.getMask();
if (event) {

View file

@ -262,7 +262,7 @@ export class Client {
})
const newMsg = new IRCMessage(ourTags, prefix, command, params);
const msgToSend = newMsg.toString();
console.log(`SENT: ${msgToSend}`);
//console.log(`SENT: ${msgToSend}`);
this.socket.write(`${msgToSend}\r\n`);
}

View file

@ -14,6 +14,7 @@ export class IRCUser {
private txnIdStore: Set<string>
public nextBatch: string
private initialSync: boolean
private syncIntervalID: NodeJS.Timeout;
constructor(public mxid: string, private accessToken: string, private server: Server) {
this.clients = new Set();
this.channels = new Map();
@ -26,10 +27,11 @@ export class IRCUser {
this.txnIdStore = new Set();
this.nextBatch = "";
this.initialSync = false;
this.syncIntervalID = setInterval(this.doSync.bind(this), 15000);
}
isSynced() {
return this.initialSync;
return this.nextBatch !== "";
}
getVerification() {
@ -53,15 +55,21 @@ export class IRCUser {
}
doSync(): void {
if (!this.isSynced()) {
console.log("not syncing, initial sync not completed");
return;
}
const endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`;
axios.get(endpoint).then(response => {
const data = response.data;
this.nextBatch = data.next_batch;
const rooms = data.rooms;
if (rooms['join']) {
if (rooms && rooms['join']) {
for (const roomId of Object.keys(rooms.join)) {
const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server);
rooms.join[roomId].state.events.forEach((nextEvent: any) => targetChannel.routeMatrixEvent(nextEvent));
rooms.join[roomId].timeline.events.forEach((nextEvent: any) => {
targetChannel.routeMatrixEvent(nextEvent)
});
}
}
})

View file

@ -72,7 +72,7 @@ function addToTags(key: string): boolean {
}
export function parseIRCMessage(rawLine: string) {
console.log(`RAW: ${rawLine}`);
//console.log(`RAW: ${rawLine}`);
let restOfMessage = rawLine;
let parsedTags: Map<string, string> = new Map();
let prefix = '';
@ -118,6 +118,6 @@ export function parseIRCMessage(rawLine: string) {
if (lastParam !== '') {
params.push(lastParam);
}
console.log(parsedTags, prefix, command, params);
//console.log(parsedTags, prefix, command, params);
return new IRCMessage(parsedTags, prefix, command, params);
}