Skip to content

Commit

Permalink
feat(Members): implement member removal
Browse files Browse the repository at this point in the history
  • Loading branch information
striker4150 committed Jun 3, 2022
1 parent c75676e commit 93222a1
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export { default as BaseManager } from './managers/BaseManager'
export { default as UserManager } from './managers/UserManager'
export { default as MessageLikeManager } from './managers/MessageLikeManager'
export { default as Member } from './structures/Member'
export { default as FormerMember } from './structures/FormerMember'
export { default as FormerMember, FormerMemberState } from './structures/FormerMember'
export { default as FormerMemberManager } from './managers/FormerMemberManager'
export { default as MemberManager } from './managers/MemberManager'
export { default as Channel, ChannelType, SendableChannelInterface } from './structures/Channel'
Expand Down
3 changes: 3 additions & 0 deletions src/managers/BaseManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ export default abstract class BaseManager<T extends Base, TCtor extends new (...
this.cache.set(obj.id, obj)
return obj
}
_remove(data: T): boolean {
return this.cache.delete(data.id)
}
}
16 changes: 12 additions & 4 deletions src/managers/MemberManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import type { PostMemberRemoveResponse } from 'groupme-api-types'
import type { BaseGroup, Client, Collection } from '..'
import { BaseManager, FormerMemberManager, Member } from '..'
import { BaseManager, FormerMember, FormerMemberManager, FormerMemberState, Member } from '..'

interface MemberManagerInterface {
add(id: string): Promise<Member>
add(ids: string[]): Promise<Collection<string, Member>>
remove(member: Member): Promise<this>
remove(member: Member): Promise<FormerMember>
}

export default class MemberManager extends BaseManager<Member, typeof Member> implements MemberManagerInterface {
Expand All @@ -21,7 +22,14 @@ export default class MemberManager extends BaseManager<Member, typeof Member> im
add(ids: string | string[]): Promise<Member> | Promise<Collection<string, Member>> {
throw new Error('Method not implemented.')
}
remove(member: Member): Promise<this> {
throw new Error('Method not implemented.')
async remove(member: Member): Promise<FormerMember> {
await this.client.rest.api<PostMemberRemoveResponse>(
'POST',
`groups/${this.group.id}/members/${member.memberID}/remove`,
)
const formerMember = new FormerMember(this.client, this.group, member.user, member, FormerMemberState.Removed)
this.former._upsert(formerMember)
this.group.members._remove(member)
return formerMember
}
}
24 changes: 19 additions & 5 deletions src/structures/FormerMember.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import type { APIMember } from 'groupme-api-types'
import type { Client, Group, User } from '..'
import type { BaseGroup, Client, User } from '..'
import { Member } from '..'

enum State {
export enum FormerMemberState {
Exited = 'exited',
ExitedRemoved = 'exited_removed',
Removed = 'removed',
Expand All @@ -14,9 +14,23 @@ interface FormerMemberInterface {
}

export default class FormerMember extends Member implements FormerMemberInterface {
state: State
constructor(client: Client, group: Group, user: User, data: APIMember, state: State) {
super(client, group, user, data)
state: FormerMemberState
constructor(client: Client, group: BaseGroup, user: User, data: APIMember, state: FormerMemberState)
constructor(client: Client, group: BaseGroup, user: User, data: Member, state: FormerMemberState)
constructor(client: Client, group: BaseGroup, user: User, data: APIMember | Member, state: FormerMemberState) {
let memberData = data
if (memberData instanceof Member) {
memberData = {
id: memberData.memberID,
image_url: memberData.image_url,
muted: memberData.muted,
name: memberData.user.name,
nickname: memberData.nickname,
roles: memberData.roles,
user_id: memberData.user.id,
}
}
super(client, group, user, memberData)
this.state = state
}
ban(): Promise<this> {
Expand Down
11 changes: 8 additions & 3 deletions src/structures/Member.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { APIMember } from 'groupme-api-types'
import type { BaseGroup, Client, User } from '..'
import { MemberRole } from 'groupme-api-types'
import type { BaseGroup, Client, FormerMember, User } from '..'
import { Base } from '..'

interface MemberInterface {}
Expand All @@ -12,7 +13,7 @@ export default class Member extends Base implements MemberInterface {
image_url: string | null
muted: boolean
// autokicked: boolean
roles: ('admin' | 'owner' | 'user')[]
roles: MemberRole[]

constructor(client: Client, group: BaseGroup, user: User, data: APIMember) {
super(client, user.id)
Expand Down Expand Up @@ -41,7 +42,7 @@ export default class Member extends Base implements MemberInterface {
}

get isAdmin(): boolean {
return this.roles.includes('admin') || this.isOwner
return this.roles.includes(MemberRole.Admin) || this.isOwner
}

get isOwner(): boolean {
Expand Down Expand Up @@ -71,4 +72,8 @@ export default class Member extends Base implements MemberInterface {
get canTransferGroup(): boolean {
return this.isOwner
}

async remove(): Promise<FormerMember> {
return this.group.members.remove(this)
}
}

0 comments on commit 93222a1

Please sign in to comment.