Skip to content

Commit

Permalink
a better fix for the login issue given bad token
Browse files Browse the repository at this point in the history
  • Loading branch information
kinabalu committed Sep 22, 2024
1 parent 2f57afe commit e9b7205
Show file tree
Hide file tree
Showing 2 changed files with 204 additions and 173 deletions.
338 changes: 179 additions & 159 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,198 +256,218 @@ export default class VoiceNotesPlugin extends Plugin {
}

async processNote(recording: any, voiceNotesDir: string, isSubnote: boolean = false, parentTitle: string = '', unsyncedCount: { count: number }): Promise<void> {
if (!recording.title) {
new Notice(`Unable to grab voice recording with id: ${recording.id}`);
return;
}
try {
if (!recording.title) {
new Notice(`Unable to grab voice recording with id: ${recording.id}`);
return;
}

const title = this.sanitizedTitle(recording.title, recording.created_at);
const recordingPath = normalizePath(`${voiceNotesDir}/${title}.md`);
const title = this.sanitizedTitle(recording.title, recording.created_at);
const recordingPath = normalizePath(`${voiceNotesDir}/${title}.md`);

// Process sub-notes, whether the note already exists or not
if (recording.subnotes && recording.subnotes.length > 0) {
for (const subnote of recording.subnotes) {
await this.processNote(subnote, voiceNotesDir, true, title, unsyncedCount);
// Process sub-notes, whether the note already exists or not
if (recording.subnotes && recording.subnotes.length > 0) {
for (const subnote of recording.subnotes) {
await this.processNote(subnote, voiceNotesDir, true, title, unsyncedCount);
}
}
}

// Check if the recording contains any excluded tags
if (recording.tags && recording.tags.some((tag: { name: string }) => this.settings.excludeTags.includes(tag.name))) {
unsyncedCount.count++;
return;
}
// Check if the recording contains any excluded tags
if (recording.tags && recording.tags.some((tag: { name: string }) => this.settings.excludeTags.includes(tag.name))) {
unsyncedCount.count++;
return;
}

// Check if the note already exists
const noteExists = await this.app.vault.adapter.exists(recordingPath);
// Check if the note already exists
const noteExists = await this.app.vault.adapter.exists(recordingPath);

// If the note doesn't exist, or if it's a sub-note, it's treated as follows
if (!noteExists || isSubnote) {
// Prepare data for the template
const creationTypes = ['summary', 'points', 'tidy', 'todo', 'tweet', 'blog', 'email', 'custom'];
const creations = Object.fromEntries(
creationTypes.map(type => [type, recording.creations.find((creation: { type: string }) => creation.type === type)])
);
// If the note doesn't exist, or if it's a sub-note, it's treated as follows
if (!noteExists || isSubnote) {
// Prepare data for the template
const creationTypes = ['summary', 'points', 'tidy', 'todo', 'tweet', 'blog', 'email', 'custom'];
const creations = Object.fromEntries(
creationTypes.map(type => [type, recording.creations.find((creation: { type: string }) => creation.type === type)])
);

const { transcript } = recording;
const { transcript } = recording;

// Destructure creations object to get individual variables if needed
const { summary, points, tidy, todo, tweet, blog, email, custom } = creations;
// Destructure creations object to get individual variables if needed
const { summary, points, tidy, todo, tweet, blog, email, custom } = creations;

let audioLink = '';
if (this.settings.downloadAudio) {
const audioPath = normalizePath(`${voiceNotesDir}/audio`);
if (!await this.app.vault.adapter.exists(audioPath)) {
await this.app.vault.createFolder(audioPath);
}
const outputLocationPath = normalizePath(`${audioPath}/${recording.recording_id}.mp3`);
if (!await this.app.vault.adapter.exists(outputLocationPath)) {
const signedUrl = await this.vnApi.getSignedUrl(recording.recording_id);
await this.vnApi.downloadFile(this.fs, signedUrl.url, outputLocationPath);
let audioLink = '';
if (this.settings.downloadAudio) {
const audioPath = normalizePath(`${voiceNotesDir}/audio`);
if (!await this.app.vault.adapter.exists(audioPath)) {
await this.app.vault.createFolder(audioPath);
}
const outputLocationPath = normalizePath(`${audioPath}/${recording.recording_id}.mp3`);
if (!await this.app.vault.adapter.exists(outputLocationPath)) {
const signedUrl = await this.vnApi.getSignedUrl(recording.recording_id);
await this.vnApi.downloadFile(this.fs, signedUrl.url, outputLocationPath);
}
audioLink = `![[${recording.recording_id}.mp3]]`;
}
audioLink = `![[${recording.recording_id}.mp3]]`;
}

// Handle attachments
let attachments = '';
if (recording.attachments && recording.attachments.length > 0) {
const attachmentsPath = normalizePath(`${voiceNotesDir}/attachments`);
if (!await this.app.vault.adapter.exists(attachmentsPath)) {
await this.app.vault.createFolder(attachmentsPath);
}
attachments = (await Promise.all(recording.attachments.map(async (data: any) => {
if (data.type === 1) {
return `- ${data.description}`;
} else if (data.type === 2) {
const filename = getFilenameFromUrl(data.url);
const attachmentPath = normalizePath(`${attachmentsPath}/${filename}`);
await this.vnApi.downloadFile(this.fs, data.url, attachmentPath);
return `- ![[${filename}]]`;
// Handle attachments
let attachments = '';
if (recording.attachments && recording.attachments.length > 0) {
const attachmentsPath = normalizePath(`${voiceNotesDir}/attachments`);
if (!await this.app.vault.adapter.exists(attachmentsPath)) {
await this.app.vault.createFolder(attachmentsPath);
}
}))).join('\n');
}
attachments = (await Promise.all(recording.attachments.map(async (data: any) => {
if (data.type === 1) {
return `- ${data.description}`;
} else if (data.type === 2) {
const filename = getFilenameFromUrl(data.url);
const attachmentPath = normalizePath(`${attachmentsPath}/${filename}`);
await this.vnApi.downloadFile(this.fs, data.url, attachmentPath);
return `- ![[${filename}]]`;
}
}))).join('\n');
}

// Prepare context for Jinja template
const formattedPoints = points ? points.content.data.map((data: string) => `- ${data}`).join('\n') : null;
const formattedTodos = todo ? todo.content.data.map((data: string) => `- [ ] ${data}${this.settings.todoTag ? ' #' + this.settings.todoTag : ''}`).join('\n') : null;
// Format tags, replacing spaces with hyphens for multi-word tags
const formattedTags = recording.tags && recording.tags.length > 0
? recording.tags.map((tag: { name: string }) => `#${tag.name.replace(/\s+/g, '-')}`).join(' ')
: null;
const context = {
recording_id: recording.recording_id,
title: title,
date: formatDate(recording.created_at, this.settings.dateFormat),
transcript: transcript,
audio_link: audioLink,
summary: summary ? summary.markdown_content : null,
tidy: tidy ? tidy.markdown_content : null,
points: formattedPoints,
todo: formattedTodos,
tweet: tweet ? tweet.markdown_content : null,
blog: blog ? blog.markdown_content : null,
email: email ? email.markdown_content : null,
custom: custom ? custom.markdown_content : null,
tags: formattedTags,
related_notes: recording.related_notes && recording.related_notes.length > 0
? recording.related_notes.map((relatedNote: { title: string; created_at: string }) =>
`- [[${this.sanitizedTitle(relatedNote.title, relatedNote.created_at)}]]`
).join('\n')
: null,
subnotes: recording.subnotes && recording.subnotes.length > 0
? recording.subnotes.map((subnote: { title: string; created_at: string }) =>
`- [[${this.sanitizedTitle(subnote.title, subnote.created_at)}]]`
).join('\n')
: null,
attachments: attachments,
parent_note: isSubnote ? `[[${parentTitle}]]` : null,
};

// Render the template using Jinja
let note = jinja.render(this.settings.noteTemplate, context).replace(/\n{3,}/g, '\n\n');
note = convertHtmlToMarkdown(note);

// Add default metadata or ensure recording_id is present
if (this.settings.useDefaultFrontmatter) {
const metadata = `---
recording_id: ${recording.recording_id}
duration: ${formatDuration(recording.duration)}
created_at: ${formatDate(recording.created_at, this.settings.dateFormat)}
updated_at: ${formatDate(recording.updated_at, this.settings.dateFormat)}
${formatTags(recording)}
---\n`;

note = metadata + note;
} else {
const frontmatterRegex = /^---\n([\s\S]*?)\n---/;
const match = note.match(frontmatterRegex);

if (match) {
// Frontmatter exists, update or add recording_id
const existingFrontmatter = match[1];
const updatedFrontmatter = existingFrontmatter.replace(/recording_id:.*\n?/, '') + `recording_id: ${recording.recording_id}\n`;
note = note.replace(frontmatterRegex, `---\n${updatedFrontmatter}---`);
} else {
// No frontmatter, create one with recording_id
// Prepare context for Jinja template
const formattedPoints = points ? points.content.data.map((data: string) => `- ${data}`).join('\n') : null;
const formattedTodos = todo ? todo.content.data.map((data: string) => `- [ ] ${data}${this.settings.todoTag ? ' #' + this.settings.todoTag : ''}`).join('\n') : null;
// Format tags, replacing spaces with hyphens for multi-word tags
const formattedTags = recording.tags && recording.tags.length > 0
? recording.tags.map((tag: { name: string }) => `#${tag.name.replace(/\s+/g, '-')}`).join(' ')
: null;
const context = {
recording_id: recording.recording_id,
title: title,
date: formatDate(recording.created_at, this.settings.dateFormat),
transcript: transcript,
audio_link: audioLink,
summary: summary ? summary.markdown_content : null,
tidy: tidy ? tidy.markdown_content : null,
points: formattedPoints,
todo: formattedTodos,
tweet: tweet ? tweet.markdown_content : null,
blog: blog ? blog.markdown_content : null,
email: email ? email.markdown_content : null,
custom: custom ? custom.markdown_content : null,
tags: formattedTags,
related_notes: recording.related_notes && recording.related_notes.length > 0
? recording.related_notes.map((relatedNote: { title: string; created_at: string }) =>
`- [[${this.sanitizedTitle(relatedNote.title, relatedNote.created_at)}]]`
).join('\n')
: null,
subnotes: recording.subnotes && recording.subnotes.length > 0
? recording.subnotes.map((subnote: { title: string; created_at: string }) =>
`- [[${this.sanitizedTitle(subnote.title, subnote.created_at)}]]`
).join('\n')
: null,
attachments: attachments,
parent_note: isSubnote ? `[[${parentTitle}]]` : null,
};

// Render the template using Jinja
let note = jinja.render(this.settings.noteTemplate, context).replace(/\n{3,}/g, '\n\n');
note = convertHtmlToMarkdown(note);

// Add default metadata or ensure recording_id is present
if (this.settings.useDefaultFrontmatter) {
const metadata = `---
recording_id: ${recording.recording_id}
---\n`;
recording_id: ${recording.recording_id}
duration: ${formatDuration(recording.duration)}
created_at: ${formatDate(recording.created_at, this.settings.dateFormat)}
updated_at: ${formatDate(recording.updated_at, this.settings.dateFormat)}
${formatTags(recording)}
---\n`;

note = metadata + note;
} else {
const frontmatterRegex = /^---\n([\s\S]*?)\n---/;
const match = note.match(frontmatterRegex);

if (match) {
// Frontmatter exists, update or add recording_id
const existingFrontmatter = match[1];
const updatedFrontmatter = existingFrontmatter.replace(/recording_id:.*\n?/, '') + `recording_id: ${recording.recording_id}\n`;
note = note.replace(frontmatterRegex, `---\n${updatedFrontmatter}---`);
} else {
// No frontmatter, create one with recording_id
const metadata = `---
recording_id: ${recording.recording_id}
---\n`;
note = metadata + note;
}
}
}

// Create or update note
if (noteExists) {
await this.app.vault.modify(this.app.vault.getFileByPath(recordingPath) as TFile, note);
} else {
await this.app.vault.create(recordingPath, note);
}
// Create or update note
if (noteExists) {
await this.app.vault.modify(this.app.vault.getFileByPath(recordingPath) as TFile, note);
} else {
await this.app.vault.create(recordingPath, note);
}

if (!this.syncedRecordingIds.includes(recording.recording_id)) {
this.syncedRecordingIds.push(recording.recording_id);
}
if (!this.syncedRecordingIds.includes(recording.recording_id)) {
this.syncedRecordingIds.push(recording.recording_id);
}

if (this.settings.deleteSynced && this.settings.reallyDeleteSynced) {
await this.vnApi.deleteRecording(recording.recording_id);
if (this.settings.deleteSynced && this.settings.reallyDeleteSynced) {
await this.vnApi.deleteRecording(recording.recording_id);
}
}
} catch (error) {
this.settings.token = undefined;
await this.saveSettings();
new Notice(`Login token was invalid, please try logging in again.`);
return;
}

}

async sync(fullSync: boolean = false) {
console.debug(`Sync running full? ${fullSync}`);
try {
console.debug(`Sync running full? ${fullSync}`);

this.syncedRecordingIds = await this.getSyncedRecordingIds();
this.syncedRecordingIds = await this.getSyncedRecordingIds();

this.vnApi = new VoiceNotesApi({});
this.vnApi.token = this.settings.token;
this.vnApi = new VoiceNotesApi({});
this.vnApi.token = this.settings.token;

const voiceNotesDir = normalizePath(this.settings.syncDirectory);
if (!(await this.app.vault.adapter.exists(voiceNotesDir))) {
new Notice('Creating sync directory for Voice Notes Sync plugin');
await this.app.vault.createFolder(voiceNotesDir);
}
const voiceNotesDir = normalizePath(this.settings.syncDirectory);
if (!(await this.app.vault.adapter.exists(voiceNotesDir))) {
new Notice('Creating sync directory for Voice Notes Sync plugin');
await this.app.vault.createFolder(voiceNotesDir);
}

const recordings = await this.vnApi.getRecordings();
let unsyncedCount = { count: 0 };
const recordings = await this.vnApi.getRecordings();
// This only happens if we aren't actually logged in, fail immediately.
if (recordings === null) {
this.settings.token = undefined;
return;
}
let unsyncedCount = { count: 0 };

if (fullSync && recordings.links.next) {
let nextPage = recordings.links.next;
if (fullSync && recordings.links.next) {
let nextPage = recordings.links.next;

do {
console.debug(`Performing a full sync ${nextPage}`);
do {
console.debug(`Performing a full sync ${nextPage}`);

const moreRecordings = await this.vnApi.getRecordingsFromLink(nextPage);
recordings.data.push(...moreRecordings.data);
nextPage = moreRecordings.links.next;
} while (nextPage);
}
const moreRecordings = await this.vnApi.getRecordingsFromLink(nextPage);
recordings.data.push(...moreRecordings.data);
nextPage = moreRecordings.links.next;
} while (nextPage);
}

if (recordings) {
new Notice(`Syncing latest Voicenotes`);
for (const recording of recordings.data) {
await this.processNote(recording, voiceNotesDir, false, '', unsyncedCount);
if (recordings) {
new Notice(`Syncing latest Voicenotes`);
for (const recording of recordings.data) {
await this.processNote(recording, voiceNotesDir, false, '', unsyncedCount);
}
}
}

new Notice(`Sync complete. ${unsyncedCount.count} recordings were not synced due to excluded tags.`);
new Notice(`Sync complete. ${unsyncedCount.count} recordings were not synced due to excluded tags.`);
} catch (error) {
this.settings.token = undefined;
await this.saveSettings();
new Notice(`Login token was invalid, please try logging in again.`);
return;
}
}
}
Loading

0 comments on commit e9b7205

Please sign in to comment.