feathermost/js/mm_client.js

300 lines
7.4 KiB
JavaScript
Raw Normal View History

2020-03-31 12:09:33 +02:00
const mm_client = (function() { "use strict";
class MattermostClient {
constructor (endpoint, credentials_provider) {
this.endpoint = endpoint;
2020-03-31 12:09:33 +02:00
this.credentials = credentials_provider;
const creds = this.credentials.get(this.endpoint);
this.token = creds ? creds.token : null;
console.info(`Created MattermostClient for ${this.endpoint}, ${this.token ? "found token" : "did not find token"}`);
2022-06-17 23:04:16 +02:00
2022-06-18 17:52:10 +02:00
this.socket = null;
2022-06-17 23:04:16 +02:00
this.channelStore = new store.ChannelStore(this);
2021-02-07 21:10:36 +01:00
}
2021-02-17 18:15:57 +01:00
async get(path, queryParams) {
const headers = this.token ? {"Authorization": `Bearer ${this.token}`} : {};
const response = await ajax.getJson(`${this.endpoint}${path}`, {headers, queryParams});
2020-03-31 12:09:33 +02:00
if (!response.ok) {
throw response;
}
2021-02-17 18:15:57 +01:00
return response.responseJson;
2020-03-31 12:09:33 +02:00
}
2021-02-17 18:15:57 +01:00
async post(path, data) {
const headers = this.token ? {"Authorization": `Bearer ${this.token}`} : {};
const response = await ajax.postJson(`${this.endpoint}${path}`, data, {headers});
2020-03-31 12:09:33 +02:00
if (!response.ok) {
throw response;
}
return response;
}
2021-02-17 18:15:57 +01:00
async paginatedGet(url, queryParams, perPage=200) {
2021-02-07 21:10:36 +01:00
let data = [];
let params = {page: 0, per_page: perPage};
if (queryParams) {
extend(params, queryParams);
}
let loadMore = true;
while (loadMore) {
if (params.page > 100) {
2021-02-17 18:15:57 +01:00
throw new Error("Requesting more than 100 pages, something looks wrong or the response is massive (>20,000 items)");
2021-02-07 21:10:36 +01:00
}
2021-02-17 18:15:57 +01:00
const response = await this.get(url, params);
data = data.concat(response);
2021-02-07 21:10:36 +01:00
2021-02-17 18:15:57 +01:00
loadMore = response.length > 0;
2021-02-07 21:10:36 +01:00
params.page++;
}
return data;
}
2022-06-09 15:26:44 +02:00
start() {
assert(this.token);
2022-06-18 17:52:10 +02:00
if (this.socket) {
try {
this.socket.close();
} catch(e) {}
}
const users = this.regetUsers();
2022-06-17 23:04:16 +02:00
const me = this.userMe().then(data => {this.me = data;});
2022-06-18 17:52:10 +02:00
const channels = this.channelStore.fetch();
2022-06-17 23:04:16 +02:00
return Promise.all([
users,
me,
channels
])
2022-06-18 17:52:10 +02:00
.then(() => this.websocket())
.then(async () => {
const curChan = currentChannel();
if (curChan.endpoint === this.endpoint) {
console.debug("Reloading channel");
const {channels, teams, unread} = await this.channelStore.get();
const channel = channels[curChan.channel_id];
const team = channel["team_id"] ? teams[channel["team_id"]] : null;
await switchToChannel(this, team, channel);
}
})
.catch(e => {
console.error("Could not connect, trying again in 5 seconds", e);
window.setTimeout(() => this.start(), 5000);
});
2022-06-09 15:26:44 +02:00
}
async loggedIn() {
if (!this.token) {
return false;
2020-03-31 12:09:33 +02:00
}
try {
const meResponse = await this.userMe();
return true;
2020-03-31 12:09:33 +02:00
} catch (e) {
if (e instanceof ajax.NotOkError && e.xhr.status == 401) {
return false;
2020-03-31 12:09:33 +02:00
} else {
throw e;
}
}
}
async logIn(login_id, password) {
2021-02-07 21:10:36 +01:00
if (await this.loggedIn()) {
throw Error("Already logged in on this server");
2020-03-31 12:09:33 +02:00
}
const response = await this.post("/users/login", {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.endpoint, login_id, token);
this.token = token;
2020-03-31 12:09:33 +02:00
return response.responseJson;
}
async logOut() {
console.log(this);
console.log(this.token);
assert(this.token, "logged in");
try {
const response = await this.post("/users/logout", this.token);
} catch (e) {
console.warn(e);
}
// Verify that the token is now invalidated
2021-02-07 21:10:36 +01:00
if (await this.loggedIn()) {
throw new Error("Failed to log out: token still works after trying to log out");
2020-03-31 12:09:33 +02:00
}
this.credentials.clear(this.endpoint);
this.token = null;
2020-03-31 12:09:33 +02:00
return response.responseJson;
}
user(user_id) {
assertIsMattermostId(user_id);
2021-02-17 18:15:57 +01:00
return this.get(`/users/${user_id}`);
2020-03-31 12:09:33 +02:00
}
2021-02-17 18:15:57 +01:00
userMe() { return this.get("/users/me"); }
myTeams() { return this.get("/users/me/teams"); }
2020-03-31 12:09:33 +02:00
myChannels(team_id) {
assertIsMattermostId(team_id);
2021-02-17 18:15:57 +01:00
return this.get(`/users/me/teams/${team_id}/channels`);
2020-03-31 12:09:33 +02:00
}
channelPosts(channel_id, beforePost=null, afterPost=null, since=null) {
assertIsMattermostId(channel_id);
assertIsNullOrMattermostId(beforePost);
assertIsNullOrMattermostId(afterPost);
2021-02-17 18:15:57 +01:00
return this.get(`/channels/${channel_id}/posts`, {
2020-03-31 12:09:33 +02:00
before: beforePost, after: afterPost, since
});
}
2021-02-07 21:10:36 +01:00
2022-06-08 23:24:59 +02:00
getUnread(team_id) {
return this.get(`/users/me/teams/${team_id}/channels/members`);
}
markChannelRead(channel_id) {
assertIsMattermostId(channel_id);
return this.post(`/channels/members/me/view`, {
channel_id: channel_id
});
}
2021-02-17 18:15:57 +01:00
writePost(channel_id, message) {
return this.post("/posts", {
"channel_id": channel_id,
"message": message
});
}
2022-06-09 00:29:24 +02:00
executeSlashCommand(channel_id, command) {
return this.post("/commands/execute", {channel_id, command});
}
getUsers() {
if (!this.users) {
2021-02-17 18:15:57 +01:00
this.users = this.paginatedGet("/users", {}).then(users => {
const newUsers = Object.create(null);
for (let user_data of users) {
const user = user_data;
newUsers[user.id] = user;
}
return newUsers;
});
}
return this.users;
2021-02-07 21:10:36 +01:00
}
2022-06-17 14:20:42 +02:00
regetUsers() {
this.users = null;
return this.getUsers();
}
2021-02-07 21:10:36 +01:00
async filePublicLink(file_id) {
2021-02-17 18:15:57 +01:00
const response = await this.get(`/files/${file_id}/link`, {});
2021-02-07 21:10:36 +01:00
return response.link;
}
websocket() {
assert(this.token, "logged in");
const endpoint_destructuring = this.endpoint.match(/^([^:]+)(:\/\/.*)/);
const protocol = endpoint_destructuring[1] === "https" ? "wss" : "ws";
const endpoint = protocol + endpoint_destructuring[2] + "/websocket";
const socket = new WebSocket(endpoint);
socket.addEventListener("error", event => {
2022-06-18 17:52:10 +02:00
console.error("Websocket errored", event);
try {
socket.close();
} catch(e) {}
this.start();
});
socket.addEventListener("close", event => {
2022-06-18 17:52:10 +02:00
this.socket = null;
console.info("Websocket closed", event);
});
socket.addEventListener("open", event => {
console.log(`Opened websocket connection to ${endpoint}`);
socket.send(`{"seq": 1, "action": "authentication_challenge", "data": {"token": "${this.token}"}}`);
});
socket.addEventListener("message", event => {
console.log("Message from server ", event.data);
const data = JSON.parse(event.data);
if (data.event === "posted") {
const post = JSON.parse(data.data.post);
pubsub.publish("MESSAGES_NEW", {...post, endpoint: this.endpoint});
2022-06-09 14:36:33 +02:00
} else if (data.event === "channel_viewed") {
pubsub.publish("CHANNEL_READ", {endpoint: this.endpoint, channel_id: data.data.channel_id, time: null});
} else if (data.event === "multiple_channels_viewed") {
console.log(data.data.channel_times);
for (let channel_id in data.data.channel_times) {
const time = data.data.channel_times[channel_id];
pubsub.publish("CHANNEL_READ", {endpoint: this.endpoint, channel_id, time});
}
2022-06-17 14:20:42 +02:00
} else if (
data.event === "user_added" ||
data.event === "user_updated" ||
data.event === "user_removed"
) {
this.regetUsers();
} else if (
data.event === "group_added" ||
data.event === "channel_created" ||
data.event === "channel_deleted"
) {
this.channelStore.fetch();
}
2022-06-09 14:36:33 +02:00
});
2022-06-18 17:52:10 +02:00
this.socket = socket;
}
2020-03-31 12:09:33 +02:00
}
let clients = Object.create(null);
function getOrCreate(endpoint) {
if (!clients[endpoint]) {
clients[endpoint] = new MattermostClient(endpoint, localstorage_credentials);
}
return clients[endpoint];
}
function getOrCreateMultiple(endpoints) {
return endpoints.map(getOrCreate);
}
function drop(endpoint) {
delete clients[endpoint];
}
return {getOrCreate, getOrCreateMultiple, drop};
2020-03-31 12:09:33 +02:00
})();