Repository: internetarchive/openlibrary

Issue:
Don't show the Yearly Reading Goal banner every login
Every time I log in, I see the aforementioned banner. I think after using "x" to remove it once or twice, it shouldn't keep reappearing, yet it does.

### Describe the problem that you'd like solved
Limit the number of times the Yearly Reading Goal banner appears upon logging in.

### Proposal & Constraints
It doesn't have to be just once, but I don't think it should be every time until the end of February (or whatever time it's set to end).

### Additional context
<!-- Add any other context or screenshots about the feature request here. -->

### Stakeholders
<!-- @ tag stakeholders of this bug -->





Discussion:
This seems like a bug! Thank you! We may be removing a cookie / localstorage value etc we shouldn't be on logout.
Hi there! I'd like to try working on this issue!
Correct me if I am wrong, https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/site/banner.html, https://github.com/internetarchive/openlibrary/blob/master/openlibrary/templates/account/yrg_banner.html
These seem like the concerning files here right?
The values should be getting stored in localstorage... if one clears their browser settings frequently or aren't using js this could happen... or we could somehow have a bug. @QuantuM410 would you like to investigate and see if you can replicate this bug?

To replicate, you should login in with you account and no yearly reading goal set, dismiss the banner, logout, log back in, and you shouldn't see a banner.

Thank you!
> The values should be getting stored in localstorage... if one clears their browser settings frequently or aren't using js this could happen... or we could somehow have a bug. @QuantuM410 would you like to investigate and see if you can replicate this bug?
> 
> To replicate, you should login in with you account and no yearly reading goal set, dismiss the banner, logout, log back in, and you shouldn't see a banner.
> 
> Thank you!

Hey @mekarpeles , I tried replicating the bug,
The banner seems to work as intended i.e. After dismissing the banner it seems this information is being stored in cookies as I tried logging out and loggin back in and it doesnt seem to display the banner anymore. I cleared the related cookies and redid the whole thing again and it displayed the banner but again after dismissing it, it doesnt pop up again any more in separate login sessions.

I think the problem might relate to user not having js enabled? I m not too sure about it. This 
https://github.com/internetarchive/openlibrary/blob/e07e4709ff37e5c68cd5475907015c786841b1c2/openlibrary/templates/account/yrg_banner.html#L9 seems to ref js

Browser : Firefox, Chrome
Build : Prod
I think that if JS is disabled, the banner would not be able to be dismissed to begin with.

@stopregionblocking, are you frequently deleting your cookies?  I'm thinking that the problem is that we are not persisting these banner preferences on the `User` (meaning that the preference only exists in the browser's storage, and disappears when the cookie is removed).
I am not. If a user doesn't consistently log in from the same computer, would that also affect this?
It would.  This preference is only being saved client-side.  We can, however, save the yearly reading goal preference on the `User`, and set a cookie on login if a preference was ever set.  We already do something similar for the `sfw` cookie.

