Пример #1
0
def create_like_notification(sender, **kwargs):
    like = kwargs['instance']
    activity = like.activity
    if activity.profile_id == like.profile_id:
        return

    send_notification_to_user(
        like.profile.user, activity.profile.user, activity.url, 'new_like',
        f'❤️ <b>{like.profile.user} liked your comment</b>: {activity.metadata.get("title", "")}'
    )
Пример #2
0
def record_award_helper(qa,
                        profile,
                        layer=1,
                        action='Beat',
                        value_multiplier=1):
    """
    Awards point awards (and referral rewards) to a winner
    """

    #max depth
    if layer > max_ref_depth:
        return

    # record points
    value = abs(value_multiplier * qa.quest.value / (10**(layer - 1)))
    from quests.views import current_round_number
    QuestPointAward.objects.create(
        questattempt=qa,
        profile=profile,
        value=value,
        action=action,
        round_number=current_round_number,
    )

    # record kudos
    if layer > 1 or settings.DEBUG:
        gitcoinbot = get_profile('gitcoinbot')
        quest = qa.quest
        btc = BulkTransferCoupon.objects.create(
            token=quest.kudos_reward,
            tag='quest',
            num_uses_remaining=1,
            num_uses_total=1,
            current_uses=0,
            secret=random.randint(10**19, 10**20),
            comments_to_put_in_kudos_transfer=
            f"Congrats on beating the '{quest.title}' Gitcoin Quest",
            sender_profile=gitcoinbot,
            metadata={
                'recipient': profile.pk,
            },
            make_paid_for_first_minutes=1,
        )
        cta_url = btc.url
        cta_text = 'Redeem Kudos'
        msg_html = f"@{qa.profile.handle} just beat '{qa.quest.title}'.  You earned {round(value,2)} quest points & a kudos for referring them."
        send_notification_to_user(gitcoinbot.user, profile.user, cta_url,
                                  cta_text, msg_html)

    # recursively record points for your referals quest
    if profile.referrer:
        return record_award_helper(qa, profile.referrer, layer + 1, action,
                                   value_multiplier)
Пример #3
0
def create_notification(sender, **kwargs):
    activity = kwargs['instance']
    if activity.activity_type == 'new_tip':
        tip = activity.tip
        if tip.recipient_profile:
            send_notification_to_user(
                activity.profile.user, tip.recipient_profile.user,
                tip.receive_url, 'new_tip',
                f'<b>New Tip</b> worth {tip.value_in_usdt_now} USD ' +
                f'recieved from {tip.from_username}')

    if activity.activity_type == 'worker_applied':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'worker_applied',
            f'<b>{activity.profile.user} applied</b> to work on {bounty.title}'
        )

    if activity.activity_type == 'worker_approved':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=activity.metadata['worker_handle']),
            bounty.url, 'worker_approved',
            f'You have been <b>approved to work on {bounty.title}</b>')

    if activity.activity_type == 'worker_rejected':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=activity.metadata['worker_handle']),
            bounty.url, 'worker_rejected',
            f'Your request to work on <b>{bounty.title} has been rejected</b>')

    if activity.activity_type == 'start_work':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'start_work',
            f'<b>{activity.profile.user} has started work</b> on {bounty.title}'
        )

    if activity.activity_type == 'work_submitted':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'work_submitted',
            f'<b>{activity.profile.user} has submitted work</b> for {bounty.title}'
        )

    if activity.activity_type == 'work_done':
        bounty = activity.bounty
        amount_paid = activity.metadata['new_bounty']['value_in_usdt_now']
        send_notification_to_user(
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            activity.profile.user, bounty.url, 'work_done',
            f'<b>{bounty.bounty_owner_github_username}</b> has paid out ' +
            f'{amount_paid} USD for your work on {bounty.title}')

    if activity.activity_type == 'stop_work':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'stop_work',
            f'<b>{activity.profile.user} has stopped work</b> on {bounty.title}'
        )

    if activity.activity_type == 'new_crowdfund':
        bounty = activity.bounty
        amount = activity.metadata['value_in_usdt_now']
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'new_crowdfund',
            f'A <b>crowdfunding contribution worth {amount} USD</b> has been attached for {bounty.title}'
        )

    if activity.activity_type == 'new_kudos':
        if activity.kudos_transfer and activity.kudos_transfer.recipient_profile:
            send_notification_to_user(
                activity.profile.user,
                activity.kudos_transfer.recipient_profile.user,
                activity.kudos_transfer.receive_url_for_recipient, 'new_kudos',
                f'You received a <b>new kudos from {activity.profile.user}</b>'
            )

    if activity.activity_type == 'status_update':
        text = activity.metadata['title']
        mentioned_profiles = get_profiles_from_text(text).exclude(
            id__in=[activity.profile_id])
        send_mention_notification_to_users(activity, mentioned_profiles)
