Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TSD Push Notification plugin #165

Closed
wants to merge 48 commits into from
Closed

Conversation

hesyifei
Copy link
Contributor

@hesyifei hesyifei commented Mar 13, 2019

This plugin was moved to a separate repo TheStanfordDaily/tsd-push-notification.


For:

Data Structure

CREATE TABLE wp_tsd_pn (
        id bigint(20) NOT NULL AUTO_INCREMENT,
        receiver_id bigint(20) unsigned NOT NULL,
        item_type varchar(20) NOT NULL,
        item_id bigint(20) unsigned NOT NULL,
        PRIMARY KEY (id),
        UNIQUE (receiver_id, item_type, item_id),
        INDEX (receiver_id, item_type),
        INDEX (item_type, item_id)
);

Suppose:

  • For subscriber 1144875:
  "subscribing": {
    "list": ["daily", "breaking", "weekly"],
    "category_ids": [2429],
    "author_ids": [239, 1002026],
    "location_ids": [9, 10, 11]
  }
  • For subscriber 1144876:
  "subscribing": {
    "list": ["breaking"],
    "category_ids": [2429, 4408],
    "author_ids": [239],
    "location_ids": [3, 9]
  }

Then,

mysql> SELECT * FROM wp_tsd_pn;
+-----+-------------+--------------+---------+
| id  | receiver_id | item_type    | item_id |
+-----+-------------+--------------+---------+
| 111 |     1144875 | author_ids   |     239 |
| 112 |     1144875 | author_ids   | 1002026 |
| 110 |     1144875 | category_ids |    2429 |
| 103 |     1144875 | list         |   51671 |
| 101 |     1144875 | list         |   51672 |
| 102 |     1144875 | list         |   51673 |
|  88 |     1144875 | location_ids |       9 |
|  89 |     1144875 | location_ids |      10 |
|  90 |     1144875 | location_ids |      11 |
| 115 |     1144876 | author_ids   |     239 |
| 113 |     1144876 | category_ids |    2429 |
| 114 |     1144876 | category_ids |    4408 |
| 104 |     1144876 | list         |   51673 |
| 116 |     1144876 | location_ids |       3 |
|  98 |     1144876 | location_ids |       9 |
+-----+-------------+--------------+---------+

item_id

item_id for:

  • item_type = list
    Custom taxonomy tsd_push_msg_receiver_group’s term_id.
    Note that the strings used in subscribing (such as "breaking") are the custom taxonomy's slug.

  • item_type = category_ids
    Category’s term_id.

  • item_type = author_ids
    Author’s ID.

  • item_type = location_ids
    Location’s id defined by tsd_locations_plugin_get_locations().

Procedure of Sending Notification to Subscribers

When the “Publish” button of a normal post is clicked:

  • Find all authors / (if possible) co-authors of the post
    • Find the subscriber for each author
    • Add these subscriber ids to $notification_receiver_ids
  • Find all categories of the post
    • Find the subscriber for each category
    • Add these subscriber ids to $notification_receiver_ids
  • Find all tags of the post
    • Find all locations related to the tags
    • Find the subscriber for each location
    • Add these subscriber ids to $notification_receiver_ids
  • Send the notification
    • Title: Post title
    • Body: Plain text of post content before <!--more--> tag
    • Data: [ "post_id" => $post_id ]
    • To: array_unique( $notification_receiver_ids );

When the “Publish” button of a tsd_push_msg post is clicked:

  • Find all tsd_push_msg_receiver_groups of the post
    • Add these subscriber ids to $notification_receiver_ids
  • Send the notification
    • Title: Post title
    • Body: Post excerpt
    • Data: Custom fields which have keys starting with tsd_pn_.
      For example, having a custom field called tsd_pn_post_id will add post_id and its value to the data.
    • To: array_unique( $notification_receiver_ids );

@hesyifei hesyifei requested a review from epicfaace March 13, 2019 05:18
Copy link
Member

@epicfaace epicfaace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @hesyifei excellent work! I've added a few comments.

Few long-term things to keep in mind:

  • Your SQL is great 👌, but I think it might be better to use an ORM such as Propel2 or wp-eloquent. This will help for future maintainability.
  • Excellent function decomposition; I would really think we should work on adding unit tests using phpunit. This will also help for future maintainability; otherwise once we're gone, people may not touch this code and it may become legacy code then 😉

That being said, it is the end of the quarter and we have to be realistic, so I'm good with merging this and deploying this since it works, without necessarily doing these large rewrites. We will definitely need to work on unit tests though at least; I will work on getting at least a continuous integration pipeline running for these tests on this repo.

I think a solution going forward would be to spin this off into a separate plugin. It doesn't look like there are many Wordpress PN plugins out there, so spinning it off would be a great open source initiative that the Daily Tech Team could do. That way, we can get more contributors on the project, and who knows, maybe even the Expo team could pick it up. We could also assemble a team next quarter that would be in change of this!

Again, great work, and what I'm saying is mostly motivated by the fact that I think this code is well-written and could be spun off into a separate plugin for maintainability and helping others as well. Let me know what you think!

'not_found' => 'No Push Notifications found.',
'not_found_in_trash' => 'No Push Notifications found in Trash.',
'featured_image' => 'Push Notification Feature Image', // Overrides the “Featured Image” phrase for this post type. Added in 4.3
'set_featured_image' => 'Set feature image', // Overrides the “Set featured image” phrase for this post type. Added in 4.3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hesyifei Can you remove these comments?

return [];
}

// TODO: Find better way to store category_ids, author_ids, location_ids.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this todo comment

'post_type' => 'tsd_pn_receiver',
'post_title' => $token,
'post_status' => "publish",
/*'meta_input' => [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this commented-out block of code

tsd_create_update_pn_receiver( $post_id, $user_subscribing[ "category_ids" ], $user_subscribing[ "author_ids" ], $user_subscribing[ "location_ids" ] );
} else {
echo "WARNING: SHOULD NOT HAVE MORE THAN 1 POST WITH SAME NAME";
return []; // TODO: return error;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe for returning errors, you can just use the WP_Error object: https://wordpress.stackexchange.com/questions/253445/wp-rest-api-how-to-change-http-response-status-code

$post_id = tsd_create_new_pn_receiver( $user_token, $user_subscribing[ "category_ids" ], $user_subscribing[ "author_ids" ], $user_subscribing[ "location_ids" ] );
} else if ( count( $this_user ) == 1 ) {
$post_id = $this_user[0]->ID;
tsd_create_update_pn_receiver( $post_id, $user_subscribing[ "category_ids" ], $user_subscribing[ "author_ids" ], $user_subscribing[ "location_ids" ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove this line of code


// Ref: https://docs.expo.io/versions/latest/guides/push-notifications/#http2-api
// TODO: "an array of up to 100 messages" - need divide 100
$response = wp_remote_post( "https://exp.host/--/api/v2/push/send", [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$response = wp_remote_post( "https://exp.host/--/api/v2/push/send", [
$response = wp_safe_remote_post( "https://exp.host/--/api/v2/push/send", [

}

tsd_pn_set_admin_notice( 'success', "Response: \n" . json_encode( $decoded_body ) . "\nYour message: \n" . json_encode( $message_body ) );
//wp_die( "Notification sent!<br />".$log_content, "Notification sent!", [ "response" => 200, "back_link" => true ] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this


$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $tsd_pn_db_table_name (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$sql = "CREATE TABLE $tsd_pn_db_table_name (
$sql = "CREATE TABLE IF NOT EXISTS $tsd_pn_db_table_name (

$charset_collate = $wpdb->get_charset_collate();

$sql = "CREATE TABLE $tsd_pn_db_table_name (
id bigint(20) NOT NULL AUTO_INCREMENT,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe you can just use ints instead of bigints for these columns

);

// DEBUG
//update_option( "tsd_pn_debug_info", [] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this

return []; // TODO: return error;
}

if ( $post_id != -1 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ( $post_id != -1 ) {
if ( $post_id !== -1 ) {

$notification_data
);

// DEBUG
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove these two lines

@hesyifei
Copy link
Contributor Author

Thanks for all the comments! 👍🏻

Since we moved this plugin to a separate repo TheStanfordDaily/tsd-push-notification, we will add TSD Push Notification Plugin as a submodule. See #167.

The comments were all addressed in the new repo. See also TheStanfordDaily/tsd-push-notification#1, TheStanfordDaily/tsd-push-notification#2.

Documentation was moved to https://github.com/TheStanfordDaily/tsd-push-notification/wiki.

@hesyifei hesyifei closed this Mar 22, 2019
@hesyifei hesyifei deleted the push-notification-plugin branch March 22, 2019 20:00
hesyifei added a commit that referenced this pull request Jun 13, 2019
Same as commit 675e91b. Forgot to add it when #165 was closed and #167 was merged.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants