Commit 8a8ea97d authored by Prajval Raval's avatar Prajval Raval

adding api wraps

parent f3878704
......@@ -6,4 +6,4 @@
"merge": {}
}
}
}
}
\ No newline at end of file
......@@ -59,3 +59,5 @@ typings/
# next.js build output
.next
.DS_Store
hooks/
\ No newline at end of file
......@@ -6,4 +6,28 @@
module.exports = {
postmessageurl: '/api/v1/chat.postMessage',
channelmessageurl: '/api/v1/channels.messages?roomName=',
loginUrl: '/api/v1/login',
meUrl: '/api/v1/me',
channellisturl: '/api/v1/channels.list.joined',
createchannelurl: '/api/v1/channels.create',
deletechannelurl: '/api/v1/channels.delete',
channelinfourl: '/api/v1/channels.info?roomName=',
userinfourl: '/api/v1/users.info?username=',
addallurl: '/api/v1/channels.addAll',
makemoderatorurl: '/api/v1/channels.addModerator',
addownerurl: '/api/v1/channels.addOwner',
archivechannelurl: '/api/v1/channels.archive',
counterurl: '/api/v1/channels.counters?roomName=',
markasreadurl: '/api/v1/subscriptions.read',
generatetokenurl: '/api/v1/users.generatePersonalAccessToken',
removetokenurl: '/api/v1/users.removePersonalAccessToken',
anonymousReadUrl: '/api/v1/channels.anonymousread?roomName=',
creategroupurl: '/api/v1/groups.create',
deletegroupurl: '/api/v1/groups.delete',
groupinfourl: '/api/v1/groups.info?roomName=',
addgroupmoderatorurl: '/api/v1/groups.addModerator',
addgroupownerurl: '/api/v1/groups.addOwner',
groupmessageurl: '/api/v1/groups.messages?roomId=',
groupcounterurl: '/api/v1/groups.counters?roomId=',
createimurl: '/api/v1/im.create',
};
......@@ -2,7 +2,7 @@ const axios = require('axios');
const apiEndpoints = require('./apiEndpoints');
const envVariables = require('./config');
const removeWhitespace = require('remove-whitespace');
const emojiTranslate = require('moji-translate');
const Jargon = require('@jargon/alexa-skill-sdk');
const {
......@@ -44,8 +44,6 @@ const getData = async (code) =>
});
const postMessage = async (channelName, message, headers, serverurl) =>
await axios
.post(
......@@ -73,6 +71,17 @@ const postMessage = async (channelName, message, headers, serverurl) =>
}
});
function slotValue(slot) {
let { value } = slot;
const resolution = (slot.resolutions && slot.resolutions.resolutionsPerAuthority && slot.resolutions.resolutionsPerAuthority.length > 0) ? slot.resolutions.resolutionsPerAuthority[0] : null;
if (resolution && resolution.status.code === 'ER_SUCCESS_MATCH') {
const resolutionValue = resolution.values[0].value;
value = resolutionValue.name;
}
return value;
}
function getStaticAndDynamicSlotValuesFromSlot(slot) {
const result = {
......@@ -118,6 +127,12 @@ function replaceWhitespacesFunc(str) {
return removeWhitespace(str);
}
function emojiTranslateFunc(str) {
const onlyEmoji = true;
return emojiTranslate.translate(str, onlyEmoji);
}
const getLastMessageType = async (channelName, headers, serverurl) =>
await axios
.get(`${ serverurl }${ apiEndpoints.channelmessageurl }${ channelName }`, {
......@@ -189,14 +204,634 @@ const getLastMessageFileDowloadURL = async (fileurl, headers) =>
console.log(err.message);
});
// Module Export of Functions
const createChannel = async (channelName, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.createchannelurl }`, {
name: channelName,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('CREATE_CHANNEL.SUCCESS', {
channelName,
});
} else {
return ri('CREATE_CHANNEL.ERROR', {
channelName,
});
}
})
.catch((err) => {
console.log(err.message);
if (
err.response.data.errorType === 'error-duplicate-channel-name'
) {
return ri('CREATE_CHANNEL.ERROR_DUPLICATE_NAME', {
channelName,
});
} else if (
err.response.data.errorType === 'error-invalid-room-name'
) {
return ri('CREATE_CHANNEL.ERROR_INVALID_NAME', {
channelName,
});
} else if (err.response.status === 401) {
return ri('CREATE_CHANNEL.AUTH_ERROR');
} else {
return ri('CREATE_CHANNEL.ERROR', {
channelName,
});
}
});
const deleteChannel = async (channelName, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.deletechannelurl }`, {
roomName: channelName,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('DELETE_CHANNEL.SUCCESS', {
channelName,
});
} else {
return ri('DELETE_CHANNEL.ERROR', {
channelName,
});
}
})
.catch((err) => {
console.log(err.message);
if (err.response.data.errorType === 'error-room-not-found') {
return ri('DELETE_CHANNEL.ERROR_NOT_FOUND', {
channelName,
});
} else if (err.response.status === 401) {
return ri('DELETE_CHANNEL.AUTH_ERROR');
} else {
return ri('DELETE_CHANNEL.ERROR', {
channelName,
});
}
});
const getRoomId = async (channelName, headers, serverurl) =>
await axios
.get(`${ serverurl }${ apiEndpoints.channelinfourl }${ channelName }`, {
headers,
})
.then((res) => res.data)
.then((res) => `${ res.channel._id }`)
.catch((err) => {
console.log(err.message);
});
const addAll = async (channelName, roomid, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.addallurl }`, {
roomId: roomid,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('ADD_ALL_TO_CHANNEL.SUCCESS', {
channelName,
});
} else {
return ri('ADD_ALL_TO_CHANNEL.ERROR');
}
})
.catch((err) => {
console.log(err.message);
if (err.response.status === 401) {
return ri('ADD_ALL_TO_CHANNEL.AUTH_ERROR');
} else {
return ri('ADD_ALL_TO_CHANNEL.ERROR_NOT_FOUND', {
channelName,
});
}
});
const getUnreadCounter = async (channelName, headers, serverurl) =>
await axios
.get(`${ serverurl }${ apiEndpoints.counterurl }${ channelName }`, {
headers,
})
.then((res) => res.data)
.then((res) => `${ res.unreads }`)
.catch((err) => {
console.log(err.message);
});
const channelUnreadMessages = async (channelName, unreadCount, headers, serverurl) =>
await axios
.get(`${ serverurl }${ apiEndpoints.channelmessageurl }${ channelName }`, {
headers,
})
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
if (unreadCount === 0) {
return ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.NO_MESSAGE');
} else {
const msgs = [];
for (let i = 0; i <= unreadCount - 1; i++) {
msgs.push(`${ res.messages[i].u.username } says, ${ res.messages[i].msg } <break time="0.7s"/> `);
}
const responseString = msgs.join(', ');
const finalMsg = ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.MESSAGE', {
respString: responseString,
unread: unreadCount,
});
console.log(finalMsg);
return finalMsg;
}
} else {
return ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.ERROR', {
channelName,
});
}
})
.catch((err) => {
console.log(err.message);
if (err.response.data.errorType === 'error-room-not-found') {
return ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.ERROR_NOT_FOUND', {
channelName,
});
} else if (err.response.status === 401) {
return ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.AUTH_ERROR');
} else {
return ri('GET_UNREAD_MESSAGES_FROM_CHANNEL.ERROR', {
channelName,
});
}
});
function replaceWhitespacesDots(str) {
return str.replace(/\s/ig, '.');
}
const getUserId = async (userName, headers, serverurl) =>
await axios
.get(`${ serverurl }${ apiEndpoints.userinfourl }${ userName }`, {
headers,
})
.then((res) => res.data)
.then((res) => `${ res.user._id }`)
.catch((err) => {
console.log(err.message);
});
const makeModerator = async (userName, channelName, userid, roomid, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.makemoderatorurl }`, {
userId: userid,
roomId: roomid,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('MAKE_MODERATOR.SUCCESS', {
userName,
channelName,
});
} else {
return ri('MAKE_MODERATOR.ERROR');
}
})
.catch((err) => {
console.log(err.message);
if (err.response.status === 401) {
return ri('MAKE_MODERATOR.AUTH_ERROR');
} else {
return ri('MAKE_MODERATOR.ERROR_NOT_FOUND', {
channelName,
});
}
});
const addOwner = async (userName, channelName, userid, roomid, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.addownerurl }`, {
userId: userid,
roomId: roomid,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('ADD_OWNER.SUCCESS', {
userName,
channelName,
});
} else {
return ri('ADD_OWNER.ERROR');
}
})
.catch((err) => {
console.log(err.message);
if (err.response.status === 401) {
return ri('ADD_OWNER.AUTH_ERROR');
} else {
return ri('ADD_OWNER.ERROR_NOT_FOUND', {
channelName,
});
}
});
const archiveChannel = async (channelName, roomid, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.archivechannelurl }`, {
roomId: roomid,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('ARCHIVE_CHANNEL.SUCCESS', {
channelName,
});
} else {
return ri('ARCHIVE_CHANNEL.ERROR');
}
})
.catch((err) => {
console.log(err.message);
if (err.response.status === 401) {
return ri('ARCHIVE_CHANNEL.AUTH_ERROR');
} else {
return ri('ARCHIVE_CHANNEL.ERROR_NOT_FOUND', {
channelName,
});
}
});
const createGroup = async (channelName, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.creategroupurl }`, {
name: channelName,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('CREATE_CHANNEL.SUCCESS', {
channelName,
});
} else {
return ri('CREATE_CHANNEL.ERROR', {
channelName,
});
}
})
.catch((err) => {
console.log(err.message);
if (err.response.data.errorType === 'error-duplicate-channel-name') {
return ri('CREATE_CHANNEL.ERROR_DUPLICATE_NAME', {
channelName,
});
} else if (err.response.status === 401) {
return ri('CREATE_CHANNEL.AUTH_ERROR');
} else if (err.response.data.errorType === 'error-invalid-room-name') {
return ri('CREATE_CHANNEL.ERROR_INVALID_NAME', {
channelName,
});
} else {
return ri('CREATE_CHANNEL.ERROR', {
channelName,
});
}
});
const deleteGroup = async (channelName, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.deletegroupurl }`, {
roomName: channelName,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('DELETE_CHANNEL.SUCCESS', {
channelName,
});
} else {
return ri('DELETE_CHANNEL.ERROR', {
channelName,
});
}
})
.catch((err) => {
console.log(err.message);
if (err.response.data.errorType === 'error-room-not-found') {
return ri('DELETE_CHANNEL.ERROR_NOT_FOUND', {
channelName,
});
} else if (err.response.status === 401) {
return ri('DELETE_CHANNEL.AUTH_ERROR');
} else {
return ri('DELETE_CHANNEL.ERROR', {
channelName,
});
}
});
const getGroupId = async (channelName, headers, serverurl) =>
await axios
.get(`${ serverurl }${ apiEndpoints.groupinfourl }${ channelName }`, {
headers,
})
.then((res) => res.data)
.then((res) => `${ res.group._id }`)
.catch((err) => {
console.log(err.message);
});
const addGroupModerator = async (userName, channelName, userid, roomid, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.addgroupmoderatorurl }`, {
userId: userid,
roomId: roomid,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('MAKE_MODERATOR.SUCCESS', {
userName,
channelName,
});
} else {
return ri('MAKE_MODERATOR.ERROR');
}
})
.catch((err) => {
console.log(err.message);
if (err.response.status === 401) {
return ri('MAKE_MODERATOR.AUTH_ERROR');
} else {
return ri('MAKE_MODERATOR.ERROR_NOT_FOUND', {
channelName,
});
}
});
const addGroupOwner = async (userName, channelName, userid, roomid, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.addgroupownerurl }`, {
userId: userid,
roomId: roomid,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('ADD_OWNER.SUCCESS', {
userName,
channelName,
});
} else {
return ri('ADD_OWNER.ERROR');
}
})
.catch((err) => {
console.log(err.message);
if (err.response.status === 401) {
return ri('ADD_OWNER.AUTH_ERROR');
} else {
return ri('ADD_OWNER.ERROR_NOT_FOUND', {
channelName,
});
}
});
const postGroupMessage = async (roomid, message, headers, serverurl) =>
await axios
.post(
`${ serverurl }${ apiEndpoints.postmessageurl }`, {
roomId: roomid,
text: message,
}, {
headers,
}
)
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('POST_MESSAGE.SUCCESS');
} else {
return ri('POST_MESSAGE.ERROR');
}
})
.catch((err) => {
console.log(err.message);
if (err.response.status === 401) {
return ri('POST_MESSAGE.AUTH_ERROR');
} else {
return ri('POST_MESSAGE.ERROR');
}
});
const groupLastMessage = async (channelName, roomid, headers, serverurl) =>
await axios
.get(`${ serverurl }${ apiEndpoints.groupmessageurl }${ roomid }`, {
headers,
})
.then((res) => res.data)
.then((res) => {
if (res.success === true) {
return ri('GET_LAST_MESSAGE_FROM_CHANNEL.SUCCESS', {
name: res.messages[0].u.username,
message: res.messages[0].msg,
});
} else {
return ri('GET_LAST_MESSAGE_FROM_CHANNEL.ERROR', {
channelName,
});
}
})
.catch((err) => {
console.log(err.message);
if (err.response.data.errorType === 'error-room-not-found') {
return ri('GET_LAST_MESSAGE_FROM_CHANNEL.ERROR_NOT_FOUND', {
channelName,
});
} else if (err.response.status === 401) {
return ri('GET_LAST_MESSAGE_FROM_CHANNEL.AUTH_ERROR');
} else {
return ri('GET_LAST_MESSAGE_FROM_CHANNEL.ERROR', {
channelName,
});
}
});
const getGroupUnreadCounter = async (roomid, headers, serverurl) =>
await axios
.get(`${ serverurl } ${ apiEndpoints.groupcounterurl }${ roomid }`, {
headers,
})
.