Пример #4
0
def create_notification(sender, **kwargs):
    activity = kwargs['instance']
    if activity.activity_type == 'new_tip':
        tip = activity.tip
        if tip.recipient_profile:
            send_notification_to_user(
                activity.profile.user, tip.recipient_profile.user,
                tip.receive_url, 'new_tip',
                f'<b>New Tip</b> worth {tip.value_in_usdt_now} USD ' +
                f'recieved from {tip.from_username}')

    if activity.activity_type == 'worker_applied':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'worker_applied',
            f'<b>{activity.profile.user} applied</b> to work on {bounty.title}'
        )

    if activity.activity_type == 'worker_approved':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=activity.metadata['worker_handle']),
            bounty.url, 'worker_approved',
            f'You have been <b>approved to work on {bounty.title}</b>')

    if activity.activity_type == 'worker_rejected':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=activity.metadata['worker_handle']),
            bounty.url, 'worker_rejected',
            f'Your request to work on <b>{bounty.title} has been rejected</b>')

    if activity.activity_type == 'start_work':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'start_work',
            f'<b>{activity.profile.user} has started work</b> on {bounty.title}'
        )

    if activity.activity_type == 'work_submitted':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'work_submitted',
            f'<b>{activity.profile.user} has submitted work</b> for {bounty.title}'
        )

    if activity.activity_type == 'work_done':
        bounty = activity.bounty
        amount_paid = activity.metadata['new_bounty']['value_in_usdt_now']
        send_notification_to_user(
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            activity.profile.user, bounty.url, 'work_done',
            f'<b>{bounty.bounty_owner_github_username}</b> has paid out ' +
            f'{amount_paid} USD for your work on {bounty.title}')

    if activity.activity_type == 'stop_work':
        bounty = activity.bounty
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'stop_work',
            f'<b>{activity.profile.user} has stopped work</b> on {bounty.title}'
        )

    if activity.activity_type == 'new_crowdfund':
        bounty = activity.bounty
        amount = activity.metadata['value_in_usdt_now']
        send_notification_to_user(
            activity.profile.user,
            get_user_model().objects.get(
                username__iexact=bounty.bounty_owner_github_username),
            bounty.url, 'new_crowdfund',
            f'A <b>crowdfunding contribution worth {amount} USD</b> has been attached for {bounty.title}'
        )

    if activity.activity_type == 'new_kudos':
        kudos_url = reverse(
            'profile_min',
            args=[activity.kudos_transfer.recipient_profile.handle, 'kudos'])

        if activity.kudos_transfer and activity.kudos_transfer.recipient_profile:
            kudos_url = activity.kudos_transfer.receive_url_for_recipient

        send_notification_to_user(
            activity.profile.user,
            activity.kudos_transfer.recipient_profile.user, kudos_url,
            'new_kudos',
            f'You received a <b>new kudos from {activity.profile.user}</b>')

    if activity.activity_type == 'status_update':
        text = activity.metadata['title']
        mentioned_profiles = get_profiles_from_text(text).exclude(
            id__in=[activity.profile_id])
        send_mention_notification_to_users(activity, mentioned_profiles)

    if activity.activity_type == 'create_ptoken':
        send_notification_to_user(
            activity.profile.user, activity.profile.user,
            activity.profile.absolute_url, 'create_ptoken',
            f'You <b>new time token {activity.ptoken.token_symbol}</b> has been created!'
        )

    if activity.activity_type == 'buy_ptoken':
        send_notification_to_user(
            activity.profile.user, activity.ptoken.token_owner_profile.user,
            activity.ptoken.token_owner_profile.absolute_url, 'buy_ptoken',
            f'New {activity.ptoken.token_symbol} <b>purchase from {activity.profile.user}</b>'
        )

    if activity.activity_type == 'accept_redemption_ptoken':
        send_notification_to_user(
            activity.ptoken.token_owner_profile.user,
            activity.redemption.redemption_requester.user,
            activity.redemption.url, 'accept_redemption_ptoken',
            f'📥 @{activity.ptoken.token_owner_profile.handle} <b>accepted</b> your request to redeem <b>{activity.redemption.total } { activity.ptoken.token_symbol }: "{activity.redemption.reason}</b>'
        )

    if activity.activity_type == 'denies_redemption_ptoken':
        redemption = activity.redemption
        if activity.redemption.redemption_state == 'denied':
            from_profile = redemption.ptoken.token_owner_profile
            to_profile = redemption.redemption_requester
            msg = f'📥 @{from_profile.handle} <b>denied</b> your request to redeem <b>{activity.redemption.total} {activity.ptoken.token_symbol}: "{activity.redemption.reason}</b>'
        else:
            if redemption.redemption_requester != redemption.canceller:
                from_profile = redemption.ptoken.token_owner_profile
                to_profile = redemption.redemption_requester
                msg = f'📥 @{from_profile.handle} <b>cancelled</b> your request to redeem <b>{activity.redemption.total} {activity.ptoken.token_symbol}: "{activity.redemption.reason}</b>'
            else:
                from_profile = redemption.redemption_requester
                to_profile = redemption.ptoken.token_owner_profile
                msg = f'📥 @{from_profile.handle} <b>cancelled</b> request to redeem <b>{activity.redemption.total} {activity.ptoken.token_symbol}: "{activity.redemption.reason}</b>'
        send_notification_to_user(from_profile.user, to_profile.user,
                                  redemption.url, 'denies_redemption_ptoken',
                                  msg)

    if activity.activity_type == 'complete_redemption_ptoken':
        redemption = activity.redemption
        send_notification_to_user(
            redemption.redemption_requester.user,
            redemption.ptoken.token_owner_profile.user, redemption.url,
            'complete_redemption_ptoken',
            f'📥 @{redemption.redemption_requester.handle} <b>completed</b> their redemption <b>{activity.redemption.total} {activity.ptoken.token_symbol}: "{activity.redemption.reason}</b>'
        )

    if activity.activity_type == 'incoming_redemption_ptoken':
        redemption = activity.redemption
        send_notification_to_user(
            redemption.ptoken.token_owner_profile.user,
            redemption.redemption_requester.user, redemption.url,
            'incoming_redemption_ptoken',
            f'Redemption "{redemption.reason}" marked as <b>ready from {redemption.ptoken.token_owner_profile}</b>'
        )
Пример #5
0
def ptoken_redemptions(request, token_id=None, redemption_state=None):
    """List and create token redemptions"""

    if token_id:
        ptoken = get_object_or_404(PersonalToken, id=token_id)
        network = request.POST.get('network')
        total = request.POST.get('total', 0)
        description = request.POST.get('description', '')

        if request.method == 'POST':
            if not request.user.is_authenticated:
                return JsonResponse(
                    {
                        'message':
                        _('You must be authenticated via github to use this feature!'
                          )
                    },
                    status=401)

            tokens_available = ptoken.get_available_amount(
                request.user.profile)
            if float(total) < 0 or float(total) > tokens_available:
                return JsonResponse(
                    {
                        'message':
                        _(f'You don\'t have enough ${ptoken.token_symbol} tokens!'
                          )
                    },
                    status=401)

            redemption = RedemptionToken.objects.create(
                ptoken=ptoken,
                network=network,
                total=total,
                reason=description,
                redemption_requester=request.user.profile)

            send_ptoken_redemption_request(ptoken.token_owner_profile, ptoken,
                                           redemption)
            send_notification_to_user(
                request.user.profile.user, ptoken.token_owner_profile.user,
                f'{reverse("dashboard")}?tab=ptoken',
                'accept_redemption_ptoken',
                f'@{request.user.profile} <b>asked</b> to redeem <b>{ redemption.total } { ptoken.token_symbol }: {redemption.reason}</b>'
            )

    redemptions = RedemptionToken.objects.filter(
        Q(redemption_requester=request.user.profile)
        | Q(ptoken__token_owner_profile=request.user.profile))

    if redemption_state in ['request', 'accepted', 'denied', 'cancelled']:
        redemptions = redemptions.filter(
            redemption_state=redemption_state).nocache()
    elif redemption_state == 'completed':
        redemptions = redemptions.filter(
            redemption_state__in=['waiting_complete', 'completed']).nocache()

    redemptions_json = []
    for redemption in redemptions.distinct():
        current_redemption = redemption.to_standard_dict()

        current_redemption[
            'avatar_url'] = redemption.redemption_requester.avatar_url
        current_redemption[
            'requester'] = redemption.redemption_requester.handle
        current_redemption['amount'] = redemption.total
        current_redemption['token_symbol'] = redemption.ptoken.token_symbol
        current_redemption['token_address'] = redemption.ptoken.token_address
        current_redemption[
            'token_owner'] = redemption.ptoken.token_owner_profile.handle
        current_redemption['token_name'] = redemption.ptoken.token_name
        current_redemption[
            'creator'] = redemption.ptoken.token_owner_profile.handle
        current_redemption[
            'canceller'] = redemption.canceller.handle if redemption.canceller else None
        redemptions_json.append(current_redemption)

    return JsonResponse(redemptions_json, safe=False)