feat: improve realtime messaging and media handling
This commit is contained in:
@@ -70,6 +70,8 @@ function webGroupConversation(array $group, string $role = 'member'): array
|
||||
'line' => 0,
|
||||
'category' => $category,
|
||||
'role' => $role,
|
||||
'nativeStatus' => (string)($group['native_status'] ?? ''),
|
||||
'nativeError' => (string)($group['native_error'] ?? ''),
|
||||
],
|
||||
];
|
||||
}
|
||||
@@ -176,6 +178,48 @@ function createNativeGroup(string $operator, string $groupId, string $name, stri
|
||||
]), '原生群组创建失败');
|
||||
}
|
||||
|
||||
function fetchNativeGroupInfo(string $groupKey): array
|
||||
{
|
||||
$payloads = [
|
||||
['group_id' => $groupKey],
|
||||
['groupId' => $groupKey],
|
||||
['target_id' => $groupKey],
|
||||
];
|
||||
$lastError = null;
|
||||
|
||||
foreach ($payloads as $payload) {
|
||||
try {
|
||||
return requireImSuccess(imAdminPost('/admin/group/get_info', $payload), '群组不存在');
|
||||
} catch (Throwable $error) {
|
||||
$lastError = $error;
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException($lastError ? ($lastError->getMessage() ?: '群组不存在') : '群组不存在');
|
||||
}
|
||||
|
||||
function nativeGroupInfoValue(array $native, string $key, string $fallback = ''): string
|
||||
{
|
||||
$sources = [$native];
|
||||
if (is_array($native['group_info'] ?? null)) {
|
||||
$sources[] = $native['group_info'];
|
||||
}
|
||||
if (is_array($native['group'] ?? null)) {
|
||||
$sources[] = $native['group'];
|
||||
if (is_array($native['group']['group_info'] ?? null)) {
|
||||
$sources[] = $native['group']['group_info'];
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($sources as $source) {
|
||||
if (isset($source[$key]) && (string)$source[$key] !== '') {
|
||||
return (string)$source[$key];
|
||||
}
|
||||
}
|
||||
|
||||
return $fallback;
|
||||
}
|
||||
|
||||
function addNativeGroupMember(string $operator, string $groupId, string $userId): void
|
||||
{
|
||||
requireImSuccess(imAdminPost('/admin/group/member/add', [
|
||||
@@ -425,11 +469,134 @@ function currentMuteSetting(PDO $appDb, string $groupId): array
|
||||
];
|
||||
}
|
||||
|
||||
function nativeGroupRoleFromType(mixed $type): string
|
||||
{
|
||||
$value = (int)$type;
|
||||
if ($value === 2) {
|
||||
return 'owner';
|
||||
}
|
||||
if ($value === 1) {
|
||||
return 'admin';
|
||||
}
|
||||
|
||||
return 'member';
|
||||
}
|
||||
|
||||
function fetchNativeGroupWithMember(PDO $imDb, string $groupId, string $userId): array
|
||||
{
|
||||
$statement = $imDb->prepare(
|
||||
'select g.*, m.`_type` as `member_type`, m.`_alias` as `member_alias`'
|
||||
. ' from `t_group` g'
|
||||
. ' join `t_group_member` m on m.`_gid` = g.`_gid`'
|
||||
. ' where g.`_gid` = ? and g.`_deleted` = 0 and m.`_mid` = ? limit 1'
|
||||
);
|
||||
$statement->execute([$groupId, $userId]);
|
||||
$group = $statement->fetch();
|
||||
|
||||
return is_array($group) ? $group : [];
|
||||
}
|
||||
|
||||
function ensureNativeGroupMember(PDO $imDb, string $groupId, string $userId): array
|
||||
{
|
||||
$group = fetchNativeGroupWithMember($imDb, $groupId, $userId);
|
||||
if ($group) {
|
||||
return $group;
|
||||
}
|
||||
|
||||
$exists = $imDb->prepare('select 1 from `t_group` where `_gid` = ? and `_deleted` = 0 limit 1');
|
||||
$exists->execute([$groupId]);
|
||||
if ($exists->fetchColumn()) {
|
||||
respond(403, '你不在该群组中');
|
||||
}
|
||||
|
||||
respond(404, '没有找到该群组');
|
||||
}
|
||||
|
||||
function nativeGroupDetailAction(PDO $imDb, string $userId, string $groupId): array
|
||||
{
|
||||
$group = ensureNativeGroupMember($imDb, $groupId, $userId);
|
||||
$ownerId = (string)($group['_owner'] ?? '');
|
||||
$users = fetchWebUserMap($imDb, [$ownerId, $userId], $userId);
|
||||
$owner = $users[$ownerId] ?? ['uid' => $ownerId, 'displayName' => $ownerId, 'contactAlias' => ''];
|
||||
$me = $users[$userId] ?? ['uid' => $userId, 'displayName' => $userId];
|
||||
$myAlias = (string)($group['member_alias'] ?? '');
|
||||
|
||||
return [
|
||||
'group' => [
|
||||
'groupId' => (string)$group['_gid'],
|
||||
'name' => (string)($group['_name'] ?? ''),
|
||||
'category' => 'group',
|
||||
'announcement' => '',
|
||||
'welcomeText' => '',
|
||||
'ownerUid' => $ownerId,
|
||||
'ownerName' => (string)($owner['contactAlias'] ?? '') !== '' ? (string)$owner['contactAlias'] : (string)$owner['displayName'],
|
||||
'myRole' => nativeGroupRoleFromType($group['member_type'] ?? 0),
|
||||
'myAlias' => $myAlias,
|
||||
'myDisplayName' => $myAlias !== '' ? $myAlias : (string)$me['displayName'],
|
||||
'muteSetting' => [
|
||||
'mode' => 'off',
|
||||
'scheduleType' => 'once',
|
||||
'startsAtMs' => 0,
|
||||
'endsAtMs' => 0,
|
||||
'dailyStart' => '',
|
||||
'dailyEnd' => '',
|
||||
'enabled' => false,
|
||||
],
|
||||
'native' => true,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
function nativeGroupMembersAction(PDO $imDb, string $userId, string $groupId): array
|
||||
{
|
||||
ensureNativeGroupMember($imDb, $groupId, $userId);
|
||||
|
||||
$statement = $imDb->prepare(
|
||||
'select `_mid`, `_alias`, `_type`, `_create_dt` from `t_group_member`'
|
||||
. ' where `_gid` = ? order by (`_type` = 2) desc, (`_type` = 1) desc, `_create_dt` asc limit 500'
|
||||
);
|
||||
$statement->execute([$groupId]);
|
||||
$members = $statement->fetchAll();
|
||||
$users = fetchWebUserMap($imDb, array_column($members, '_mid'), $userId);
|
||||
|
||||
return [
|
||||
'members' => array_map(static function (array $member) use ($users): array {
|
||||
$uid = (string)$member['_mid'];
|
||||
$user = $users[$uid] ?? [
|
||||
'uid' => $uid,
|
||||
'displayName' => $uid,
|
||||
'mobile' => '',
|
||||
'portrait' => '',
|
||||
'contactAlias' => '',
|
||||
'friendAlias' => '',
|
||||
'remarkAlias' => '',
|
||||
'isContact' => false,
|
||||
];
|
||||
$alias = trim((string)($member['_alias'] ?? ''));
|
||||
$contactAlias = trim((string)($user['contactAlias'] ?? ''));
|
||||
return [
|
||||
'uid' => $uid,
|
||||
'role' => nativeGroupRoleFromType($member['_type'] ?? 0),
|
||||
'alias' => $alias,
|
||||
'contactAlias' => $contactAlias,
|
||||
'friendAlias' => trim((string)($user['friendAlias'] ?? '')),
|
||||
'remarkAlias' => trim((string)($user['remarkAlias'] ?? '')),
|
||||
'isContact' => (bool)($user['isContact'] ?? false),
|
||||
'displayName' => $contactAlias !== '' ? $contactAlias : ($alias !== '' ? $alias : (string)$user['displayName']),
|
||||
'rawDisplayName' => (string)$user['displayName'],
|
||||
'groupDisplayName' => $alias,
|
||||
'mobile' => (string)($user['mobile'] ?? ''),
|
||||
'portrait' => (string)($user['portrait'] ?? ''),
|
||||
];
|
||||
}, $members),
|
||||
];
|
||||
}
|
||||
|
||||
function groupDetailAction(PDO $imDb, PDO $appDb, string $userId, string $groupId): array
|
||||
{
|
||||
$group = fetchWebGroup($appDb, $groupId);
|
||||
if (!$group) {
|
||||
respond(404, '没有找到该群组');
|
||||
return nativeGroupDetailAction($imDb, $userId, $groupId);
|
||||
}
|
||||
$member = fetchWebGroupMember($appDb, $groupId, $userId);
|
||||
if (!$member) {
|
||||
@@ -462,7 +629,7 @@ function groupDetailAction(PDO $imDb, PDO $appDb, string $userId, string $groupI
|
||||
function groupMembersAction(PDO $imDb, PDO $appDb, string $userId, string $groupId): array
|
||||
{
|
||||
if (!fetchWebGroupMember($appDb, $groupId, $userId)) {
|
||||
respond(403, '你不在该群组中');
|
||||
return nativeGroupMembersAction($imDb, $userId, $groupId);
|
||||
}
|
||||
|
||||
$statement = $appDb->prepare(
|
||||
@@ -512,7 +679,10 @@ function updateGroupAliasAction(PDO $appDb, string $userId, string $groupId, str
|
||||
respond(400, '本群昵称过长');
|
||||
}
|
||||
if (!fetchWebGroupMember($appDb, $groupId, $userId)) {
|
||||
respond(403, '你不在该群组中');
|
||||
ensureNativeGroupMember(imDb(), $groupId, $userId);
|
||||
$update = imDb()->prepare('update `t_group_member` set `_alias` = ? where `_gid` = ? and `_mid` = ?');
|
||||
$update->execute([$alias, $groupId, $userId]);
|
||||
return ['alias' => $alias];
|
||||
}
|
||||
|
||||
$now = webImNowMs();
|
||||
@@ -781,8 +951,16 @@ function joinWebGroupAction(PDO $appDb, string $userId, string $groupKey): array
|
||||
$group = fetchWebGroup($appDb, $groupKey);
|
||||
if (!$group) {
|
||||
try {
|
||||
$native = requireImSuccess(imAdminPost('/admin/group/get_info', ['groupId' => $groupKey]), '群组不存在');
|
||||
$native = fetchNativeGroupInfo($groupKey);
|
||||
$now = webImNowMs();
|
||||
$nativeGroupId = nativeGroupInfoValue(
|
||||
$native,
|
||||
'target_id',
|
||||
nativeGroupInfoValue($native, 'targetId', nativeGroupInfoValue($native, 'group_id', nativeGroupInfoValue($native, 'groupId', $groupKey)))
|
||||
);
|
||||
$nativeName = nativeGroupInfoValue($native, 'name', $nativeGroupId);
|
||||
$nativePortrait = nativeGroupInfoValue($native, 'portrait', '');
|
||||
$nativeOwner = nativeGroupInfoValue($native, 'owner', nativeGroupInfoValue($native, 'owner_uid', nativeGroupInfoValue($native, 'ownerUid', '')));
|
||||
$statement = $appDb->prepare(
|
||||
'insert into `t_web_groups`'
|
||||
. ' (`group_id`, `category`, `name`, `portrait`, `owner_uid`, `join_mode`, `announcement`,'
|
||||
@@ -791,33 +969,38 @@ function joinWebGroupAction(PDO $appDb, string $userId, string $groupKey): array
|
||||
. ' on duplicate key update `updated_at_ms` = values(`updated_at_ms`)'
|
||||
);
|
||||
$statement->execute([
|
||||
$groupKey,
|
||||
(string)($native['name'] ?? $groupKey),
|
||||
(string)($native['portrait'] ?? ''),
|
||||
(string)($native['owner'] ?? ''),
|
||||
$groupKey,
|
||||
$nativeGroupId,
|
||||
$nativeName,
|
||||
$nativePortrait,
|
||||
$nativeOwner,
|
||||
$nativeGroupId,
|
||||
$userId,
|
||||
$now,
|
||||
$now,
|
||||
]);
|
||||
$group = fetchWebGroup($appDb, $groupKey);
|
||||
$group = fetchWebGroup($appDb, $nativeGroupId);
|
||||
} catch (Throwable) {
|
||||
respond(404, '没有找到该群组');
|
||||
}
|
||||
}
|
||||
|
||||
$groupId = (string)$group['group_id'];
|
||||
$nativeStatus = (string)($group['native_status'] ?? '');
|
||||
$nativeError = (string)($group['native_error'] ?? '');
|
||||
upsertWebGroupMember($appDb, $groupId, $userId, 'member');
|
||||
try {
|
||||
addNativeGroupMember((string)($group['owner_uid'] ?: $userId), $groupId, $userId);
|
||||
} catch (Throwable $error) {
|
||||
// Web membership is authoritative for this web build; native group sync is best-effort.
|
||||
$nativeStatus = $nativeStatus === 'ok' ? 'partial' : ($nativeStatus ?: 'fallback');
|
||||
$nativeError = mb_substr($error->getMessage() ?: '加入原生群组失败', 0, 255, 'UTF-8');
|
||||
}
|
||||
|
||||
$role = fetchWebGroupRole($appDb, $groupId, $userId);
|
||||
return [
|
||||
'group' => fetchWebGroup($appDb, $groupId),
|
||||
'conversation' => webGroupConversation($group, $role),
|
||||
'nativeStatus' => $nativeStatus,
|
||||
'nativeError' => $nativeError,
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user