示例#1
0
    def on_success(self, data):
        event_type = "untracked"

        if data.get('direct_message'):
            event_type = "Direct Message"

            dm = DirectMessage(data)
            self.on_direct_message(dm)

        elif data.get('event') and data['event'] == 'follow':
            event_type = "Follow"

            follow = Follow(data)
            self.on_follow(follow)

        elif data.get('text'):
            event_type = "Status"
            status = Status(data)

            # ignore RTs for now
            if status.type is 'retweet':
                event_type = "{0} -- Retweet".format(event_type)
            else:
                # if it's a reply, handle as such
                # otherwise, for now, do nothing.
                if status.reply_to_user():
                    event_type = "{0} -- Reply".format(event_type)
                    self.on_reply(status)

        print "{0} data received. Type: {1}".format(self.__class__.__name__,
                                                    event_type)
        print data
示例#2
0
    def like(self, status):
        """
            user: UserProfile - User liking the status

            Returns True if the action was successful
        """

        # Check if the user already liked it

        if Like.get_or_none(user=self.user, status=status):
            return False

        # First we create the like object
        Like.create(
            user=self.user,
            status=status,
        )

        # Update the counter in the object
        Status.update({
            Status.favourites_count: Status.favourites_count + 1
        }).where(Status.id == status.id)

        # Create a redis notification
        like_status(status, self.user)

        return True
示例#3
0
文件: scraper.py 项目: raquo/hnapp
	def save_item(self, item_data, update_max_id=False, front_page=False):
		"""
		Compile and save item to the database
		Each item is committed separately for better fault tolerance
		"""
		
		item = None
		# If item was lost
		if isinstance(item_data, LostItem):
			if not item_data.id:
				return None
			item = item_data
			db.session.add(item)
			debug_print("Lost %s because %s" % (item_data.id, item_data.reason), '\n')
		else:
			if 'id' not in item_data:
				debug_print('Skipping item because empty id', '\n')
				return None
			if item_data.get('type', None) in ('story', 'comment', 'poll', 'job', None):
				debug_print("Saving %d" % item_data['id'], '\n')
				compiled_data = self.compile_item_data(item_data, front_page)
				item = Item.create_or_update(compiled_data)
			else:
				debug_print("Skipping %s %d" % (item_data['type'], item_data['id']), '\n')
		
		if update_max_id:
			Status.set_max_item_id(item_data.id if isinstance(item_data, LostItem) else item_data['id'])
		
		db.session.commit()
		
		return item
示例#4
0
    def on_success(self, data):
        event_type = "untracked"

        if data.get('direct_message'):
            event_type = "Direct Message"

            dm = DirectMessage(data)
            self.on_direct_message(dm)

        elif data.get('event') and data['event'] == 'follow':
            event_type = "Follow"

            follow = Follow(data)
            self.on_follow(follow)

        elif data.get('text'):
            event_type = "Status"
            status = Status(data)

            # ignore RTs for now
            if status.type is 'retweet':
                event_type = "{0} -- Retweet".format(event_type)
            else:
                # if it's a reply, handle as such
                # otherwise, for now, do nothing.
                if status.reply_to_user():
                    event_type = "{0} -- Reply".format(event_type)
                    self.on_reply(status)

        print "{0} data received. Type: {1}".format(self.__class__.__name__, event_type)
        print data
示例#5
0
    def save_item(self, item_data, update_max_id=False, front_page=False):
        """
		Compile and save item to the database
		Each item is committed separately for better fault tolerance
		"""

        item = None
        # If item was lost
        if isinstance(item_data, LostItem):
            item = item_data
            db.session.add(item)
            debug_print(
                "Lost %s because %s" % (item_data.id, item_data.reason), '\n')
        else:
            if item_data.get('type', None) in ('story', 'comment', 'poll',
                                               'job', None):
                debug_print("Saving %d" % item_data['id'], '\n')
                compiled_data = self.compile_item_data(item_data, front_page)
                item = Item.create_or_update(compiled_data)
            else:
                debug_print(
                    "Skipping %s %d" % (item_data['type'], item_data['id']),
                    '\n')

        if update_max_id:
            Status.set_max_item_id(item_data.id if isinstance(
                item_data, LostItem) else item_data['id'])

        db.session.commit()

        return item
