Skip to content

Commit

Permalink
feat: added user details and subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
y9san9 committed Mar 4, 2024
1 parent 686d58a commit 3dc562e
Show file tree
Hide file tree
Showing 25 changed files with 584 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import app.meetacy.sdk.types.serializable.optional.serializable
import app.meetacy.sdk.types.serializable.user.*
import app.meetacy.sdk.types.url.Url
import app.meetacy.sdk.types.user.SelfUser
import app.meetacy.sdk.types.user.SelfUserDetails
import io.ktor.client.*
import io.ktor.client.request.*
import kotlinx.serialization.Serializable
Expand All @@ -34,8 +35,8 @@ internal class UsersEngine(
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
}.bodyAsSuccess<UserSerializable>()
return GetMeRequest.Response(response.type() as SelfUser)
}.bodyAsSuccess<UserDetailsSerializable>()
return GetMeRequest.Response(response.type() as SelfUserDetails)
}

@Serializable
Expand All @@ -49,7 +50,7 @@ internal class UsersEngine(
apiVersion(request.apiVersion)
token(request.token)
setBody(body)
}.bodyAsSuccess<UserSerializable>()
}.bodyAsSuccess<UserDetailsSerializable>()
return GetUserRequest.Response(response.type())
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import app.meetacy.sdk.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.updates.AuthorizedUpdatesApi
import app.meetacy.sdk.users.AuthorizedSelfUserDetailsRepository
import app.meetacy.sdk.users.AuthorizedSelfUserRepository
import app.meetacy.sdk.users.AuthorizedUsersApi

