sayance-matrix-native-module
Version:
Native Matrix client module for Expo using Rust SDK
548 lines • 24.8 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.MatrixClient = exports.MatrixEventHelpers = void 0;
const MatrixModule = require("./MatrixModule").default;
exports.MatrixEventHelpers = {
isNextBatch: (event) => event.type === 'NextBatch',
isSyncUpdate: (event) => event.type === 'SyncUpdate',
isTimelineEvent: (event) => event.type === 'TimelineEvent',
isRoomJoined: (event) => event.type === 'RoomJoined',
isRoomLeft: (event) => event.type === 'RoomLeft',
isRoomInvite: (event) => event.type === 'RoomInvite',
isMembershipChange: (event) => event.type === 'MembershipChange',
};
class SayanceMatrixClient {
constructor() {
Object.defineProperty(this, "storageBridge", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
Object.defineProperty(this, "eventListeners", {
enumerable: true,
configurable: true,
writable: true,
value: new Map()
});
}
setStorageBridge(bridge) {
this.storageBridge = bridge;
}
async initializeClient(config) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.initializeClient)) {
throw new Error('MatrixModule.initializeClient is not available. Make sure the native module is properly linked.');
}
return MatrixModule.initializeClient(config);
}
async startSync() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.startSync)) {
throw new Error('MatrixModule.startSync is not available. Make sure the native module is properly linked.');
}
console.log('🚨 [JS] Starting Matrix sync...');
try {
await MatrixModule.startSync();
console.log('✅ [JS] Matrix sync started successfully');
}
catch (error) {
console.error('❌ [JS] Failed to start Matrix sync:', error);
throw error;
}
}
async stopSync() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.stopSync)) {
throw new Error('MatrixModule.stopSync is not available. Make sure the native module is properly linked.');
}
console.log('🛑 [JS] Stopping Matrix sync...');
try {
await MatrixModule.stopSync();
console.log('✅ [JS] Matrix sync stopped successfully');
}
catch (error) {
console.error('❌ [JS] Failed to stop Matrix sync:', error);
throw error;
}
}
async pollSyncUpdates(nextBatch) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.pollSyncUpdates)) {
throw new Error('MatrixModule.pollSyncUpdates is not available. Make sure the native module is properly linked.');
}
try {
const events = await MatrixModule.pollSyncUpdates(nextBatch);
console.log('📊 [JS] Polled sync updates:', (events === null || events === void 0 ? void 0 : events.length) || 0, 'events');
return events || [];
}
catch (error) {
console.error('❌ [JS] Failed to poll sync updates:', error);
throw error;
}
}
async getSyncState() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getCurrentSyncState)) {
throw new Error('MatrixModule.getCurrentSyncState is not available. Make sure the native module is properly linked.');
}
try {
const state = await MatrixModule.getCurrentSyncState();
console.log('📊 [JS] Current sync state:', state.state);
return state;
}
catch (error) {
console.error('❌ [JS] Failed to get sync state:', error);
throw error;
}
}
async hasPendingEvents() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.hasPendingEvents)) {
throw new Error('MatrixModule.hasPendingEvents is not available. Make sure the native module is properly linked.');
}
try {
const hasPending = await MatrixModule.hasPendingEvents();
console.log('📊 [JS] Has pending events:', hasPending);
return hasPending;
}
catch (error) {
console.error('❌ [JS] Failed to check pending events:', error);
throw error;
}
}
async getPollStats() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getPollStats)) {
throw new Error('MatrixModule.getPollStats is not available. Make sure the native module is properly linked.');
}
try {
const stats = await MatrixModule.getPollStats();
console.log('📊 [JS] Poll stats:', stats);
return stats;
}
catch (error) {
console.error('❌ [JS] Failed to get poll stats:', error);
throw error;
}
}
async sendMessage(messageData) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.sendMessage)) {
throw new Error('MatrixModule.sendMessage is not available. Make sure the native module is properly linked.');
}
return MatrixModule.sendMessage(messageData);
}
async getJoinedRooms() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getJoinedRooms)) {
throw new Error('MatrixModule.getJoinedRooms is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getJoinedRooms();
}
async getUserId() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getUserId)) {
throw new Error('MatrixModule.getUserId is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getUserId();
}
async getHomeserverUrl() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getHomeserverUrl)) {
throw new Error('MatrixModule.getHomeserverUrl is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getHomeserverUrl();
}
async sendTyping(roomId, isTyping, timeoutMs = 5000) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.sendTyping)) {
throw new Error('MatrixModule.sendTyping is not available. Make sure the native module is properly linked.');
}
return MatrixModule.sendTyping(roomId, isTyping, timeoutMs);
}
async sendReadReceipt(roomId, eventId) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.sendReadReceipt)) {
throw new Error('MatrixModule.sendReadReceipt is not available. Make sure the native module is properly linked.');
}
return MatrixModule.sendReadReceipt(roomId, eventId);
}
async restoreSession(accessToken, userId, deviceId) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.restoreSession)) {
throw new Error('MatrixModule.restoreSession is not available. Make sure the native module is properly linked.');
}
return MatrixModule.restoreSession(accessToken, userId, deviceId);
}
async logout() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.logout)) {
throw new Error('MatrixModule.logout is not available. Make sure the native module is properly linked.');
}
return MatrixModule.logout();
}
async createRoom(request) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.createRoom)) {
throw new Error('MatrixModule.createRoom is not available. Make sure the native module is properly linked.');
}
return MatrixModule.createRoom(request);
}
async joinRoom(roomIdOrAlias) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.joinRoom)) {
throw new Error('MatrixModule.joinRoom is not available. Make sure the native module is properly linked.');
}
return MatrixModule.joinRoom(roomIdOrAlias);
}
async leaveRoom(roomId) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.leaveRoom)) {
throw new Error('MatrixModule.leaveRoom is not available. Make sure the native module is properly linked.');
}
return MatrixModule.leaveRoom(roomId);
}
async uploadFile(fileData) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.uploadFile)) {
throw new Error('MatrixModule.uploadFile is not available. Make sure the native module is properly linked.');
}
return MatrixModule.uploadFile(fileData);
}
async downloadFile(mxcUrl) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.downloadFile)) {
throw new Error('MatrixModule.downloadFile is not available. Make sure the native module is properly linked.');
}
return MatrixModule.downloadFile(mxcUrl);
}
async getFileInfo(mxcUrl) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getFileInfo)) {
throw new Error('MatrixModule.getFileInfo is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getFileInfo(mxcUrl);
}
async sendFileMessage(roomId, fileData, messageBody) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.sendFileMessage)) {
throw new Error('MatrixModule.sendFileMessage is not available. Make sure the native module is properly linked.');
}
return MatrixModule.sendFileMessage(roomId, fileData, messageBody);
}
async shareFileToRoom(roomId, mxcUrl, filename, contentType, messageBody) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.shareFileToRoom)) {
throw new Error('MatrixModule.shareFileToRoom is not available. Make sure the native module is properly linked.');
}
return MatrixModule.shareFileToRoom(roomId, mxcUrl, filename, contentType, messageBody);
}
async getFileInfoFromEvent(roomId, eventId) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getFileInfoFromEvent)) {
throw new Error('MatrixModule.getFileInfoFromEvent is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getFileInfoFromEvent(roomId, eventId);
}
async setupEncryption() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.setupEncryption)) {
throw new Error('MatrixModule.setupEncryption is not available. Make sure the native module is properly linked.');
}
return MatrixModule.setupEncryption();
}
async isEncryptionEnabled() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.isEncryptionEnabled)) {
throw new Error('MatrixModule.isEncryptionEnabled is not available. Make sure the native module is properly linked.');
}
return MatrixModule.isEncryptionEnabled();
}
async verifyDevice(userId, deviceId) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.verifyDevice)) {
throw new Error('MatrixModule.verifyDevice is not available. Make sure the native module is properly linked.');
}
return MatrixModule.verifyDevice(userId, deviceId);
}
async getUserDevices(userId) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getUserDevices)) {
throw new Error('MatrixModule.getUserDevices is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getUserDevices(userId);
}
async sendEncryptedMessage(messageData) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.sendEncryptedMessage)) {
throw new Error('MatrixModule.sendEncryptedMessage is not available. Make sure the native module is properly linked.');
}
return MatrixModule.sendEncryptedMessage(messageData);
}
async decryptEvent(event) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.decryptEvent)) {
throw new Error('MatrixModule.decryptEvent is not available. Make sure the native module is properly linked.');
}
return MatrixModule.decryptEvent(event);
}
async shareRoomKey(roomId, userIds) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.shareRoomKey)) {
throw new Error('MatrixModule.shareRoomKey is not available. Make sure the native module is properly linked.');
}
return MatrixModule.shareRoomKey(roomId, userIds);
}
async backupKeys() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.backupKeys)) {
throw new Error('MatrixModule.backupKeys is not available. Make sure the native module is properly linked.');
}
return MatrixModule.backupKeys();
}
async restoreKeysFromBackup(recoveryKey) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.restoreKeysFromBackup)) {
throw new Error('MatrixModule.restoreKeysFromBackup is not available. Make sure the native module is properly linked.');
}
return MatrixModule.restoreKeysFromBackup(recoveryKey);
}
async storeCryptoData(key, value) {
if (this.storageBridge) {
return this.storageBridge.storeCryptoData(key, value);
}
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.storeCryptoData)) {
throw new Error('MatrixModule.storeCryptoData is not available. Make sure the native module is properly linked.');
}
return MatrixModule.storeCryptoData(key, value);
}
async getCryptoData(key) {
if (this.storageBridge) {
return this.storageBridge.getCryptoData(key);
}
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getCryptoData)) {
throw new Error('MatrixModule.getCryptoData is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getCryptoData(key);
}
async deleteCryptoData(key) {
if (this.storageBridge) {
return this.storageBridge.deleteCryptoData(key);
}
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.deleteCryptoData)) {
throw new Error('MatrixModule.deleteCryptoData is not available. Make sure the native module is properly linked.');
}
return MatrixModule.deleteCryptoData(key);
}
async storeSessionData(session) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.storeSessionData)) {
throw new Error('MatrixModule.storeSessionData is not available. Make sure the native module is properly linked.');
}
return MatrixModule.storeSessionData(session);
}
async getSessionData(sessionId) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getSessionData)) {
throw new Error('MatrixModule.getSessionData is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getSessionData(sessionId);
}
async getRoomEvents(roomId, limit) {
if (this.storageBridge) {
return this.storageBridge.getEventData(roomId, limit);
}
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getRoomEvents)) {
throw new Error('MatrixModule.getRoomEvents is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getRoomEvents(roomId, limit);
}
async handleSyncResponse(syncResponseJson) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.handleSyncResponse)) {
throw new Error('MatrixModule.handleSyncResponse is not available. Make sure the native module is properly linked.');
}
return MatrixModule.handleSyncResponse(syncResponseJson);
}
async migrateCryptoData(jsData) {
const entries = Object.entries(jsData);
const batchSize = 100;
for (let i = 0; i < entries.length; i += batchSize) {
const batch = entries.slice(i, i + batchSize);
await Promise.all(batch.map(([key, value]) => this.storeCryptoData(key, value)));
}
}
async getMigrationStatus() {
try {
let totalCryptoKeys = 0;
let cryptoMigrated = false;
try {
await this.getCryptoData('migration_test_key');
cryptoMigrated = true;
if (this.storageBridge) {
totalCryptoKeys = 1;
}
}
catch {
cryptoMigrated = false;
}
let totalEvents = 0;
let eventsMigrated = false;
try {
if (this.storageBridge) {
const testEvents = await this.storageBridge.getEventData('!test:matrix.org', 1);
eventsMigrated = true;
totalEvents = testEvents.length;
}
}
catch {
eventsMigrated = false;
}
return {
cryptoMigrated,
eventsMigrated,
totalCryptoKeys,
totalEvents,
};
}
catch (error) {
console.error('Failed to get migration status:', error);
return {
cryptoMigrated: false,
eventsMigrated: false,
totalCryptoKeys: 0,
totalEvents: 0,
};
}
}
async verifyMigration() {
const issues = [];
try {
try {
await this.isEncryptionEnabled();
}
catch (error) {
issues.push(`Encryption verification failed: ${error}`);
}
if (this.storageBridge) {
try {
await this.storageBridge.storeCryptoData('test_key', 'test_value');
const retrievedValue = await this.storageBridge.getCryptoData('test_key');
if (retrievedValue !== 'test_value') {
issues.push('Storage bridge data integrity check failed');
}
await this.storageBridge.deleteCryptoData('test_key');
}
catch (error) {
issues.push(`Storage bridge verification failed: ${error}`);
}
}
return {
success: issues.length === 0,
issues,
};
}
catch (error) {
console.error('Failed to verify migration:', error);
return {
success: false,
issues: [`Migration verification failed: ${error}`],
};
}
}
onSyncUpdate(listener) {
const eventType = 'syncUpdate';
if (!this.eventListeners.has(eventType)) {
this.eventListeners.set(eventType, new Set());
}
this.eventListeners.get(eventType).add(listener);
return {
remove: () => {
const listeners = this.eventListeners.get(eventType);
if (listeners) {
listeners.delete(listener);
if (listeners.size === 0) {
this.eventListeners.delete(eventType);
}
}
},
};
}
onCryptoEvent(listener) {
const eventType = 'cryptoEvent';
if (!this.eventListeners.has(eventType)) {
this.eventListeners.set(eventType, new Set());
}
this.eventListeners.get(eventType).add(listener);
return {
remove: () => {
const listeners = this.eventListeners.get(eventType);
if (listeners) {
listeners.delete(listener);
if (listeners.size === 0) {
this.eventListeners.delete(eventType);
}
}
},
};
}
onTimelineEvent(roomId, listener) {
const eventType = `timelineEvent:${roomId}`;
if (!this.eventListeners.has(eventType)) {
this.eventListeners.set(eventType, new Set());
}
this.eventListeners.get(eventType).add(listener);
return {
remove: () => {
const listeners = this.eventListeners.get(eventType);
if (listeners) {
listeners.delete(listener);
if (listeners.size === 0) {
this.eventListeners.delete(eventType);
}
}
},
};
}
onRoomInvite(listener) {
const eventType = 'roomInvite';
if (!this.eventListeners.has(eventType)) {
this.eventListeners.set(eventType, new Set());
}
this.eventListeners.get(eventType).add(listener);
return {
remove: () => {
const listeners = this.eventListeners.get(eventType);
if (listeners) {
listeners.delete(listener);
if (listeners.size === 0) {
this.eventListeners.delete(eventType);
}
}
},
};
}
onMembershipChange(listener) {
const eventType = 'membershipChange';
if (!this.eventListeners.has(eventType)) {
this.eventListeners.set(eventType, new Set());
}
this.eventListeners.get(eventType).add(listener);
return {
remove: () => {
const listeners = this.eventListeners.get(eventType);
if (listeners) {
listeners.delete(listener);
if (listeners.size === 0) {
this.eventListeners.delete(eventType);
}
}
},
};
}
addListener(event, listener) {
return MatrixModule.addListener(event, listener);
}
removeListener(event, listener) {
MatrixModule.removeListener(event, listener);
}
async setNextBatchToken(nextBatch) {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.setNextBatchToken)) {
throw new Error('MatrixModule.setNextBatchToken is not available. Make sure the native module is properly linked.');
}
return MatrixModule.setNextBatchToken(nextBatch);
}
async getNextBatchToken() {
if (!(MatrixModule === null || MatrixModule === void 0 ? void 0 : MatrixModule.getNextBatchToken)) {
throw new Error('MatrixModule.getNextBatchToken is not available. Make sure the native module is properly linked.');
}
return MatrixModule.getNextBatchToken();
}
removeAllListeners() {
this.eventListeners.clear();
}
}
__exportStar(require("./index.d"), exports);
exports.MatrixClient = SayanceMatrixClient;
const defaultClient = new SayanceMatrixClient();
exports.default = defaultClient;
//# sourceMappingURL=index.js.map