Skip to content

Commit

Permalink
Add post login follow action
Browse files Browse the repository at this point in the history
  • Loading branch information
shilp.thapak authored and ShilpThapak committed Jan 19, 2025
1 parent 3e18ea8 commit 07cba1d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
2 changes: 1 addition & 1 deletion openlibrary/macros/Follow.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
<button type="submit" class="cta-btn cta-btn--$(css_treatment)">$_("Unfollow" if following else "Follow")</button>
</form>
$else:
<a class="cta-btn cta-btn--primary" href="/account/login?redir_url=$(ctx.path)">$_("Follow")</a>
<a class="cta-btn cta-btn--primary" href="/account/login?redir_url=$(ctx.path)&action=follow">$_("Follow")</a>
</div>
53 changes: 51 additions & 2 deletions openlibrary/plugins/upstream/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from datetime import datetime
from math import ceil
from typing import TYPE_CHECKING, Any, Final
from urllib.parse import urlparse
from urllib.parse import urlparse, parse_qs

import requests
import web
Expand Down Expand Up @@ -414,6 +414,44 @@ def render_error(self, error_key, i):
f.note = get_login_error(error_key)
return render.login(f)

def get_post_login_action(self, web, referer):
"""Extract the 'action' parameter from the query string.
Return the action if it is valid, else return None
"""
allowed_actions = ["follow"]
if not referer:
return None
qs = web.ctx.env.get('QUERY_STRING', "")
parsed_query = parse_qs(qs)
action = parsed_query.get("action", [None])[0]
if action in allowed_actions:
return action
return None

def perform_post_login_action(self, i, ol_account):
if not i.action or not i.redirect or i.action != "follow":
return None

redirect_path = urlparse(i.redirect).path
if not redirect_path or "/" not in redirect_path:
return None

publisher = redirect_path.split("/")[-1]
if not publisher:
return None

# Check if publisher exists
if not OpenLibraryAccount.get_by_username(publisher):
return None

# Check is user has already following the publisher
if PubSub.is_subscribed(subscriber=ol_account.username, publisher=publisher):
flash_message = "You are already following this user!"
else:
PubSub.subscribe(subscriber=ol_account.username, publisher=publisher)
flash_message = "You are now following this user!"
return flash_message

def GET(self):
referer = web.ctx.env.get('HTTP_REFERER', '')
# Don't set referer if request is from offsite
Expand All @@ -424,9 +462,14 @@ def GET(self):
this_host = this_host.split(':', 1)[0]
if parsed_referer.hostname != this_host:
referer = None
i = web.input(redirect=referer)

# Get the post login action. Example: follow, want to read or borrow
action = self.get_post_login_action(web, referer)

i = web.input(redirect=referer, action=action)
f = forms.Login()
f['redirect'].value = i.redirect
f['action'].value = i.action
return render.login(f)

def POST(self):
Expand Down Expand Up @@ -470,6 +513,12 @@ def POST(self):
"/account/login",
"/account/create",
]

# Processing post login action
flash_message = self.perform_post_login_action(i, ol_account)
if flash_message:
add_flash_message('note', _(flash_message))

if i.redirect == "" or any(path in i.redirect for path in blacklist):
i.redirect = "/account/books"
stats.increment('ol.account.xauth.login')
Expand Down
1 change: 1 addition & 0 deletions openlibrary/plugins/upstream/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def find_ia_account(email=None):
Textbox('username', description=_('Username'), klass='required'),
Password('password', description=_('Password'), klass='required'),
Hidden('redirect'),
Hidden('action')
)
forms.login = Login

Expand Down
1 change: 1 addition & 0 deletions openlibrary/templates/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ <h1 class="ol-signup-hero__title">$_("Log In")</h1>
</div>

<input type="hidden" id="redirect" value="$form.redirect.value" name="redirect" />
<input type="hidden" id="action" value="$form.action.value" name="action" />
<input type="hidden" id="debug_token" value="" name="debug_token"/>

<div class="formElement bottom">
Expand Down

0 comments on commit 07cba1d

Please sign in to comment.