add expire date to plugin

This commit is contained in:
Maxime Cannoodt 2022-06-29 14:32:02 +02:00
parent dcf607c0dc
commit 03cc855a1c
6 changed files with 41 additions and 16 deletions

View File

@ -76,7 +76,7 @@ export default class NoteSharingPlugin extends Plugin {
} }
async shareNote(mdText: string) { async shareNote(mdText: string) {
const url = await this.noteSharingService.shareNote(mdText); const res = await this.noteSharingService.shareNote(mdText);
new SharedNoteSuccessModal(this, url).open(); new SharedNoteSuccessModal(this, res.view_url, res.expire_time).open();
} }
} }

View File

@ -1,6 +1,12 @@
import moment, { type Moment } from "moment";
import { requestUrl } from "obsidian"; import { requestUrl } from "obsidian";
import { encryptMarkdown } from "./crypto/encryption"; import { encryptMarkdown } from "./crypto/encryption";
type Response = {
view_url: string;
expire_time: Moment;
};
export class NoteSharingService { export class NoteSharingService {
private _url: string; private _url: string;
@ -12,16 +18,20 @@ export class NoteSharingService {
* @param mdText Markdown file to share. * @param mdText Markdown file to share.
* @returns link to shared note with attached decryption key. * @returns link to shared note with attached decryption key.
*/ */
public async shareNote(mdText: string): Promise<string> { public async shareNote(mdText: string): Promise<Response> {
mdText = this.sanitizeNote(mdText); mdText = this.sanitizeNote(mdText);
const cryptData = encryptMarkdown(mdText); const cryptData = encryptMarkdown(mdText);
let url = await this.postNote(cryptData.ciphertext, cryptData.hmac); const res = await this.postNote(cryptData.ciphertext, cryptData.hmac);
url += `#${cryptData.key}`; console.log(res);
console.log(`Note shared: ${url}`); res.view_url += `#${cryptData.key}`;
return url; console.log(`Note shared: ${res.view_url}`);
return res;
} }
private async postNote(ciphertext: string, hmac: string): Promise<string> { private async postNote(
ciphertext: string,
hmac: string
): Promise<Response> {
const res = await requestUrl({ const res = await requestUrl({
url: `${this._url}/note`, url: `${this._url}/note`,
method: "POST", method: "POST",
@ -30,7 +40,9 @@ export class NoteSharingService {
}); });
if (res.status == 200 && res.json != null) { if (res.status == 200 && res.json != null) {
return res.json.view_url; const returnValue = res.json;
returnValue.expire_time = moment(returnValue.expire_time);
return <Response>returnValue;
} }
throw Error("Did not get expected response from server on note POST."); throw Error("Did not get expected response from server on note POST.");
} }
@ -43,7 +55,6 @@ export class NoteSharingService {
if (match) { if (match) {
mdText = match[1].trim(); mdText = match[1].trim();
} }
console.log(mdText);
return mdText; return mdText;
} }

View File

@ -1,6 +1,9 @@
<script lang="ts"> <script lang="ts">
import moment, { type Moment } from "moment";
import { now } from "svelte/internal";
export let url: string; export let url: string;
export let expireDays: number; export let expireTime: Moment;
let buttonText = "Copy"; let buttonText = "Copy";
let buttonTextTimeout: string | number | NodeJS.Timeout; let buttonTextTimeout: string | number | NodeJS.Timeout;
@ -29,7 +32,13 @@
on:click={onCopy}>{buttonText}</button on:click={onCopy}>{buttonText}</button
> >
</div> </div>
<p id="subtext">🔐 End-to-end encrypted • Expires in {expireDays} days</p> <p id="subtext">
🔐 End-to-end encrypted • Expires in {expireTime.diff(
moment(),
"days"
) + 1}
days
</p>
</div> </div>
<style scoped> <style scoped>

View File

@ -1,15 +1,17 @@
import type NoteSharingPlugin from "main"; import type NoteSharingPlugin from "main";
import { Modal } from "obsidian"; import { Modal } from "obsidian";
import Component from "./SharedNoteSuccessComponent.svelte"; import Component from "./SharedNoteSuccessComponent.svelte";
import type { Moment } from "moment";
export class SharedNoteSuccessModal extends Modal { export class SharedNoteSuccessModal extends Modal {
private url: string; private url: string;
private component: Component; private component: Component;
private expire_time: Moment;
constructor(plugin: NoteSharingPlugin, url: string) { constructor(plugin: NoteSharingPlugin, url: string, expire_time: Moment) {
super(plugin.app); super(plugin.app);
this.url = url; this.url = url;
this.expire_time = expire_time;
this.render(); this.render();
} }
@ -22,7 +24,7 @@ export class SharedNoteSuccessModal extends Modal {
target: this.contentEl, target: this.contentEl,
props: { props: {
url: this.url, url: this.url,
expireDays: 0, expireTime: this.expire_time,
}, },
}); });
} }

Binary file not shown.

View File

@ -30,7 +30,10 @@ app.post("/note/", async (req: Request<{}, {}, EncryptedNote>, res) => {
const savedNote = await prisma.encryptedNote.create({ const savedNote = await prisma.encryptedNote.create({
data: { ...note, expire_time: addDays(new Date(), 14) }, data: { ...note, expire_time: addDays(new Date(), 14) },
}); });
res.json({ view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}` }); res.json({
view_url: `${process.env.FRONTEND_URL}/note/${savedNote.id}`,
expire_time: savedNote.expire_time,
});
}); });
// Get encrypted note // Get encrypted note