Prechádzať zdrojové kódy

Merge pull request #1051 from Arantor/release-2.1

I like that message. Now tell the author I liked it.
Arantor 11 rokov pred
rodič
commit
21eac5dee1

+ 15 - 0
Sources/Likes.php

@@ -149,12 +149,27 @@ function issueLike($like_type, $like_content)
 	}
 	else
 	{
+		// Insert the like.
 		$smcFunc['db_insert']('insert',
 			'{db_prefix}user_likes',
 			array('content_id' => 'int', 'content_type' => 'string-6', 'id_member' => 'int', 'like_time' => 'int'),
 			array($like_content, $like_type, $context['user']['id'], time()),
 			array('content_id', 'content_type', 'id_member')
 		);
+
+		// Add a background task to process sending alerts.
+		$smcFunc['db_insert']('insert',
+			'{db_prefix}background_tasks',
+			array('task_file' => 'string', 'task_class' => 'string', 'task_data' => 'string', 'claimed_time' => 'int'),
+			array('$sourcedir/tasks/Likes-Notify.php', 'Likes_Notify_Background', serialize(array(
+				'content_id' => $like_content,
+				'content_type' => $like_type,
+				'sender_id' => $context['user']['id'],
+				'sender_name' => $context['user']['name'],
+				'time' => time(),
+			)), 0),
+			array('id_task')
+		);
 	}
 
 	// Now, how many people like this content now? We *could* just +1 / -1 the relevant container but that has proven to become unstable.

+ 100 - 0
Sources/tasks/Likes-Notify.php

@@ -0,0 +1,100 @@
+<?php
+
+/**
+ * This task handles notifying users when something is liked.
+ *
+ * Simple Machines Forum (SMF)
+ *
+ * @package SMF
+ * @author Simple Machines http://www.simplemachines.org
+ * @copyright 2013 Simple Machines and individual contributors
+ * @license http://www.simplemachines.org/about/smf/license.php BSD
+ *
+ * @version 2.1 Alpha 1
+ */
+
+class Likes_Notify_Background extends SMF_BackgroundTask
+{
+	public function execute()
+	{
+		global $smcFunc, $sourcedir;
+
+		$author = false;
+		// We need to figure out who the owner of this is.
+		if ($this->_details['content_type'] == 'msg')
+		{
+			$request = $smcFunc['db_query']('', '
+				SELECT mem.id_member, mem.id_group, mem.id_post_group, mem.additional_groups, b.member_groups
+				FROM {db_prefix}messages AS m
+					INNER JOIN {db_prefix}members AS mem ON (m.id_member = mem.id_member)
+					INNER JOIN {db_prefix}boards AS b ON (m.id_board = b.id_board)
+				WHERE id_msg = {int:msg}',
+				array(
+					'msg' => $this->_details['content_id'],
+				)
+			);
+			if ($row = $smcFunc['db_fetch_assoc']($request))
+			{
+				// Before we assign the author, let's just check that the author can see the board this is in...
+				// as it'd suck to notify someone their post was liked when in a board they can't see.
+				$groups = explode(',', $row['additional_groups']);
+				$groups[] = $row['id_group'];
+				$groups[] = $row['id_post_group'];
+				$allowed = explode(',', $row['member_groups']);
+
+				// If the user is in group 1 anywhere, they can see everything anyway.
+				if (in_array(1, $groups) || count(array_intersect($allowed, $groups)) != 0)
+					$author = $row['id_member'];
+			}
+			$smcFunc['db_free_result']($request);
+		}
+		else
+		{
+			// This isn't something we know natively how to support. Call the hooks, if they're dealing with it, return false, otherwise return the user id.
+			$hook_results = call_integration_hook('integrate_find_like_author', array($this->_details['content_type'], $this->_details['content_id']));
+			foreach ($hook_results as $result)
+				if (!empty($result))
+				{
+					$author = $result;
+					break;
+				}
+		}
+
+		// If we didn't have a member... leave.
+		if (empty($author))
+			return true;
+
+		// If the person who sent the notification is the person whose content it is, do nothing.
+		if ($author == $this->_details['sender_id'])
+			return true;
+
+		require_once($sourcedir . '/Subs-Notify.php');
+		$prefs = getNotifyPrefs($author, $this->_details['content_type'] . '_like');
+
+		// The likes setup doesn't support email notifications because that would be too many emails.
+		// As a result, the value should really just be non empty.
+
+		// First, grab the default if there is one.
+		if (!isset($prefs[$author]) && isset($prefs[0]))
+			$prefs[$author] = $prefs[0];
+
+		// Second, check the value. If no value or it's empty, they didn't want alerts, oh well.
+		if (empty($prefs[$author][$this->_details['content_type'] . '_like']))
+			return true;
+
+		// Issue, update, move on.
+		$smcFunc['db_insert']('insert',
+			'{db_prefix}user_alerts',
+			array('alert_time' => 'int', 'id_member' => 'int', 'id_member_started' => 'int', 'member_name' => 'string',
+				'content_type' => 'string', 'content_id' => 'int', 'content_action' => 'string', 'is_read' => 'int', 'extra' => 'string'),
+			array($this->_details['time'], $author, $this->_details['sender_id'], $this->_details['sender_name'],
+				$this->_details['content_type'], $this->_details['content_id'], 'like', 0, ''),
+			array('id_alert')
+		);
+
+		updateMemberData($author, array('alerts' => '+'));
+
+		return true;
+	}
+}
+?>

+ 2 - 1
other/install_2-1_mysql.sql

@@ -2141,7 +2141,8 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 
 INSERT INTO {$db_prefix}user_alerts_prefs
 	(id_member, alert_pref, alert_value)
-VALUES (0, 'member_register', 1);
+VALUES (0, 'member_register', 1),
+	(0, 'msg_like', 1);
 # --------------------------------------------------------
 
 #

+ 2 - 3
other/install_2-1_postgresql.sql

@@ -2721,9 +2721,8 @@ CREATE TABLE {$db_prefix}user_drafts (
 # Dumping data for table `user_alerts_prefs`
 #
 
-INSERT INTO {$db_prefix}user_alerts_prefs
-	(id_member, alert_pref, alert_value)
-VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
 # --------------------------------------------------------
 
 #

+ 2 - 3
other/install_2-1_sqlite.sql

@@ -2301,9 +2301,8 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 # Dumping data for table `user_alerts_prefs`
 #
 
-INSERT INTO {$db_prefix}user_alerts_prefs
-	(id_member, alert_pref, alert_value)
-VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
 # --------------------------------------------------------
 
 #

+ 2 - 3
other/install_2-1_sqlite3.sql

@@ -2301,9 +2301,8 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 # Dumping data for table `user_alerts_prefs`
 #
 
-INSERT INTO {$db_prefix}user_alerts_prefs
-	(id_member, alert_pref, alert_value)
-VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
 # --------------------------------------------------------
 
 #

+ 2 - 1
other/upgrade_2-1_mysql.sql

@@ -278,7 +278,8 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
 
 INSERT INTO {$db_prefix}user_alerts_prefs
 	(id_member, alert_pref, alert_value)
-VALUES (0, 'member_register', 1);
+VALUES (0, 'member_register', 1),
+	(0, 'msg_like', 1);
 ---#
 
 /******************************************************************************/

+ 2 - 3
other/upgrade_2-1_postgresql.sql

@@ -352,9 +352,8 @@ CREATE TABLE {$db_prefix}user_alerts_prefs (
   PRIMARY KEY (id_member, alert_pref)
 );
 
-INSERT INTO {$db_prefix}user_alerts_prefs
-	(id_member, alert_pref, alert_value)
-VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
 ---#
 
 /******************************************************************************/

+ 2 - 3
other/upgrade_2-1_sqlite.sql

@@ -331,9 +331,8 @@ CREATE TABLE IF NOT EXISTS {$db_prefix}user_alerts_prefs (
   PRIMARY KEY (id_member, alert_pref)
 );
 
-INSERT INTO {$db_prefix}user_alerts_prefs
-	(id_member, alert_pref, alert_value)
-VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'member_register', 1);
+INSERT INTO {$db_prefix}user_alerts_prefs (id_member, alert_pref, alert_value) VALUES (0, 'msg_like', 1);
 ---#
 
 /******************************************************************************/