-
Notifications
You must be signed in to change notification settings - Fork 1
/
friendship-service.ts
134 lines (122 loc) · 4.83 KB
/
friendship-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { HttpRequest } from 'express-hibernate-wrapper';
import { load } from 'hibernatets';
import { Position } from '../../../public/users';
import { ApiProxy } from '../api.proxy';
import { MessageCommunciation, MessageHandlerRegistration } from '../message-communication';
import { FriendShip } from '../user/friendship';
import { User } from '../user/user';
interface FriendMap {
[nickname: string]: {
status: 'offline' | 'online',
index: number
position?: Position,
room?: string
jitsiRoom?: string,
joinedAt?: string,
referenceUuid: string
};
}
const apiProxy = new ApiProxy();
@MessageHandlerRegistration
export class FriendshipService {
async friendscheck(data: unknown, req: HttpRequest<User>) {
const currentUserId = +req.user.id;
const friendships = await load(FriendShip, 'originalUser = ?', [currentUserId], {
deep: {
'friendedUser': 'TRUE = TRUE'
}
});
if (req.user.readyForFriends) {
req.user.readyForFriends = Math.floor((Date.now() + (1000 * 10)) / 1000);
}
return friendships.map(friendship => friendship.friendedUser.nickName);
}
async friendstatus(data: { withAdmin?: boolean } = {}, req: HttpRequest<User>): Promise<FriendMap> {
const map = await apiProxy.getUserMap(true);
const friiendMap: FriendMap = {};
req.user.friends.forEach((friend, index) => {
friiendMap[friend.friendedUser.nickName] = {
status: 'offline',
index: index + 1,
referenceUuid: friend.friendedUser.referenceUuid
};
});
for (let room in map) {
const roomData = map[room];
for (let user of roomData.users) {
const friend = req.user.friends.find(friendShip => friendShip.friendedUser.pusherUuid === user.pusherUuid);
if (friend || (req.user.adminPrivileges && data.withAdmin)) {
if (!friiendMap[user.name]) {
friiendMap[user.name] = { index: -1, status: 'online', referenceUuid: null };
}
friiendMap[user.name].position = user.position;
friiendMap[user.name].jitsiRoom = user.jitsiRoom;
friiendMap[user.name].joinedAt = user.joinedAt;
friiendMap[user.name].status = 'online';
friiendMap[user.name].room = room;
}
}
}
return friiendMap;
}
async readyfriendship(data: unknown, req: HttpRequest<User>) {
const readyUsers = await load(User, 'readyForFriends > ? ', [Math.floor(Date.now() / 1000)], {
deep: {
friends: 'TRUE=TRUE',
friendedUser: {
depths: 6,
filter: 'TRUE=TRUE'
}
}
});
const newFriends = [];
for (const user of readyUsers) {
const alreadyHasFriendWithId = user.friends.some(friend => {
return friend.friendedUser.id === req.user.id;
});
if (user.id !== req.user.id && !alreadyHasFriendWithId) {
const friendship = new FriendShip();
friendship.friendedUser = req.user;
user.friends.push(friendship);
const friendshipCurrentUser = new FriendShip();
friendshipCurrentUser.friendedUser = user;
req.user.friends.push(friendshipCurrentUser);
newFriends.push(user.nickName);
}
}
req.user.readyForFriends = Math.floor((Date.now() + (1000 * 10)) / 1000);
return {
new: newFriends,
friends: req.user.friends.map(friend => {
return friend.friendedUser.nickName;
})
};
}
unreadyfriendship(data: unknown, req: HttpRequest<User>) {
req.user.readyForFriends = null;
return true;
}
async chatmessage(data: { message: string }, req: HttpRequest<User>, ws) {
const messageParts = data.message.trim()
.split(' ');
const index = messageParts.shift();
const message = messageParts.join(' ');
if (index === 'global') {
if (req.user.adminPrivileges) {
MessageCommunciation.sendToAllUsers({
type: 'receivemessage',
author: req.user.nickName + ' - global',
message: message
});
}
} else {
const friendId = req.user.friends[+index - 1].friendedUser.id;
MessageCommunciation.sendToUserById(friendId, {
type: 'receivemessage',
author: req.user.nickName,
message: message
});
}
return true;
}
}