Fix issues related to AntiDopamine mode
authorr <r@freesoftwareextremist.com>
Thu, 3 Sep 2020 06:26:32 +0000 (06:26 +0000)
committerr <r@freesoftwareextremist.com>
Thu, 3 Sep 2020 06:29:03 +0000 (06:29 +0000)
- Add AntiDopamine mode description
- Update fluoride to support AntiDopamine mode

mastodon/notification.go
model/settings.go
renderer/model.go
service/service.go
static/fluoride.js
templates/header.tmpl
templates/settings.tmpl

index 5983396e9b6acb068c985d846b0570283e324e27..656e6a1d33d300ac9a6143e09a127b6cd8ee44c1 100644 (file)
@@ -23,7 +23,7 @@ type Notification struct {
 }
 
 // GetNotifications return notifications.
-func (c *Client) GetNotifications(ctx context.Context, pg *Pagination, excludes ...string) ([]*Notification, error) {
+func (c *Client) GetNotifications(ctx context.Context, pg *Pagination, excludes []string) ([]*Notification, error) {
        var notifications []*Notification
        params := url.Values{}
        for _, exclude := range excludes {
index b7c77cf3d0456d56012008196c82b8873544ffd5..03e95819c093a0fd2633a72e97b06961e75cfe3a 100644 (file)
@@ -9,7 +9,7 @@ type Settings struct {
        AutoRefreshNotifications bool   `json:"auto_refresh_notifications"`
        FluorideMode             bool   `json:"fluoride_mode"`
        DarkMode                 bool   `json:"dark_mode"`
-       AntiDopamineMode         bool   `json:"anti_dopamine_mode"`
+       AntiDopamineMode         bool   `json:"anti_dopamine_mode"`
 }
 
 func NewSettings() *Settings {
index 7ea8c22d1c751b9bacb1678b4306fede582de055..4b177dbcac945780407d85acbe856a0c3eb28bc0 100644 (file)
@@ -6,13 +6,13 @@ import (
 )
 
 type Context struct {
-       HideAttachments bool
-       MaskNSFW        bool
-       FluorideMode    bool
-       ThreadInNewTab  bool
-       DarkMode        bool
-       CSRFToken       string
-       UserID          string
+       HideAttachments  bool
+       MaskNSFW         bool
+       FluorideMode     bool
+       ThreadInNewTab   bool
+       DarkMode         bool
+       CSRFToken        string
+       UserID           string
        AntiDopamineMode bool
 }
 
index cc59cb6e3254230099b0c04a98783567e1f79038..c56d96ac4245184e96fb64931c58a305acf68305 100644 (file)
@@ -410,12 +410,11 @@ func (svc *service) ServeNotificationPage(c *model.Client, maxID string,
                Limit: 20,
        }
 
-       dope := c.Session.Settings.AntiDopamineMode
-       if dope {
-               excludes = append(excludes, "follow", "favourite", "reblog")
+       if c.Session.Settings.AntiDopamineMode {
+               excludes = []string{"follow", "favourite", "reblog"}
        }
 
-       notifications, err := c.GetNotifications(ctx, &pg, excludes...)
+       notifications, err := c.GetNotifications(ctx, &pg, excludes)
        if err != nil {
                return
        }
index 6c51694e0b462345db7c31fbb33ff7023ba3d4b4..e055b6d637b8935370e884ce10953f0aa853079b 100644 (file)
@@ -7,11 +7,19 @@ var reverseActions = {
        "unretweet": "retweet"
 };
 
-function getCSRFToken() {
+var csrfToken = "";
+var antiDopamineMode = false;
+
+function checkCSRFToken() {
        var tag = document.querySelector("meta[name='csrf_token']");
        if (tag)
-               return tag.getAttribute("content");
-       return "";
+               csrfToken = tag.getAttribute("content");
+}
+
+function checkAntiDopamineMode() {
+       var tag = document.querySelector("meta[name='antidopamine_mode']");
+       if (tag)
+               antiDopamineMode = tag.getAttribute("content") === "true";
 }
 
 function http(method, url, body, type, success, error) {
@@ -50,11 +58,13 @@ function handleLikeForm(id, f) {
                        updateActionForm(id, forms[i], reverseActions[action]);
                }
 
-               var body = "csrf_token=" + encodeURIComponent(getCSRFToken());
+               var body = "csrf_token=" + encodeURIComponent(csrfToken);
                var contentType = "application/x-www-form-urlencoded";
                http("POST", "/fluoride/" + action + "/" + id, 
                        body, contentType, function(res, type) {
 
+                       if (antiDopamineMode)
+                               return;
                        var data = JSON.parse(res);
                        var count = data.data;
                        if (count === 0)
@@ -87,11 +97,13 @@ function handleRetweetForm(id, f) {
                        updateActionForm(id, forms[i], reverseActions[action]);
                }
 
-               var body = "csrf_token=" + encodeURIComponent(getCSRFToken());
+               var body = "csrf_token=" + encodeURIComponent(csrfToken);
                var contentType = "application/x-www-form-urlencoded";
                http("POST", "/fluoride/" + action + "/" + id, 
                        body, contentType, function(res, type) {
 
+                       if (antiDopamineMode)
+                               return;
                        var data = JSON.parse(res);
                        var count = data.data;
                        if (count === 0)
@@ -193,6 +205,9 @@ function handleStatusLink(a) {
 }
 
 document.addEventListener("DOMContentLoaded", function() { 
+       checkCSRFToken();
+       checkAntiDopamineMode();
+
        var statuses = document.querySelectorAll(".status-container");
        for (var i = 0; i < statuses.length; i++) {
                var s = statuses[i];
index 8e67353431bf68010bdec44df269ec788b503d2b..2d76f3304d4a389f6c6515b3861bd40d0cc89154 100644 (file)
@@ -10,6 +10,9 @@
        {{if .CSRFToken}}
        <meta name="csrf_token" content="{{.CSRFToken}}">
        {{end}}
+       {{if $.Ctx.AntiDopamineMode}}
+       <meta name="antidopamine_mode" content="{{$.Ctx.AntiDopamineMode}}">
+       {{end}}
        {{if .AutoRefresh}}
        <meta http-equiv="refresh" content="30">
        {{end}}
index ab5f03c3bac20306dc0fe49f2cd71d6006aa65dd..67386a4b47094c55563ab1108d7b549f04ea72f8 100644 (file)
                <input id="fluoride-mode" name="fluoride_mode" type="checkbox" value="true" {{if .Settings.FluorideMode}}checked{{end}}>
                <label for="fluoride-mode"> Enable <abbr title="Enable JavaScript based functionality, e.g., like/retweet without page reload and reply preview on thread page">fluoride mode</abbr> </label>
        </div>
-       <div class="settings-form-field">
-               <input id="dark-mode" name="dark_mode" type="checkbox" value="true" {{if .Settings.DarkMode}}checked{{end}}>
-               <label for="dark-mode"> Use dark theme </label>
-       </div>
        <div class="settings-form-field">
                <input id="anti-dopamine-mode" name="anti_dopamine_mode" type="checkbox"
                value="true" {{if .Settings.AntiDopamineMode}}checked{{end}}>
-               <label for="anti-dopamine-mode"> Remove addictive social media features </label>
+               <label for="anti-dopamine-mode"> Enable <abbr title="Remove like/retweet/unread notification count and disable like/retweet/follow notifications">anti-dopamine mode</abbr> </label>
+       </div>
+       <div class="settings-form-field">
+               <input id="dark-mode" name="dark_mode" type="checkbox" value="true" {{if .Settings.DarkMode}}checked{{end}}>
+               <label for="dark-mode"> Use dark theme </label>
        </div>
        <button type="submit"> Save </button>
 </form>