diff --git a/manifest.json b/manifest.json index 4870a49..23bd63b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "notetweet", "name": "NoteTweetšŸ¦", - "version": "0.4.0", + "version": "0.4.1", "minAppVersion": "0.9.12", "description": "This plugin allows you to post tweets directly from Obsidian.", "author": "Christian B. B. Houmann", diff --git a/package.json b/package.json index 1245e12..9fb3718 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "notetweet", - "version": "0.4.0", + "version": "0.4.1", "description": "Post tweets from Obsidian", "main": "src/main.js", "scripts": { diff --git a/src/ErrorModule/ILogger.ts b/src/ErrorModule/ILogger.ts new file mode 100644 index 0000000..da13394 --- /dev/null +++ b/src/ErrorModule/ILogger.ts @@ -0,0 +1,7 @@ +export interface ILogger { + logError(msg: string): void; + + logWarning(msg: string): void; + + logMessage(msg: string): void; +} \ No newline at end of file diff --git a/src/ErrorModule/consoleErrorLogger.ts b/src/ErrorModule/consoleErrorLogger.ts new file mode 100644 index 0000000..bd176a7 --- /dev/null +++ b/src/ErrorModule/consoleErrorLogger.ts @@ -0,0 +1,32 @@ +import {ErrorLevel} from "./errorLevel"; +import {NoteTweetError} from "./noteTweetError"; +import {NoteTweetLogger} from "./noteTweetLogger"; + +export class ConsoleErrorLogger extends NoteTweetLogger { + public ErrorLog: NoteTweetError[] = []; + + public logError(errorMsg: string) { + const error = this.getNoteTweetError(errorMsg, ErrorLevel.Error); + this.addMessageToErrorLog(error); + + console.error(this.formatOutputString(error)); + } + + public logWarning(warningMsg: string) { + const warning = this.getNoteTweetError(warningMsg, ErrorLevel.Warning); + this.addMessageToErrorLog(warning); + + console.warn(this.formatOutputString(warning)); + } + + public logMessage(logMsg: string) { + const log = this.getNoteTweetError(logMsg, ErrorLevel.Log); + this.addMessageToErrorLog(log); + + console.log(this.formatOutputString(log)); + } + + private addMessageToErrorLog(error: NoteTweetError): void { + this.ErrorLog.push(error); + } +} \ No newline at end of file diff --git a/src/ErrorModule/errorLevel.ts b/src/ErrorModule/errorLevel.ts new file mode 100644 index 0000000..7c445aa --- /dev/null +++ b/src/ErrorModule/errorLevel.ts @@ -0,0 +1 @@ +export enum ErrorLevel { Error = "ERROR", Warning = "WARNING", Log = "LOG"} \ No newline at end of file diff --git a/src/ErrorModule/guiLogger.ts b/src/ErrorModule/guiLogger.ts new file mode 100644 index 0000000..8e45ee3 --- /dev/null +++ b/src/ErrorModule/guiLogger.ts @@ -0,0 +1,20 @@ +import {NoteTweetLogger} from "./noteTweetLogger"; +import {TweetErrorModal} from "../Modals/TweetErrorModal"; +import NoteTweet from "../main"; +import {Notice} from "obsidian"; + +export class GuiLogger extends NoteTweetLogger { + constructor(private plugin: NoteTweet) { + super(); + } + + logError(msg: string): void { + new TweetErrorModal(this.plugin.app, msg).open(); + } + + logWarning(msg: string): void { + new Notice(msg); + } + + logMessage(msg: string): void {} +} \ No newline at end of file diff --git a/src/ErrorModule/logManager.ts b/src/ErrorModule/logManager.ts new file mode 100644 index 0000000..79c19dd --- /dev/null +++ b/src/ErrorModule/logManager.ts @@ -0,0 +1,25 @@ +import {ILogger} from "./ILogger"; + +class LogManager { + public static loggers: ILogger[] = []; + + public register(logger: ILogger): LogManager { + LogManager.loggers.push(logger); + + return this; + } + + logError(message: string) { + LogManager.loggers.forEach(logger => logger.logError(message)); + } + + logWarning(message: string) { + LogManager.loggers.forEach(logger => logger.logError(message)); + } + + logMessage(message: string) { + LogManager.loggers.forEach(logger => logger.logMessage(message)); + } +} + +export const log = new LogManager(); diff --git a/src/ErrorModule/noteTweetError.ts b/src/ErrorModule/noteTweetError.ts new file mode 100644 index 0000000..fe3eea0 --- /dev/null +++ b/src/ErrorModule/noteTweetError.ts @@ -0,0 +1,7 @@ +import {ErrorLevel} from "./errorLevel"; + +export interface NoteTweetError { + message: string, + level: ErrorLevel, + time: number +} \ No newline at end of file diff --git a/src/ErrorModule/noteTweetLogger.ts b/src/ErrorModule/noteTweetLogger.ts new file mode 100644 index 0000000..d2f54f6 --- /dev/null +++ b/src/ErrorModule/noteTweetLogger.ts @@ -0,0 +1,19 @@ +import {ILogger} from "./ILogger"; +import {NoteTweetError} from "./noteTweetError"; +import {ErrorLevel} from "./errorLevel"; + +export abstract class NoteTweetLogger implements ILogger { + abstract logError(msg: string): void; + + abstract logMessage(msg: string): void; + + abstract logWarning(msg: string): void; + + protected formatOutputString(error: NoteTweetError): string { + return `NoteTweet: (${error.level}) ${error.message}`; + } + + protected getNoteTweetError(message: string, level: ErrorLevel): NoteTweetError { + return {message, level, time: Date.now()}; + } +} \ No newline at end of file diff --git a/src/Modals/PostTweetModal.ts b/src/Modals/PostTweetModal.ts index 22e6f1e..6df6b20 100644 --- a/src/Modals/PostTweetModal.ts +++ b/src/Modals/PostTweetModal.ts @@ -2,6 +2,7 @@ import { App, Modal, Notice } from "obsidian"; import { TwitterHandler } from "../TwitterHandler"; import { TweetsPostedModal } from "./TweetsPostedModal/TweetsPostedModal"; import { TweetErrorModal } from "./TweetErrorModal"; +import {log} from "../ErrorModule/logManager"; export class PostTweetModal extends Modal { private readonly twitterHandler: TwitterHandler; @@ -42,7 +43,7 @@ export class PostTweetModal extends Modal { this.createTweetButton(contentEl); } catch (e) { - new Notice(e); + log.logWarning(e); this.close(); return; } @@ -78,7 +79,7 @@ export class PostTweetModal extends Modal { tempTextarea.style.height = tempTextarea.scrollHeight + "px"; } catch (e) { - new Notice(e); + log.logWarning(e); return; } }); @@ -192,7 +193,7 @@ export class PostTweetModal extends Modal { try { this.createTextarea(textZone); } catch (e) { - new Notice(e); + log.logWarning(e); return; } } @@ -202,7 +203,7 @@ export class PostTweetModal extends Modal { try { this.createTextarea(textZone); } catch (e) { - new Notice(e); + log.logWarning(e); return; } } @@ -320,7 +321,7 @@ export class PostTweetModal extends Modal { (txt) => txt.length > this.MAX_TWEET_LENGTH || txt == "" ) != null ) { - new Notice("At least one of your tweets is too long or empty."); + log.logWarning("At least one of your tweets is too long or empty."); return; } @@ -333,7 +334,7 @@ export class PostTweetModal extends Modal { ); postedModal.open(); } catch (e) { - new TweetErrorModal(this.app, e.data || e).open(); + log.logError(`unable to post tweet. ${e}`); } this.close(); @@ -354,7 +355,7 @@ export class PostTweetModal extends Modal { return { tweet: insertedTweet, index: insertAboveIndex }; } catch (e) { - new Notice(e); + log.logWarning(e); return; } } @@ -374,7 +375,7 @@ export class PostTweetModal extends Modal { return insertedTextarea; } catch (e) { - new Notice(e); + log.logWarning(e); } } @@ -387,28 +388,4 @@ export class PostTweetModal extends Modal { this.textAreas[insertedIndex].value = ""; this.textAreas[insertedIndex].focus(); } - - /* private insertTweetBelowWithText(textarea: HTMLTextAreaElement, textZone: HTMLDivElement, insertText: string){ - // Insert tweet, assign to var. Pass that var in again. - // It'll be reverse order if I don't insert below each one. For inserting above, you can just insert as you normally would. - if (insertText.length > this.MAX_TWEET_LENGTH) { - let sliced = this.textInputHandler(insertText); // First, make sure the text is sized correctly. - let tweet: HTMLTextAreaElement = textarea; - - let tweetIndex = this.insertTweetBelow(tweet, textZone); - tweet = this.textAreas[tweetIndex]; - this.insertTweetBelowWithText(tweet, textZone, sliced.slice(1).join()); - - // sliced.forEach(chunk => { - // console.log("!!!!") - // let x = this.insertTweetBelow(tweet, textZone); - // tweet = x.insertedTweet; - // tweet.value = chunk; - // }); - } - else { - let {insertedTweet, insertedIndex} = this.insertTweetBelow(textarea, textZone); - this.textAreas[insertedIndex].value = insertText; - } - }*/ } diff --git a/src/Modals/TweetErrorModal.ts b/src/Modals/TweetErrorModal.ts index fdba3bb..c43dc2f 100644 --- a/src/Modals/TweetErrorModal.ts +++ b/src/Modals/TweetErrorModal.ts @@ -11,7 +11,7 @@ export class TweetErrorModal extends Modal { onOpen() { let { contentEl } = this; - contentEl.setText(`Post failed: ${this.errorMessage}`); + contentEl.setText(`NoteTweet: ${this.errorMessage}`); } onClose() { diff --git a/src/TwitterHandler.ts b/src/TwitterHandler.ts index 4f9a1cd..15e9769 100644 --- a/src/TwitterHandler.ts +++ b/src/TwitterHandler.ts @@ -1,5 +1,6 @@ import { StatusesUpdate, TwitterClient } from "twitter-api-client"; import NoteTweet from "./main"; +import {log} from "./ErrorModule/logManager"; export class TwitterHandler { private twitterClient: TwitterClient; @@ -76,6 +77,8 @@ export class TwitterHandler { if (media_id) { media_ids.push(media_id.media_id_string); processedTweet = processedTweet.replace(this.IMAGE_REGEX, ""); + } else { + log.logWarning(`image '${fileName}' found but could not upload it to Twitter. Data is null/undefined: ${!!media_data}.`); } } @@ -90,7 +93,8 @@ export class TwitterHandler { }); return true; - } catch { + } catch(e) { + log.logError(`error in deleting tweets. ${e}`); return false; } } diff --git a/src/main.ts b/src/main.ts index f318377..779e109 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,9 @@ import {TweetsPostedModal} from "./Modals/TweetsPostedModal/TweetsPostedModal"; import {TweetErrorModal} from "./Modals/TweetErrorModal"; import {SecureModeGetPasswordModal} from "./Modals/SecureModeGetPasswordModal/SecureModeGetPasswordModal"; import {PostTweetModal} from "./Modals/PostTweetModal"; +import {log} from "./ErrorModule/logManager"; +import {ConsoleErrorLogger} from "./ErrorModule/consoleErrorLogger"; +import {GuiLogger} from "./ErrorModule/guiLogger"; const WELCOME_MESSAGE: string = "Loading NoteTweetšŸ¦. Thanks for installing."; const UNLOAD_MESSAGE: string = "Unloaded NoteTweet."; @@ -87,9 +90,8 @@ export default class NoteTweet extends Plugin { }); /*END.DEVCMD*/ - this.registerObsidianProtocolHandler("notetweet", params => { - console.log(params); - }) + log.register(new ConsoleErrorLogger()) + .register(new GuiLogger(this)); this.addSettingTab(new NoteTweetSettingsTab(this.app, this)); } @@ -145,7 +147,7 @@ export default class NoteTweet extends Plugin { try { threadContent = this.parseThreadFromText(content); } catch (e) { - new TweetErrorModal(this.app, e).open(); + log.logError(`error in parsing thread in file ${file?.name}. ${e}`); return; } @@ -162,7 +164,7 @@ export default class NoteTweet extends Plugin { postedTweets.forEach((tweet) => this.appendPostTweetTag(tweet.text)); } } catch (e) { - new TweetErrorModal(this.app, e.data || e).open(); + log.logError(`failed attempted to post tweets. ${e}`); } } @@ -192,10 +194,10 @@ export default class NoteTweet extends Plugin { await this.appendPostTweetTag(tweet.text); } } catch (e) { - new TweetErrorModal(this.app, e.data || e).open(); + log.logError(`failed attempt to post selected. ${e}`); } } else { - new TweetErrorModal(this.app, "nothing selected.").open(); + log.logWarning(`tried to post selected but nothing was selected.`) } } @@ -210,11 +212,11 @@ export default class NoteTweet extends Plugin { modal.waitForClose .then(async () => { if (this.twitterHandler.isConnectedToTwitter) await callback(); - else new Notice("Could not connect to Twitter"); + else log.logWarning("could not connect to Twitter"); }) .catch(() => { modal.close(); - new Notice("Could not connect to Twitter."); + log.logWarning("could not connect to Twitter."); }); } diff --git a/versions.json b/versions.json index 2a4e96b..50c14ff 100644 --- a/versions.json +++ b/versions.json @@ -1,5 +1,5 @@ { "0.3.3": "0.9.12", "0.3.4": "0.12.0", - "0.4.0": "0.12.3" + "0.4.1": "0.12.3" }