Example #1
0
async def delete_pin(channel_id, message_id):
    user_id = await token_check()
    _ctype, guild_id = await channel_check(user_id, channel_id)

    await channel_perm_check(user_id, channel_id, 'manage_messages')

    await app.db.execute(
        """
    DELETE FROM channel_pins
    WHERE channel_id = $1 AND message_id = $2
    """, channel_id, message_id)

    row = await app.db.fetchrow(
        """
    SELECT message_id
    FROM channel_pins
    WHERE channel_id = $1
    ORDER BY message_id ASC
    LIMIT 1
    """, channel_id)

    timestamp = snowflake_datetime(row['message_id'])

    await app.dispatcher.dispatch(
        'channel', channel_id, 'CHANNEL_PINS_UPDATE', {
            'channel_id': str(channel_id),
            'last_pin_timestamp': timestamp.isoformat()
        })

    return '', 204
Example #2
0
async def add_pin(channel_id, message_id):
    """Add a pin to a channel"""
    user_id = await token_check()
    _ctype, guild_id = await channel_check(user_id, channel_id)

    await channel_perm_check(user_id, channel_id, 'manage_messages')

    await app.db.execute(
        """
    INSERT INTO channel_pins (channel_id, message_id)
    VALUES ($1, $2)
    """, channel_id, message_id)

    row = await app.db.fetchrow(
        """
    SELECT message_id
    FROM channel_pins
    WHERE channel_id = $1
    ORDER BY message_id ASC
    LIMIT 1
    """, channel_id)

    timestamp = snowflake_datetime(row['message_id'])

    await app.dispatcher.dispatch(
        'channel', channel_id, 'CHANNEL_PINS_UPDATE', {
            'channel_id': str(channel_id),
            'last_pin_timestamp': timestamp_(timestamp)
        })

    await send_sys_message(app, channel_id, MessageType.CHANNEL_PINNED_MESSAGE,
                           message_id, user_id)

    return '', 204
Example #3
0
async def get_payment(payment_id: int, db=None):
    """Get a single payment's information."""
    if not db:
        db = app.db

    row = await db.fetchrow(
        """
    SELECT id::text, source_id, subscription_id, user_id,
           amount, amount_refunded, currency,
           description, status, tax, tax_inclusive
    FROM user_payments
    WHERE id = $1
    """, payment_id)

    drow = dict(row)

    drow.pop('source_id')
    drow.pop('subscription_id')
    drow.pop('user_id')

    drow['created_at'] = snowflake_datetime(int(drow['id']))

    drow['payment_source'] = await get_payment_source(row['user_id'],
                                                      row['source_id'], db)

    drow['subscription'] = await get_subscription(row['subscription_id'], db)

    return drow
Example #4
0
async def process_subscription(app, subscription_id: int):
    """Process a single subscription."""
    sub = await get_subscription(subscription_id, app.db)

    user_id = int(sub['user_id'])

    if sub['status'] != SubscriptionStatus.ACTIVE:
        log.debug('ignoring sub {}, not active', subscription_id)
        return

    # if the subscription is still active
    # (should get cancelled status on failed
    #  payments), then we should update premium status
    first_payment_id = await app.db.fetchval(
        """
    SELECT MIN(id)
    FROM user_payments
    WHERE subscription_id = $1
    """, subscription_id)

    first_payment_ts = snowflake_datetime(first_payment_id)

    premium_since = await app.db.fetchval(
        """
    SELECT premium_since
    FROM users
    WHERE id = $1
    """, user_id)

    premium_since = premium_since or datetime.datetime.fromtimestamp(0)

    delta = abs(first_payment_ts - premium_since)

    # if the time difference between the first payment
    # and the premium_since column is more than 24h
    # we update it.
    if delta.total_seconds() < 24 * HOURS:
        return

    old_flags = await app.db.fetchval(
        """
    SELECT flags
    FROM users
    WHERE id = $1
    """, user_id)

    new_flags = old_flags | UserFlags.premium_early
    log.debug('updating flags {}, {} => {}', user_id, old_flags, new_flags)

    await app.db.execute(
        """
    UPDATE users
    SET premium_since = $1, flags = $2
    WHERE id = $3
    """, first_payment_ts, new_flags, user_id)

    # dispatch updated user to all possible clients
    await mass_user_update(user_id, app)
Example #5
0
async def _process_user_payments(app, user_id: int):
    payments = await get_payment_ids(user_id, app.db)

    if not payments:
        log.debug('no payments for uid {}, skipping', user_id)
        return

    log.debug('{} payments for uid {}', len(payments), user_id)

    latest_payment = max(payments)

    payment_data = await get_payment(latest_payment, app.db)

    # calculate the difference between this payment
    # and now.
    now = datetime.datetime.now()
    payment_tstamp = snowflake_datetime(int(payment_data['id']))

    delta = now - payment_tstamp

    sub_id = int(payment_data['subscription']['id'])
    subscription = await get_subscription(sub_id, app.db)

    # if the max payment is X days old, we create another.
    # X is 30 for monthly subscriptions of nitro,
    # X is 365 for yearly subscriptions of nitro
    threshold = THRESHOLDS[subscription['payment_gateway_plan_id']]

    log.debug('delta {} delta days {} threshold {}', delta, delta.days,
              threshold)

    if delta.days > threshold:
        log.info('creating payment for sid={}', sub_id)

        # create_payment does not call any Stripe
        # or BrainTree APIs at all, since we'll just
        # give it as free.
        await create_payment(sub_id, app)
    else:
        log.debug('sid={}, missing {} days', sub_id, threshold - delta.days)