mirror of
https://github.com/fluencelabs/fluent-pad
synced 2025-04-25 00:42:14 +00:00
rewriting user online status checks with peer is_connected api
This commit is contained in:
parent
314847738a
commit
87cc957580
@ -2,12 +2,12 @@ import { FluenceClient, Particle, sendParticle, sendParticleAsFetch } from '@flu
|
||||
|
||||
import {
|
||||
fluentPadServiceId,
|
||||
notifyOnlineFnName,
|
||||
notifyTextUpdateFnName,
|
||||
notifyUserAddedFnName,
|
||||
notifyUserRemovedFnName,
|
||||
history,
|
||||
userList,
|
||||
notifyOnlineFnName,
|
||||
} from './constants';
|
||||
|
||||
export interface ServiceResult {
|
||||
@ -50,16 +50,10 @@ export const updateOnlineStatuses = async (client: FluenceClient) => {
|
||||
(fold allUsers.$.users! u
|
||||
(par
|
||||
(seq
|
||||
(call u.$.relay_id! ("op" "identity") [])
|
||||
(call u.$.relay_id! ("peer" "is_connected") [u.$.peer_id!] isOnline)
|
||||
(seq
|
||||
(call u.$.peer_id! ("op" "identity") [])
|
||||
(seq
|
||||
(call u.$.relay_id! ("op" "identity") [])
|
||||
(seq
|
||||
(call myRelay ("op" "identity") [])
|
||||
(call myPeerId (fluentPadServiceId notifyOnline) [u.$.peer_id!])
|
||||
)
|
||||
)
|
||||
(call myRelay ("op" "identity") [])
|
||||
(call myPeerId (fluentPadServiceId notifyOnline) [u.$.peer_id! isOnline])
|
||||
)
|
||||
)
|
||||
(next u)
|
||||
@ -92,7 +86,7 @@ export const notifySelfAdded = (client: FluenceClient, name: string) => {
|
||||
(par
|
||||
(seq
|
||||
(call u.$.relay_id! ("op" "identity") [])
|
||||
(call u.$.peer_id! (fluentPadServiceId notifyUserAdded) [myUser setOnline])
|
||||
(call u.$.peer_id! (fluentPadServiceId notifyUserAdded) [myUser true])
|
||||
)
|
||||
(next u)
|
||||
)
|
||||
@ -107,14 +101,11 @@ export const notifySelfAdded = (client: FluenceClient, name: string) => {
|
||||
myPeerId: client.selfPeerId,
|
||||
fluentPadServiceId: fluentPadServiceId,
|
||||
notifyUserAdded: notifyUserAddedFnName,
|
||||
myUser: [
|
||||
{
|
||||
name: name,
|
||||
peer_id: client.selfPeerId,
|
||||
relay_id: client.relayPeerId,
|
||||
},
|
||||
],
|
||||
setOnline: true,
|
||||
myUser: {
|
||||
name: name,
|
||||
peer_id: client.selfPeerId,
|
||||
relay_id: client.relayPeerId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
@ -128,9 +119,20 @@ export const getUserList = async (client: FluenceClient) => {
|
||||
(call myRelay ("op" "identity") [])
|
||||
(seq
|
||||
(call userlistNode (userlist "get_users") [] allUsers)
|
||||
(seq
|
||||
(call myRelay ("op" "identity") [])
|
||||
(call myPeerId (fluentPadServiceId notifyUserAdded) [allUsers.$.users!])
|
||||
(fold allUsers.$.users! u
|
||||
(par
|
||||
(seq
|
||||
(call u.$.relay_id! ("op" "identity") [])
|
||||
(seq
|
||||
(call u.$.relay_id! ("peer" "is_connected") [u.$.peer_id!] isOnline)
|
||||
(seq
|
||||
(call myRelay ("op" "identity") [])
|
||||
(call myPeerId (fluentPadServiceId notifyUserAdded) [u isOnline])
|
||||
)
|
||||
)
|
||||
)
|
||||
(next u)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
@ -142,7 +144,6 @@ export const getUserList = async (client: FluenceClient) => {
|
||||
myPeerId: client.selfPeerId,
|
||||
fluentPadServiceId: fluentPadServiceId,
|
||||
notifyUserAdded: notifyUserAddedFnName,
|
||||
immediately: true,
|
||||
},
|
||||
);
|
||||
|
||||
|
@ -14,17 +14,8 @@ interface User {
|
||||
id: PeerIdB58;
|
||||
name: string;
|
||||
isOnline: boolean;
|
||||
shouldBecomeOnline: boolean;
|
||||
}
|
||||
|
||||
const turnUserAsOfflineCandidate = (u: User): User => {
|
||||
return {
|
||||
...u,
|
||||
isOnline: u.shouldBecomeOnline,
|
||||
shouldBecomeOnline: false,
|
||||
};
|
||||
};
|
||||
|
||||
const refreshTimeoutMs = 2000;
|
||||
|
||||
export const UserList = (props: { selfName: string }) => {
|
||||
@ -33,13 +24,6 @@ export const UserList = (props: { selfName: string }) => {
|
||||
|
||||
useEffect(() => {
|
||||
const listRefreshTimer = setInterval(() => {
|
||||
setUsers((prev) => {
|
||||
const newUsers = Array.from(prev).map(
|
||||
([key, user]) => [key, turnUserAsOfflineCandidate(user)] as const,
|
||||
);
|
||||
return new Map(newUsers);
|
||||
});
|
||||
|
||||
// don't block
|
||||
withErrorHandlingAsync(async () => {
|
||||
await api.updateOnlineStatuses(client);
|
||||
@ -47,23 +31,21 @@ export const UserList = (props: { selfName: string }) => {
|
||||
}, refreshTimeoutMs);
|
||||
|
||||
const unsub1 = subscribeToEvent(client, fluentPadServiceId, notifyUserAddedFnName, (args, _) => {
|
||||
const [users, setOnline] = args as [api.User[], boolean];
|
||||
const [user, isOnline] = args as [api.User, boolean];
|
||||
console.log(user, isOnline);
|
||||
setUsers((prev) => {
|
||||
const u = user;
|
||||
const result = new Map(prev);
|
||||
for (let u of users) {
|
||||
if (result.has(u.peer_id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const isCurrentUser = u.peer_id === client.selfPeerId;
|
||||
|
||||
result.set(u.peer_id, {
|
||||
name: u.name,
|
||||
id: u.peer_id,
|
||||
isOnline: isCurrentUser || setOnline,
|
||||
shouldBecomeOnline: isCurrentUser || setOnline,
|
||||
});
|
||||
if (result.has(u.peer_id)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
result.set(u.peer_id, {
|
||||
name: u.name,
|
||||
id: u.peer_id,
|
||||
isOnline: isOnline,
|
||||
});
|
||||
|
||||
return result;
|
||||
});
|
||||
});
|
||||
@ -78,18 +60,13 @@ export const UserList = (props: { selfName: string }) => {
|
||||
});
|
||||
|
||||
const unsub3 = subscribeToEvent(client, fluentPadServiceId, notifyOnlineFnName, (args, _) => {
|
||||
const [userOnline] = args as [PeerIdB58[]];
|
||||
const [user, onlineStatus] = args as [PeerIdB58, boolean];
|
||||
setUsers((prev) => {
|
||||
const result = new Map(prev);
|
||||
|
||||
for (let u of userOnline) {
|
||||
const toSetOnline = result.get(u);
|
||||
if (toSetOnline) {
|
||||
toSetOnline.shouldBecomeOnline = true;
|
||||
toSetOnline.isOnline = true;
|
||||
}
|
||||
const u = result.get(user);
|
||||
if (u) {
|
||||
result.set(user, { ...u, isOnline: onlineStatus });
|
||||
}
|
||||
|
||||
return result;
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user