Here are more details about how this can be done:
1. Update the `hide_banner` `POST` [handler](https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/openlibrary/api.py#L654) to do the following:
  - Determine if a Yearly Reading Goal banner is being hidden.  The `cookie-name` passed for such banners will start with `yrg`
  - If a Yearly Reading Goal banner is being dismissed, get a reference the the current `User` and add a `yrg_banner_pref` preference with the `cookie-name` as the value. The `update_preferences` [method](https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/core/models.py#L814) can be used to do this.

2. Update the login `POST` [handler](https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/upstream/account.py#L310) to set a yearly reading goal cookie if such a preference was set.
  - This can be after the `sfw` cookie is set, [here](https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/upstream/account.py#L342-L343).

I think that should fix things. @QuantuM410, do you still want to work on this?
@jimchamp Sure, I'm happy to help out with this issue!
Thanks! Feel free to reach out if you need more details or anything.
@QuantuM410 are you still working on this?  Do you have any questions?
> @QuantuM410 are you still working on this?  Do you have any questions?

Hey! Yep I am working on this, I was a little occupied with my end semester examinations and also seemed to face an issue while updating the POST method to save ```yrg24``` (the hide banner cookie ref) preference and then on login just set cookies if the user has a preference for the above. Whenever I try to run the ```docker compose up``` again I run into an error (something related to reindexing solr 123 and then it exits home-1).
Best of luck with your studies!

I think that the reindexing Solr error has been resolved.  You may have to update your local `master` with the latest code and rebase your feature branch.  Either way, this bug shouldn't prevent your local Open Library instance from functioning (it may affect search, but that's unrelated to this issue).
> Best of luck with your studies!
> 
> I think that the reindexing Solr error has been resolved.  You may have to update your local `master` with the latest code and rebase your feature branch.  Either way, this bug shouldn't prevent your local Open Library instance from functioning (it may affect search, but that's unrelated to this issue).

Ahh makes sense! On it, will update soon! Thanks ☺️
Hey @jimchamp! Here's an update:
I am able to save the yrg_banner_pref in user preferences and was able to implement setting the hide_banner cookies on login if the preference was selected but before making a PR I have some queries regarding the following

I can see in my console that the cookie-name is yrg24, will this be the name for every user or can it be different for different systems such as yrg and some other numbers?

Is the yrg cookie supposed to be stored only if the safe_mode condition is true and be nested inside the sfw cookie conditon or can it be outside of that condition. I am currently keeping it outside instead of nesting it inside the sfw cookie condition. (I didn't really understand what sfw cookie does so just got curious)
https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/upstream/account.py#L342

Moreover I wasn't able to see the changes when I set the yrg cookies in the POST method in this class
https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/upstream/account.py#L306
here
https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/upstream/account.py#L342-L343
but I was able to set the cookies on login if I set the cookies in this class POST method instead https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/upstream/account.py#L351
after this
https://github.com/internetarchive/openlibrary/blob/2f590171b1d95cc124c44fb6ec647c85f1ca9581/openlibrary/plugins/upstream/account.py#L408-L409



Resolving patch:
diff --git a/openlibrary/plugins/openlibrary/api.py b/openlibrary/plugins/openlibrary/api.py
--- a/openlibrary/plugins/openlibrary/api.py
+++ b/openlibrary/plugins/openlibrary/api.py
@@ -652,11 +652,16 @@ class hide_banner(delegate.page):
     path = '/hide_banner'
 
     def POST(self):
+        user = accounts.get_current_user()
         data = json.loads(web.data())
 
         # Set truthy cookie that expires in 30 days:
         DAY_SECONDS = 60 * 60 * 24
         cookie_duration_days = int(data.get('cookie-duration-days', 30))
+
+        if user and data['cookie-name'].startswith('yrg'):
+            user.save_preferences({'yrg_banner_pref': data['cookie-name']})
+
         web.setcookie(
             data['cookie-name'], '1', expires=(cookie_duration_days * DAY_SECONDS)
         )
diff --git a/openlibrary/plugins/upstream/account.py b/openlibrary/plugins/upstream/account.py
--- a/openlibrary/plugins/upstream/account.py
+++ b/openlibrary/plugins/upstream/account.py
@@ -348,6 +348,15 @@ def POST(self):
                 ol_account = OpenLibraryAccount.get(email=audit['ia_email'])
                 if ol_account and ol_account.get_user().get_safe_mode() == 'yes':
                     web.setcookie('sfw', 'yes', expires=expires)
+                if (
+                    ol_account
+                    and 'yrg_banner_pref' in ol_account.get_user().preferences()
+                ):
+                    web.setcookie(
+                        ol_account.get_user().preferences()['yrg_banner_pref'],
+                        '1',
+                        expires=(3600 * 24 * 365),
+                    )
         # Fallback to infogami user/pass
         else:
             from infogami.plugins.api.code import login as infogami_login
@@ -414,6 +423,12 @@ def POST(self):
         ol_account = OpenLibraryAccount.get(email=email)
         if ol_account and ol_account.get_user().get_safe_mode() == 'yes':
             web.setcookie('sfw', 'yes', expires=expires)
+        if ol_account and 'yrg_banner_pref' in ol_account.get_user().preferences():
+            web.setcookie(
+                ol_account.get_user().preferences()['yrg_banner_pref'],
+                '1',
+                expires=(3600 * 24 * 365),
+            )
         blacklist = [
             "/account/login",
             "/account/create",


Test patch: