fenos/mex

laravel 4 的多参与者聊天 API

1.0.7 2014-05-18 14:08 UTC

This package is not auto-updated.

Last update: 2024-09-14 14:41:36 UTC


README

包已过时,在生产环境中无作用

=====

Build Status License Latest Stable Version

大家好,我想向大家介绍 Mex,Mex 是一个简单但功能强大的 API,用于构建内部多参与者聊天系统。这个 API 拥有众多特性,语法非常直观,现在您有了使用 laravel 4 构建自己的多参与者聊天的工具。享受它吧。

安装

步骤 1

在您的 composer.json 中添加它

"fenos/mex": "1.0.*"

然后运行 composer update

步骤 2

将以下字符串添加到 app/config/app.php

提供者数组

'Fenos\Mex\MexServiceProvider'

别名数组

'Mex'    => 'Fenos\Mex\Facades\Mex'

步骤 3

迁移

确保您的 app/config/database.php 设置正确,然后输入迁移

php artisan migrate --package="fenos/mex"

步骤 4

包含关系

为了包含 mex 需要知道的关系,您只需在您的模型中导入以下 trait 即可,就像这样


    class User extends Eloquent
    {

        use \Fenos\Mex\Models\MexRelations;

    }

这就完成了。

文档

作为对 Mex 的第一次接触,您需要知道这个包是基于对话的,所以在执行任何操作之前,您必须检索当前对话来工作。那么,我们可以开始了。

对话

您将使用的代码,简而言之,总是用于初始化对话

Mex::conversation($conversation_id);

另一个有用的方法是 participants,使用它,您可以通过仅提供参与者 ID 来检索对话。但请注意,在对话开始时,您必须通过对话 ID 获取它。因为如果任何参与者加入或离开对话,对话的参与者数量将不同,再次检索它时,您必须通过加入的参与者或没有离开的参与者传递新的参与者。

Mex::conversation()->participants(1,2,3,4);

存在对话

如果您想检查是否存在上述对话,您可以通过两种不同的方式来做。

拥有对话的 ID

try
{
    Mex::conversation($conversation_id)->exists();
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // do your staff
}

只有参与者的 ID

try
{
    Mex::conversation()->participants(1,2,3,4)->exists();
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // do your staff
}

participants() 方法中,您将传递参与者的 ID(目前为 "Users")。但请记住,您甚至可以传递一个 数组

创建对话

创建对话非常简单,让我展示代码,在这种情况下,展示代码比解释它更容易。然后审查它。

// this will create a multi conversation between users 1 - 2 and 3
Mex::conversation()->participants(1,2,3)->create(['founder_id' => 1]);

所以这里发生了什么?我在告诉 Mex 我正在处理一个 conversation,我知道,我需要创建它,所以我传递了 "将开始聊天" 的参与者,然后我通过一个数组传递创始人 ID,简单吗?

请注意,founder_id 必须指定,但您甚至可以在参与者的值/数组中省略它。

另外,非常重要,创建方法不会检查这些参与者之间是否已经启动了对话,所以在创建之前,请确保您已经检查了这一点。


$participants = [1,2,3];
$founder = Auth::user()->id;
try
{
    Mex::conversation()->participants($participants)->exists();

    // here means that exists so you can just send a message in the conversation we will see it later
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
    Mex::conversation()->participants($participants)->create(['founder_id' => $founder]);
}

这就完成了!

存档与恢复对话

存档

存档对话并不意味着删除它,只是将其存储为存档,可能是一些你不希望保持活跃的旧对话。使用Mex,你可以轻松快速地完成这项操作,让我来展示给你看。

try
{
    Mex::conversation($conversation_id)->from($user_id)->archive(); // that's it, the user 1 has archived the conversation
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

第二种方法

try
{
    Mex::conversation()->participants(1,2,3)->from($user_id)->archive(); // that's it, the user 1 has archived the conversation
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

正如你在这里看到的,我使用了名为 from() 的新方法,这个方法对于确定谁执行了操作非常重要。因为你知道,如果 用户ID 1 存档了包含 用户ID 2 的对话,那么 只有 用户ID 1 会将其视为已存档,而用户2仍然正常。所以,在执行仅关注单个用户的所有操作时,始终使用 from() 指定用户。

恢复

要恢复对话并将其再次设置为“活跃”,请使用 restore() 方法。

try
{
    Mex::conversation($conversation_id)->from($user_id)->restore(); // that's it, the user 1 has restored the conversation
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

你甚至可以使用参与者方法。

列出对话

你需要检索当前用户发起或被邀请的对话的漂亮列表吗?好吧,使用这个方法,但请记住,存档的对话将不会显示在这里。

你对这个方法有什么期待?对话列表/参与者信息/最后发送的消息

Mex::conversation()->from($user_id)->lists();

目前,此方法将获取关于该用户的全部对话,但如果该用户有1000个对话?别担心,你有3个过滤器,我相信你了解它们。

// limit the result to 10 conversation and order it
Mex::conversation()->from($user_id)->lists( array('limit' => 10,'orderBy' => 'DESC') );

// paginate it
Mex::conversation()->from($user_id)->lists( array(['orderBy' => 'ASC', 'paginate' => 10) );

新过滤器

如果你想要在参与者结果中排除创始人信息,请使用带有 founder 过滤器的 false 值,如下所示

Mex::conversation()->from($user_id)->lists( array(['orderBy' => 'ASC', 'paginate' => 10, 'founder' => false) );

如果你需要检索存档的对话列表,请使用此方法

// you can have the parameters as above
Mex::conversation()->from($user_id)->archivedLists();

加入与退出对话

该用户已经与其他人开始了对话,但想添加其他人。你可以通过使用 join() 方法为你的聊天提供此功能,看看效果如何。

try
{
    Mex::conversation($conversation_id)->join(4,5); // user 4 and 5 are joined now in the conversation

catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

相反,如果你想离开对话,请使用 leave()

try
{
    Mex::conversation($conversation_id)->leave(4,5); // user 4 and 5 are leaved now in the conversation

catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

强制删除对话

删除对话意味着,用户将不再看到该对话 只有执行操作的用户,其他参与者仍然会看到该对话。并且可以使用 restore 方法将其恢复,以防万一。请记住,如果用户删除对话,则用户不会离开对话,这取决于你。让我们看看如何强制删除对话。

try
{
    Mex::conversation($id)->from($user_id)->forceRemove();

catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

消息

现在你已经了解了对话,现在是时候关注消息了(没有什么比对话更难了)。看看它是如何轻松工作的。

发送消息

发送消息非常简单,让我先展示代码,然后再进行审查。

try
{
    Mex::conversation($conversation_id)->message('Text of your message')->from(1)->send();

catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

我告诉过你,这几乎是自我解释的,但我会进行审查。我们正在获取对话并传递其ID,在这个对话中,我们知道必须发送消息。接下来,我们使用 message() 方法,并在其中包含消息文本。接下来,哪个用户发送了消息?我用 from() 方法指定了他。现在Mex知道了所有信息,以将消息发送到正确的地方,并使用 send()

现在我想同时审查创建对话和发送消息!这是聊天必须拥有的主要操作。


try
{
    // check if the conversation exists
    $conversation = Mex::conversation()->participants(1,2,3)->exists();

    // yes exists, the method above give to me the ID of the conversation and i send only the message
    Mex::conversation($conversation->conversation_id)->message('Conversation exists')->from(1)->send();
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // the conversation doesn't exists so I create a new
    $newConversation = Mex::conversation()->participants(1,2,3)->create(['founder_id'=> 1]);

    // and I send the message to the current conversation
    $newConversation->message('New conversation')->from(1)->send();
}

酷吧?

获取对话中的消息

最后,我们几乎完成了,现在是时候获取对话的消息了。这里有两种获取对话消息的方法。

首先,正如我在文档中提到的,方法 from() 对于让Mex知道哪个用户想要检索消息非常重要,所以我会使用以下方法。

try
{
    Mex::conversation($conversation_id)->from(1)->get();
}
catch()
{
    // conversation not found
}

使用 from() 方法,外部用户无法访问不属于当前对话ID的任何对话,它将抛出 ConversationNotFoundException

此外,如果用户删除了一些消息,使用 from() 显然不会显示它们,因为它们已被删除,但 不是对话中的其他用户

第二种方法获取完全的对话记录,包括所有已删除的消息,因此对用户来说可能没有太大用处,但我还是记录了下来。

try
{
    Mex::conversation($conversation_id)->get();
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

如果您想要在 get() 方法中限制、排序或分页消息,请传递数组。

Mex::conversation($conversation_id)->from(1)->get(['limit' => 10,'orderBy' => 'DESC']);
Mex::conversation($conversation_id)->from(1)->get(['orderBy' => 'DESC','paginate' => '10']); // paginate as last paramater of the array

同样重要的是,您可以检索“活跃”和“存档”对话的消息,但不能检索已被强制删除的对话。这会抛出 ConversationNotFoundException

删除消息

删除消息的好特点是,只有删除消息的用户将不再看到它,但其他参与者仍然可以看到。让我们看看用户是如何删除消息的。

try
{
    // this delete the message with id 1 for the user with 1 on the conversation with id 1!! :)
    Mex::conversation($conversaton_id)->message($message_id)->from($user_id)->delete();

    // if you prefer to use from() method after the conversation() method do it is the same
    Mex::conversation($conversaton_id)->from($user_id)->message($message_id)->delete();
}
catch(\Fenos\Mex\Exceptions\ConversationNotFoundException $e)
{
    // conversation not found
}

注意

您可以通过第一次发布看到这一点,它似乎很有希望。如果时间允许,Mex 将会有已经设计好的新功能。例如,它将像多态一样出现,您可以在迁移文件中看到表已经为它设置好了 :)。其余的功能将会带来更多惊喜。 :)

我用 <3 做了它。

测试

要运行测试,请确保已安装 phpUnit 和 Mockery。

致谢

© 版权所有 Fabrizio Fenoglio

在 MIT 许可下发布包。