2022-09-12 14:51:33 +00:00
|
|
|
import axios from 'axios';
|
2021-09-13 20:41:05 +00:00
|
|
|
|
2022-11-09 16:41:39 +00:00
|
|
|
import data, { TDataChannels, TDataGroups, TDataTeams, TDataUsers, TUserRegularChannels } from '../data';
|
2022-09-12 14:51:33 +00:00
|
|
|
import random from './random';
|
2021-05-20 13:50:42 +00:00
|
|
|
|
|
|
|
const TEAM_TYPE = {
|
|
|
|
PUBLIC: 0,
|
|
|
|
PRIVATE: 1
|
|
|
|
};
|
2020-07-15 16:28:34 +00:00
|
|
|
|
2021-07-02 17:39:39 +00:00
|
|
|
const { server } = data;
|
2020-07-15 16:28:34 +00:00
|
|
|
|
|
|
|
const rocketchat = axios.create({
|
2021-09-13 20:41:05 +00:00
|
|
|
baseURL: `${server}/api/v1/`,
|
2021-07-02 17:39:39 +00:00
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json;charset=UTF-8'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const login = async (username: string, password: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Logging in as user ${username}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
const response = await rocketchat.post('login', {
|
|
|
|
user: username,
|
|
|
|
password
|
|
|
|
});
|
|
|
|
const { userId } = response.data.data;
|
|
|
|
const { authToken } = response.data.data;
|
|
|
|
rocketchat.defaults.headers.common['X-User-Id'] = userId;
|
|
|
|
rocketchat.defaults.headers.common['X-Auth-Token'] = authToken;
|
|
|
|
return { authToken, userId };
|
|
|
|
};
|
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const createUser = async (username: string, password: string, name: string, email: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Creating user ${username}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
|
|
|
await rocketchat.post('users.create', {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
name,
|
|
|
|
email
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(JSON.stringify(error));
|
|
|
|
throw new Error('Failed to create user');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const createChannelIfNotExists = async (channelname: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Creating public channel ${channelname}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
|
|
|
const room = await rocketchat.post('channels.create', {
|
|
|
|
name: channelname
|
|
|
|
});
|
|
|
|
return room;
|
|
|
|
} catch (createError) {
|
2021-09-13 20:41:05 +00:00
|
|
|
try {
|
|
|
|
// Maybe it exists already?
|
|
|
|
const room = rocketchat.get(`channels.info?roomName=${channelname}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
return room;
|
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(createError));
|
|
|
|
console.log(JSON.stringify(infoError));
|
|
|
|
throw new Error('Failed to find or create public channel');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const createTeamIfNotExists = async (teamname: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Creating private team ${teamname}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
|
|
|
await rocketchat.post('teams.create', {
|
|
|
|
name: teamname,
|
|
|
|
type: TEAM_TYPE.PRIVATE
|
|
|
|
});
|
|
|
|
} catch (createError) {
|
2021-09-13 20:41:05 +00:00
|
|
|
try {
|
|
|
|
// Maybe it exists already?
|
|
|
|
await rocketchat.get(`teams.info?teamName=${teamname}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(createError));
|
|
|
|
console.log(JSON.stringify(infoError));
|
|
|
|
throw new Error('Failed to find or create private team');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const createGroupIfNotExists = async (groupname: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Creating private group ${groupname}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
|
|
|
await rocketchat.post('groups.create', {
|
|
|
|
name: groupname
|
|
|
|
});
|
|
|
|
} catch (createError) {
|
2021-09-13 20:41:05 +00:00
|
|
|
try {
|
|
|
|
// Maybe it exists already?
|
|
|
|
await rocketchat.get(`groups.info?roomName=${groupname}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(createError));
|
|
|
|
console.log(JSON.stringify(infoError));
|
|
|
|
throw new Error('Failed to find or create private group');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const changeChannelJoinCode = async (roomId: string, joinCode: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Changing channel Join Code ${roomId}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
|
|
|
await rocketchat.post('method.call/saveRoomSettings', {
|
|
|
|
message: JSON.stringify({
|
2022-08-25 19:53:19 +00:00
|
|
|
msg: 'method',
|
|
|
|
id: random(10),
|
2021-07-02 17:39:39 +00:00
|
|
|
method: 'saveRoomSettings',
|
2021-09-13 20:41:05 +00:00
|
|
|
params: [roomId, { joinCode }]
|
2021-07-02 17:39:39 +00:00
|
|
|
})
|
|
|
|
});
|
|
|
|
} catch (createError) {
|
|
|
|
console.log(JSON.stringify(createError));
|
|
|
|
throw new Error('Failed to create protected channel');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const sendMessage = async (user: { username: string; password: string }, channel: string, msg: string, tmid?: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`Sending message to ${channel}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
try {
|
|
|
|
await login(user.username, user.password);
|
2021-10-26 16:11:50 +00:00
|
|
|
const response = await rocketchat.post('chat.postMessage', { channel, msg, tmid });
|
|
|
|
return response.data;
|
2021-07-02 17:39:39 +00:00
|
|
|
} catch (infoError) {
|
|
|
|
console.log(JSON.stringify(infoError));
|
|
|
|
throw new Error('Failed to find or create private group');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
const setup = async () => {
|
2021-07-02 17:39:39 +00:00
|
|
|
await login(data.adminUser, data.adminPassword);
|
|
|
|
|
|
|
|
for (const userKey in data.users) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(data.users, userKey)) {
|
2022-09-12 14:51:33 +00:00
|
|
|
const user = data.users[userKey as TDataUsers];
|
2021-07-02 17:39:39 +00:00
|
|
|
await createUser(user.username, user.password, user.username, user.email);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const channelKey in data.channels) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(data.channels, channelKey)) {
|
2022-09-12 14:51:33 +00:00
|
|
|
const channel = data.channels[channelKey as TDataChannels];
|
2021-09-13 20:41:05 +00:00
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
channel: { _id }
|
|
|
|
}
|
|
|
|
} = await createChannelIfNotExists(channel.name);
|
2021-07-02 17:39:39 +00:00
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
if ('joinCode' in channel) {
|
2021-07-02 17:39:39 +00:00
|
|
|
await changeChannelJoinCode(_id, channel.joinCode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await login(data.users.regular.username, data.users.regular.password);
|
|
|
|
|
2022-11-04 17:09:58 +00:00
|
|
|
for (const channelKey in data.userRegularChannels) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(data.userRegularChannels, channelKey)) {
|
2022-11-09 16:41:39 +00:00
|
|
|
const channel = data.userRegularChannels[channelKey as TUserRegularChannels];
|
|
|
|
await createChannelIfNotExists(channel.name);
|
2022-11-04 17:09:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-02 17:39:39 +00:00
|
|
|
for (const groupKey in data.groups) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(data.groups, groupKey)) {
|
2022-09-12 14:51:33 +00:00
|
|
|
const group = data.groups[groupKey as TDataGroups];
|
2021-07-02 17:39:39 +00:00
|
|
|
await createGroupIfNotExists(group.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const teamKey in data.teams) {
|
|
|
|
if (Object.prototype.hasOwnProperty.call(data.teams, teamKey)) {
|
2022-09-12 14:51:33 +00:00
|
|
|
const team = data.teams[teamKey as TDataTeams];
|
2021-07-02 17:39:39 +00:00
|
|
|
await createTeamIfNotExists(team.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-07-15 16:28:34 +00:00
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const get = (endpoint: string) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`GET /${endpoint}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
return rocketchat.get(endpoint);
|
|
|
|
};
|
2021-03-31 21:01:20 +00:00
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
const post = (endpoint: string, body: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
console.log(`POST /${endpoint} ${JSON.stringify(body)}`);
|
2021-07-02 17:39:39 +00:00
|
|
|
return rocketchat.post(endpoint, body);
|
|
|
|
};
|
2021-03-31 21:01:20 +00:00
|
|
|
|
2022-09-12 14:51:33 +00:00
|
|
|
export { setup, sendMessage, get, post, login };
|