mirror of
https://git.sr.ht/~emerson/reflectionircd
synced 2025-08-05 16:59:10 +00:00
add basic matrix syncing
This commit is contained in:
parent
c6b7488a9c
commit
de2fd40feb
4 changed files with 18 additions and 6 deletions
|
@ -135,6 +135,7 @@ export class Channel {
|
||||||
return;
|
return;
|
||||||
this.eventIDsSeen.add(event["event_id"]);
|
this.eventIDsSeen.add(event["event_id"]);
|
||||||
this.handleMatrixMessage(event);
|
this.handleMatrixMessage(event);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
case 'm.room.power_levels':
|
case 'm.room.power_levels':
|
||||||
this.handleMatrixPL(event);
|
this.handleMatrixPL(event);
|
||||||
|
@ -149,6 +150,9 @@ export class Channel {
|
||||||
}
|
}
|
||||||
|
|
||||||
joinMatrixUser(matrixUser: MatrixUser, event: any) {
|
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);
|
this.matrixUsers.set(matrixUser.nick, matrixUser);
|
||||||
const prefix = matrixUser.getMask();
|
const prefix = matrixUser.getMask();
|
||||||
if (event) {
|
if (event) {
|
||||||
|
|
|
@ -262,7 +262,7 @@ export class Client {
|
||||||
})
|
})
|
||||||
const newMsg = new IRCMessage(ourTags, prefix, command, params);
|
const newMsg = new IRCMessage(ourTags, prefix, command, params);
|
||||||
const msgToSend = newMsg.toString();
|
const msgToSend = newMsg.toString();
|
||||||
console.log(`SENT: ${msgToSend}`);
|
//console.log(`SENT: ${msgToSend}`);
|
||||||
this.socket.write(`${msgToSend}\r\n`);
|
this.socket.write(`${msgToSend}\r\n`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,6 +14,7 @@ export class IRCUser {
|
||||||
private txnIdStore: Set<string>
|
private txnIdStore: Set<string>
|
||||||
public nextBatch: string
|
public nextBatch: string
|
||||||
private initialSync: boolean
|
private initialSync: boolean
|
||||||
|
private syncIntervalID: NodeJS.Timeout;
|
||||||
constructor(public mxid: string, private accessToken: string, private server: Server) {
|
constructor(public mxid: string, private accessToken: string, private server: Server) {
|
||||||
this.clients = new Set();
|
this.clients = new Set();
|
||||||
this.channels = new Map();
|
this.channels = new Map();
|
||||||
|
@ -26,10 +27,11 @@ export class IRCUser {
|
||||||
this.txnIdStore = new Set();
|
this.txnIdStore = new Set();
|
||||||
this.nextBatch = "";
|
this.nextBatch = "";
|
||||||
this.initialSync = false;
|
this.initialSync = false;
|
||||||
|
this.syncIntervalID = setInterval(this.doSync.bind(this), 15000);
|
||||||
}
|
}
|
||||||
|
|
||||||
isSynced() {
|
isSynced() {
|
||||||
return this.initialSync;
|
return this.nextBatch !== "";
|
||||||
}
|
}
|
||||||
|
|
||||||
getVerification() {
|
getVerification() {
|
||||||
|
@ -53,15 +55,21 @@ export class IRCUser {
|
||||||
}
|
}
|
||||||
|
|
||||||
doSync(): void {
|
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}`;
|
const endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}&since=${this.nextBatch}`;
|
||||||
axios.get(endpoint).then(response => {
|
axios.get(endpoint).then(response => {
|
||||||
const data = response.data;
|
const data = response.data;
|
||||||
this.nextBatch = data.next_batch;
|
this.nextBatch = data.next_batch;
|
||||||
const rooms = data.rooms;
|
const rooms = data.rooms;
|
||||||
if (rooms['join']) {
|
if (rooms && rooms['join']) {
|
||||||
for (const roomId of Object.keys(rooms.join)) {
|
for (const roomId of Object.keys(rooms.join)) {
|
||||||
const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server);
|
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)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
@ -72,7 +72,7 @@ function addToTags(key: string): boolean {
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseIRCMessage(rawLine: string) {
|
export function parseIRCMessage(rawLine: string) {
|
||||||
console.log(`RAW: ${rawLine}`);
|
//console.log(`RAW: ${rawLine}`);
|
||||||
let restOfMessage = rawLine;
|
let restOfMessage = rawLine;
|
||||||
let parsedTags: Map<string, string> = new Map();
|
let parsedTags: Map<string, string> = new Map();
|
||||||
let prefix = '';
|
let prefix = '';
|
||||||
|
@ -118,6 +118,6 @@ export function parseIRCMessage(rawLine: string) {
|
||||||
if (lastParam !== '') {
|
if (lastParam !== '') {
|
||||||
params.push(lastParam);
|
params.push(lastParam);
|
||||||
}
|
}
|
||||||
console.log(parsedTags, prefix, command, params);
|
//console.log(parsedTags, prefix, command, params);
|
||||||
return new IRCMessage(parsedTags, prefix, command, params);
|
return new IRCMessage(parsedTags, prefix, command, params);
|
||||||
}
|
}
|
Loading…
Add table
Reference in a new issue