-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRoom.js
159 lines (157 loc) · 5.03 KB
/
Room.js
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const {
gRequest,
// QUERY_ROOM_LIST,
QUERY_ROOM,
// UPDATE_ACTIVE,
UPDATE_MEMBERS,
NEW_ROOM
} = require("./graphqlClient");
const { sameUser } = require("./utils");
const Rooms = {};
// 只保留需要的字段
const filterMemberFields = (member = null) => {
if (!member) return member;
let keeps = ["id", "uid", "username", "avator", "creator", "photo"];
let tmp = {};
Object.keys(member).forEach(k => {
if (keeps.includes(k) && typeof member[k] !== "undefined") {
tmp[k] = member[k];
}
});
return tmp;
};
class Room {
constructor({ id, temp = "false", link = "" }) {
this.id = id;
this.name = "";
this.temp = temp === "false" ? false : true;
this.link = link;
this.creator = null;
this.active = false;
this.members = null;
this.users = {};
this.tabs = null;
this.workspaceData = null;
this.keepUsers = [];
}
get activeUsers() {
console.log("current users", this.users);
return Object.values(this.users);
}
async fetchData() {
console.log("start fetch", this.temp, typeof this.temp);
if (this.temp) { return; }
const result = await gRequest(QUERY_ROOM, {
id: this.id,
});
console.log("data fetched", result.portal_room);
if (result && result.portal_room[0]) {
const [{ creator, active, members, link, name, windows }] = result.portal_room;
this.name = name;
this.link = link;
this.active = active;
this.creator = creator;
this.members = members;
this.windows = windows;
// 激活当前房间
// if (!active) {
// this.setActive();
// }
}
}
saveToDatabase() {
// 写回数据库
let roomName = this.keepUsers.map(u => u.username).join(",");
let creator = (this.keepUsers.find(u => u.creator == true) || { username: "" }).username;
let { id, link } = this;
const params = { creator, host: creator, name: roomName, id, link, members: this.keepUsers };
gRequest(NEW_ROOM, params).then((wtf) => {
console.log(wtf);
});
}
// setActive() {
// console.log("active the room");
// // 设置为活跃房间
// gRequest(UPDATE_ACTIVE, { active: true, id: this.id }).then((wtf) => {
// console.log(wtf);
// });
// }
// setInactive() {
// // 设置为不活跃房间
// gRequest(UPDATE_ACTIVE, { active: false, id: this.id }).then((wtf) => {
// console.log(wtf);
// });
// }
appendMember(member) {
// 更新参与者
if (!this.members) return;
// 如果没有uid,就pass掉
if (!member.uid) return;
const filterd = this.members.filter((m) => sameUser(m, member));
console.log("filterd", filterd);
if (filterd.length == 0) {
// append member
console.log("append member", member);
gRequest(UPDATE_MEMBERS, {
member: filterMemberFields(member),
id: this.id,
});
}
}
addActiveUser(sid, user) {
console.log("add active user", user);
// 新增活跃用户
this.users[sid] = user;
return user;
}
beHost(sid, enable = true) {
const tmps = Object.entries(this.users);
const newUsers = enable ? Object.fromEntries(tmps.map(([key, user]) => {
return [key, { ...user, host: key == sid, follow: key !== sid }];
})) : Object.fromEntries(tmps.map(([key, user]) => {
return [key, { ...user, host: false, follow: false }];
}));
this.users = { ...newUsers };
}
updateUser(sid, params) {
if (this.users[sid]) {
this.users[sid] = { ...this.users[sid], ...params };
}
}
addKeepUser(sid) {
// 该用户选择保留临时房间
const avtiveUser = this.activeUsers.find(u => u.id == sid);
const filterd = this.keepUsers.filter((u) => sameUser(u, avtiveUser));
if (filterd.length == 0) {
this.keepUsers = [...this.keepUsers, avtiveUser];
}
}
destory() {
if (this.temp && this.keepUsers.length) {
// 临时room,走一下存储逻辑
console.log("save to db");
this.saveToDatabase();
} else {
// 非临时room,设置为非活跃状态
// this.setInactive();
}
// 释放掉
Rooms[this.id] = null;
}
removeActiveUser(sid) {
delete this.users[sid];
// 房间没人了
if (this.activeUsers.length == 0) {
this.destory();
}
}
}
const getRoomInstance = async ({ id, temp, link }) => {
if (!Rooms[id]) {
Rooms[id] = new Room({ id, temp, link });
}
await Rooms[id].fetchData();
return Rooms[id];
};
module.exports = getRoomInstance;
module.exports.Rooms = Rooms;