Skip to content

Commit

Permalink
Merge pull request #30 from djmango/toastSignIn
Browse files Browse the repository at this point in the history
Toast sign in
  • Loading branch information
zakialvi-e64 authored Nov 29, 2023
2 parents 8d68ba5 + 3a5e10a commit 2d3d1b4
Show file tree
Hide file tree
Showing 6 changed files with 1,459 additions and 50 deletions.
16 changes: 16 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 44 additions & 7 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TranscriptionSettings,
DEFAULT_SETTINGS,
TranscriptionSettingTab,
SWIFTINK_AUTH_CALLBACK
} from "./settings";

export default class Transcription extends Plugin {
Expand Down Expand Up @@ -99,11 +100,41 @@ export default class Transcription extends Plugin {
}

// If the user is still null, prompt them to sign in
if (this.user == null)
new Notice(
"Transcription: Please sign in to Swiftink.io via the settings tab",
4000,
);
// if (this.user == null) {
// const notice = new Notice("Transcription: You are signed out. Please click to Sign In", 16 * 1000);

// notice.noticeEl.addEventListener('click', () => {
// window.open(SWIFTINK_AUTH_CALLBACK, '_blank');
// });


// }

if (this.user == null) {
const noticeContent = document.createDocumentFragment();

// Create the text node
const textNode = document.createTextNode("Transcription: You are signed out. Please ");

// Create the hyperlink
const signInLink = document.createElement('a');
//signInLink.href = SWIFTINK_AUTH_CALLBACK;
signInLink.target = '_blank';
signInLink.textContent = 'Sign In';

// Append the text and link to the document fragment
noticeContent.appendChild(textNode);
noticeContent.appendChild(signInLink);

// Create the notice with the content
const notice = new Notice(noticeContent, 16 * 1000);
notice.noticeEl.addEventListener('click', () => {
window.open(SWIFTINK_AUTH_CALLBACK, '_blank');
});
}



}
}

Expand All @@ -126,7 +157,8 @@ export default class Transcription extends Plugin {
const linkedFileExtension = linkedFilePath.split(".").pop();
if (
linkedFileExtension === undefined ||
!Transcription.transcribeFileExtensions.includes(linkedFileExtension.toLowerCase(),
!Transcription.transcribeFileExtensions.includes(
linkedFileExtension.toLowerCase(),
)
) {
if (this.settings.debug)
Expand Down Expand Up @@ -155,6 +187,7 @@ export default class Transcription extends Plugin {
};

const transcribeAndWrite = async (parent_file: TFile, file: TFile) => {

if (this.settings.debug) console.log("Transcribing " + file.path);

this.transcription_engine
Expand Down Expand Up @@ -246,6 +279,7 @@ export default class Transcription extends Plugin {
},
});


// Register a command to transcribe a media file when right-clicking on it
// this.registerEvent(
// // if (!Transcription.transcribeFileExtensions.includes(view.file.extension.toLowerCase())) return;
Expand Down Expand Up @@ -347,6 +381,7 @@ export default class Transcription extends Plugin {
element.innerHTML = `Manage ${this.user?.email}`;
});
}

return;
},
);
Expand Down Expand Up @@ -409,6 +444,8 @@ export default class Transcription extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
}


}

export { Transcription };
export { Transcription };
41 changes: 22 additions & 19 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,26 @@ import { App, PluginSettingTab, Setting, Notice } from "obsidian";
import { Transcription } from "./main";

interface TranscriptionSettings {
timestamps: boolean;
timestampFormat: string;
translate: boolean;
language: string;
verbosity: number;
whisperASRUrl: string;
debug: boolean;
transcription_engine: string;
embedAdditionalFunctionality: boolean;
embedSummary: boolean;
embedOutline: boolean;
embedKeywords: boolean;
swiftink_access_token: string | null;
swiftink_refresh_token: string | null;
timestamps: boolean;
timestampFormat: string;
translate: boolean;
language: string;
verbosity: number;
whisperASRUrl: string;
debug: boolean;
transcription_engine: string;
embedAdditionalFunctionality: boolean;
embedSummary: boolean;
embedOutline: boolean;
embedKeywords: boolean;
swiftink_access_token: string | null;
swiftink_refresh_token: string | null;


}

