Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(iridium): group add member #4429

Merged
merged 5 commits into from
Aug 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions components/views/group/invite/Invite.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import Vue from 'vue'
import { Conversation } from '~/libraries/Iridium/chat/types'
import iridium from '~/libraries/Iridium/IridiumManager'
import { Friend } from '~/libraries/Iridium/friends/types'
import { User } from '~/libraries/Iridium/users/types'

export default Vue.extend({
data() {
return {
recipients: [] as Friend[],
recipients: [] as User[],
isLoading: false,
recipient: '',
error: '',
Expand All @@ -31,20 +31,20 @@ export default Vue.extend({
},
methods: {
async confirm() {
if (!this.recipients.length) {
if (!this.recipients.length || !this.conversationId) {
return
}
this.error = ''
this.isLoading = true

await Promise.all(
this.recipients.map(async (recipient) => {
await iridium.groups.addMemberToGroup(
this.$route.params.id,
recipient.did,
)
}),
).catch((e) => (this.error = e.message))
try {
await iridium.chat.addMembersToGroup(
this.conversationId,
this.recipients.map((user) => user.did),
)
} catch (e) {
this.error = (e as Error).message
}
if (this.error) {
return
}
Expand Down
8 changes: 5 additions & 3 deletions components/views/navigation/sidebar/list/item/Item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ export default Vue.extend({
accounts: (state) => (state as RootState).accounts,
}),
...mapGetters('settings', ['getTimestamp', 'getDate']),
user(): User | null {
return iridium.users.getUser(this.conversation.participants[0])
user(): User | undefined {
return this.participants.find(
(user) => user.did !== iridium.connector?.id,
)
},
participants(): User[] {
return this.conversation.participants.map((did) => {
Expand Down Expand Up @@ -144,7 +146,7 @@ export default Vue.extend({
this.isLoading = false
},
async leaveGroup() {
iridium.groups.leaveGroup(this.conversation.id)
iridium.chat.leaveGroup(this.conversation.id)
},
/**
* @method openConversation
Expand Down
104 changes: 104 additions & 0 deletions libraries/Iridium/chat/ChatManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ export default class ChatManager extends Emitter<ConversationMessage> {
name: payload.name,
participants,
})
} else if (payload.type === 'add_member') {
await this.appendParticipantsToConversation(payload.id, participants)
} else if (payload.type === 'remove_member') {
await this.removeParticipantsFromConversation(payload.id, participants)
}
}

Expand Down Expand Up @@ -455,6 +459,106 @@ export default class ChatManager extends Emitter<ConversationMessage> {
return id
}

async addMembersToGroup(id: string, newMembers: string[]) {
if (!this.iridium.connector) {
throw new Error('no iridium connector')
}

const conversation = this.getConversation(id)
if (!conversation) {
throw new Error('conversation not found')
}

for (const participant of conversation.participants) {
if (newMembers.includes(participant)) {
throw new Error(`already a member: ${participant}`)
}
}

// notify existing members of conversation about the new members
let event: IridiumConversationEvent = {
id,
type: 'add_member',
participants: newMembers,
}

await this.iridium.connector.publish('/chat/announce', event, {
encrypt: {
recipients: conversation.participants,
},
})

// locally append the new members to our state
this.appendParticipantsToConversation(id, newMembers)

// notify the new membes of the conversation
event = {
id,
type: 'create',
name: conversation.name,
participants: conversation.participants,
}

await this.iridium.connector.publish('/chat/announce', event, {
encrypt: {
recipients: newMembers,
},
})
}

async appendParticipantsToConversation(id: string, participants: string[]) {
const conversation = this.getConversation(id)
if (!conversation) {
throw new Error('conversation not found')
}
conversation.participants.push(...participants)

await this.set(
`/conversations/${id}/participants`,
conversation.participants,
)
}

async leaveGroup(id: string) {
if (!this.iridium.connector) {
throw new Error('no iridium connector')
}

const conversation = this.getConversation(id)
if (!conversation) {
throw new Error('conversation not found')
}

const event: IridiumConversationEvent = {
id,
type: 'remove_member',
participants: [this.iridium.connector.id],
}

await this.iridium.connector.publish('/chat/announce', event, {
encrypt: {
recipients: conversation.participants,
},
})

await this.deleteConversation(id)
}

async removeParticipantsFromConversation(id: string, participants: string[]) {
const conversation = this.getConversation(id)
if (!conversation) {
throw new Error('conversation not found')
}
conversation.participants = conversation.participants.filter(
(did) => !participants.includes(did),
)

await this.set(
`/conversations/${id}/participants`,
conversation.participants,
)
}

async deleteConversation(id: Conversation['id']) {
if (!this.hasConversation(id)) {
return
Expand Down