【问题标题】:PHP custom chat, coding a block functionPHP自定义聊天,编码一个block函数
【发布时间】:2013-11-21 19:17:56
【问题描述】:

我尝试构建一个 Block 函数,但没有任何运气,我的 SQL 技能没有我希望的那么好。

我有一张名为“messages”的表和一张名为“blocks”的表 现在,有 1 个文件将所有内容同步到聊天,我想做的是如果用户 1 阻止了用户 2,那么用户 1 的消息不应该到达用户 2,用户 2 的消息不应该到达用户 1。短期内,如果你阻止了一个你不能和他/她说话的人,而他/她不能和你说话!

"blocks" table:
id bigint(20)
user_id tinyint(20)
block_id tinyint(20)

"messages" table:
id bigint(20)
timestamp datetime
dest_type varchar(255)
dest_id bigint(20)
source_type varchar(255)
source_id bigint(20)
message_type varchar(255)
message text

在“blocks”中,user_id 是块行的所有者 ID。 而block_id是所有者想要屏蔽的id。 如果"messages.source_id = blocks.block_id OR messages.block_id = blocks.user_id" 比不要让信息得到低谷。我知道让别人为我编写代码是很不礼貌的,但我想问,有人可以试一试吗?

这里是 sync.php 文件: http://pastebin.com/8iiSCXGS

非常感谢!

【问题讨论】:

  • 那么messages.source_id是消息创建者的ID?这是私信操作吗?从创建者的角度来看,如果他们阻止了某人,您希望限制他们在您创建消息的表单中可用。从被屏蔽用户的角度来看,你想让他们知道自己是否被屏蔽了吗?
  • 你好,是的,没错!我希望sync.php 不接收来自阻止我和我阻止的用户的消息。如上所示,所有块都存储在块表中。在私人聊天中,我已经修复了阻止功能!但是在开放的房间里,每个人都互相交谈,sync.php 文件在用户之间发送所有消息,这就是为什么 sync.php 必须检查块表以查看它是否应该接收消息,这非常复杂(至少对我来说......)
  • 好的,所以它仍然会将消息存储在数据库中,但不会显示它们。我相信这可以通过将表格连接在一起来实现。
  • 是的,消息将被发送到send.php,它将存储它们,sync.php 然后下载它们并将它们显示给正确的用户。我们怎样才能把他们联合起来?我讨厌那个tbh:P

标签: php mysql


【解决方案1】:

我没有深入研究您的代码,但也许这会有所帮助。

让我们从简化的数据库结构开始,如下所示:

CREATE TABLE `blocks` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `user_id`       INT UNSIGNED NOT NULL,
  `block_id`      INT UNSIGNED NOT NULL );

INSERT INTO `blocks` (`user_id`,`block_id`) VALUES
  (1,2),(3,4),(2,1);

CREATE TABLE `messages` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  `author_id`     BIGINT NOT NULL,
  `message`       TEXT NOT NULL );

INSERT INTO `messages` (`author_id`,`message`) VALUES
  (1,"Message from user #1, who has a mutual block in place with user #2"),
  (2,"Message from user #2, who has a mutual block in place with user #1"),
  (3,"Message from user #3, who has blocked user #4"),
  (4,"Message from user #4, who has been blocked by user #3"),
  (5,"Message from user #5, who takes no part in all this blocking business");

现在假设用户$n访问了网站(其中1≤$n≤5)。要确定可以显示哪些消息,我们需要对 messagesblocks 表执行 LEFT JOIN — 即,我们要考虑 messages 的每一行以及包含相关的 blocks 的任何行信息(具体来说,消息的作者已阻止用户$n,或已被用户$n 阻止)。如果$n=1,我们有以下内容:

SELECT * FROM `messages`
  LEFT JOIN `blocks`
    ON (`author_id`=`block_id` AND `user_id`=1)
    OR (`author_id`=`user_id` AND `block_id`=1);

这是该查询的结果:

+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
| id | author_id | message                                                               | id   | user_id | block_id |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
|  1 |         1 | Message from user #1, who has a mutual block in place with user #2    | NULL |    NULL |     NULL |
|  2 |         2 | Message from user #2, who has a mutual block in place with user #1    |    1 |       1 |        2 |
|  2 |         2 | Message from user #2, who has a mutual block in place with user #1    |    3 |       2 |        1 |
|  3 |         3 | Message from user #3, who has blocked user #4                         | NULL |    NULL |     NULL |
|  4 |         4 | Message from user #4, who has been blocked by user #3                 | NULL |    NULL |     NULL |
|  5 |         5 | Message from user #5, who takes no part in all this blocking business | NULL |    NULL |     NULL |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
6 rows in set (0.00 sec)

如您所见,我们想要的行是最后三列为 NULL 的行,这意味着没有阻止规则影响向该特定用户显示此特定消息。因此,要提取这些消息,我们只需在查询末尾添加WHERE block_id IS NULL

SELECT * FROM `messages`
  LEFT JOIN `blocks`
    ON (`author_id`=`block_id` AND `user_id`=1)
    OR (`author_id`=`user_id` AND `block_id`=1)
  WHERE `block_id` IS NULL;
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
| id | author_id | message                                                               | id   | user_id | block_id |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
|  1 |         1 | Message from user #1, who has a mutual block in place with user #2    | NULL |    NULL |     NULL |
|  3 |         3 | Message from user #3, who has blocked user #4                         | NULL |    NULL |     NULL |
|  4 |         4 | Message from user #4, who has been blocked by user #3                 | NULL |    NULL |     NULL |
|  5 |         5 | Message from user #5, who takes no part in all this blocking business | NULL |    NULL |     NULL |
+----+-----------+-----------------------------------------------------------------------+------+---------+----------+
4 rows in set (0.01 sec)

如果您将不同的用户 ID 替换到此查询中,您应该会得到您想要的结果。

【讨论】:

  • 我明天起床去看看!
  • 谢谢你 squeamish ossifrage 这帮助我解决了问题:D
  • 不客气。我可以补充一下,如果您想在公共网站上实施这种“忽略”系统,请确保版主仍然可以看到忽略他们的人的消息,否则垃圾邮件发送者可能会忽略每个人,然后填写带有垃圾链接和其他垃圾的网站,而从未被检测到。
  • 是的,我已经确保无法阻止版主。
猜你喜欢
  • 1970-01-01
  • 2021-05-19
  • 2013-04-04
  • 2019-05-05
  • 1970-01-01
  • 1970-01-01
  • 2011-09-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多