koishi-plugin-adapter-matrix
Version:
Matrix Adapter for koishi
114 lines • 4.42 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Internal = void 0;
const image_size_1 = __importDefault(require("image-size"));
class Internal {
constructor(bot) {
this.bot = bot;
this.txnId = Math.round(Math.random() * 1000);
}
async uploadFile(filename = 'file', buffer, mimetype) {
const headers = {};
if (mimetype)
headers['content-type'] = mimetype;
return (await this.bot.http.post(`/media/v3/upload?filename=${filename}`, buffer, { headers })).content_uri;
}
async sendTextMessage(roomId, userId, content, reply) {
const eventContent = {
msgtype: 'm.text',
body: content,
};
if (reply)
eventContent['m.relates_to'] = { 'm.in_reply_to': { 'event_id': reply } };
const response = await this.bot.http.put(`/client/v3/rooms/${roomId}/send/m.room.message/${this.txnId++}?user_id=${userId}`, eventContent);
return response.event_id;
}
async sendMediaMessage(roomId, userId, type, buffer, reply, mimetype, filename = 'file') {
const uri = await this.uploadFile(filename, buffer, mimetype);
let info = undefined;
if (type === 'image') {
const { width, height } = (0, image_size_1.default)(buffer);
info = {
size: buffer.byteLength,
h: height, w: width,
mimetype,
};
}
const eventContent = {
msgtype: `m.${type}`,
body: filename,
url: uri,
info,
};
if (reply)
eventContent['m.relates_to'] = { 'm.in_reply_to': { 'event_id': reply } };
const response = await this.bot.http.put(`/client/v3/rooms/${roomId}/send/m.room.message/${this.txnId++}?user_id=${userId}`, eventContent);
return response.event_id;
}
async getEvent(roomId, eventId) {
return await this.bot.http.get(`/client/v3/rooms/${roomId}/event/${eventId}`);
}
async redactEvent(roomId, eventId, reason) {
const event = await this.bot.http.put(`/client/v3/rooms/${roomId}/redact/${eventId}/${this.txnId++}`, { reason });
return event.event_id;
}
async getProfile(userId) {
return await this.bot.http.get(`/client/v3/profile/${userId}`);
}
async setDisplayName(userId, displayname) {
await this.bot.http.put(`/client/v3/profile/${userId}/displayname`, { displayname });
}
async setAvatar(userId, buffer, mimetype) {
const uri = await this.uploadFile('avatar', buffer, mimetype);
await this.bot.http.put(`/client/v3/profile/${userId}/avatar_url`, { avatar_url: uri });
}
async joinRoom(roomId, reason) {
return await this.bot.http.post(`/client/v3/join/${roomId}`, { reason });
}
async leaveRoom(roomId, reason) {
return await this.bot.http.post(`/client/v3/rooms/${roomId}/leave`, { reason });
}
async sync(fullSstate = false) {
return await this.bot.http.get('/client/v3/sync', {
params: { full_state: fullSstate }
});
}
async getState(roomId) {
return await this.bot.http.get(`/client/v3/rooms/${roomId}/state`);
}
async getJoinedRooms() {
return await this.bot.http.get('/client/v3/joined_rooms');
}
async register(username, asToken) {
return await this.bot.ctx.http.post(this.bot.endpoint + '/client/v3/register', {
type: 'm.login.application_service',
username,
}, {
headers: {
'Authorization': `Bearer ${asToken}`
}
});
}
async login(username, asToken) {
return await this.bot.ctx.http.post(this.bot.endpoint + '/client/v3/login', {
type: 'm.login.application_service',
identifier: {
type: 'm.id.user',
user: username,
}
}, {
headers: {
'Authorization': `Bearer ${asToken}`
}
});
}
getAssetUrl(mxc) {
// mxc://
return `${this.bot.endpoint}/_matrix/media/v3/download/${mxc.substring(6)}`;
}
}
exports.Internal = Internal;
//# sourceMappingURL=types.js.map