Expand All @@ -36,11 +37,12 @@ public class AuthorizedMeetacyApi @UnsafeConstructor constructor(
public val notifications: AuthorizedNotificationsApi = AuthorizedNotificationsApi(api = this)
public val updates: AuthorizedUpdatesApi = AuthorizedUpdatesApi(api = this)

public suspend fun getMe(): AuthorizedSelfUserRepository =
AuthorizedSelfUserRepository(
data = base.getMe(token),
public suspend fun getMe(): AuthorizedSelfUserDetailsRepository =
AuthorizedSelfUserDetailsRepository(
data = base.getMe(token).data,
api = this
)

public suspend fun search(location: Location?, prompt: String): List<AuthorizedSearchItemRepository> =
base.search(token, location, prompt).map { AuthorizedSearchItemRepository.of(it.data, api = this) }

Expand Down
10 changes: 8 additions & 2 deletions api/src/commonMain/kotlin/app/meetacy/sdk/MeetacyApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import app.meetacy.sdk.types.annotation.UnsafeConstructor
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.location.Location
import app.meetacy.sdk.types.user.SelfUser
import app.meetacy.sdk.types.user.SelfUserDetails
import app.meetacy.sdk.updates.UpdatesApi
import app.meetacy.sdk.users.SelfUserDetailsRepository
import app.meetacy.sdk.users.UsersApi

public class MeetacyApi(
Expand All @@ -29,8 +31,12 @@ public class MeetacyApi(
public val notifications: NotificationsApi = NotificationsApi(api = this)
public val updates: UpdatesApi = UpdatesApi(api = this)

public suspend fun getMe(token: Token): SelfUser {
return engine.execute(GetMeRequest(token)).me
public suspend fun getMe(token: Token): SelfUserDetailsRepository {
val user = engine.execute(GetMeRequest(token)).me
return SelfUserDetailsRepository(
data = user,
api = this
)
}

public suspend fun search(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package app.meetacy.sdk.engine.requests

import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.user.SelfUser
import app.meetacy.sdk.types.user.SelfUserDetails

public data class GetMeRequest(
val token: Token
): MeetacyRequest<GetMeRequest.Response>{
public data class Response(val me: SelfUser)
public data class Response(val me: SelfUserDetails)
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package app.meetacy.sdk.engine.requests

import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.user.User
import app.meetacy.sdk.types.user.UserDetails
import app.meetacy.sdk.types.user.UserId

public data class GetUserRequest(
val token: Token,
val userId: UserId?
) : MeetacyRequest<GetUserRequest.Response> {
public data class Response(
val user: User
)
public data class Response(val user: UserDetails)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package app.meetacy.sdk.users

import app.meetacy.sdk.AuthorizedMeetacyApi
import app.meetacy.sdk.MeetacyApi
import app.meetacy.sdk.files.FileRepository
import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.user.*

public class AuthorizedRegularUserDetailsRepository(
override val data: RegularUserDetails,
private val api: AuthorizedMeetacyApi
) : AuthorizedUserDetailsRepository {
override val base: RegularUserDetailsRepository get() = RegularUserDetailsRepository(data, api.base)

override val id: UserId get() = data.id
override val nickname: String get() = data.nickname
override val avatar: FileRepository? get() = FileRepository(data.avatarId, api)
override val relationship: Relationship get() = data.relationship
override val username: Username? get() = data.username
override val subscribersAmount: Amount.OrZero get() = data.subscribersAmount
override val subscriptionsAmount: Amount.OrZero get() = data.subscriptionsAmount

override val isSelf: Boolean get() = false
override val email: Nothing? get() = null
override val emailVerified: Nothing? get() = null

public suspend fun addFriend() {
api.friends.add(data.id)
}

public suspend fun deleteFriend() {
api.friends.delete(data.id)
}

public suspend fun usernameAvailable(username: Username): Username {
return api.users.usernameAvailable(username)
}

override suspend fun updated(): AuthorizedRegularUserDetailsRepository {
// Cast is fine since we already know that user associated with that id is regular
return api.users.get(id) as AuthorizedRegularUserDetailsRepository
}

override fun toUser(): AuthorizedRegularUserRepository {
return AuthorizedRegularUserRepository(data.toUser(), api)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package app.meetacy.sdk.users

import app.meetacy.sdk.AuthorizedMeetacyApi
import app.meetacy.sdk.files.FileRepository
import app.meetacy.sdk.types.email.Email
import app.meetacy.sdk.types.user.RegularUser
import app.meetacy.sdk.types.user.Relationship
import app.meetacy.sdk.types.user.UserId
Expand All @@ -13,11 +14,14 @@ public class AuthorizedRegularUserRepository(
) : AuthorizedUserRepository {
override val base: RegularUserRepository get() = RegularUserRepository(data, api.base)

public val id: UserId get() = data.id
public val nickname: String get() = data.nickname
public val avatar: FileRepository? get() = FileRepository(data.avatarId, api)
public val username: Username? get() = data.username
public val relationship: Relationship get() = data.relationship
override val id: UserId get() = data.id
override val nickname: String get() = data.nickname
override val avatar: FileRepository? get() = FileRepository(data.avatarId, api)
override val username: Username? get() = data.username
override val relationship: Relationship get() = data.relationship

override val email: Nothing? get() = null
override val emailVerified: Nothing? get() = null

public suspend fun addFriend() {
api.friends.add(data.id)
Expand All @@ -26,4 +30,8 @@ public class AuthorizedRegularUserRepository(
public suspend fun deleteFriend() {
api.friends.delete(data.id)
}

override suspend fun details(): AuthorizedUserDetailsRepository {
return api.users.get(id)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package app.meetacy.sdk.users

import app.meetacy.sdk.AuthorizedMeetacyApi
import app.meetacy.sdk.MeetacyApi
import app.meetacy.sdk.auth.AuthApi
import app.meetacy.sdk.auth.AuthorizedAuthApi
import app.meetacy.sdk.files.AuthorizedFilesApi
import app.meetacy.sdk.files.FileRepository
import app.meetacy.sdk.files.FilesApi
import app.meetacy.sdk.friends.AuthorizedFriendsApi
import app.meetacy.sdk.friends.FriendsApi
import app.meetacy.sdk.invitations.AuthorizedInvitationsApi
import app.meetacy.sdk.invitations.InvitationsApi
import app.meetacy.sdk.meetings.AuthorizedMeetingsApi
import app.meetacy.sdk.meetings.MeetingsApi
import app.meetacy.sdk.notifications.AuthorizedNotificationsApi
import app.meetacy.sdk.notifications.NotificationsApi
import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.auth.Token
import app.meetacy.sdk.types.email.Email
import app.meetacy.sdk.types.file.FileId
import app.meetacy.sdk.types.optional.Optional
import app.meetacy.sdk.types.user.SelfUserDetails
import app.meetacy.sdk.types.user.UserId
import app.meetacy.sdk.types.user.Username
import app.meetacy.sdk.updates.AuthorizedUpdatesApi
import app.meetacy.sdk.updates.UpdatesApi

public class AuthorizedSelfUserDetailsRepository(
override val data: SelfUserDetails,
public val api: AuthorizedMeetacyApi
) : AuthorizedUserDetailsRepository {
override val base: SelfUserDetailsRepository get() = SelfUserDetailsRepository(data, api.base)

override val id: UserId get() = data.id
override val email: Email? get() = data.email
override val nickname: String get() = data.nickname
override val emailVerified: Boolean get() = data.emailVerified
override val username: Username? get() = data.username
override val avatar: FileRepository? get() = FileRepository(data.avatarId, api)
override val subscribersAmount: Amount.OrZero get() = data.subscribersAmount
override val subscriptionsAmount: Amount.OrZero get() = data.subscriptionsAmount

override val isSelf: Boolean get() = true
override val relationship: Nothing? get() = null

public val token: Token get() = api.token
public val files: AuthorizedFilesApi get() = api.files
public val auth: AuthorizedAuthApi get() = api.auth
public val friends: AuthorizedFriendsApi get() = api.friends
public val users: AuthorizedUsersApi get() = api.users
public val meetings: AuthorizedMeetingsApi get() = api.meetings
public val invitations: AuthorizedInvitationsApi get() = api.invitations
public val notifications: AuthorizedNotificationsApi get() = api.notifications
public val updates: AuthorizedUpdatesApi get() = api.updates

public suspend fun edited(
nickname: String,
username: Username?,
avatarId: FileId?
): AuthorizedSelfUserRepository = api.users.edit(nickname, username, avatarId)

public suspend fun edited(
nickname: Optional<String> = Optional.Undefined,
username: Optional<Username?> = Optional.Undefined,
avatarId: Optional<FileId?> = Optional.Undefined
): AuthorizedSelfUserRepository = api.users.edit(nickname, username, avatarId)

public suspend fun usernameAvailable(username: Username): Username {
return api.users.usernameAvailable(username)
}

override suspend fun updated(): AuthorizedSelfUserDetailsRepository {
return api.getMe()
}

override fun toUser(): AuthorizedSelfUserRepository {
return AuthorizedSelfUserRepository(data.toUser(), api)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@ public class AuthorizedSelfUserRepository(
) : AuthorizedUserRepository {
override val base: SelfUserRepository get() = SelfUserRepository(data, api.base)

public val id: UserId get() = data.id
public val email: Email? get() = data.email
public val nickname: String get() = data.nickname
public val emailVerified: Boolean get() = data.emailVerified
public val username: Username? get() = data.username
public val avatar: FileRepository? get() = FileRepository(data.avatarId, api)
override val id: UserId get() = data.id
override val email: Email? get() = data.email
override val nickname: String get() = data.nickname
override val emailVerified: Boolean get() = data.emailVerified
override val username: Username? get() = data.username
override val avatar: FileRepository? get() = FileRepository(data.avatarId, api)

override val relationship: Nothing? get() = null

public val token: Token get() = api.token
public val files: AuthorizedFilesApi get() = api.files
Expand All @@ -52,5 +54,7 @@ public class AuthorizedSelfUserRepository(
avatarId: Optional<FileId?> = Optional.Undefined
): AuthorizedSelfUserRepository = api.users.edit(nickname, username, avatarId)

public suspend fun updated(): AuthorizedSelfUserRepository = api.getMe()
override suspend fun details(): AuthorizedUserDetailsRepository {
return api.getMe()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package app.meetacy.sdk.users

import app.meetacy.sdk.AuthorizedMeetacyApi
import app.meetacy.sdk.files.FileRepository
import app.meetacy.sdk.types.amount.Amount
import app.meetacy.sdk.types.email.Email
import app.meetacy.sdk.types.user.*

public sealed interface AuthorizedUserDetailsRepository {
public val data: UserDetails
public val base: UserDetailsRepository

public val isSelf: Boolean
public val relationship: Relationship?
public val id: UserId
public val nickname: String
public val username: Username?
public val email: Email?
public val emailVerified: Boolean?
public val avatar: FileRepository?
public val subscribersAmount: Amount.OrZero
public val subscriptionsAmount: Amount.OrZero

public fun toUser(): AuthorizedUserRepository
public suspend fun updated(): AuthorizedUserDetailsRepository

public companion object {
public fun of(
data: UserDetails,
api: AuthorizedMeetacyApi
): AuthorizedUserDetailsRepository = when (data) {
is RegularUserDetails -> AuthorizedRegularUserDetailsRepository(data, api)
is SelfUserDetails -> AuthorizedSelfUserDetailsRepository(data, api)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package app.meetacy.sdk.users

import app.meetacy.sdk.AuthorizedMeetacyApi
import app.meetacy.sdk.types.user.RegularUser
import app.meetacy.sdk.types.user.SelfUser
import app.meetacy.sdk.types.user.User
import app.meetacy.sdk.files.FileRepository
import app.meetacy.sdk.types.email.Email
import app.meetacy.sdk.types.file.FileId
import app.meetacy.sdk.types.user.*

public sealed interface AuthorizedUserRepository {
public val data: User
public val base: UserRepository

public val id: UserId
public val email: Email?
public val nickname: String
public val emailVerified: Boolean?
public val username: Username?
public val relationship: Relationship?
public val avatar: FileRepository?

public suspend fun details(): AuthorizedUserDetailsRepository

public companion object {
public fun of(
data: User,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,17 @@ import app.meetacy.sdk.types.user.Username
* When modifying this class, corresponding classes should be altered:
* - [AuthorizedSelfUserRepository]
* - [AuthorizedRegularUserRepository]
* - [AuthorizedRegularUserDetailsRepository]
* - [AuthorizedSelfUserDetailsRepository]
*/
public class AuthorizedUsersApi(private val api: AuthorizedMeetacyApi) {
public val token: Token get() = api.token
public val base: UsersApi get() = api.base.users

public suspend fun get(userId: UserId): User = base.get(token, userId)
public suspend fun get(userId: UserId): AuthorizedUserDetailsRepository {
val user = base.get(token, userId)
return AuthorizedUserDetailsRepository.of(user.data, api)
}

public suspend fun edit(
nickname: String,
Expand Down
Loading

0 comments on commit 3dc562e

Please sign in to comment.