def delete_follow(self, social_feature_factory_contract, update_task, session, tx_receipt, block_number, block_datetime, follow_state_changes): new_follow_events = social_feature_factory_contract.events.UserFollowDeleted( ).processReceipt(tx_receipt) for entry in new_follow_events: event_args = entry["args"] follower_user_id = event_args._followerUserId followee_user_id = event_args._followeeUserId if (follower_user_id in follow_state_changes) and ( followee_user_id in follow_state_changes[follower_user_id]): follow_state_changes[follower_user_id][ followee_user_id].is_delete = True else: follow = Follow( blockhash=update_task.web3.toHex(entry.blockHash), blocknumber=block_number, follower_user_id=follower_user_id, followee_user_id=followee_user_id, is_current=True, is_delete=True, created_at=block_datetime, ) if follower_user_id in follow_state_changes: follow_state_changes[follower_user_id][ followee_user_id] = follow else: follow_state_changes[follower_user_id] = { followee_user_id: follow }
def follow(id): if request.method =='POST': if current_user.id == id : flash("Sorry, you can't follow yourself", "success") else: follow = Follow.query.filter_by(author = current_user.id, user_id = id).first() if not follow: new_follow = Follow(author = current_user.id, user_id = id) db.session.add(new_follow) db.session.commit() flash("Successfully followed user", "success") if follow: db.session.delete(follow) db.session.commit() flash("Successfully unfollowed user", "success") return redirect(url_for("user.userposts", id = id))
def setup_search(app_module): with app_module.app_context(): db = get_db() # Import app so that it'll run migrations against the db now = datetime.now() blocks = [ Block( blockhash=hex(1), number=1, parenthash="0x01", is_current=False, ), Block( blockhash=hex(2), number=2, parenthash="0x02", is_current=False, ), Block( blockhash=hex(3), number=3, parenthash="0x03", is_current=True, ), ] tracks = [ Track( blockhash=hex(1), blocknumber=1, track_id=1, is_current=True, is_delete=False, owner_id=1, route_id="", track_segments=[], genre="", updated_at=now, created_at=now, is_unlisted=False, title="the track 1", download={"cid": None, "is_downloadable": False, "requires_follow": False}, ), Track( blockhash=hex(2), blocknumber=2, track_id=2, is_current=True, is_delete=False, owner_id=2, route_id="", track_segments=[], genre="", updated_at=now, created_at=now, is_unlisted=False, title="the track 2", download={"cid": None, "is_downloadable": True, "requires_follow": False}, ), Track( blockhash=hex(3), blocknumber=3, track_id=3, is_current=True, is_delete=False, owner_id=1, route_id="", track_segments=[], genre="", updated_at=now, created_at=now, is_unlisted=False, title="xyz", download={"cid": None, "is_downloadable": True, "requires_follow": False}, ), ] # need users for the lexeme dict to work users = [ User( blockhash=hex(1), blocknumber=1, user_id=1, is_current=True, handle="", wallet="", name="user 1", updated_at=now, created_at=now, ), User( blockhash=hex(2), blocknumber=2, user_id=2, is_current=True, handle="", name="user 2", wallet="", updated_at=now, created_at=now, ), User( blockhash=hex(3), blocknumber=3, user_id=3, is_current=True, handle="", wallet="", name="fdwea", updated_at=now, created_at=now, ), ] follows = [ Follow( blockhash=hex(1), blocknumber=1, follower_user_id=2, followee_user_id=1, is_current=True, is_delete=False, created_at=now, ) ] playlists = [ Playlist( blockhash=hex(1), blocknumber=1, playlist_id=1, playlist_owner_id=1, is_album=False, is_private=False, playlist_name="playlist 1", playlist_contents={"track_ids": [{"track": 1, "time": 1}]}, is_current=True, is_delete=False, updated_at=now, created_at=now, ), Playlist( blockhash=hex(2), blocknumber=2, playlist_id=2, playlist_owner_id=2, is_album=True, is_private=False, playlist_name="album 1", playlist_contents={"track_ids": [{"track": 2, "time": 2}]}, is_current=True, is_delete=False, updated_at=now, created_at=now, ), ] saves = [ Save( blockhash=hex(1), blocknumber=1, user_id=1, save_item_id=1, save_type=SaveType.track, created_at=now, is_current=True, is_delete=False, ), Save( blockhash=hex(1), blocknumber=1, user_id=1, save_item_id=1, save_type=SaveType.playlist, created_at=now, is_current=True, is_delete=False, ), Save( blockhash=hex(1), blocknumber=1, user_id=1, save_item_id=2, save_type=SaveType.album, created_at=now, is_current=True, is_delete=False, ), ] balances = [ UserBalance( user_id=1, balance=0, associated_wallets_balance=0, associated_sol_wallets_balance=0, waudio=0, ) ] with db.scoped_session() as session: for block in blocks: session.add(block) session.flush() for track in tracks: session.add(track) for user in users: session.add(user) session.flush() for follow in follows: session.add(follow) session.flush() for playlist in playlists: session.add(playlist) session.flush() for save in saves: session.add(save) session.flush() for balance in balances: session.add(balance) session.flush() # Refresh the lexeme matview _update_aggregate_track(session) session.execute("REFRESH MATERIALIZED VIEW track_lexeme_dict;") session.execute( UPDATE_AGGREGATE_USER_QUERY, {"prev_indexed_aggregate_block": 0} ) session.execute("REFRESH MATERIALIZED VIEW user_lexeme_dict;") session.execute("REFRESH MATERIALIZED VIEW aggregate_playlist;") session.execute("REFRESH MATERIALIZED VIEW playlist_lexeme_dict;") session.execute("REFRESH MATERIALIZED VIEW album_lexeme_dict;") try: output = subprocess.run( ["npm", "run", "dev"], env=os.environ, capture_output=True, text=True, cwd="es-indexer", timeout=5, ) raise Exception( f"Elasticsearch indexing stopped: {output.stderr}. With env: {os.environ}" ) except subprocess.TimeoutExpired as timeout: if "catchup done" not in timeout.output.decode("utf-8"): raise Exception("Elasticsearch failed to index")
def test_profile_completion_challenge_with_playlists(app): redis_conn = redis.Redis.from_url(url=REDIS_URL) # create user with app.app_context(): db = get_db() block = Block(blockhash="0x1", number=BLOCK_NUMBER) user = User( blockhash="0x1", blocknumber=BLOCK_NUMBER, txhash="xyz", user_id=1, is_current=True, handle="TestHandle", handle_lc="testhandle", wallet="0x123", is_creator=False, is_verified=False, name="test_name", created_at=datetime.now(), updated_at=datetime.now(), ) with db.scoped_session() as session: bus = ChallengeEventBus(redis_conn) # set challenge as active for purposes of test session.query(Challenge).filter( Challenge.id == "profile-completion").update({ "active": True, "starting_block": BLOCK_NUMBER }) # Register events with the bus bus.register_listener(ChallengeEvent.profile_update, profile_challenge_manager) bus.register_listener(ChallengeEvent.repost, profile_challenge_manager) bus.register_listener(ChallengeEvent.follow, profile_challenge_manager) bus.register_listener(ChallengeEvent.favorite, profile_challenge_manager) session.add(block) session.flush() session.add(user) # Process dummy event just to get this thing initted bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] # We should have completed a single step (name) assert state.current_step_count == 1 and not state.is_complete # Do a repost repost = Repost( blockhash="0x1", blocknumber=BLOCK_NUMBER, user_id=1, repost_item_id=1, repost_type=RepostType.playlist, is_current=True, is_delete=False, created_at=datetime.now(), ) session.add(repost) session.flush() bus.dispatch(ChallengeEvent.repost, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 2 and not state.is_complete # Do a save save = Save( blockhash="0x1", blocknumber=BLOCK_NUMBER, user_id=1, save_item_id=1, save_type=SaveType.playlist, is_current=True, is_delete=False, created_at=datetime.now(), ) session.add(save) session.flush() bus.dispatch(ChallengeEvent.favorite, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) session.flush() state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 3 and not state.is_complete # Do 1 follow, then 5 total follows follow = Follow( blockhash="0x1", blocknumber=BLOCK_NUMBER, is_current=True, is_delete=False, created_at=datetime.now(), follower_user_id=1, followee_user_id=2, ) session.add(follow) session.flush() bus.dispatch(ChallengeEvent.follow, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) session.flush() state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] # Assert 1 follow didn't do anything assert state.current_step_count == 3 and not state.is_complete follows = [ Follow( blockhash="0x1", blocknumber=BLOCK_NUMBER, is_current=True, is_delete=False, created_at=datetime.now(), follower_user_id=1, followee_user_id=3, ), Follow( blockhash="0x1", blocknumber=BLOCK_NUMBER, is_current=True, is_delete=False, created_at=datetime.now(), follower_user_id=1, followee_user_id=4, ), Follow( blockhash="0x1", blocknumber=BLOCK_NUMBER, is_current=True, is_delete=False, created_at=datetime.now(), follower_user_id=1, followee_user_id=5, ), Follow( blockhash="0x1", blocknumber=BLOCK_NUMBER, is_current=True, is_delete=False, created_at=datetime.now(), follower_user_id=1, followee_user_id=6, ), ] session.add_all(follows) session.flush() bus.dispatch(ChallengeEvent.follow, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 4 and not state.is_complete # profile_picture session.query(User).filter(User.user_id == 1).update( {"profile_picture": "profilepictureurl"}) session.flush() bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 5 and not state.is_complete # profile description session.query(User).filter(User.user_id == 1).update( {"bio": "profiledescription"}) session.flush() bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 6 and not state.is_complete # Undo it, ensure that our count goes down session.query(User).filter(User.user_id == 1).update({"bio": None}) session.flush() bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 5 and not state.is_complete # profile_cover_photo session.query(User).filter(User.user_id == 1).update({ "bio": "profiledescription", "cover_photo": "test_cover_photo" }) session.flush() bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 7 and state.is_complete == True # ensure that if we lose some data now that the thing is complete, we don't change the status of the challenge session.query(User).filter(User.user_id == 1).update( {"cover_photo": None}) session.flush() bus.dispatch(ChallengeEvent.profile_update, BLOCK_NUMBER, 1) bus.flush() bus.process_events(session) state = profile_challenge_manager.get_user_challenge_state( session, ["1"])[0] assert state.current_step_count == 7 and state.is_complete == True