2020-07-15 16:28:34 +00:00
|
|
|
const axios = require('axios').default;
|
|
|
|
const data = require('../data');
|
|
|
|
|
|
|
|
let server = data.server
|
|
|
|
|
|
|
|
const rocketchat = axios.create({
|
|
|
|
baseURL: `${server}/api/v1/`,
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json;charset=UTF-8',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
const login = async (username, password) => {
|
|
|
|
console.log(`Logging in as user ${username}`)
|
|
|
|
const response = await rocketchat.post('login', {
|
|
|
|
"user": username,
|
|
|
|
"password": password
|
|
|
|
})
|
|
|
|
const userId = response.data.data.userId
|
|
|
|
const authToken = response.data.data.authToken
|
|
|
|
rocketchat.defaults.headers.common['X-User-Id'] = userId
|
|
|
|
rocketchat.defaults.headers.common['X-Auth-Token'] = authToken
|
2021-04-19 14:31:43 +00:00
|
|
|
return { authToken, userId };
|
2020-07-15 16:28:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const createUser = async (username, password, name, email) => {
|
|
|
|
console.log(`Creating user ${username}`)
|
|
|
|
try {
|
|
|
|
await rocketchat.post('users.create', {
|
|
|
|
"username": username,
|
|
|
|
"password": password,
|
|
|
|
"name": name,
|
|
|
|
"email": email
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
console.log(JSON.stringify(error))
|
|
|
|
throw "Failed to create user"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createChannelIfNotExists = async (channelname) => {
|
2020-07-22 16:32:21 +00:00
|
|
|
console.log(`Creating public channel ${channelname}`)
|
2020-07-15 16:28:34 +00:00
|
|
|
try {
|
2020-12-01 17:30:39 +00:00
|
|
|
const room = await rocketchat.post('channels.create', {
|
2020-07-15 16:28:34 +00:00
|
|
|
"name": channelname
|
|
|
|
})
|
2020-12-01 17:30:39 +00:00
|
|
|
return room
|
2020-07-15 16:28:34 +00:00
|
|
|
} catch (createError) {
|
|
|
|
try { //Maybe it exists already?
|
2020-12-01 17:30:39 +00:00
|
|
|
const room = rocketchat.get(`channels.info?roomName=${channelname}`)
|
|
|
|
return room
|
2020-07-15 16:28:34 +00:00
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(createError))
|
|
|
|
console.log(JSON.stringify(infoError))
|
2020-07-22 16:32:21 +00:00
|
|
|
throw "Failed to find or create public channel"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const createGroupIfNotExists = async (groupname) => {
|
|
|
|
console.log(`Creating private group ${groupname}`)
|
|
|
|
try {
|
|
|
|
await rocketchat.post('groups.create', {
|
|
|
|
"name": groupname
|
|
|
|
})
|
|
|
|
} catch (createError) {
|
|
|
|
try { //Maybe it exists already?
|
2020-11-30 20:00:31 +00:00
|
|
|
await rocketchat.get(`groups.info?roomName=${groupname}`)
|
2020-07-22 16:32:21 +00:00
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(createError))
|
|
|
|
console.log(JSON.stringify(infoError))
|
|
|
|
throw "Failed to find or create private group"
|
2020-07-15 16:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-01 17:30:39 +00:00
|
|
|
const changeChannelJoinCode = async (roomId, joinCode) => {
|
|
|
|
console.log(`Changing channel Join Code ${roomId}`)
|
|
|
|
try {
|
|
|
|
await rocketchat.post('method.call/saveRoomSettings', {
|
|
|
|
message: JSON.stringify({
|
|
|
|
method: 'saveRoomSettings',
|
|
|
|
params: [
|
|
|
|
roomId,
|
|
|
|
{ joinCode }
|
|
|
|
]
|
|
|
|
})
|
|
|
|
})
|
|
|
|
} catch (createError) {
|
|
|
|
console.log(JSON.stringify(createError))
|
|
|
|
throw "Failed to create protected channel"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 14:31:43 +00:00
|
|
|
const sendMessage = async (user, channel, msg) => {
|
|
|
|
console.log(`Sending message to ${channel}`)
|
2020-11-30 20:00:31 +00:00
|
|
|
try {
|
|
|
|
await login(user.username, user.password);
|
2021-04-19 14:31:43 +00:00
|
|
|
await rocketchat.post('chat.postMessage', { channel, msg });
|
2020-11-30 20:00:31 +00:00
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(infoError))
|
|
|
|
throw "Failed to find or create private group"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:28:34 +00:00
|
|
|
const setup = async () => {
|
|
|
|
await login(data.adminUser, data.adminPassword)
|
|
|
|
|
|
|
|
for (var userKey in data.users) {
|
|
|
|
if (data.users.hasOwnProperty(userKey)) {
|
|
|
|
const user = data.users[userKey]
|
|
|
|
await createUser(user.username, user.password, user.username, user.email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var channelKey in data.channels) {
|
|
|
|
if (data.channels.hasOwnProperty(channelKey)) {
|
|
|
|
const channel = data.channels[channelKey]
|
2020-12-01 17:30:39 +00:00
|
|
|
const { data: { channel: { _id } } } = await createChannelIfNotExists(channel.name)
|
|
|
|
|
|
|
|
if (channel.joinCode) {
|
|
|
|
await changeChannelJoinCode(_id, channel.joinCode);
|
|
|
|
}
|
2020-07-15 16:28:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-22 16:32:21 +00:00
|
|
|
await login(data.users.regular.username, data.users.regular.password)
|
|
|
|
|
|
|
|
for (var groupKey in data.groups) {
|
|
|
|
if (data.groups.hasOwnProperty(groupKey)) {
|
|
|
|
const group = data.groups[groupKey]
|
|
|
|
await createGroupIfNotExists(group.name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 16:28:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-31 21:01:20 +00:00
|
|
|
const get = (endpoint) => {
|
|
|
|
console.log(`GET /${ endpoint }`)
|
|
|
|
return rocketchat.get(endpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
const post = (endpoint, body) => {
|
|
|
|
console.log(`POST /${ endpoint } ${ JSON.stringify(body) }`)
|
|
|
|
return rocketchat.post(endpoint, body);
|
|
|
|
}
|
|
|
|
|
2020-11-30 20:00:31 +00:00
|
|
|
module.exports = {
|
2021-04-19 14:31:43 +00:00
|
|
|
setup, sendMessage, get, post, login
|
2020-11-30 20:00:31 +00:00
|
|
|
}
|