示例#6
0
    def dislike(self, status):

        # Check if the like exists
        like = Like.get_or_none(user=self.user, status=status)
        if not like:
            return False
        like.delete().execute()
        Status.update({
            Status.favourites_count: Status.favourites_count - 1
        }).where(Status.id == status.id)
示例#7
0
	def post(self):
		status = Status()

		user = users.get_current_user()
		if not user:
			self.redirect('/')

		status.name = self.request.get('name')
		status.put()
		self.redirect('/admin/')
示例#8
0
def create_example_data():
    if not Cell.select():
        user = User(name='Example User', login_name='user', password='******')
        batman = User(name='Batman', login_name='batman', password='******')
        user.save()
        batman.save()

        board1 = Board(name='First')
        board2 = Board(name='Second')
        board3 = Board(name='Third')
        batman1 = Board(name='Batmobil')
        batman2 = Board(name='BatBarlang')
        batman3 = Board(name='BatRobin')

        board1.save()
        board2.save()
        board3.save()
        batman1.save()
        batman2.save()
        batman3.save()

        Boardstable.create(board=board1, user=user)
        Boardstable.create(board=board2, user=user)
        Boardstable.create(board=board3, user=user)

        Boardstable.create(board=batman1, user=batman)
        Boardstable.create(board=batman2, user=batman)
        Boardstable.create(board=batman3, user=batman)

        Cell.create(text="Rocket",
                    name="Weapon",
                    order=1,
                    board=board1,
                    status=Status.get(Status.status == "new"))
        Cell.create(text="Pistol",
                    name="Weapon",
                    order=2,
                    board=board1,
                    status=Status.get(Status.status == "new"))
        Cell.create(text="Sword",
                    name="Weapon",
                    order=3,
                    board=board1,
                    status=Status.get(Status.status == "new"))
        Cell.create(text="Kalasnyikov",
                    name="Weapon",
                    order=1,
                    board=board1,
                    status=Status.get(Status.status == "progress"))
        Cell.create(text="Grenade",
                    name="Weapon",
                    order=1,
                    board=board1,
                    status=Status.get(Status.status == "done"))
 def post(self):
     if self.user:
         status = self.request.get('status')
         if status:
             s = Status(parent = status_key(),
                        user = self.user,
                        content = status)
             s.put()
             self.redirect("/newsfeed")
         else:
             self.render("errorpage.html", error = "Status cannot be empty!")
     else:
         self.redirect("/login")
示例#10
0
    def on_get(self, req, resp, user):

        auth_user = try_logged_jwt(auth_backend, req, resp)

        if auth_user and auth_user.id == user:
            photos = Status.select().join(User).where(UserProfile.username == auth_user.username).order_by(Status.created_at.desc())
        #Must to considerer the case of friends relation
        else:
            photos = Status.select().join(User).where(UserProfile.username == user).where(Status.public == True).order_by(Status.created_at.desc())

        query = [photo.to_model() for photo in photos]
        resp.body = json.dumps(query, default=str)
        resp.status = falcon.HTTP_200
