From 9d309b2e8c5b35401d39efef8fda9d7deafea466 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Wed, 12 Jul 2017 20:37:34 +0200 Subject: [PATCH] Added status messages --- auth/google.go | 4 ++ layout/layout.pixy | 7 +++ mixins/Postable.pixy | 8 +-- .../delete-private-data.go | 59 ++++++++++--------- scripts/Actions.ts | 29 +++++---- scripts/AnimeNotifier.ts | 8 +++ scripts/StatusMessage.ts | 37 ++++++++++++ styles/status-message.scarlet | 18 ++++++ utils/Icon.go | 2 +- 9 files changed, 124 insertions(+), 48 deletions(-) create mode 100644 scripts/StatusMessage.ts create mode 100644 styles/status-message.scarlet diff --git a/auth/google.go b/auth/google.go index 7839beb9..8355305a 100644 --- a/auth/google.go +++ b/auth/google.go @@ -90,6 +90,10 @@ func InstallGoogleAuth(app *aero.Application) { return ctx.Error(http.StatusBadRequest, "Failed parsing user data (JSON)", err) } + if googleUser.Sub == "" { + return ctx.Error(http.StatusBadRequest, "Failed retrieving Google data", errors.New("Empty ID")) + } + // Change googlemail.com to gmail.com googleUser.Email = strings.Replace(googleUser.Email, "googlemail.com", "gmail.com", 1) diff --git a/layout/layout.pixy b/layout/layout.pixy index 3ab5cb01..c6cac28d 100644 --- a/layout/layout.pixy +++ b/layout/layout.pixy @@ -25,10 +25,17 @@ component Layout(app *aero.Application, ctx *aero.Context, user *arn.User, openG #content-container main#content.fade!= content LoadingAnimation + StatusMessage if user != nil #user(data-id=user.ID) script(src="/scripts") +component StatusMessage + #status-message.fade.fade-out + #status-message-text + a.status-message-action.action(href="#", data-trigger="click", data-action="closeStatusMessage") + RawIcon("close") + component LoadingAnimation #loading.sk-cube-grid.fade .sk-cube.hide diff --git a/mixins/Postable.pixy b/mixins/Postable.pixy index 6df39fff..c0c8e366 100644 --- a/mixins/Postable.pixy +++ b/mixins/Postable.pixy @@ -33,18 +33,18 @@ component Postable(post arn.Postable, user *arn.User, highlightAuthorID string) if user.ID != post.Author().ID if post.LikedBy(user.ID) a.post-tool.post-unlike.action(id="unlike-" + post.ID(), title="Unlike", data-action="unlike", data-trigger="click") - RawIcon("thumbs-down") + Icon("thumbs-down") else a.post-tool.post-like.action(id="like-" + post.ID(), title="Like", data-action="like", data-trigger="click") - RawIcon("thumbs-up") + Icon("thumbs-up") if user.ID == post.Author().ID a.post-tool.post-edit.action(data-action="editPost", data-trigger="click", data-id=post.ID(), title="Edit") - RawIcon("pencil") + Icon("pencil") if post.Type() != "Thread" a.post-tool.post-permalink.ajax(href=post.Link(), title="Permalink") - RawIcon("link") + Icon("link") //- if type === "Messages" && user && (user.ID === post.authorId || user.ID === post.recipientId) //- a.post-tool.post-delete(onclick=`if(confirm("Do you really want to delete this ${typeSingular.toLowerCase()} from ${post.author.nick}?")) $.delete${typeSingular}("${post.ID}")`, title="Delete") diff --git a/patches/delete-private-data/delete-private-data.go b/patches/delete-private-data/delete-private-data.go index 94df6456..6b3d4ad2 100644 --- a/patches/delete-private-data/delete-private-data.go +++ b/patches/delete-private-data/delete-private-data.go @@ -1,40 +1,43 @@ package main -// This patch is disabled because it would be dangerous to run it accidentally. +import ( + "github.com/animenotifier/arn" + "github.com/fatih/color" +) func main() { - // color.Yellow("Deleting private user data") + color.Yellow("Deleting private user data") - // // Get a stream of all users - // allUsers, err := arn.StreamUsers() + // Get a stream of all users + allUsers, err := arn.StreamUsers() - // if err != nil { - // panic(err) - // } + if err != nil { + panic(err) + } - // arn.DB.DeleteTable("EmailToUser") - // arn.DB.DeleteTable("GoogleToUser") + arn.DB.DeleteTable("EmailToUser") + arn.DB.DeleteTable("GoogleToUser") - // // Iterate over the stream - // count := 0 - // for user := range allUsers { - // count++ - // println(count, user.Nick) + // Iterate over the stream + count := 0 + for user := range allUsers { + count++ + println(count, user.Nick) - // // Delete private data - // user.Email = "" - // user.Gender = "" - // user.FirstName = "" - // user.LastName = "" - // user.IP = "" - // user.Accounts.Facebook.ID = "" - // user.Accounts.Google.ID = "" - // user.AgeRange = arn.UserAgeRange{} - // user.Location = arn.UserLocation{} + // Delete private data + user.Email = "" + user.Gender = "" + user.FirstName = "" + user.LastName = "" + user.IP = "" + user.Accounts.Facebook.ID = "" + user.Accounts.Google.ID = "" + user.AgeRange = arn.UserAgeRange{} + user.Location = arn.UserLocation{} - // // Save in DB - // user.Save() - // } + // Save in DB + user.Save() + } - // color.Green("Finished.") + color.Green("Finished.") } diff --git a/scripts/Actions.ts b/scripts/Actions.ts index 119512c3..d4aafbd4 100644 --- a/scripts/Actions.ts +++ b/scripts/Actions.ts @@ -40,7 +40,7 @@ export function save(arn: AnimeNotifier, input: HTMLInputElement | HTMLTextAreaE throw body } }) - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) .then(() => { arn.loading(false) @@ -54,6 +54,11 @@ export function save(arn: AnimeNotifier, input: HTMLInputElement | HTMLTextAreaE }) } +// Close status message +export function closeStatusMessage(arn: AnimeNotifier) { + arn.statusMessage.close() +} + // Load export function load(arn: AnimeNotifier, element: HTMLElement) { let url = element.dataset.url || (element as HTMLAnchorElement).getAttribute("href") @@ -112,7 +117,7 @@ export function savePost(arn: AnimeNotifier, element: HTMLElement) { arn.post(apiEndpoint, updates) .then(() => arn.reloadContent()) - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) } // like @@ -121,7 +126,7 @@ export function like(arn: AnimeNotifier, element: HTMLElement) { arn.post(apiEndpoint + "/like", null) .then(() => arn.reloadContent()) - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) } // unlike @@ -130,7 +135,7 @@ export function unlike(arn: AnimeNotifier, element: HTMLElement) { arn.post(apiEndpoint + "/unlike", null) .then(() => arn.reloadContent()) - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) } // Forum reply @@ -147,7 +152,7 @@ export function forumReply(arn: AnimeNotifier) { arn.post("/api/new/post", post) .then(() => arn.reloadContent()) .then(() => textarea.value = "") - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) } // Create thread @@ -164,7 +169,7 @@ export function createThread(arn: AnimeNotifier) { arn.post("/api/new/thread", thread) .then(() => arn.app.load("/forum/" + thread.tags[0])) - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) } // Create soundtrack @@ -180,15 +185,9 @@ export function createSoundTrack(arn: AnimeNotifier, button: HTMLButtonElement) tags: [anime.value, osu.value], } - button.innerText = "Adding..." - button.disabled = true - arn.post("/api/new/soundtrack", soundtrack) .then(() => arn.app.load("/music")) - .catch(err => { - console.error(err) - arn.reloadContent() - }) + .catch(err => arn.statusMessage.showError(err)) } // Search @@ -250,7 +249,7 @@ export function addAnimeToCollection(arn: AnimeNotifier, button: HTMLElement) { return arn.reloadContent() }) - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) .then(() => arn.loading(false)) } @@ -274,7 +273,7 @@ export function removeAnimeFromCollection(arn: AnimeNotifier, button: HTMLElemen return arn.app.load("/+" + userNick + "/animelist") }) - .catch(console.error) + .catch(err => arn.statusMessage.showError(err)) .then(() => arn.loading(false)) } diff --git a/scripts/AnimeNotifier.ts b/scripts/AnimeNotifier.ts index 3e3cc16a..12501b26 100644 --- a/scripts/AnimeNotifier.ts +++ b/scripts/AnimeNotifier.ts @@ -3,6 +3,7 @@ import { Diff } from "./Diff" import { displayAiringDate, displayDate } from "./DateView" import { findAll, delay, canUseWebP } from "./Utils" import { MutationQueue } from "./MutationQueue" +import { StatusMessage } from "./StatusMessage" import * as actions from "./Actions" export class AnimeNotifier { @@ -10,6 +11,7 @@ export class AnimeNotifier { user: HTMLElement title: string webpEnabled: boolean + statusMessage: StatusMessage visibilityObserver: IntersectionObserver imageFound: MutationQueue @@ -89,6 +91,12 @@ export class AnimeNotifier { this.app.content = this.app.find("content") this.app.loading = this.app.find("loading") + // Status message + this.statusMessage = new StatusMessage( + this.app.find("status-message"), + this.app.find("status-message-text") + ) + // Let's start this.app.run() } diff --git a/scripts/StatusMessage.ts b/scripts/StatusMessage.ts new file mode 100644 index 00000000..0820b90e --- /dev/null +++ b/scripts/StatusMessage.ts @@ -0,0 +1,37 @@ +import { delay } from "./Utils" + +export class StatusMessage { + container: HTMLElement + text: HTMLElement + + constructor(container: HTMLElement, text: HTMLElement) { + this.container = container + this.text = text + } + + show(message: string, duration?: number) { + let messageId = String(Date.now()) + + this.text.innerText = message + + this.container.classList.remove("fade-out") + this.container.dataset.messageId = messageId + + delay(duration || 4000).then(() => { + if(this.container.dataset.messageId !== messageId) { + return + } + + this.close() + }) + } + + showError(message: string, duration?: number) { + this.show(message, duration) + this.container.classList.add("error-message") + } + + close() { + this.container.classList.add("fade-out") + } +} \ No newline at end of file diff --git a/styles/status-message.scarlet b/styles/status-message.scarlet new file mode 100644 index 00000000..e0c916a7 --- /dev/null +++ b/styles/status-message.scarlet @@ -0,0 +1,18 @@ +#status-message + horizontal + position fixed + bottom 0 + left 0 + width 100% + padding calc(content-padding / 2) content-padding + +#status-message-text + flex 1 + text-align center + +.status-message-action + color white !important + +.error-message + color white + background-color hsl(0, 75%, 50%) \ No newline at end of file diff --git a/utils/Icon.go b/utils/Icon.go index 87886781..dfc6ea25 100644 --- a/utils/Icon.go +++ b/utils/Icon.go @@ -24,5 +24,5 @@ func Icon(name string) string { // RawIcon ... func RawIcon(name string) string { - return strings.Replace(svgIcons[name], "class='icon'", "class='raw-icon'", 1) + return strings.Replace(svgIcons[name], "class='icon", "class='raw-icon", 1) }