Esempio n. 1
0
def api_tweet(body):
    # Don't allow blank tweets using zero width spaces
    body = body.replace(ZERO_WIDTH_SPACE, str()).strip()
    if not len(body):
        return {"error": "Cannot submit a blank tweet"}, 400

    current = db.get(web.whoami(), {})
    newtweet = dict(body=body, ts=int(time.time()), likes=[])
    new = {**current, "tweets": current.get("tweets", []) + [newtweet]}
    db[web.whoami()] = new

    print(new)
    return {"success": True}
Esempio n. 2
0
def like(author, ts, action):

    current_user = web.whoami()

    # validate arguments
    if not ts.isdigit():
        return {"error": "Bad ts"}, 400
    ts = int(ts)
    if action not in ["like", "unlike"]:
        return {"error": "Invalid action"}, 400

    # find matching tweet.
    author_data = db.get(author, {})
    tweet_ind = find_matching(author_data, ts)
    if tweet_ind is None:
        return {"error": "Tweet not found"}, 404
    tweets = author_data.get("tweets", [])

    # Convert to a unique set so we can add and remove and prevent double liking
    likes = set(tweets[tweet_ind].get("likes", []))
    if action == "like":
        likes.add(current_user)
    else:
        likes.discard(current_user)
    tweets[tweet_ind]["likes"] = list(likes)

    db[author] = {**author_data, "tweets": tweets}
    return {"success": True}
Esempio n. 3
0
def delete(author, ts):
    if not ts.isdigit():
        return {"error": "Bad ts"}, 400
    ts = int(ts)
    author_data = db.get(author, {})

    match_ind = find_matching(author_data, ts)
    if match_ind is None:
        return {"error": "Tweet not found"}, 404

    print(f"{web.whoami()!r} trying to delete tweet by {author!r}")
    # Moderators bypass this check, they can delete anything
    if not is_mod() and author != web.whoami():
        return {"error": "Permission denied"}, 401

    db[author] = {
        **author_data, "tweets": [
            t for i, t in enumerate(author_data.get("tweets", []))
            if i != match_ind
        ]
    }
    return {"success": True}
Esempio n. 4
0
def home():
    return web.render_template("home.html", name=web.whoami(), mod=is_mod())
Esempio n. 5
0
def is_mod():
    return web.whoami() in REPLTWEET_MODS