-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #402 from xmtp/np/group-members
- Loading branch information
Showing
9 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
android/src/main/java/expo/modules/xmtpreactnativesdk/wrappers/MemberWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package expo.modules.xmtpreactnativesdk.wrappers | ||
|
||
import com.google.gson.GsonBuilder | ||
import org.xmtp.android.library.libxmtp.Member | ||
import org.xmtp.android.library.libxmtp.PermissionLevel | ||
|
||
class MemberWrapper { | ||
companion object { | ||
fun encodeToObj(member: Member): Map<String, Any> { | ||
val permissionString = when (member.permissionLevel) { | ||
PermissionLevel.MEMBER -> "member" | ||
PermissionLevel.ADMIN -> "admin" | ||
PermissionLevel.SUPER_ADMIN -> "super_admin" | ||
} | ||
return mapOf( | ||
"inboxId" to member.inboxId, | ||
"addresses" to member.addresses, | ||
"permissionLevel" to permissionString | ||
) | ||
} | ||
|
||
fun encode(member: Member): String { | ||
val gson = GsonBuilder().create() | ||
val obj = encodeToObj(member) | ||
return gson.toJson(obj) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// MemberWrapper.swift | ||
// XMTPReactNative | ||
// | ||
// Created by Naomi Plasterer on 6/3/24. | ||
// | ||
|
||
import Foundation | ||
import XMTP | ||
|
||
// Wrapper around XMTP.Group to allow passing these objects back into react native. | ||
struct MemberWrapper { | ||
static func encodeToObj(_ member: XMTP.Member) throws -> [String: Any] { | ||
let permissionString = switch member.permissionLevel { | ||
case .Member: | ||
"member" | ||
case .Admin: | ||
"admin" | ||
case .SuperAdmin: | ||
"super_admin" | ||
} | ||
return [ | ||
"inboxId": member.inboxId, | ||
"addresses": member.addresses, | ||
"permissionLevel": permissionString, | ||
] | ||
} | ||
|
||
static func encode(_ member: XMTP.Member) throws -> String { | ||
let obj = try encodeToObj(member) | ||
let data = try JSONSerialization.data(withJSONObject: obj) | ||
guard let result = String(data: data, encoding: .utf8) else { | ||
throw WrapperError.encodeError("could not encode member") | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export class Member { | ||
inboxId: string | ||
addresses: string[] | ||
permissionLevel: 'member' | 'admin' | 'super_admin' | ||
|
||
constructor( | ||
inboxId: string, | ||
addresses: string[], | ||
permissionLevel: 'member' | 'admin' | 'super_admin' | ||
) { | ||
this.inboxId = inboxId | ||
this.addresses = addresses | ||
this.permissionLevel = permissionLevel | ||
} | ||
|
||
static from(json: string): Member { | ||
const entry = JSON.parse(json) | ||
return new Member(entry.inboxId, entry.addresses, entry.permissionLevel) | ||
} | ||
} |