forked from keinBot/cuffeo
Update src/clients/matrix.mjs
This commit is contained in:
@@ -305,49 +305,16 @@ export default class matrix extends EventEmitter {
|
|||||||
if (recipient.startsWith('@')) {
|
if (recipient.startsWith('@')) {
|
||||||
console.log(`[Matrix] Sending DM to ${recipient}`);
|
console.log(`[Matrix] Sending DM to ${recipient}`);
|
||||||
try {
|
try {
|
||||||
// Try to find existing DM
|
// Always create a fresh DM room
|
||||||
// Note: implementation details vary by SDK version, simplified approach:
|
const createOpts = {
|
||||||
const accountData = this.client.getAccountData('m.direct');
|
visibility: "private",
|
||||||
let roomId;
|
invite: [recipient],
|
||||||
|
is_direct: true
|
||||||
if (accountData) {
|
};
|
||||||
const directRooms = accountData.getContent();
|
const result = await this.client.createRoom(createOpts);
|
||||||
const rooms = directRooms[recipient];
|
if (result && result.room_id) {
|
||||||
if (rooms && rooms.length > 0) {
|
await this.send(result.room_id, msg);
|
||||||
roomId = rooms[0]; // Pick first one
|
console.log(`[Matrix] Sent DM to ${recipient} in new room ${result.room_id}`);
|
||||||
// Check if we are still in it? Assumed yes for now.
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!roomId) {
|
|
||||||
// Create new DM
|
|
||||||
const createOpts = {
|
|
||||||
visibility: "private",
|
|
||||||
invite: [recipient],
|
|
||||||
is_direct: true
|
|
||||||
};
|
|
||||||
const result = await this.client.createRoom(createOpts);
|
|
||||||
roomId = result.room_id;
|
|
||||||
|
|
||||||
// Manually update m.direct to ensure we remember this room for this user
|
|
||||||
if (roomId) {
|
|
||||||
try {
|
|
||||||
const newDirects = accountData ? accountData.getContent() : {};
|
|
||||||
const userRooms = newDirects[recipient] || [];
|
|
||||||
if (!userRooms.includes(roomId)) {
|
|
||||||
userRooms.push(roomId);
|
|
||||||
newDirects[recipient] = userRooms;
|
|
||||||
await this.client.setAccountData('m.direct', newDirects);
|
|
||||||
console.log(`[Matrix] Updated m.direct for ${recipient} with ${roomId}`);
|
|
||||||
}
|
|
||||||
} catch (err) {
|
|
||||||
console.error(`[Matrix] Failed to update m.direct:`, err);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (roomId) {
|
|
||||||
await this.send(roomId, msg);
|
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error(`[Matrix] Failed to send DM to ${recipient}:`, e);
|
console.error(`[Matrix] Failed to send DM to ${recipient}:`, e);
|
||||||
|
|||||||
Reference in New Issue
Block a user