From d8bc09074c77904594c9e7119084b7d3763f01b4 Mon Sep 17 00:00:00 2001 From: Redume Date: Tue, 22 Nov 2022 21:36:35 +0300 Subject: [PATCH] Analytics has been done, it is possible to turn it off --- api/controllers/settings.go | 19 ++++++-- functions/database.go | 35 ++------------ functions/logger.go | 28 ++++++++++++ main.go | 4 -- web/src/about.html | 2 +- web/src/gallery.html | 1 + web/src/settings.html | 15 ++++-- web/static/scripts/analytics.js | 27 +++++++++++ web/static/scripts/switch.js | 81 +++++++++++++++++++++++++++------ 9 files changed, 156 insertions(+), 56 deletions(-) create mode 100644 web/static/scripts/analytics.js diff --git a/api/controllers/settings.go b/api/controllers/settings.go index c312417..e768dd7 100644 --- a/api/controllers/settings.go +++ b/api/controllers/settings.go @@ -27,14 +27,17 @@ var SettingsGet = func(c *fiber.Ctx) error { } }(querySettings) - var startup, wallpaper, save_logg int + var startup, wallpaper, save_logg, analytics int for querySettings.Next() { - err := querySettings.Scan(&startup, &wallpaper, &save_logg) + err := querySettings.Scan(&startup, &wallpaper, &save_logg, &analytics) if err != nil { functions.Logger(err.Error()) } - var data = map[string]interface{}{"startup": startup, "wallpaper": wallpaper, "save_logg": save_logg} + var data = map[string]interface{}{"startup": startup, + "wallpaper": wallpaper, + "save_logg": save_logg, + "analytics": analytics} utils.Respond(c, data) } @@ -50,8 +53,9 @@ var SettingsUpdate = func(c *fiber.Ctx) error { startup := c.FormValue("startup") wallpaper := c.FormValue("wallpaper") save_logg := c.FormValue("save_logg") + analytics := c.FormValue("analytics") - if startup == "" && wallpaper == "" && save_logg == "" { + if startup == "" && wallpaper == "" && save_logg == "" && analytics == "" { utils.Respond(c, utils.Message(false, "All fields are required.")) return nil } @@ -81,6 +85,13 @@ var SettingsUpdate = func(c *fiber.Ctx) error { } } + if analytics != "" { + _, err := db.Exec("UPDATE settings SET analytics = ?", analytics) + if err != nil { + functions.Logger(err.Error()) + } + } + utils.Respond(c, utils.Message(true, "The settings have been applied successfully.")) return nil } diff --git a/functions/database.go b/functions/database.go index dfced22..52dd11c 100644 --- a/functions/database.go +++ b/functions/database.go @@ -2,9 +2,6 @@ package functions import ( "database/sql" - "encoding/json" - "github.com/rodkranz/fetch" - _ "github.com/mattn/go-sqlite3" ) @@ -26,7 +23,8 @@ func Database() { CREATE TABLE IF NOT EXISTS settings ( startup INTEGER DEFAULT 0, wallpaper INTEGER DEFAULT 0, - save_logg INTEGER DEFAULT 0 + save_logg INTEGER DEFAULT 1, + analytics INTEGER DEFAULT 1 );` _, err = db.Exec(sqlTable) @@ -34,39 +32,14 @@ func Database() { Logger(err.Error()) } - stmt, err := db.Prepare("INSERT INTO settings(startup, wallpaper, save_logg) values(?,?,?)") + stmt, err := db.Prepare("INSERT INTO settings(startup, wallpaper, save_logg, analytics) values(?,?,?,?)") if err != nil { Logger(err.Error()) } - _, err = stmt.Exec(0, 0, 0) + _, err = stmt.Exec(0, 0, 1, 1) if err != nil { Logger(err.Error()) } } } - -func getDatabase() int { - client := fetch.NewDefault() - res, err := client.Get("http://localhost:3000/api/get/settings", nil) - if err != nil { - panic(err) - } - - body, err := res.ToString() - if err != nil { - panic(err) - } - - type DatabaseStruct struct { - Save_logg int `json:"save_logg"` - } - - var Database DatabaseStruct - err = json.Unmarshal([]byte(body), &Database) - if err != nil { - panic(err) - } - - return Database.Save_logg -} diff --git a/functions/logger.go b/functions/logger.go index e746826..65bf736 100644 --- a/functions/logger.go +++ b/functions/logger.go @@ -1,11 +1,14 @@ package functions import ( + "encoding/json" "fmt" "os" "runtime" "strings" "time" + + "github.com/rodkranz/fetch" ) func Logger(text string) { @@ -93,3 +96,28 @@ func CreateFolder(name string) error { } return nil } + +func getDatabase() int { + client := fetch.NewDefault() + res, err := client.Get("http://localhost:3000/api/get/settings", nil) + if err != nil { + panic(err) + } + + body, err := res.ToString() + if err != nil { + panic(err) + } + + type DatabaseStruct struct { + Save_logg int `json:"save_logg"` + } + + var Database DatabaseStruct + err = json.Unmarshal([]byte(body), &Database) + if err != nil { + panic(err) + } + + return Database.Save_logg +} diff --git a/main.go b/main.go index 5a2d450..6a9bd9d 100644 --- a/main.go +++ b/main.go @@ -68,10 +68,6 @@ func main() { return controllers.SettingsGet(c) }) - get.Get("/settings", func(c *fiber.Ctx) error { - return controllers.SettingsGet(c) - }) - err := app.Listen(":3000") if err != nil { return diff --git a/web/src/about.html b/web/src/about.html index 679beaa..ca2214b 100644 --- a/web/src/about.html +++ b/web/src/about.html @@ -53,5 +53,5 @@ - + \ No newline at end of file diff --git a/web/src/gallery.html b/web/src/gallery.html index cd425ef..f6bad11 100644 --- a/web/src/gallery.html +++ b/web/src/gallery.html @@ -71,5 +71,6 @@ + \ No newline at end of file diff --git a/web/src/settings.html b/web/src/settings.html index 380b847..74f4521 100644 --- a/web/src/settings.html +++ b/web/src/settings.html @@ -52,11 +52,20 @@

Create log files

-

Every Nasa will create files in which errors will be written that will help developers when correcting errors.

+

EveryNasa will create files in which errors will be written that will help developers when correcting errors.

- -
+ + + +
+

Enable Analytics

+

EveryNasa will collect Anonymous Usage data.

+
+ + +
+

Create an application shortcut

EveryNasa will create an application shortcut on your desktop.

diff --git a/web/static/scripts/analytics.js b/web/static/scripts/analytics.js new file mode 100644 index 0000000..82f5db7 --- /dev/null +++ b/web/static/scripts/analytics.js @@ -0,0 +1,27 @@ +import { initializeApp } from "https://www.gstatic.com/firebasejs/9.14.0/firebase-app.js"; +import { getAnalytics } from "https://www.gstatic.com/firebasejs/9.14.0/firebase-analytics.js"; + +$.ajax({ + url: 'http://localhost:3000/api/get/settings', + type: 'GET', + dataType: 'json', + success: function (data) { + if (data["analytics"] === 1) { + const firebaseConfig = { + apiKey: "AIzaSyCeHtV4wmB9xJY4vfcpt7wX-WvlV-5S6v4", + authDomain: "everynasa-181a1.firebaseapp.com", + databaseURL: "https://everynasa-181a1-default-rtdb.firebaseio.com", + projectId: "everynasa-181a1", + storageBucket: "everynasa-181a1.appspot.com", + messagingSenderId: "369869513900", + appId: "1:369869513900:web:2ff68e57f95a36bf87ab09", + measurementId: "G-JN83RYFK56" + }; + + const app = initializeApp(firebaseConfig); + getAnalytics(app); + } else { + $("#analytics").remove(); + } + } +}) \ No newline at end of file diff --git a/web/static/scripts/switch.js b/web/static/scripts/switch.js index ae4376e..b28277f 100644 --- a/web/static/scripts/switch.js +++ b/web/static/scripts/switch.js @@ -8,17 +8,26 @@ $(document).ready(async function() { const $loggingSwitch = $("#settings_saveLoggSwitch"); const $loggingSwitchTogglerName = $("#settings_saveLoggTogglerName"); + const $analyticsSwitch = $("#settings_analyticsSwitch"); + const $analyticsSwitchTogglerName = $("#settings_analyticsTogglerName"); + getSettings().then((data) => { - if (data["wallpaper"] === 1) { - $wallpaperSwitch.attr("checked", "true"); - $wallpaperSwitchTogglerName.text("On"); - } else if (data["startup"] === 1) { - $startupSwitch.attr("checked", "true"); - $startupSwitchTogglerName.text("On"); - } else if (data["save_logg"] === 1) { - $loggingSwitch.attr("checked", "true"); - $loggingSwitchTogglerName.text("On"); - } + if (data["wallpaper"] === 1) { + $wallpaperSwitch.attr("checked", "true"); + $wallpaperSwitchTogglerName.text("On"); + } + if (data["startup"] === 1) { + $startupSwitch.attr("checked", "true"); + $startupSwitchTogglerName.text("On"); + } + if (data["save_logg"] === 1) { + $loggingSwitch.attr("checked", "true"); + $loggingSwitchTogglerName.text("On"); + } + if (data["analytics"] === 1) { + $analyticsSwitch.attr("checked", "true"); + $analyticsSwitchTogglerName.text("On"); + } }); $wallpaperSwitch.click(async function() { @@ -67,7 +76,6 @@ $(document).ready(async function() { }) }); - $startupSwitch.click(async function() { $.ajax({ url: "http://localhost:3000/api/get/settings", @@ -106,7 +114,6 @@ $(document).ready(async function() { $startupSwitchTogglerName.text("On"); $startupSwitch.attr("checked", "true"); - toast(data.message); } else { toast("Failed to apply settings."); @@ -161,7 +168,55 @@ $(document).ready(async function() { }); } }, - }) + }); + }); + + $analyticsSwitch.click(async function() { + $.ajax({ + url: "http://localhost:3000/api/get/settings", + type: "GET", + success: function (data) { + if (data["analytics"] === 1) { + $.ajax({ + url: "http://localhost:3000/api/update/settings", + type: "POST", + data: { + "analytics": 0, + }, + success: function (data) { + if(data["status"]) { + $analyticsSwitchTogglerName.text("Off"); + $analyticsSwitch.removeAttr("checked"); + + toast(data.message); + } else { + toast("Failed to apply settings."); + } + }, + }); + } else { + $.ajax({ + url: "http://localhost:3000/api/update/settings", + type: "POST", + data: { + "analytics": 1, + }, + success: function (data) { + if(data["status"]) { + $analyticsSwitchTogglerName.text("On"); + $analyticsSwitch.attr("checked", "true"); + + $("body").append("") + + toast(data.message); + } else { + toast("Failed to apply settings."); + } + }, + }); + } + }, + }); }); });