feathermost/js/mm_client.js
2020-08-25 12:52:38 +02:00

118 lines
3 KiB
JavaScript

const mm_client = (function() { "use strict";
class MattermostApi {
constructor(endpoint) {
this._endpoint = endpoint;
}
get id() {
return this._endpoint;
}
async get(path, token, queryParams) {
const headers = token ? {"Authorization": `Bearer ${token}`} : {};
const response = await ajax.getJson(`${this._endpoint}${path}`, {headers, queryParams});
if (!response.ok) {
throw response;
}
return response;
}
async post(path, token, data) {
const headers = token ? {"Authorization": `Bearer ${token}`} : {};
const response = await ajax.postJson(`${this._endpoint}${path}`, data, {headers});
if (!response.ok) {
throw response;
}
return response;
}
}
class MattermostClient {
constructor (api, credentials_provider) {
this.api = api;
this.credentials = credentials_provider;
const creds = this.credentials.get(this.api.id);
this.token = creds ? creds.token : null;
console.info(`Created MattermostClient for ${this.api.id}, ${this.token ? "found token" : "did not find token"}`);
}
async authenticatedGet(url, queryParams) {
assert(this.token, "logged in");
const response = await this.api.get(url, this.token, queryParams);
return response.responseJson;
}
async loggedIn() {
if (!this.token) {
return false;
}
try {
const meResponse = await this.userMe();
return true;
} catch (e) {
if (e instanceof ajax.NotOkError && e.xhr.status == 401) {
return false;
} else {
throw e;
}
}
}
async logIn(login_id, password) {
if (this.token && await this.tokenWorks()) {
throw Error("Already logged in on this server");
}
const response = await this.api.post("/users/login", undefined, {login_id, password});
const token = response.getResponseHeader("Token");
if (!token) {
throw Error("No Token header in response to log in request");
}
this.credentials.store(this.api.id, login_id, token);
this.token = token;
return response.responseJson;
}
async logOut() {
assert(this.token, "logged in");
const response = await this.api.post("/users/logout", this.token);
// Verify that the token is now invalidated
if (await loggedIn()) {
throw new Error("Failed to log out: token still works after trying to log out");
}
this.credentials.clear(this.api.id);
this.token = null;
return response.responseJson;
}
user(user_id) {
assertIsMattermostId(user_id);
return this.authenticatedGet(`/users/${user_id}`);
}
userMe() { return this.authenticatedGet("/users/me"); }
myTeams() { return this.authenticatedGet("/users/me/teams"); }
myChannels(team_id) {
assertIsMattermostId(team_id);
return this.authenticatedGet(`/users/me/teams/${team_id}/channels`);
}
channelPosts(channel_id, beforePost=null, afterPost=null, since=null) {
assertIsMattermostId(channel_id);
assertIsNullOrMattermostId(beforePost);
assertIsNullOrMattermostId(afterPost);
return this.authenticatedGet(`/channels/${channel_id}/posts`, {
before: beforePost, after: afterPost, since
});
}
}
return {MattermostApi, MattermostClient};
})();