群组管理


新建群组

	EMError error;
	string subject = getStringAttrFromJson(json, "subject");//群名称
	string description = getStringAttrFromJson(json, "description");//群简介
	string welcomeMessage = getStringAttrFromJson(json, "welcomeMessage");//欢迎辞
	string style = getStringAttrFromJson(json, "style");//群类型
	string maxUserCount = getStringAttrFromJson(json, "maxUserCount");//最大成员数
	stringstream stream;
	stream << maxUserCount;
	int nMaxUserCount;
	stream >> nMaxUserCount;
	EMGroupSetting setting;
	EMGroupSetting::EMGroupStyle emGroupStyle = EMGroupSetting::EMGroupStyle::PRIVATE_OWNER_INVITE;//私有群,成员无邀请权限
	if (style.compare("PRIVATE_MEMBER_INVITE") == 0)
	{
		emGroupStyle = EMGroupSetting::EMGroupStyle::PRIVATE_MEMBER_INVITE;//私有群,成员有邀请权限
	}
	else if (style.compare("PUBLIC_JOIN_APPROVAL") == 0)
	{
		emGroupStyle = EMGroupSetting::EMGroupStyle::PUBLIC_JOIN_APPROVAL;//公有群,加入需要验证
	}
	else if (style.compare("PUBLIC_JOIN_OPEN") == 0)
	{
		emGroupStyle = EMGroupSetting::EMGroupStyle::PUBLIC_JOIN_OPEN;//公有群,开放加入
	}
	setting.setStyle(emGroupStyle);
	setting.setMaxUserCount(nMaxUserCount);
	EMGroupMemberList members = getArrayAttrFromJson(json, "members");
	EMGroupPtr group = g_client->getGroupManager().createGroup(subject, description, welcomeMessage, setting, members, error);
	string ret;
	if (error.mErrorCode == EMError::EM_NO_ERROR)
	{
		ret = "{\"id\":\"" + group->groupId()
			+ "\",\"subject\":\"" + group->groupSubject() + "\"}";
		callback->Success(ret);
	}
	else
	{
		callback->Failure(error.mErrorCode, error.mDescription);
	}

群组加人

	EMError error;
	string id = getStringAttrFromJson(json, "id");
	string welcomeMessage = getStringAttrFromJson(json, "welcomeMessage");
	EMGroupMemberList members = getArrayAttrFromJson(json, "members");
	EMGroupPtr group = g_client->getGroupManager().addGroupMembers(id, members, welcomeMessage, error);
	string ret;
	if (error.mErrorCode == EMError::EM_NO_ERROR)
	{
		ret = "{\"id\":\"" + group->groupId()
			+ "\",\"subject\":\"" + group->groupSubject() + "\"}";
		callback->Success(ret);
	}
	else
	{
		callback->Failure(error.mErrorCode, error.mDescription);
	}

群组踢人

EMGroupPtr group = g_client->getGroupManager().removeGroupMembers(id, members, error);

退出群组

g_client->getGroupManager().leaveGroup(id, error);

解散群组

g_client->getGroupManager().destroyGroup(id, error);

接受群邀请

EMGroupPtr group = g_client->getGroupManager().acceptInvitationFromGroup(id, user, error);

拒绝群邀请

EMGroupPtr group = g_client->getGroupManager().declineInvitationFromGroup(id, user, error);

接受加入群申请

EMGroupPtr group = g_client->getGroupManager().acceptJoinGroupApplication(id, user, error);

拒绝加入群申请

EMGroupPtr group = g_client->getGroupManager().declineJoinGroupApplication(id, user, reason, error);

获取群组列表

//从服务器获取自己加入的和创建的群组列表,此api获取的群组sdk会自动保存到内存和db。
	EMError error;
	EMGroupList groupList = g_client->getGroupManager().fetchAllMyGroups(error);
	string ret;
	if (error.mErrorCode == EMError::EM_NO_ERROR) {
		for (EMGroupPtr group : groupList)
		{
			ret += "{\"jid\":\"blahblah\",\"name\":\"";
			ret += group->groupSubject();
			ret += "\",\"roomId\":\"";
			ret += group->groupId();
			ret += "\"},";
		}
		if (!ret.empty())
		{
			string tmp = ret.substr(0, ret.length() - 1);
			ret = "[" + tmp + "]";
		}
		string enc = Utils::URLEncode(ret);

		callback->Success(ret);
		lock_guard<std::mutex> guard(Utils::group_mutex);
		Utils::g_bGroupListDownloaded = true;
	}
	else
	{
		callback->Failure(error.mErrorCode, error.mDescription);
	}

修改群名称

EMGroupPtr group = g_client->getGroupManager().changeGroupSubject(id, subject, error);

修改群简介

EMGroupPtr group = g_client->getGroupManager().changeGroupDescription(id, subject, error);

群组信息

//获取群成员
	EMError error;
	string id = getStringAttrFromJson(json, "id");
	if (!id.empty())
	{
		string ret;
		const EMGroupMemberList *gml = g_client->getGroupManager().fetchGroupSpecification(id, error)->groupMembers();
		if (gml == NULL)
		{
			return;
		}
		for (string member : *gml)
		{
			ret += "{\"jid\":\"";
			ret += member;
			ret += "\",\"affiliation\":\"";
			ret += "member";
			ret += "\"},";
		}
		if (!ret.empty())
		{
			string tmp = ret.substr(0, ret.length() - 1);
			ret = "[" + tmp + "]";
		}
		if (error.mErrorCode != EMError::EM_NO_ERROR)
		{
			callback->Failure(error.mErrorCode, error.mDescription);
		}
		else
		{
			callback->Success(ret);
		}
	}
//获取群主
std::string owner = g_client->getGroupManager().fetchGroupSpecification(id, error)->groupOwner();

加入某个群组

只能用于加入公开群。

const EMGroupPtr group = g_client->getGroupManager().joinPublicGroup(id, error);

群组事件监听

class GroupManagerListener : public EMGroupManagerListener {
public:
	GroupManagerListener() {
	}
	virtual void onReceiveInviteFromGroup(const std::string groupId, const std::string& inviter, const std::string& inviteMessage);
	virtual void onReceiveInviteAcceptionFromGroup(const EMGroupPtr group, const std::string& invitee);
	virtual void onReceiveInviteDeclineFromGroup(const EMGroupPtr group, const std::string& invitee, const std::string &reason);
	virtual void onAutoAcceptInvitationFromGroup(const EMGroupPtr group, const std::string& inviter, const std::string& inviteMessage);
	virtual void onLeaveGroup(const EMGroupPtr group, EMGroup::EMGroupLeaveReason reason);
	virtual void onReceiveJoinGroupApplication(const EMGroupPtr group, const std::string& from, const std::string& message);
	virtual void onReceiveAcceptionFromGroup(const EMGroupPtr group);
	virtual void onReceiveRejectionFromGroup(const std::string &groupId, const std::string &reason);
	virtual void onUpdateMyGroupList(const std::vector<EMGroupPtr> &list);
};
...
...
GroupManagerListener *mGroupManagerListener = new GroupManagerListener();
g_client->getGroupManager().addListener(mGroupManagerListener);


上一页:好友管理

下一页:聊天室