Making WordPress.org


Ignore:
Timestamp:
01/23/2015 10:40:25 PM (9 years ago)
Author:
nacin
Message:

Trac mentions: Handle special cases for core trac.

  • Block mention notifications when the user has blocked the ticket.
  • Subscribe users to tickets once they've been mentioned.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/dotorg/trac/mentions-handler.php

    r1182 r1183  
    11<?php
     2
     3define( 'BLOCKED',    0 );
     4define( 'SUBSCRIBED', 1 );
     5define( 'MENTIONED',  2 );
    26
    37require dirname( dirname( __DIR__ ) ) . '/includes/slack-config.php';
     
    2832}
    2933
     34function wporg_get_trac_ticket_subscription_status( $username, $ticket ) {
     35    global $wpdb;
     36    add_db_table( 'trac_core', '_ticket_subs' ); // HyperDB
     37    $status = $wpdb->get_var( $wpdb->prepare(
     38        "SELECT status FROM _ticket_subs WHERE username = %s AND ticket = %s",
     39        $username, $ticket
     40    ) );
     41
     42    if ( is_numeric( $status ) ) {
     43        return (int) $status;
     44    }
     45    return false;
     46}
     47
    3048if ( ! wporg_user_has_visited_trac( $user_login ) ) {
    3149    $wpdb->insert( 'trac_users', compact( 'user_login' ) );
     
    3351
    3452add_filter( 'wporg_notifications_notify_username', function( $notify, $username ) use ( $type, $payload, $wpdb ) {
     53    // Core Trac has notifications configured, see if the user has blocked the ticket.
     54    if ( $payload->trac === 'core' ) {
     55        $status = wporg_get_trac_ticket_subscription_status( $username, $payload->ticket_id );
     56        if ( BLOCKED === $status ) {
     57            return false;
     58        }
     59    }
     60
    3561    if ( $type === 'ticket' ) {
    3662        // Don't need a query to say we can notify the owner and reporter.
     
    4167
    4268    if ( wporg_user_has_visited_trac( $username ) ) {
     69        // If on Core Trac, a user is not a reporter, owner, or subscriber, subscribe them.
     70        if ( isset( $status ) && false === $status ) {
     71            $wpdb->insert( '_ticket_subs', array(
     72                'username' => $username,
     73                'ticket'   => $payload->ticket_id,
     74                'status'   => MENTIONED,
     75            ) );
     76        }
     77
    4378        return true;
    4479    }
Note: See TracChangeset for help on using the changeset viewer.