Skip to content

Commit

Permalink
Merge pull request #139 from jupiter-project/dev
Browse files Browse the repository at this point in the history
logger for accepting invites
  • Loading branch information
joramirezStackit authored Feb 4, 2022
2 parents d7e6339 + 7059c45 commit d61aab0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
6 changes: 2 additions & 4 deletions controllers/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,8 @@ module.exports = (app, passport, jobs, websocket) => {
logger.info(`======================================================================================\n\n`);
const {channelAddress} = req.body
if(!gu.isWellFormedJupiterAddress(channelAddress)) throw new mError.MetisErrorBadJupiterAddress(`channelAddress: ${channelAddress}`)
const memberAccountProperties = await instantiateGravityAccountProperties(
req.user.passphrase,
req.user.password
);
const memberAccountProperties = req.user.gravityAccountProperties;

chanService.acceptInvitation(memberAccountProperties, channelAddress)
.then(() => {
websocket.of('/chat').to(channelAddress).emit('newMemberChannel');
Expand Down
30 changes: 23 additions & 7 deletions services/chanService.js
Original file line number Diff line number Diff line change
Expand Up @@ -201,29 +201,44 @@ class ChanService {
*/
async getChannelInvitationContainersSentToAccount(sentToMemberAccountProperties) {
logger.verbose(`#### getChannelInvitationContainersSentToAccount(sentToMemberAccountProperties)`);
if(!(sentToMemberAccountProperties instanceof GravityAccountProperties)) {throw new Error('sentToMemberAccountProperties is not well formed')};
if(!(sentToMemberAccountProperties instanceof GravityAccountProperties)) {throw new Error('sentToMemberAccountProperties is not well formed')}
try {
// First: Get All Invitations. @TODO later we'll only get invitations for the past X days.
const recordTag = channelConfig.channelInviteRecord;
const invitationContainers = await this.jupiterTransactionsService.getReadableTaggedMessageContainers(
sentToMemberAccountProperties,
recordTag,
false)
false);
// Second: Only get invitations sent to the member. Not invitations the member sent to others.
const invitationContainersFilteredByTo = invitationContainers.filter( invitationContainer => invitationContainer.message.inviteeAddress === sentToMemberAccountProperties.address);

logger.debug(`Total invitations: ${invitationContainersFilteredByTo.length}`);

// Third: Get All channels the user is a member of
const memberChannels = await this.getMemberChannels(sentToMemberAccountProperties);

logger.debug(`This user belong to ${memberChannels.length} channels`);
// Fourth: Get the difference
const invitationContainersNotInExistingChannels = invitationContainersFilteredByTo.filter(invitationContainer =>
!memberChannels.some(channel=> channel.address === invitationContainer.message.channelRecord.address))
!memberChannels.some(channel=> channel.address === invitationContainer.message.channelRecord.address)
)

logger.debug(`Total not accepted invitations: ${invitationContainersNotInExistingChannels.length}`);

// Fifth: Get the declined Invitations
const declinedTagList = channelConfig.channelInvitationDeclinedList;
const declinedTag = channelConfig.channelInvitationDeclinedRecord;
const declinedInvitationContainers = await this.gravityService.getAllMessageContainersReferencedByList(sentToMemberAccountProperties, declinedTag, declinedTagList)

logger.debug(`Total declined invitations: ${declinedInvitationContainers.length}`);

// Sixth: Remove declined Invitations from invitation list
const filteredInvitationContainers = invitationContainersNotInExistingChannels.filter( invitationContainer =>
! declinedInvitationContainers.some(declinedInvitationContainer => declinedInvitationContainer.transactionId === invitationContainer.transactionId)
)
!declinedInvitationContainers.some(declinedInvitationContainer => declinedInvitationContainer.transactionId === invitationContainer.transactionId)
);

logger.debug(`Total not accepted, not declined invitations: ${filteredInvitationContainers.length}`);

// Seventh: De-dup
const dedupedInvitationContainers = filteredInvitationContainers.reduce((reduced, invitationContainer) => {
if( reduced.some( ic => ic.message.channelRecord.address === invitationContainer.message.channelRecord.address)){
Expand All @@ -233,6 +248,7 @@ class ChanService {
return reduced;
},[]);

logger.debug(`Total after all filters: ${dedupedInvitationContainers.length}`);

// Eighth: Filter out bad channelRecords.
const validInvitationContainers = dedupedInvitationContainers.filter( invitationContainer => {
Expand All @@ -246,7 +262,7 @@ class ChanService {
console.log(`\n\n\n`);
console.log('REMOVEME=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-')
console.log(`invitationContainer:`);
console.log(invitationContainer)
logger.sensitive(`${invitationContainer}`);
console.log('valid.errors')
console.log(valid.errors)
console.log(`REMOVEME=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=--=-=-=-=-=-\n\n\n`)
Expand Down Expand Up @@ -407,7 +423,7 @@ class ChanService {
const invitationContainers = await this.getChannelInvitationContainersSentToAccount(memberAccountProperties);
// Second: confirm the channel being invited to is in the invitation list
const hasInvitation = invitationContainers.some(invitationContainer => invitationContainer.message.channelRecord.address === channelAddress )
if(!hasInvitation){throw new Error('Invitation Not Found')}
if(!hasInvitation){throw new Error(`Invitation Not Found: ${memberAccountProperties.address} invited to ${channelAddress}`)}
//Third: Add the records to both Channel and User Accounts
const invitationContainer = invitationContainers.find(invitationContainer => invitationContainer.message.channelRecord.address === channelAddress)
if(invitationContainer === undefined){throw new Error('Invitation Not Found..')}
Expand Down

0 comments on commit d61aab0

Please sign in to comment.