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 get(self): number_of_users = await self.application.objects.count( UserProfile.select()) number_of_statuses = await self.application.objects.count( Status.select()) response = { "version": "2.0", "software": { "name": "Anfora", "version": "Anfora {}".format(VERSION), }, "protocols": ["activitypub"], "services": { "inbound": [], "outbound": [] }, "openRegistrations": False, "usage": { "users": { "total": number_of_users }, "localPosts": number_of_statuses }, "metadata": { "sourceCode": "https://github.com/anforaProject/anfora", "nodeName": NODENAME, }, } self.write(response)
def on_get(self, req, resp): resp.append_header("Content-Type","application/json; profile=http://nodeinfo.diaspora.software/ns/schema/2.0#") response = { "version": "2.0", "software": { "name": "Anfora", "version": "Anfora {}".format(VERSION), }, "protocols": ["activitypub"], "services": {"inbound": [], "outbound": []}, "openRegistrations": False, "usage": { "users": { "total": UserProfile.select().count() }, "localPosts": Status.select().count() }, "metadata": { "sourceCode": "https://github.com/anforaProject/anfora", "nodeName": NODENAME, }, } resp.body = json.dumps(response) resp.status = falcon.HTTP_200
async def get(self): query = await self.application.objects.execute( UserProfile.select().limit(5)) data = [n.to_json() for n in query] self.write(json.dumps(data, default=str)) self.set_status(200)
async def get(self, username: str): try: user = await self.application.objects.get( UserProfile.select().join(User).where( User.username == username.lower())) self.redirect(f'/accounts/{user.id}') except: self.redirect('/404')
async def get(self): users = await self.application.objects.execute( UserProfile.select().limit(5)) posts = await self.application.objects.execute( Status.select().where(Status.in_reply_to == None).limit(15)) data = { 'users': [user.to_json() for user in users], 'statuses': [status.to_json() for status in posts] } self.write(json.dumps(data, default=str)) self.set_status(200)
async def test_async_new_user_free(): try: user = await objects.get( UserProfile.select().where(User.email == '*****@*****.**')) await objects.delete(user, recursive=True) except: pass res = await new_user_async(username='******', password='******', email='*****@*****.**', description="test user", send_confirmation=False) assert res != False await objects.delete(res, recursive=True)
def build_all_timelines(): r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost')) database.connect() users = UserProfile.select() total = users.count() for index, user in enumerate(users): print("Distributing {}'s timeline. {} of {}".format( user.username, index, total)) #This can be done using a zip function and 2 generators but I have more #access to the db. This way I'm using more memory pairs = ((photo.id, photo.id) for photo in user.statuses) pairs = [item for pair in pairs for item in pair] for follower in user.followers(): if pairs: tagName = "feed:home:{}".format(follower.id) r.zadd(tagName, *pairs) if pairs: tagName = "feed:home:{}".format(user.id) r.zadd(tagName, *pairs) if not database.is_closed(): database.close()
def spread_status(status: Status, mentions: List[str], ap_spread: bool = False) -> None: """ Given a status we have to: - Populate users timelines - Notifiy mentions - Send via AP TODO if we are spreading to local follers (for example from a external request) the ap_spread should be false but if the local user is the one creating the spread tag we should use ap_spread = true """ r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost')) time = status.created_at.timestamp() #Populate all the followers timelines json_file = json.dumps(status.to_json(), default=str) for follower in status.user.followers(): TimelineManager(follower).push_home(status) # Add it to notifications r.publish(f'timeline:{follower.id}', f'update {json_file}') #Add id to the own timeline TimelineManager(status.user).push_home(status) r.publish(f'timeline:{status.user.id}', f'update {json_file}') # Notify mentioned people remote = [] # Notify mentions for user in mentions: # We have two formats: local user and remote user # if the user is local the user string is of the from # 'username' otherwise is '*****@*****.**' #target_user = User.get_or_none(User.username==user) target_user = ActivityPubId(user).get_or_create_remote_user() if target_user: # First we check that there is an user with this username # and then we get it's profile if not target_user.is_remote: NotificationManager( status.user).create_mention_notification(target_user) else: remote.append(target_user) # Spread via AP if ap_spread: create = generate_create_note(status, remote) # Can this be the coolest var in the project? # it's just a set of addresses. Here I'm taking care # to minimize the number of requests to external actors # targeting shared inboxes (like a pro, look at me mom) targets_spotted = set() remote_followers = (UserProfile.select().join( FollowerRelation, on=FollowerRelation.user).where((UserProfile.is_remote == True) & ( FollowerRelation.follows == status.user))) for follower in remote_followers: if follower.public_inbox: targets_spotted.add(follower.public_inbox) else: targets_spotted.add(follower.uris.inbox) # We're ready mike, drop cargo to the target print(targets_spotted) for inbox in targets_spotted: push_to_remote_actor(inbox, create.to_json())
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())
def on_get(self, req, resp): nUsers = UserProfile.select().count() resp.status = falcon.HTTP_200 resp.body = json.dumps({"users": nUsers})