Skip to content

Commit

Permalink
Merge pull request #17 from RydalWater/post-mvp-fixes-and-enhancements
Browse files Browse the repository at this point in the history
Post-v0.1.0 Fixes, Updates and Enhancements (Add connections corrections)
  • Loading branch information
RydalWater authored Oct 29, 2024
2 parents 2b0c43c + ece5aa2 commit ac66b47
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<div style="height: 55vh; overflow-y: auto;">
<div class="input-group input-group-sm">
<input class="form-control" type="text", name="follow_user", placeholder="Enter a Users Public Key or NIP05 Address" aria-describedby="upload"></input>
<button class="btn btn-outline-secondary" type="submit" id="submit"><i class="fa-solid fa-plus"></i></button>
<button class="btn btn-outline-secondary" type="submit" id="submit" name="follow" value="add"><i class="fa-solid fa-plus"></i></button>
</div>
<div class="accordion mt-2" id="socialAccordion">
<div class="accordion-item">
Expand Down
32 changes: 17 additions & 15 deletions openlibrarian_root/almanac/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ async def user_relays(request):
await edit_relay_list(session['relays'], session["mod_relays"], session['nsec'])
await async_set_session_info(request, relays=session["mod_relays"])


session = await async_get_session_info(request)
return render(request, 'almanac/user_relays.html', session)

Expand All @@ -172,23 +171,26 @@ async def user_friends(request):
else:
friends = connctions['friends']
muted = connctions['muted']

notification = None

# POST requests
if request.method == 'POST':
if request.POST.get('refresh') or request.POST.get('follow_user'):

# Attempt to add new follow
if request.POST.get('follow_user'):
notification = await add_follow(session['relays'], npub=session['npub'], nsec=session['nsec'], follow_id=request.POST.get('follow_user'))
elif request.method == 'POST':
print (request.POST)
# Attempt to add new follow
if request.POST.get('follow_user'):
notification = await add_follow(session['relays'], npub=session['npub'], nsec=session['nsec'], follow_id=request.POST.get('follow_user'))
elif request.POST.get('follow'):
notification = "Please provide npub or nip05."
else:
notification = "Refreshed."

# Fetch lists again
friends = await fetch_social_list(relays=session['relays'], npub=session['npub'], list_type="follow")
muted = await fetch_social_list(relays=session['relays'], npub=session['npub'], list_type="mute")
# Cache the friends list for 30 minutes
await cache_delete(await cache_key(key_str,session))
await cache_set(await cache_key(key_str,session), {'friends': friends, 'muted': muted}, 1800)
# Fetch lists again
friends = await fetch_social_list(relays=session['relays'], npub=session['npub'], list_type="follow")
muted = await fetch_social_list(relays=session['relays'], npub=session['npub'], list_type="mute")

# Cache the friends list for 30 minutes
await cache_delete(await cache_key(key_str,session))
await cache_set(await cache_key(key_str,session), {'friends': friends, 'muted': muted}, 1800)

return render(request, 'almanac/user_friends.html', {'session': session, 'friends': friends, 'muted': muted, 'notification': notification})
2 changes: 1 addition & 1 deletion openlibrarian_root/static/js/add-toast.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ if (notification && notification !== "None") {
const toastContainer = document.getElementById('toastContainer');
const toast = document.createElement('div');
toast.classList.add('toast', 'align-items-center', 'text-white', 'bg-success', 'border-0');
if (notification === "Book already in library." || notification === "Already Following.") {
if (notification === "Book already in library." || notification === "Already Following." || notification === "Invalid npub or nip05." || notification === "Please provide npub or nip05.") {
toast.classList.remove('bg-success');
toast.classList.add('bg-danger');
}
Expand Down
11 changes: 6 additions & 5 deletions openlibrarian_root/utils/Connections.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ async def add_follow(relays: dict, npub: str = None, nsec: str = None, follow_id
profile = await get_nip05_profile(follow_id,None)
follow_key = profile.public_key()
except:
return "Invalid npub or nip05"
return "Invalid npub or nip05."

# Keys
keys = Keys.parse(nsec)
Expand All @@ -131,14 +131,15 @@ async def add_follow(relays: dict, npub: str = None, nsec: str = None, follow_id
follow_filter = Filter().kind(Kind.from_enum(KindEnum.CONTACT_LIST())).author(keys.public_key()).limit(1)

# Get following list and extract p tags
follow_events = await nostr_get(client=client, filters=[follow_filter], relays_dict=relays, wait=10, connect=True, disconnect=True)
follow_events = await nostr_get(client=client, filters=[follow_filter], relays_dict=relays, wait=10, connect=True, disconnect=False)

for follow in follow_events:
if Tag.public_key(follow_key) not in follow.tags():
follow.tags().append(Tag.public_key(follow_key))
follow_tags = follow.tags()
if Tag.public_key(follow_key) not in follow_tags:
follow_tags.append(Tag.public_key(follow_key))

# Build follow event
follow_builder = EventBuilder(kind=Kind.from_enum(KindEnum.CONTACT_LIST()), tags=follow.tags(), content="")
follow_builder = EventBuilder(kind=Kind.from_enum(KindEnum.CONTACT_LIST()), tags=follow_tags, content="")

# Post events
await nostr_post(client=client, relays_dict=relays, eventbuilder=follow_builder, connect=True, disconnect=True)
Expand Down

0 comments on commit ac66b47

Please sign in to comment.