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) {
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();
}
}

View File

@ -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<string> {
public async shareNote(mdText: string): Promise<Response> {
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<string> {
private async postNote(
ciphertext: string,
hmac: string
): Promise<Response> {
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 <Response>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;
}

View File

@ -1,6 +1,9 @@
<script lang="ts">
import moment, { type Moment } from "moment";
import { now } from "svelte/internal";
export let url: string;
export let expireDays: number;
export let expireTime: Moment;
let buttonText = "Copy";
let buttonTextTimeout: string | number | NodeJS.Timeout;
@ -29,7 +32,13 @@
on:click={onCopy}>{buttonText}</button
>
</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>
<style scoped>

View File

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