2022-04-21 09:46:33 +00:00
|
|
|
import { Client4 } from "@mattermost/client";
|
2022-04-19 14:54:47 +00:00
|
|
|
import { applyQuery, QueryProviderEvent } from "../query/engine";
|
2022-04-25 09:24:13 +00:00
|
|
|
import { readPage } from "@silverbulletmd/plugos-silverbullet-syscall/space";
|
|
|
|
import { parseMarkdown } from "@silverbulletmd/plugos-silverbullet-syscall/markdown";
|
2022-04-21 09:46:33 +00:00
|
|
|
import { extractMeta } from "../query/data";
|
|
|
|
import { niceDate } from "../core/dates";
|
|
|
|
import { Post } from "@mattermost/types/lib/posts";
|
2022-04-28 09:55:38 +00:00
|
|
|
import { ServerChannel } from "@mattermost/types/lib/channels";
|
|
|
|
import { UserProfile } from "@mattermost/types/lib/users";
|
|
|
|
import { Team } from "@mattermost/types/lib/teams";
|
2022-04-21 09:46:33 +00:00
|
|
|
|
|
|
|
type AugmentedPost = Post & {
|
|
|
|
// Dates we can use to filter
|
|
|
|
createdAt: string;
|
|
|
|
updatedAt: string;
|
|
|
|
editedAt: string;
|
|
|
|
};
|
2022-04-19 14:54:47 +00:00
|
|
|
|
|
|
|
// https://community.mattermost.com/private-core/pl/rbp7a7jtr3f89nzsefo6ftqt3o
|
|
|
|
|
|
|
|
function mattermostDesktopUrlForPost(
|
|
|
|
url: string,
|
|
|
|
teamName: string,
|
|
|
|
postId: string
|
|
|
|
) {
|
|
|
|
return `${url.replace("https://", "mattermost://")}/${teamName}/pl/${postId}`;
|
|
|
|
}
|
2022-04-21 09:46:33 +00:00
|
|
|
type MattermostConfig = {
|
|
|
|
url: string;
|
|
|
|
token: string;
|
2022-04-28 09:55:38 +00:00
|
|
|
defaultTeam: string;
|
2022-04-21 09:46:33 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
async function getConfig(): Promise<MattermostConfig> {
|
|
|
|
let { text } = await readPage("mattermost-config");
|
|
|
|
let parsedContent = await parseMarkdown(text);
|
|
|
|
let pageMeta = await extractMeta(parsedContent);
|
|
|
|
return pageMeta as MattermostConfig;
|
|
|
|
}
|
|
|
|
|
|
|
|
function augmentPost(post: AugmentedPost) {
|
|
|
|
if (post.create_at) {
|
|
|
|
post.createdAt = niceDate(new Date(post.create_at));
|
|
|
|
}
|
|
|
|
if (post.update_at) {
|
|
|
|
post.updatedAt = niceDate(new Date(post.update_at));
|
|
|
|
}
|
|
|
|
if (post.edit_at) {
|
|
|
|
post.editedAt = niceDate(new Date(post.edit_at));
|
|
|
|
}
|
|
|
|
}
|
2022-04-19 14:54:47 +00:00
|
|
|
|
2022-04-28 09:55:38 +00:00
|
|
|
class CachingClient4 {
|
|
|
|
constructor(public client: Client4) {}
|
|
|
|
|
|
|
|
private channelCache = new Map<string, ServerChannel>();
|
|
|
|
async getChannelCached(channelId: string): Promise<ServerChannel> {
|
|
|
|
let channel = this.channelCache.get(channelId);
|
|
|
|
if (channel) {
|
|
|
|
return channel;
|
|
|
|
}
|
|
|
|
channel = await this.client.getChannel(channelId);
|
|
|
|
this.channelCache.set(channelId, channel!);
|
|
|
|
return channel!;
|
|
|
|
}
|
|
|
|
|
|
|
|
private teamCache = new Map<string, Team>();
|
|
|
|
async getTeamCached(teamId: string): Promise<Team> {
|
|
|
|
let team = this.teamCache.get(teamId);
|
|
|
|
if (team) {
|
|
|
|
return team;
|
|
|
|
}
|
|
|
|
team = await this.client.getTeam(teamId);
|
|
|
|
this.teamCache.set(teamId, team!);
|
|
|
|
return team!;
|
|
|
|
}
|
|
|
|
|
|
|
|
private userCache = new Map<string, UserProfile>();
|
|
|
|
async getUserCached(userId: string): Promise<UserProfile> {
|
|
|
|
let user = this.userCache.get(userId);
|
|
|
|
if (user) {
|
|
|
|
return user;
|
|
|
|
}
|
|
|
|
user = await this.client.getUser(userId);
|
|
|
|
this.userCache.set(userId, user!);
|
|
|
|
return user!;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 14:54:47 +00:00
|
|
|
export async function savedPostsQueryProvider({
|
|
|
|
query,
|
2022-04-28 09:55:38 +00:00
|
|
|
}: QueryProviderEvent): Promise<any[]> {
|
2022-04-21 09:46:33 +00:00
|
|
|
let config = await getConfig();
|
|
|
|
let client = new Client4();
|
2022-04-28 09:55:38 +00:00
|
|
|
let cachingClient = new CachingClient4(client);
|
2022-04-21 09:46:33 +00:00
|
|
|
client.setUrl(config.url);
|
|
|
|
client.setToken(config.token);
|
2022-04-19 14:54:47 +00:00
|
|
|
let me = await client.getMe();
|
2022-04-21 09:46:33 +00:00
|
|
|
let postCollection = await client.getFlaggedPosts(me.id);
|
|
|
|
let savedPosts: AugmentedPost[] = [];
|
|
|
|
for (let order of postCollection.order) {
|
|
|
|
let post = postCollection.posts[order];
|
|
|
|
augmentPost(post);
|
|
|
|
savedPosts.push(post);
|
|
|
|
}
|
2022-04-28 09:55:38 +00:00
|
|
|
let resultSavedPosts = [];
|
2022-04-19 14:54:47 +00:00
|
|
|
savedPosts = applyQuery(query, savedPosts);
|
|
|
|
for (let savedPost of savedPosts) {
|
2022-04-28 09:55:38 +00:00
|
|
|
let channel = await cachingClient.getChannelCached(savedPost.channel_id);
|
|
|
|
let teamName = config.defaultTeam;
|
|
|
|
if (channel.team_id) {
|
|
|
|
let team = await cachingClient.getTeamCached(channel.team_id);
|
|
|
|
teamName = team.name;
|
|
|
|
}
|
|
|
|
resultSavedPosts.push({
|
|
|
|
...savedPost,
|
|
|
|
user: await cachingClient.getUserCached(savedPost.user_id),
|
|
|
|
channel: channel,
|
|
|
|
teamName: teamName,
|
|
|
|
url: mattermostDesktopUrlForPost(client.url, teamName, savedPost.id),
|
|
|
|
});
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|
2022-04-28 09:55:38 +00:00
|
|
|
return resultSavedPosts;
|
2022-04-19 14:54:47 +00:00
|
|
|
}
|