示例#11
0
    async def get(self, user):

        #r = await aioredis.create_connection(
        #    f"redis://{os.environ.get('REDIS_HOST', 'localhost')}",
        #    loop=IOLoop.current().asyncio_loop,
        #    encoding='utf-8'
        #)

        local = self.get_argument('media_ids', False)
        max_id = self.get_argument('max_id', None)
        since_id = self.get_argument('since_id', None)
        limit = self.get_argument('limit', 20)

        statuses = []
        errors = 0

        for post in TimelineManager(user).query(since_id=since_id,
                                                max_id=max_id,
                                                local=True,
                                                limit=limit):
            status = Status.get_or_none(id=int(post))
            if status:
                json_data = status.to_json()
                count = await self.application.objects.count(
                    Like.select().join(UserProfile).switch(Like).join(
                        Status).switch(Like).where(Like.user.id == user.id,
                                                   Like.status.id == status))
                if count:
                    json_data["favourited"] = True

                statuses.append(json_data)

        self.write(json.dumps(statuses, default=str))
示例#12
0
    async def get(self, user):
        photos = await self.application.objects.execute(Status.select().where(
            Status.user == user).order_by(Status.created_at.desc()))

        query = [photo.to_json() for photo in photos]
        print(query)
        self.write(json.dumps(query, default=str))
示例#13
0
    def get(self, username):
        user = User.get_or_none(username=username)
        pagination = ap_pagination(self)
        if user:
            user = user.profile.get()

            # Retrive statuses
            objects = []
            for k in TimelineManager(user).query():
                try:
                    objects.append(
                        Status.get(
                            Status.identifier == status).to_activitystream())
                except:
                    pass

            print(objects)
            # create collection page
            collectionPage = activities.OrderedCollectionPage(
                map(activities.Note, objects), **pagination)

            # create collection
            collection = activities.OrderedCollection([collectionPage])
            self.write(json.dumps(collectionPage.to_json(context=True)))
            self.set_status(200)
示例#14
0
    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)
示例#15
0
    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
示例#16
0
def generate_create_note(status: Status, users: List[UserProfile]) -> Create:

    note = status.to_activitystream()

    for user in users:
        note['cc'].append(user.ap_id)

    note['to'] += ['https://mstdn.io/users/yabirgb']

    t = Note(note)

    data = {
        'id': note['id'] + '/activity',
        'type': "Create",
        'actor': note['attributedTo'],
        'published': note['published'],
        'object': t,
    }

    if note.get('to'):
        data['to'] = note['to']

    if note.get('cc'):
        data['cc'] = note['cc']

    return Create(data)
示例#17
0
def handle_create(activity):

    PUBLIC_CONTEXT = "https://www.w3.org/ns/activitystreams#Public"

    # Confirm that the status has images on it. At least one

    attachments = activity.object.attachment
    valid_atachments = []
    for attachment in attachments:
        if 'image' in attachment.type:
            valid_atachments.append(attachment)

    # If not end the job
    if not len(valid_atachments):
        return

    # Check who is the actor
    ap_id = ""
    if isinstance(activity.actor, activities.Actor):
        ap_id = activity.actor.id
    elif isinstance(activity.actor, str):
        ap_id = activity.actor

    # Get the profile of the creator
    actor = ActivityPubId(ap_id).get_or_create_remote_user()

    # Get the targets of the status
    targets = [
        x for x in activity.get("to", []) + activity.get("cc", []) +
        activity.get('bcc', [])
    ]

    is_public = PUBLIC_CONTEXT in targets

    followers_of = [
        ActivityPubId(x.replace('/followers', '')).get_or_create_remote_user()
        for x in targets
    ]
    directs = [
        x.split('/')[-1] for x in targets if urlparse(x).hostname == DOMAIN
    ]

    # Data for the new status
    note = activity.object
    data = {
        "caption": note.content,
        "visibility": is_public,
        "user": actor,
        "sensitive": False,
        "remote": True,
        "story": False,
        "ap_id": note.id,
        "identifier": note.id.split('/')[-1]
    }

    try:
        status = Status.create(**data)
        spread_status(status, [], False)
    except:
        return
def create_status_nodes(sql_context, sc):
    distinct_statuses = sql_context.sql(
        "SELECT DISTINCT(Status) from projects_tbl")
    distinct_statuses = distinct_statuses.rdd.filter(
        lambda x: x.Status is not None)
    statuses_nodes = distinct_statuses.map(lambda x: Status(x))
    sc.parallelize(statuses_nodes.collect()).foreachPartition(
        util.create_nodes_in)
