forked from bestony/neshouse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.js
109 lines (106 loc) · 4.09 KB
/
admin.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
AV.init({
appId: AVAPPID,
appKey: AVAPPKEY,
});
function admin() {
return {
// Control State
isShowShareArea: false,
// Info State
roomName: null,
roomId: null,
userId: null,
shareText: "",
nickName: null,
userName: null,
usernameExist: false,
async init() {
username = window.localStorage.getItem("username");
if (username) {
this.usernameExist = true;
} else {
this.usernameExist = false;
}
},
loginAsAdmin() {
var win = window.open(BASEURL + "?invite=" + this.roomId + "&user=" + this.userName, '_blank');
win.focus();
},
generateShareText() {
return "I've created a chat room on NESHouse. Copy & open the following link in your browser and let's chat! " + BASEURL + "?invite=" + this.roomId
},
copyLink() {
if (!this.roomId || !this.userId) {
alert("There was an error creating the room, please refresh this page.");
return;
}
;
navigator.permissions.query({ name: "clipboard-write" }).then(result => {
if (result.state == "granted" || result.state == "prompt") {
navigator.clipboard.writeText(this.generateShareText())
} else {
alert("Your browser forbids text copying. Please select and copy the text in the box yourself.")
console.log(result.state)
}
});
},
createNewUser() {
window.localStorage.removeItem("username");
this.usernameExist = false;
},
async createRoom() {
// Generate Username
let username = window.localStorage.getItem("username");
// Sign up a user
const user = new AV.User();
let userObj = null;
try {
if (!username) {
if (!this.nickName) {
alert("You have to choose a nickname. :)")
return
}
username = Math.floor(Math.random() * 10000).toString(5) + Math.floor(Math.random() * 10000).toString(5);
localStorage.setItem("username", username);
user.setUsername(username);
user.setPassword(username);
user.set('nickname', this.nickName);
userObj = await user.signUp()
} else {
userObj = await AV.User.logIn(username, username);
if (this.nickName) {
user.set('nickname', this.nickName);
user.save();
}
}
// record user state
this.userId = userObj.id;
this.userName = username;
try {
// Create Room and make currentUser as Admin
if (!this.roomName) {
alert("You have to name the room. :)")
return
}
const Room = AV.Object.extend('Room');
const room = new Room();
room.set("title", this.roomName);
room.set("adminUser", userObj.id);
let roomObj = await room.save();
// record Room State
this.roomId = roomObj.id;
console.log("roomID", roomObj.id);
// Show Share Area
this.isShowShareArea = true;
this.shareText = this.generateShareText();
} catch (e) {
alert("Failed to create a chat room, please shoot an email to [email protected].");
console.log(e);
}
} catch (e) {
alert("Failed to register the account, please shoot an email to [email protected].");
console.log(e);
}
}
}
}