diff --git a/plugin/main.ts b/plugin/main.ts index dcea995..4ebc72a 100644 --- a/plugin/main.ts +++ b/plugin/main.ts @@ -76,7 +76,7 @@ export default class NoteSharingPlugin extends Plugin { } async shareNote(mdText: string) { - const url = await this.noteSharingService.shareNote(mdText); - new SharedNoteSuccessModal(this, url).open(); + const res = await this.noteSharingService.shareNote(mdText); + new SharedNoteSuccessModal(this, res.view_url, res.expire_time).open(); } } diff --git a/plugin/src/NoteSharingService.ts b/plugin/src/NoteSharingService.ts index d66179a..f371c74 100644 --- a/plugin/src/NoteSharingService.ts +++ b/plugin/src/NoteSharingService.ts @@ -1,6 +1,12 @@ +import moment, { type Moment } from "moment"; import { requestUrl } from "obsidian"; import { encryptMarkdown } from "./crypto/encryption"; +type Response = { + view_url: string; + expire_time: Moment; +}; + export class NoteSharingService { private _url: string; @@ -12,16 +18,20 @@ export class NoteSharingService { * @param mdText Markdown file to share. * @returns link to shared note with attached decryption key. */ - public async shareNote(mdText: string): Promise { + public async shareNote(mdText: string): Promise { mdText = this.sanitizeNote(mdText); const cryptData = encryptMarkdown(mdText); - let url = await this.postNote(cryptData.ciphertext, cryptData.hmac); - url += `#${cryptData.key}`; - console.log(`Note shared: ${url}`); - return url; + const res = await this.postNote(cryptData.ciphertext, cryptData.hmac); + console.log(res); + res.view_url += `#${cryptData.key}`; + console.log(`Note shared: ${res.view_url}`); + return res; } - private async postNote(ciphertext: string, hmac: string): Promise { + private async postNote( + ciphertext: string, + hmac: string + ): Promise { const res = await requestUrl({ url: `${this._url}/note`, method: "POST", @@ -30,7 +40,9 @@ export class NoteSharingService { }); if (res.status == 200 && res.json != null) { - return res.json.view_url; + const returnValue = res.json; + returnValue.expire_time = moment(returnValue.expire_time); + return returnValue; } throw Error("Did not get expected response from server on note POST."); } @@ -43,7 +55,6 @@ export class NoteSharingService { if (match) { mdText = match[1].trim(); } - console.log(mdText); return mdText; } diff --git a/plugin/src/ui/SharedNoteSuccessComponent.svelte b/plugin/src/ui/SharedNoteSuccessComponent.svelte index 4903006..86955c8 100644 --- a/plugin/src/ui/SharedNoteSuccessComponent.svelte +++ b/plugin/src/ui/SharedNoteSuccessComponent.svelte @@ -1,6 +1,9 @@