import { NotificationTypes, INotification, IEnvelope, MatrixAccessTokenExpiredError, IErrorResponseData, NotificationData } from "@iamo/notification"; import { IDataStorage, Task } from "@iamo/utils"; import SDK from "../index.js"; /** * Notification support using Matrix implementation. * */ export declare class Notification { private sdk; private messagingServerURL; /** initialiazes the Notification API * @param sdk sdk instance */ constructor(sdk: SDK); /** * send message to a receiver * * * **Example** * * ```js * // Create send data content * const envelope: IEnvelope = { * from: 'alice', * to: 'bob', * type: 'Send' * txnHash: '', * data, * }; * * // Send notification. For other type of notification, see API docs of Wallet. * await sdk.notification.send( * envelope, * async (err: Error) => { * // access token recovery function in case saved token is expired. * }, * 'messageId', * ); * ``` * * @param envelope message data content * @param accessTokenRecovery recovery function that's called when saved access token is expired * @param messageID message ID (Matrix specific) */ send(envelope: IEnvelope, accessTokenRecovery: (error: Error) => Promise, messageID?: string): Promise; /** * Receive all notification data * * @param userID user id (e.g, ethereum account address) * @returns notification history data */ receiveAll(userID: string): Promise; /** * Set storage data * * @param storage data storage object */ setStorage(storage: IDataStorage): void; /** * Get access token from notification server * In Matrix case, this function attempts following sequence: * * 1) Check user id already exists in matrix server * 2) If it exits, this function call login and get access token from the server * 3) If not, thsi function call register and get access token from the server * * @param userID user id (e.g, ethereum adderss) * @param password password * @param maxRetryCount retry count for in case api call fails * @returns Access token data */ getAccessToken(userID: string, password: string, maxRetryCount?: number): Promise; /** * Start message observer task * * @param taskName task name * @param receiverID receiver's user ID * @param callbackMap callback function map for each message types * @param accessTokenRecovery recovery function that's called when saved access token is expired * @param waitForInSec observation interval in second * @returns task object * * ```js * * const acecssTokenRecovery = (err: Error) => { * // For example, this code aquire access token again when it's expired. * sdk.notification.getAccessToken('alice' 'password'); * }; * * // ** Start a message polling process * // Define callback function for each message types * const callbackMap = new Map Promise>(); * callbackMap.set( * 'Response', * async (notification: INotification) => { ... }, // Client defines function for each Message Type * ); * * // Process start. * const task = await sdk.notification.startMessageObserverTask( * 'process-1', // Process Name * 'alice', // Sender address * callbackMap, // callback function map * accessTokenRecovery, // function called when access token is expired. * 3 //observation interval in second * ); * * // Register created task in TaskManager(@iamo/utils) for task management (e.g, shutdown all tasks when app closes) * TaskManager.addTask(task.getName(), task) * * ``` */ startMessageObserverTask(taskName: string, receiverID: string, callbackMap: Map Promise>, accessTokenRecovery: (error: Error) => Promise, waitForInSec?: number): Promise; /** * Convert matrix user id to user id * * @param matrixID user id in matrix user id format (@userid:matrix.org) * @returns user id */ convertMatrixIDToUserID(matrixID: string): string; /** * Check if matrixID is userId's matrix user id * * @param matrixID user id in matrix user id format (@userid:matrix.org) * @param userId user id * @returns true if the matrixID is the userId's matrix user id, otherwise false */ isUser(matrixID: string, userId: string): boolean; /** * Check if userId already exists in Matrix server * * @param userId user id * @returns true if userId exists in Matrix server, otherwise false */ userExists(userId: string): Promise; } export default Notification; export { INotification, NotificationTypes, IEnvelope, MatrixAccessTokenExpiredError, IErrorResponseData, NotificationData, };