new heart feature

2025-11-05

I've written an update to this post

Just added a new heart feature that uses the same shelf db for the honey experiment. It is inspired by the upvote feature on Bear's discovery feed and although it does not get factored into a feed aggregate, I really like it as a way to just quickly say "hi". One thing I don't like about the current implementation is that the redirect causes the page to bounce back up. Let me know if you have any ideas on preventing it (without JavaScript of course :p).

This sounds silly but TIL that len({}) on a dict or list is an O(1) operation and that Python simply does a dynamic lookup.

# db.py
HEARTS_DB = "data/hearts.shelf"

class Hearts:
    def heart(self, post_id, ip):
        with shelve.open(HEARTS_DB, "c") as shelf:
            hearts = shelf.get(post_id, {})
            hearts[ip] = datetime.now().isoformat()
            shelf[post_id] = hearts

    def get_count(self, post_id):
        with shelve.open(HEARTS_DB, "c") as shelf:
            hearts = shelf.get(post_id, {})
            return len(hearts)

# server.py
@app.route("/heart")
def heart():
    post_id = request.args.get("post_id")
    ip = request.remote_addr or ""
    in_honey = honey.get_ip(ip)

    if ip and not in_honey:
        hearts.heart(post_id, request.remote_addr)
    return redirect("/")