107 lines
2.7 KiB
JavaScript
107 lines
2.7 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) {
|
|
this.api = api;
|
|
}
|
|
|
|
async logIn(login_id, password) {
|
|
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");
|
|
}
|
|
credentials.store(this.api.id, login_id, token);
|
|
return response.responseJson;
|
|
}
|
|
|
|
async logOut() {
|
|
const stored = credentials.get(this.api.id);
|
|
if (!stored || !stored.token) {
|
|
throw Error("No token stored");
|
|
}
|
|
const response = await this.api.post("/users/logout", stored.token);
|
|
|
|
// Verify that the token is now invalidated
|
|
let tokenWorks;
|
|
try {
|
|
const meResponse = await this.usersMe();
|
|
tokenWorks = true;
|
|
} catch (e) {
|
|
if (e instanceof ajax.NotOkError && e.xhr.status == 401) {
|
|
tokenWorks = false;
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
if (tokenWorks) {
|
|
throw new Error("Failed to log out: token still works after trying to log out");
|
|
}
|
|
|
|
credentials.clear(this.api.id);
|
|
return response.responseJson;
|
|
}
|
|
|
|
async usersMe() {
|
|
const stored = credentials.get(this.api.id);
|
|
if (!stored || !stored.token) {
|
|
throw Error("No token stored");
|
|
}
|
|
const response = await this.api.get("/users/me", stored.token);
|
|
return response.responseJson;
|
|
}
|
|
|
|
async myTeams() {
|
|
const stored = credentials.get(this.api.id);
|
|
const response = await this.api.get("/users/me/teams", stored.token);
|
|
return response.responseJson;
|
|
}
|
|
|
|
async myChannels(team_id) {
|
|
const stored = credentials.get(this.api.id);
|
|
const response = await this.api.get(`/users/me/teams/${team_id}/channels`, stored.token);
|
|
return response.responseJson;
|
|
}
|
|
|
|
async channelPosts(channel_id, beforePost=null, afterPost=null, since=null) {
|
|
const stored = credentials.get(this.api.id);
|
|
const response = await this.api.get(`/channels/${channel_id}/posts`, stored.token, {
|
|
before: beforePost, after: afterPost, since
|
|
});
|
|
return response.responseJson;
|
|
}
|
|
}
|
|
|
|
return {MattermostApi, MattermostClient};
|
|
|
|
})();
|