def get_status_list():
    status_list = Status.select()
    board_id = request.form["board_id"]
    result = []
    for status in status_list:
        result.append(status.status)
    init_cell_list(board_id)
    return jsonify(result)
示例#20
0
    def on_post(self, req, resp):

        if req.get_param('media_ids'):
            user = req.context['user']

            status = Status(
                caption=req.get_param('status') or '',
                visibility=bool(req.get_param('visibility')), #False if None
                user=user,
                sensitive=bool(req.get_param('sensitive')),
                remote=False,
                story=bool(req.get_param('is_story'))
            )

            if status.sensitive:
                status.spoliet_text=req.get_param('spoiler_text')

            status.save()

            if  req.get_param('media_ids') != None:
                    for image in req.get_param('media_ids').split(','):
                        m = Media.get_or_none(media_name=image)
                        m.status = status
                        m.save()

            #Increment the number of posts uploaded
            UserProfile.update({UserProfile.statuses_count: UserProfile.statuses_count + 1}).where(UserProfile.id == user.id).execute()
            spread_status(status)
            resp.status = falcon.HTTP_200
            resp.body = json.dumps(status.to_json(),default=str)

        elif req.get_param('in_reply_to_id'):

            replying_to = Status.get_or_none(id=req.get_param('in_reply_to_id'))
            if replying_to:
                status = Status(
                    caption = req.get_param('status'),
                    user = user,
                    remote = False,
                    story = False,
                    in_reply_to = replying_to,
                    sensitive = replying_to.sensitive,
                    spoiler_text = req.get_param('spoiler_text') or replying_to.spoiler_text
                )
            else:
                resp.status = falcon.HTTP_500
                resp.body = json.dumps({"Error": "Replying to bad ID"})
        else:
            resp.status = falcon.HTTP_500
            resp.body = json.dumps({"Error": "No photo attached"})
示例#21
0
    async def get(self):

        query = await self.application.objects.execute(
            Status.select().where(Status.in_reply_to == None).limit(15))

        data = [n.to_json() for n in query]

        self.write(json.dumps(data, default=str))
        self.set_status(200)
示例#22
0
 def on_post(self, req, resp, id):
     status = Status.get_or_none(id=id)
     if status:
         user = req.context['user']
         UserManager(user).dislike(status)
         resp.body = json.dumps(status, default=str)
         resp.status = falcon.HTTP_200
     else:
         resp.status = falcon.HTTP_404
示例#23
0
 def on_delete(self, req, resp, id):
     status = Status.get_or_none(id=id)
     if status != None:
         if req.context['user'].id == status.user.id:
             remove_status(status)
             resp.status = falcon.HTTP_200
         else:
             resp.status = falcon.HTTP_401
     else:
         resp.status = falcon.HTTP_500
示例#24
0
    def on_get(self, req, resp, pid):
        #photo = self.model.get_or_none(identifier=pid)
        photo = Status.get_or_none(identifier=pid)

        if photo != None:
            result = photo.json()
        else:
            result = json.dumps({"Error": 'Not found'})

        resp.body = result
        resp.set_header('Response by:', 'zinat')
        resp.status = falcon.HTTP_200
示例#25
0
    def on_get(self, req, resp, id):
        #photo = self.model.get_or_none(identifier=pid)
        photo = Status.get_or_none(id=id)

        if photo != None:
            result = photo.json()
            resp.status = falcon.HTTP_200
        else:
            result = json.dumps({"Error": 'Not found'})
            resp.status = falcon.HTTP_404

        resp.body = result
示例#26
0
    async def get(self, id):
        try:
            photos = await self.application.objects.execute(
                Status.select().join(UserProfile).where(
                    (Status.user.id == id)
                    & Status.in_reply_to_id.is_null()).order_by(
                        Status.created_at.desc()))

            query = [photo.to_json() for photo in photos]
            self.write(json.dumps(query, default=str))
        except User.DoesNotExist:
            self.write({"Error": "User not found"})
