start labeled-response work

This commit is contained in:
emerson 2022-03-29 18:23:20 -04:00
parent 19fe53c826
commit 211142586c
3 changed files with 75 additions and 16 deletions

View file

@ -23,6 +23,7 @@ export class Client {
["echo-message", ""], ["echo-message", ""],
["extended-join", ""], ["extended-join", ""],
["invite-notify", ""], ["invite-notify", ""],
["labeled-response", ""],
["message-tags", ""], ["message-tags", ""],
["sasl", "PLAIN"], ["sasl", "PLAIN"],
["server-time", ""], ["server-time", ""],
@ -208,7 +209,16 @@ export class Client {
"reason": (message.params.length === 2) ? message.params[1] : "" "reason": (message.params.length === 2) ? message.params[1] : ""
} }
const newTxnid = randomUUID(); const newTxnid = randomUUID();
this.apiCall.put(`/rooms/${targetChannel.roomId}/redact/${eventId}/${newTxnid}`, data).catch(function (error) { this.apiCall.put(`/rooms/${targetChannel.roomId}/redact/${eventId}/${newTxnid}`, data).then(r => {
const maybeEventID = r.data["event_id"];
if (maybeEventID) {
this.server.eventIdStore.set(maybeEventID, this);
const maybeLabel = message.tags.get("label") || "";
if (maybeLabel !== "") {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
}
}
}).catch(function (error) {
if (error.response) { if (error.response) {
console.log(error.response.data); console.log(error.response.data);
} else if (error.request) { } else if (error.request) {
@ -236,7 +246,16 @@ export class Client {
"reason": reason, "reason": reason,
"user_id": targetUser.mxid "user_id": targetUser.mxid
} }
this.apiCall.post(`/rooms/${targetChannel.roomId}/invite`, data).catch(function (error) { this.apiCall.post(`/rooms/${targetChannel.roomId}/invite`, data).then(r => {
const maybeEventID = r.data["event_id"];
if (maybeEventID) {
this.server.eventIdStore.set(maybeEventID, this);
const maybeLabel = message.tags.get("label") || "";
if (maybeLabel !== "") {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
}
}
}).catch(function (error) {
if (error.response) { if (error.response) {
console.log(error.response.data); console.log(error.response.data);
} else if (error.request) { } else if (error.request) {
@ -264,7 +283,16 @@ export class Client {
"reason": reason, "reason": reason,
"user_id": targetUser.mxid "user_id": targetUser.mxid
} }
this.apiCall.post(`/rooms/${targetChannel.roomId}/kick`, data).catch(function (error) { this.apiCall.post(`/rooms/${targetChannel.roomId}/kick`, data).then(r => {
const maybeEventID = r.data["event_id"];
if (maybeEventID) {
this.server.eventIdStore.set(maybeEventID, this);
const maybeLabel = message.tags.get("label") || "";
if (maybeLabel !== "") {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
}
}
}).catch(function (error) {
if (error.response) { if (error.response) {
console.log(error.response.data); console.log(error.response.data);
} else if (error.request) { } else if (error.request) {
@ -332,8 +360,16 @@ export class Client {
} }
} }
const newTxnid = randomUUID(); const newTxnid = randomUUID();
this.server.txnIdStore.set(newTxnid, this); this.apiCall.put(`/rooms/${targetChannel.roomId}/send/m.room.message/${newTxnid}`, content).then(r => {
this.apiCall.put(`/rooms/${targetChannel.roomId}/send/m.room.message/${newTxnid}`, content).catch(function (error) { const maybeEventID = r.data["event_id"];
if (maybeEventID) {
this.server.eventIdStore.set(maybeEventID, this);
const maybeLabel = message.tags.get("label") || "";
if (maybeLabel !== "") {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
}
}
}).catch(function (error) {
if (error.response) { if (error.response) {
console.log(error.response.data); console.log(error.response.data);
} else if (error.request) { } else if (error.request) {
@ -427,8 +463,16 @@ export class Client {
} }
} }
const newTxnid = randomUUID(); const newTxnid = randomUUID();
this.server.txnIdStore.set(newTxnid, this); this.apiCall.put(`/rooms/${targetChannel.roomId}/send/m.reaction/${newTxnid}`, content).then(r => {
this.apiCall.put(`/rooms/${targetChannel.roomId}/send/m.reaction/${newTxnid}`, content).catch(function (error) { const maybeEventID = r.data["event_id"];
if (maybeEventID) {
this.server.eventIdStore.set(maybeEventID, this);
const maybeLabel = message.tags.get("label") || "";
if (maybeLabel !== "") {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
}
}
}).catch(function (error) {
if (error.response) { if (error.response) {
console.log(error.response.data); console.log(error.response.data);
} else if (error.request) { } else if (error.request) {
@ -465,7 +509,16 @@ export class Client {
return; return;
} }
const topic = message.params[1]; const topic = message.params[1];
this.apiCall.put(`/rooms/${targetChannel.roomId}/state/m.room.topic`, {"topic": topic}).catch(function (error) { this.apiCall.put(`/rooms/${targetChannel.roomId}/state/m.room.topic`, {"topic": topic}).then(r => {
const maybeEventID = r.data["event_id"];
if (maybeEventID) {
this.server.eventIdStore.set(maybeEventID, this);
const maybeLabel = message.tags.get("label") || "";
if (maybeLabel !== "") {
this.server.eventIDToLabel.set(maybeEventID, maybeLabel)
}
}
}).catch(function (error) {
if (error.response) { if (error.response) {
console.log(error.response.data); console.log(error.response.data);
} else if (error.request) { } else if (error.request) {

View file

@ -17,7 +17,8 @@ export class Server {
public ourMatrixUser: MatrixUser; public ourMatrixUser: MatrixUser;
private clients: Set<Client> private clients: Set<Client>
public nickToMatrixUser: Map<string, MatrixUser> public nickToMatrixUser: Map<string, MatrixUser>
public txnIdStore: Map<string, Client> public eventIdStore: Map<string, Client>
public eventIDToLabel: Map<string, string>
private nextBatch: string private nextBatch: string
private isSyncing: boolean private isSyncing: boolean
private initialSync: boolean private initialSync: boolean
@ -39,7 +40,8 @@ export class Server {
this.matrixUsers = new Map(); this.matrixUsers = new Map();
this.nickToMatrixUser = new Map(); this.nickToMatrixUser = new Map();
this.clients = new Set(); this.clients = new Set();
this.txnIdStore = new Map(); this.eventIdStore = new Map();
this.eventIDToLabel = new Map();
this.nextBatch = ""; this.nextBatch = "";
this.isSyncing = false; this.isSyncing = false;
this.initialSync = false; this.initialSync = false;
@ -272,6 +274,9 @@ export class Server {
const reaction = reactionData["key"]; const reaction = reactionData["key"];
tags.set('+draft/reply', targetMsgid); tags.set('+draft/reply', targetMsgid);
tags.set('+draft/react', reaction); tags.set('+draft/react', reaction);
if (this.eventIDToLabel.has(event["event_id"])) {
tags.set("label", this.eventIDToLabel.get(event["event_id"]) || "")
}
this.sendToAllWithCap("message-tags", sourceUser.getMask(), "TAGMSG", [targetChannel.name], tags) this.sendToAllWithCap("message-tags", sourceUser.getMask(), "TAGMSG", [targetChannel.name], tags)
} }
@ -402,8 +407,10 @@ export class Server {
tags.set('msgid', event["event_id"]); tags.set('msgid', event["event_id"]);
tags.set('account', sourceUser.accountName); tags.set('account', sourceUser.accountName);
tags.set('time', new Date(event["origin_server_ts"]).toISOString()) tags.set('time', new Date(event["origin_server_ts"]).toISOString())
if (this.eventIDToLabel.has(event["event_id"])) {
tags.set("label", this.eventIDToLabel.get(event["event_id"]) || "")
}
const maybeReply = content["m.relates_to"]?.["m.in_reply_to"]?.["event_id"]; const maybeReply = content["m.relates_to"]?.["m.in_reply_to"]?.["event_id"];
const maybeTxnId: string = event["unsigned"]?.["transaction_id"] || "";
if (maybeReply) { if (maybeReply) {
tags.set('+draft/reply', maybeReply); tags.set('+draft/reply', maybeReply);
} }
@ -426,11 +433,10 @@ export class Server {
msgArray.forEach((msg: string) => { msgArray.forEach((msg: string) => {
if (msg) { if (msg) {
this.clients.forEach((client) => { this.clients.forEach((client) => {
if (this.txnIdStore.get(maybeTxnId) === client) { if (this.eventIdStore.get(event["event_id"]) === client) {
if (client.enabledCaps.has('echo-message')) { if (client.enabledCaps.has('echo-message')) {
client.sendMessage(sourceUser.getMask(), ircCommand, [targetChannel.name, msg], tags) client.sendMessage(sourceUser.getMask(), ircCommand, [targetChannel.name, msg], tags)
} }
} else { } else {
client.sendMessage(sourceUser.getMask(), ircCommand, [targetChannel.name, msg], tags) client.sendMessage(sourceUser.getMask(), ircCommand, [targetChannel.name, msg], tags)
} }

View file

@ -1,7 +1,7 @@
import { readFileSync } from "fs"; import { readFileSync } from "fs";
import { connect } from "tls"; import { connect } from "tls";
const config = JSON.parse(readFileSync('config.json')); const config = JSON.parse(readFileSync(process.argv[2]));
const client = connect({ const client = connect({
host: 'localhost', host: 'localhost',
@ -9,7 +9,7 @@ const client = connect({
rejectUnauthorized: false rejectUnauthorized: false
}, () => { }, () => {
client.write("CAP LS 302\r\n"); client.write("CAP LS 302\r\n");
client.write("CAP REQ :account-tag batch draft/channel-rename echo-message extended-join invite-notify message-tags sasl server-time\r\n"); client.write("CAP REQ :account-tag batch draft/channel-rename echo-message reflectionircd.chat/edit-message reflectionircd.chat/extended-invite extended-join invite-notify labeled-response message-tags sasl server-time\r\n");
const saslPassword = Buffer.from(`\0user\0${config.SASLPassword}`).toString('base64'); const saslPassword = Buffer.from(`\0user\0${config.SASLPassword}`).toString('base64');
client.write(`AUTHENTICATE ${saslPassword}\r\n`) client.write(`AUTHENTICATE ${saslPassword}\r\n`)
client.write("CAP END\r\n") client.write("CAP END\r\n")