const SWIFTINK_AUTH_CALLBACK =
"https://swiftink.io/login/?callback=obsidian://swiftink_auth";
"https://swiftink.io/login/?callback=obsidian://swiftink_auth";

const DEFAULT_SETTINGS: TranscriptionSettings = {
timestamps: false,
Expand All @@ -36,6 +38,7 @@ const DEFAULT_SETTINGS: TranscriptionSettings = {
embedKeywords: true,
swiftink_access_token: null,
swiftink_refresh_token: null,

};

const LANGUAGES = {
Expand Down Expand Up @@ -216,7 +219,7 @@ class TranscriptionSettingTab extends PluginSettingTab {
dropdown.addOption(
value,
key.charAt(0).toUpperCase() +
key.slice(1).toLowerCase(),
key.slice(1).toLowerCase(),
);
}
dropdown.setValue(this.plugin.settings.language);
Expand Down Expand Up @@ -388,7 +391,7 @@ class TranscriptionSettingTab extends PluginSettingTab {
.setValue(this.plugin.settings.embedAdditionalFunctionality)
.onChange(async (value) => {
this.plugin.settings.embedAdditionalFunctionality =
value;
value;
await this.plugin.saveSettings();
}),
);
Expand Down Expand Up @@ -452,14 +455,14 @@ class TranscriptionSettingTab extends PluginSettingTab {
const help = containerEl.createEl("p");
help.classList.add("swiftink-settings");
help.innerHTML =
"Questions? Please see our <a href='https://www.swiftink.io/docs'>Documentation</a> or email us at <a href='mailto:[email protected]'>[email protected]</a> 🙂";
"Questions? Please see our <a href='https://www.swiftink.io/docs'>Documentation</a> or email us at <a href='mailto:[email protected]'>[email protected]</a> 🙂";
help.style.textAlign = "center";
help.style.fontSize = "0.85em";

const disclaimer = containerEl.createEl("p");
disclaimer.classList.add("swiftink-settings");
disclaimer.innerHTML =
"By proceeding you agree to our <a href='https://www.swiftink.io/terms'>Terms of Service</a> and <a href='https://www.swiftink.io/privacy'>Privacy Policy</a>.";
"By proceeding you agree to our <a href='https://www.swiftink.io/terms'>Terms of Service</a> and <a href='https://www.swiftink.io/privacy'>Privacy Policy</a>.";
disclaimer.style.textAlign = "center";
disclaimer.style.fontSize = "0.85em";

Expand Down
8 changes: 4 additions & 4 deletions src/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ export class StatusBar {


displayMessage(message: string, timeout: number, force = false, kek_mode = false) {
// Don't show the same message twice
// Don't show the same message twice
if (this.messages[0] && this.messages[0].message === message) return;

this.messages.push(new StatusBarMessage(`Transcribe: ${message.slice(0, 100)}`, timeout, force, kek_mode));
this.display();
}

display() {
// First check if there are any forced messages, if so, clear the queue and queue the last forced message
// First check if there are any forced messages, if so, clear the queue and queue the last forced message
if (this.hasForcedMessage()) {
const lastForced = this.messages.filter((message) => message.force).pop();
if (lastForced) this.messages = [lastForced];
Expand Down Expand Up @@ -91,12 +91,12 @@ class StatusBarMessage {
timeShown: number;
kek_mode: boolean;

messageAge = function() {
messageAge = function () {
if (!this.timeShown) return 0;
return Date.now() - this.timeShown;
}

messageTimedOut = function() {
messageTimedOut = function () {
return this.messageAge() >= this.timeout;
}

Expand Down
40 changes: 20 additions & 20 deletions src/transcribe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class TranscriptionEngine {
);
const start = new Date();
this.transcriptionEngine =
this.transcription_engines[this.settings.transcription_engine];
this.transcription_engines[this.settings.transcription_engine];
return this.transcriptionEngine(file).then((transcription) => {
if (this.settings.debug)
console.log(`Transcription: ${transcription}`);
Expand All @@ -85,7 +85,7 @@ export class TranscriptionEngine {
await this.vault.readBinary(file),
]);
const [request_body, boundary_string] =
await payloadGenerator(payload_data);
await payloadGenerator(payload_data);

let args = "task=transcribe";
if (this.settings.language != "auto")
Expand Down Expand Up @@ -137,7 +137,7 @@ export class TranscriptionEngine {
// Declare progress notice for uploading
let uploadProgressNotice: Notice | null = null;

const uploadPromise = new Promise<tus.Upload>((resolve, reject) => {
const uploadPromise = new Promise<tus.Upload>((resolve) => {
const upload = new tus.Upload(new Blob([fileStream]), {
endpoint: `https://auth.swiftink.io/storage/v1/upload/resumable`,
retryDelays: [0, 3000, 5000, 10000, 20000],
Expand All @@ -154,7 +154,7 @@ export class TranscriptionEngine {
onProgress: (bytesUploaded, bytesTotal) => {
const percentage = (
(bytesUploaded / bytesTotal) *
100
100
).toFixed(2);

// Create a notice message with the progress
Expand Down Expand Up @@ -192,6 +192,7 @@ export class TranscriptionEngine {

resolve(upload);
},

});

upload.start();
Expand All @@ -205,12 +206,7 @@ export class TranscriptionEngine {
console.log("Failed to upload to Swiftink: ", error);
}

// // Close the progress notice on upload failure
// if (uploadProgressNotice) {
// uploadProgressNotice.hide();
// }

return Promise.reject(error);
return Promise.reject(new Notice(`Failed to upload ${filename} to Swiftink`));
}

// Declare progress notice for transcription
Expand All @@ -220,7 +216,10 @@ export class TranscriptionEngine {
const url = `${api_base}/transcripts/`;
const headers = { Authorization: `Bearer ${token}` };
const body: paths["/transcripts/"]["post"]["requestBody"]["content"]["application/json"] =
{ name: filename, url: fileUrl, };
{
name: filename,
url: fileUrl,
};

if (this.settings.language != "auto")
body.language = this.settings
Expand All @@ -245,15 +244,15 @@ export class TranscriptionEngine {
}

let transcript: components["schemas"]["TranscriptSchema"] =
transcript_create_res.json;
transcript_create_res.json;
if (this.settings.debug) console.log(transcript);

let completed_statuses = ["transcribed", "complete"];

if (
this.settings.embedSummary ||
this.settings.embedOutline ||
this.settings.embedKeywords
this.settings.embedOutline ||
this.settings.embedKeywords
) {
completed_statuses = ["complete"];
}
Expand All @@ -268,6 +267,7 @@ export class TranscriptionEngine {
transcriptionProgressNotice = new Notice(noticeMessage, 800 * 100);
} else {
transcriptionProgressNotice.setMessage(noticeMessage);

}
};

Expand All @@ -283,7 +283,7 @@ export class TranscriptionEngine {

if (
transcript.status &&
completed_statuses.includes(transcript.status)
completed_statuses.includes(transcript.status)
) {
clearInterval(poll);

Expand Down Expand Up @@ -345,9 +345,9 @@ export class TranscriptionEngine {

if (
this.settings.embedSummary &&
transcript.summary &&
transcript.summary !==
"Insufficient information for a summary."
transcript.summary &&
transcript.summary !==
"Insufficient information for a summary."
)
transcript_text += `## Summary\n${transcript.summary}`;

Expand All @@ -356,7 +356,7 @@ export class TranscriptionEngine {

if (
this.settings.embedOutline &&
transcript.heading_segments.length > 0
transcript.heading_segments.length > 0
)
transcript_text += `## Outline\n${this.segmentsToTimestampedString(
transcript.heading_segments,
Expand All @@ -368,7 +368,7 @@ export class TranscriptionEngine {

if (
this.settings.embedKeywords &&
transcript.keywords.length > 0
transcript.keywords.length > 0
)
transcript_text += `## Keywords\n${transcript.keywords.join(
", "
Expand Down
Loading

0 comments on commit 2d3d1b4

Please sign in to comment.