示例#27
0
    def __init__(self, id):
        """
            id: int - Id of the status representing
        """

        self.status = Status.get_or_none(Status.id == id)
        self.LIVE_UPDATES = 'timeline:{}'

        if not self.status:
            raise ValueError(f'Could not find status with id {id}')

        self.id = id
示例#28
0
def upload_image(user, message, description, ident
                    ,sensitive, dimensions, filename):

    return Status.create(title=filename,
                         user=user,
                         message = message or '',
                         description = description or '',
                         sensitive = sensitive or '',
                         width = dimensions[0],
                         height=dimensions[1],
                         media_name=ident,
                         )
示例#29
0
    async def get(self, id):
        try:
            user = await self.application.objects.get(User, id=id)
            user = user.profile.get()
            photos = await self.application.objects.execute(
                Status.select().where(Status.user == user).order_by(
                    Status.created_at.desc()))

            query = [photo.to_json() for photo in photos]
            self.write(json.dumps(query, default=str))
        except User.DoesNotExist:
            self.write({"Error": "User not found"})
def rearrange_different_statused_cells(newid, oldid, oldstatus, newstatus,
                                       id_in_db):
    moved_cell = Cell.get(Cell.id == id_in_db)
    # newStatused rearrange
    cells = Cell.select().where((Cell.status == Status.get(
        Status.status == newstatus)) & (Cell.board == moved_cell.board)
                                & (Cell.order >= newid))
    for cell in cells:
        cell.order += 1
        cell.save()
    # oldStatus rearrange
    cells = Cell.select().where((Cell.status == Status.get(
        Status.status == oldstatus)) & (Cell.board == moved_cell.board)
                                & (Cell.order > oldid))
    for cell in cells:
        cell.order -= 1
        cell.save()

    moved_cell.order = newid
    moved_cell.status = Status.get(Status.status == newstatus)
    moved_cell.save()
示例#31
0
def like_status(status: Status, user: UserProfile) -> None:
    """
        status - the status target of the like - Status
        user - the user liking the post - UserProfile
    """

    r = redis.StrictRedis(host=os.environ.get('REDIS_HOST', 'localhost'))

    NotificationManager(user).create_like_notification(status)

    json_file = json.dumps(status.to_json(), default=str)

    r.publish(f'timeline:{status.user.id}', f'notification {json_file}')
示例#32
0
    def on_post(self, req, resp, album, user):
        photo = Status.get_or_none(Status.id == req.get_param('photo'))
        album = Album.get_or_none(Album.id == album)

        print(album, photo)

        if album and photo:
            RelationAlbumStatus.create(album=album, photo=photo)

            resp.body = json.dumps({"Result": "Status successfully modified"})
            resp.status = falcon.HTTP_200
        else:
            resp.body = json.dumps({"Error": "Error creating relation"})
            resp.status = falcon.HTTP_500
示例#33
0
    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)
示例#34
0
文件: scraper.py 项目: raquo/hnapp
	def save_newest_items(self):
		"""
		Get list of item dicts from the newest items by id
		Includes all acceptable kinds of items (ignores pollopts)
		Saves items in order of ascending ID – important for parent_id and root_id references
		"""
		
		# Get latest item ids from db and from api
		max_api_id = self.fetch_max_item_id()
		max_db_id = Status.get_max_item_id()
		
		# If no new data, quit. We don't want to use this function to update items
		if (max_api_id <= max_db_id):
			return
		
		# Save latest items and update max id
		save = lambda item_data: self.save_item(item_data, update_max_id=True)
		self.fetch_items(range(max_db_id+1, max_api_id), callback=save)
示例#35
0
	def getById(id):
		k = db.Key.from_path('Status', int(id))
		return Status.get(k)