Skip to content

Commit

Permalink
Merge pull request #9 from RyoJerryYu/chore-obsidian-review-fix
Browse files Browse the repository at this point in the history
feat: obsidian review fix
  • Loading branch information
RyoJerryYu authored May 30, 2024
2 parents f36fd0d + 12c6910 commit c8fe676
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 112 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 YiBing Lin
Copyright (c) 2024 RyoJerryYu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
9 changes: 4 additions & 5 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{
"id": "memos-sync",
"name": "Memos Sync",
"version": "0.3.3",
"minAppVersion": "0.15.0",
"description": "Syncing memos from a [Memos](https://github.com/usememos/memos) server to Obsidian daily note. Fully compatible with official Daily Notes plugin, Calendar plugin and Periodic Notes plugin.",
"version": "0.3.4",
"minAppVersion": "1.5.12",
"description": "Syncing memos from a [Memos](https://github.com/usememos/memos) server to your daily note. Fully compatible with official Daily Notes plugin, Calendar plugin and Periodic Notes plugin.",
"author": "RyoJerryYu",
"authorUrl": "https://github.com/RyoJerryYu",
"isDesktopOnly": true,
"js": "main.js"
"isDesktopOnly": true
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-memos-sync",
"version": "0.3.3",
"version": "0.3.4",
"description": "Syncing Memos to Obsidian daily note.",
"main": "main.js",
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions src/api/memos-proto-v0.22.0/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gen/** linguist-generated=true
79 changes: 54 additions & 25 deletions src/api/memos-v0.19.1.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import axios, { AxiosInstance } from "axios";
import { AxiosRequestConfig } from "axios";
import * as log from "@/utils/log";
import { RequestUrlResponse, requestUrl } from "obsidian";

export type ResourceType = {
name?: string;
Expand Down Expand Up @@ -28,50 +29,42 @@ export type FetchError = {
};

export class MemosClient0191 {
private axios: AxiosInstance;

constructor(
private endpoint: string, // http://localhost:5230
private token: string
) {
this.axios = axios.create({
headers: {
Authorization: `Bearer ${token}`,
Accept: "application/json",
},
});
}
private token: string,
) {}

listMemos = async (
limit: number,
offset: number
offset: number,
): Promise<DailyRecordType[] | undefined> => {
try {
const { data } = await this.axios.get<
DailyRecordType[] | FetchError
>(this.endpoint + `/api/v1/memo`, {
params: {
limit: limit,
offset: offset,
rowStatus: "NORMAL",
const data = await this.getJSON<DailyRecordType[] | FetchError>(
this.endpoint + `/api/v1/memo`,
{
params: {
limit: limit,
offset: offset,
rowStatus: "NORMAL",
},
},
});
);

if (Array.isArray(data)) {
return data;
}

throw new Error(
data.message || data.msg || data.error || JSON.stringify(data)
data.message || data.msg || data.error || JSON.stringify(data),
);
} catch (error) {
log.error(`Failed to fetch daily memos: ${error}`);
}
};

listResources = async () => {
const { data } = await this.axios.get<ResourceType[] | FetchError>(
this.endpoint + `/api/v1/resource`
const data = await this.getJSON<ResourceType[] | FetchError>(
this.endpoint + `/api/v1/resource`,
);
return data;
};
Expand All @@ -84,10 +77,46 @@ export class MemosClient0191 {
const resourceURL = `${this.endpoint}/o/r/${
resource.uid || resource.name || resource.id
}`;
const { data } = await this.axios.get(resourceURL, {
const data = await this.getRaw(resourceURL, {
responseType: "arraybuffer",
});

return data;
};

private getJSON = async <T = any, D = any>(
url: string,
config?: AxiosRequestConfig<D>,
): Promise<T> => {
const res = await this.get(url, config);
return res.json;
};

private getRaw = async <D = any>(
url: string,
config?: AxiosRequestConfig<D>,
): Promise<ArrayBuffer> => {
const res = await this.get(url, config);
return res.arrayBuffer;
};

private get = async <D = any>(
url: string,
config?: AxiosRequestConfig<D>,
): Promise<RequestUrlResponse> => {
const urlObj = new URL(url);
if (config?.params) {
Object.entries(config.params).forEach(([key, value]) => {
urlObj.searchParams.append(key, String(value));
});
}
const res = await requestUrl({
url: urlObj.toString(),
headers: {
Authorization: `Bearer ${this.token}`,
Accept: "application/json",
},
});
return res;
};
}
10 changes: 5 additions & 5 deletions src/api/memos-v0.22.0.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ export function newClients(endpoint: string, token: string) {
endpoint,
FetchTransport({
credentials: "include",
})
}),
);
const clientFactory = createClientFactory().use(
(call, options) =>
call.next(call.request, {
...options,
metadata: Metadata(options.metadata).set(
"authorization",
`Bearer ${token}`
`Bearer ${token}`,
),
})!
})!,
);

return {
memoCli: clientFactory.create(
MemoServiceDefinition,
channel
channel,
) as MemoCli,
resourceCli: clientFactory.create(
ResourceServiceDefinition,
channel
channel,
) as ResourceCli,
};
}
62 changes: 31 additions & 31 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
import {
App,
Editor,
MarkdownView,
Modal,
Notice,
Plugin,
PluginSettingTab,
Setting,
SliderComponent,
setTooltip,
} from "obsidian";
import * as React from "react";
import { createRoot } from "react-dom/client";
import { App, Plugin, PluginSettingTab, Setting } from "obsidian";
import { DailyMemos } from "@/services/DailyMemos/DailyMemos";
import { MemosSyncPluginSettings } from "@/types/PluginSettings";
import { appHasDailyNotesPluginLoaded } from "obsidian-daily-notes-interface";
Expand Down Expand Up @@ -44,7 +31,7 @@ export default class MemosSyncPlugin extends Plugin {
this.settings = Object.assign(
{},
MEMOS_SYNC_DEFAULT_SETTINGS,
await this.loadData()
await this.loadData(),
);
};

Expand Down Expand Up @@ -102,24 +89,29 @@ class MemosSyncSettingTab extends PluginSettingTab {
text: "Attention: Daily Notes is not enabled.",
attr: {
style: "color: red",
}
},
});
this.containerEl.createEl("p", {
text: "Daily Notes feature is not enabled. Please enable the official Daily Notes plugin or daily notes feature in Periodic Notes plugin. Otherwise, this plugin will not work properly.",
text: "Daily Notes feature is not enabled.",
attr: {
style: "color: red",
}
})

},
});
this.containerEl.createEl("p", {
text: "Please enable the official Daily Notes plugin or daily notes feature in Periodic Notes plugin. Otherwise, this plugin will not work properly.",
attr: {
style: "color: red",
},
});
}

this.containerEl.createEl("h3", { text: "Settings for Memos Sync" });

new Setting(this.containerEl)
.setName("Daily Memos Header")
.setName("Daily memos header")
.setDesc("The header for the daily memos section.")
.addText((textfield) => {
textfield.setPlaceholder(MEMOS_SYNC_DEFAULT_SETTINGS.dailyMemosHeader);
textfield.setPlaceholder(
MEMOS_SYNC_DEFAULT_SETTINGS.dailyMemosHeader,
);
textfield.setValue(this.plugin.settings.dailyMemosHeader);
textfield.onChange((value) => {
this.saveSettings({
Expand All @@ -129,10 +121,12 @@ class MemosSyncSettingTab extends PluginSettingTab {
});

new Setting(this.containerEl)
.setName("Attachment Folder")
.setName("Attachment folder")
.setDesc("The folder for attachments.")
.addText((textfield) => {
textfield.setPlaceholder(MEMOS_SYNC_DEFAULT_SETTINGS.attachmentFolder);
textfield.setPlaceholder(
MEMOS_SYNC_DEFAULT_SETTINGS.attachmentFolder,
);
textfield.setValue(this.plugin.settings.attachmentFolder);
textfield.onChange((value) => {
this.saveSettings({
Expand All @@ -141,9 +135,11 @@ class MemosSyncSettingTab extends PluginSettingTab {
});
});

new Setting(this.containerEl).setName("Memos API").setHeading();

new Setting(this.containerEl)
.setName("Memos API Version")
.setDesc("Memos API Version")
.setName("Memos API version")
.setDesc("Which version your Memos server.")
.addDropdown((dropDown) => {
dropDown.addOptions({
"v0.19.1": "before v0.21.x",
Expand All @@ -161,7 +157,9 @@ class MemosSyncSettingTab extends PluginSettingTab {
.setName("Memos API URL")
.setDesc("Memos API URL, e.g. http://localhost:5230")
.addText((textfield) => {
textfield.setPlaceholder(MEMOS_SYNC_DEFAULT_SETTINGS.memosAPIURL);
textfield.setPlaceholder(
MEMOS_SYNC_DEFAULT_SETTINGS.memosAPIURL,
);
textfield.setValue(this.plugin.settings.memosAPIURL);
textfield.onChange((value) => {
this.saveSettings({
Expand All @@ -171,10 +169,12 @@ class MemosSyncSettingTab extends PluginSettingTab {
});

new Setting(this.containerEl)
.setName("Memos API Token")
.setName("Memos API token")
.setDesc("Memos API token.")
.addText((textfield) => {
textfield.setPlaceholder(MEMOS_SYNC_DEFAULT_SETTINGS.memosAPIToken);
textfield.setPlaceholder(
MEMOS_SYNC_DEFAULT_SETTINGS.memosAPIToken,
);
textfield.setValue(this.plugin.settings.memosAPIToken);
textfield.onChange((value) => {
this.saveSettings({
Expand Down
Loading

0 comments on commit c8fe676

Please sign in to comment.