move sync state to user and add message sending

This commit is contained in:
emerson 2021-12-06 12:07:40 -05:00
parent 1f4ca9e708
commit c5aae31973
No known key found for this signature in database
GPG key ID: 270669502DA603E3
2 changed files with 51 additions and 26 deletions

View file

@ -15,7 +15,6 @@ export class Client {
localUsername: string
localRealname: string
deviceId: string
initialSync: boolean
constructor(private socket: Socket, public server: Server) {
this.user = null;
this.capVersion = '301';
@ -36,7 +35,6 @@ export class Client {
this.localUsername = 'none';
this.localRealname = 'none';
this.deviceId = "";
this.initialSync = false;
this.socket.on('data', (data) => this.receiveData(data));
//this.socket.on('close', (e) => {if (this.user) this.user.handleClientClose(this, e)});
}
@ -179,24 +177,28 @@ export class Client {
this.user = thisIRCUser;
this.sendMessage(this.server.name, '900', numerics['900'](this.user.getMask(), this.user.nick), new Map());
this.sendMessage(this.server.name, '903', numerics['903'](this.user.nick), new Map());
axios.get(`https://matrix.org/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => {
const data = response.data;
const rooms = data.rooms;
if (this.user === null)
return;
if (rooms['join']) {
for (const roomId of Object.keys(rooms.join)) {
const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server);
this.user.channels.set(targetChannel.name, targetChannel);
rooms.join[roomId].state.events.forEach((nextEvent: any) => targetChannel.routeMatrixEvent(nextEvent));
}
}
if (this.user === null)
return;
this.user.nextBatch = data.next_batch;
console.log(this.user.nextBatch);
if (this.user.isSynced()) {
this.user.addClient(this, message.tags);
})
} else {
axios.get(`https://matrix.org/_matrix/client/v3/sync?access_token=${accessToken}`).then(response => {
const data = response.data;
const rooms = data.rooms;
if (this.user === null)
return;
if (rooms['join']) {
for (const roomId of Object.keys(rooms.join)) {
const targetChannel = this.server.matrixRooms.get(roomId) || new Channel(roomId, this.server);
this.user.channels.set(targetChannel.name, targetChannel);
rooms.join[roomId].state.events.forEach((nextEvent: any) => targetChannel.routeMatrixEvent(nextEvent));
}
}
if (this.user === null)
return;
this.user.nextBatch = data.next_batch;
console.log(this.user.nextBatch);
this.user.addClient(this, message.tags);
})
}
}
})
}
@ -228,7 +230,7 @@ export class Client {
this.sendMessage(this.server.name, '376', numerics['376'](this.user.nick), message.tags);
this.sendMessage(this.user.nick, 'MODE', [this.user.nick, '+i'], message.tags);
if (!this.user.isUserSynced())
if (!this.user.isSynced())
this.sendMessage(this.server.name, 'NOTICE', [this.user.nick, 'Please wait for initial sync, this may take a while if you are in many large channels'], message.tags);
}

View file

@ -13,6 +13,7 @@ export class IRCUser {
public realname: string
private txnIdStore: Set<string>
public nextBatch: string
private initialSync: boolean
constructor(public mxid: string, private accessToken: string, private server: Server) {
this.clients = new Set();
this.channels = new Map();
@ -24,10 +25,11 @@ export class IRCUser {
this.realname = this.accountName;
this.txnIdStore = new Set();
this.nextBatch = "";
this.initialSync = false;
}
isUserSynced() {
return this.nextBatch !== "";
isSynced() {
return this.initialSync;
}
getVerification() {
@ -51,10 +53,7 @@ export class IRCUser {
}
doSync(): void {
let endpoint = `https://matrix.org/_matrix/client/v3/sync?access_token=${this.accessToken}`;
if (this.nextBatch !== "")
endpoint = `${endpoint}&since=${this.nextBatch}`;
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;
@ -67,4 +66,28 @@ export class IRCUser {
}
})
}
sendToAll(prefix: string, command: string, params: string[], tags: Map<string, string> = new Map(), skipClient: Client|null = null) {
this.clients.forEach(client => {
if (client !== skipClient) {
client.sendMessage(prefix, command, params, tags);
}
})
}
sendToAllWithCap(cap: string, prefix: string, command: string, params: string[], tags: Map<string, string> = new Map(), skipClient: Client|null = null) {
this.clients.forEach(client => {
if (client !== skipClient && client.enabledCaps.has(cap)) {
client.sendMessage(prefix, command, params, tags);
}
})
}
sendToAllWithoutCap(cap: string, prefix: string, command: string, params: string[], tags: Map<string, string> = new Map(), skipClient: Client|null = null) {
this.clients.forEach(client => {
if (client !== skipClient && !client.enabledCaps.has(cap)) {
client.sendMessage(prefix, command, params, tags);
}
})
}
}