def on_get(self, req, resp, id): max_id = req.get_param('max_ids') since_id = req.get_param('since_id') limit = req.get_param('limit') or self.MAX_ELEMENTS if max_id and since_id: follows = UserProfile.select().join( FollowerRelation, on=FollowerRelation.follows).where( FollowerRelation.user.id == id, UserProfile.id > since_id, UserProfile.id < max_id).limit(limit) elif max_id: follows = UserProfile.select().join( FollowerRelation, on=FollowerRelation.follows).where( FollowerRelation.user.id == id, UserProfile.id < max_id).limit(limit) elif since_id: follows = UserProfile.select().join( FollowerRelation, on=FollowerRelation.follows).where( FollowerRelation.user.id == id, UserProfile.id > since_id).limit(limit) else: follows = FollowerRelation.select().join( UserProfile, on=FollowerRelation.user).where( FollowerRelation.follows.id == id).limit(limit).order_by( FollowerRelation.id.desc()) following = [follow.follows.to_json() for follow in follows] resp.body = json.dumps(following, default=str) resp.satatus = falcon.HTTP_200
async def is_following_async(self, app, target): from models.followers import FollowerRelation count = await app.count(FollowerRelation.select().where( (FollowerRelation.user == self.user) & (FollowerRelation.follows == target))) return count > 0
def follow(self, target, valid=False): """ The current user follows the target account. target: An instance of User valid: Boolean to force a valid Follow. This means that the user doesn't have to accept the follow """ from models.followers import FollowerRelation FollowerRelation.create(user = self, follows =target, valid=valid) followers_increment = UserProfile.update({UserProfile.followers_count: UserProfile.followers_count + 1}).where(UserProfile.id == target.id) following_increment = UserProfile.update({UserProfile.following_count: UserProfile.following_count + 1}).where(UserProfile.id == self.id) following_increment.execute() followers_increment.execute()
def handle_follow(activity): followed = User.get_or_none(ap_id=activity.object) print("=> Handling follow") if followed: if isinstance(activity.actor, activities.Actor): ap_id = activity.actor.id elif isinstance(activity.actor, str): ap_id = activity.actor follower = ActivityPubId(ap_id).get_or_create_remote_user() FollowerRelation.create( user = follower, follows = followed ) response = {'Type': 'Accept','Object':activity} else: print("error handling follow")
def on_post(self, req, resp, username): user = User.get_or_none(username == username) if req.context['user'].username != username: resp.status = falcon.HTTP_401 resp.body = json.dumps({"Error": "Access denied"}) payload = req.get_param('data') activity = json.loads(payload, object_hook=as_activitystream) if activity.object.type == "Note": obj = activity.object activity = activities.Create(to=user.uris.followers, actor=user.uris.id, object=obj) activity.validate() if activity.type == "Create": if activity.object.type != "Note": resp.status = falcon.HTTP_500 resp.body = json.dumps({"Error": "You only can create notes"}) activity.object.id = photo.uris.id activity.id = store(activity, user) #deliver(activity) resp.body = json.dumps({"Success": "Delivered successfully"}) resp.status = falcon.HTTP_200 #Convert to jpeg else: resp.status = falcon.HTTP_500 resp.body = json.dumps({"Error": "No photo attached"}) if activity.type == "Follow": followed = ActivityPubId( activity.object).get_or_create_remote_user() user = req.context["user"] #print(followed.ap_id, user.username, followed.username) f = FollowerRelation.create(user=user, follows=followed) activity.actor = user.uris.id activity.to = followed.uris.id #activity.id = store(activity, user) deliver(activity) resp.body = json.dumps({"Success": "Delivered successfully"}) resp.status = falcon.HTTP_200
def is_following(self, user): from models.followers import FollowerRelation return (FollowerRelation.select().where( (FollowerRelation.user == self) & (FollowerRelation.follows == user)).exists())
from manage_db import connect from models.followers import FollowerRelation from models.user import UserProfile, User from models.status import Status connect() a = FollowerRelation.select() print("Follows:") for u in a: print(u) print("====================") print("Users:") for u in UserProfile.select(): print(u.username, " con ap_id ", str(u.ap_id)) print(list(u.timeline())) print("----") print("====================") print(UserProfile.select().count()) print("========") print("Statuss") for p in Status.select(): print(p) print("========") print("Following test:") t = User.get(username="******").profile print(t.followers().count())
connect() create_tables() passw = Argon2().generate_password_hash("test") #yab = User.create(username="******") yab, created = User.get_or_create(username="******", defaults={ 'password':passw, 'preferredUsername': "******", "name": "Yabir Test", 'email':"*****@*****.**", 'confirmation_sent_at':datetime.datetime.now(), 'last_sign_in_at':1 }) lol, created = User.get_or_create(username="******", password=passw, email="*****@*****.**", confirmation_sent_at=datetime.datetime.now(),last_sign_in_at=1) FollowerRelation.create( user = lol, follows = yab, valid = True ) """ photos = (Status.select().where(Status.upload_date >= datetime.date.today())) for photo in photos: print(photo) print(photo.json()) """