From d2c379bf2294f5a360ca22f437ecf2bc83a0fc67 Mon Sep 17 00:00:00 2001 From: Vadym Hrynyk <100761978+sliderzxc@users.noreply.github.com> Date: Mon, 31 Jul 2023 16:20:31 +0300 Subject: [PATCH] feat (#29): adapt gravatar avatar --- .../api/grpc/GrpcTimeMatesRequestsEngine.kt | 16 ++---- .../timemates/api/grpc/mappers/UsersMapper.kt | 11 +++-- .../io/timemates/api/users/UsersService.proto | 44 +++++++---------- .../api/users/requests/EditUserRequest.proto | 28 ++++++----- .../users/requests/SetGravatarRequest.proto | 7 --- .../io/timemates/api/users/types/User.proto | 49 ++++++++++--------- .../timemates/sdk/users/profile/ProfileApi.kt | 12 ++--- .../profile/requests/EditProfileRequest.kt | 3 +- .../requests/SetGravatarAvatarRequest.kt | 11 ----- .../sdk/users/profile/types/Avatar.kt | 30 ++++++++++++ .../timemates/sdk/users/profile/types/User.kt | 3 +- 11 files changed, 108 insertions(+), 106 deletions(-) delete mode 100644 grpc-engine/src/main/proto/io/timemates/api/users/requests/SetGravatarRequest.proto delete mode 100644 sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/SetGravatarAvatarRequest.kt create mode 100644 sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/Avatar.kt diff --git a/grpc-engine/src/main/kotlin/io/timemates/api/grpc/GrpcTimeMatesRequestsEngine.kt b/grpc-engine/src/main/kotlin/io/timemates/api/grpc/GrpcTimeMatesRequestsEngine.kt index 6001b2f..ef9f211 100644 --- a/grpc-engine/src/main/kotlin/io/timemates/api/grpc/GrpcTimeMatesRequestsEngine.kt +++ b/grpc-engine/src/main/kotlin/io/timemates/api/grpc/GrpcTimeMatesRequestsEngine.kt @@ -64,7 +64,7 @@ import io.timemates.sdk.timers.sessions.requests.StopTimerRequest import io.timemates.sdk.timers.types.value.TimerId import io.timemates.sdk.users.profile.requests.EditProfileRequest import io.timemates.sdk.users.profile.requests.GetUsersRequest -import io.timemates.sdk.users.profile.requests.SetGravatarAvatarRequest +import io.timemates.sdk.users.profile.types.Avatar import io.timemates.sdk.users.settings.requests.EditEmailRequest import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.map @@ -91,7 +91,6 @@ import io.timemates.api.timers.sessions.requests.stopTimerRequest as buildStopTi import io.timemates.api.users.requests.CreateProfileRequestOuterClass.CreateProfileRequest as GrpcCreateProfileRequest import io.timemates.api.users.requests.EditUserRequestOuterClass.EditUserRequest as GrpcEditUserRequest import io.timemates.api.users.requests.GetUsersRequestOuterClass.GetUsersRequest as GrpcGetUsersRequest -import io.timemates.api.users.requests.SetGravatarRequestOuterClass.SetGravatarRequest as GrpcSetGravatarRequest import io.timemates.sdk.common.types.Empty as SdkEmpty /** @@ -211,7 +210,10 @@ public class GrpcTimeMatesRequestsEngine( .apply { request.name?.let { name = it.string } request.description?.let { description = it.string } - request.avatarId?.let { avatarId = it.string } + request.avatar?.let { + avatarId = (it as? Avatar.FileId)?.string + gravatarId = (it as? Avatar.GravatarId)?.string + } }.build(), headers = authorizedMetadata(request.accessHash), ).let { SdkEmpty } @@ -222,14 +224,6 @@ public class GrpcTimeMatesRequestsEngine( .build(), ).let { GetUsersRequest.Result(it.usersList.map { usersMapper.grpcUserToSdkUser(it) }) } - is SetGravatarAvatarRequest -> usersService.setGravatar( - GrpcSetGravatarRequest.newBuilder() - .apply { - request.email.let { email = it.string } - }.build(), - headers = authorizedMetadata(request.accessHash), - ).let { SdkEmpty } - is EditEmailRequest -> unsupported() is CreateTimerRequest -> timersService.createTimer( diff --git a/grpc-engine/src/main/kotlin/io/timemates/api/grpc/mappers/UsersMapper.kt b/grpc-engine/src/main/kotlin/io/timemates/api/grpc/mappers/UsersMapper.kt index aef44af..4fc1a08 100644 --- a/grpc-engine/src/main/kotlin/io/timemates/api/grpc/mappers/UsersMapper.kt +++ b/grpc-engine/src/main/kotlin/io/timemates/api/grpc/mappers/UsersMapper.kt @@ -1,8 +1,8 @@ package io.timemates.api.grpc.mappers -import io.timemates.api.users.types.UserOuterClass.User +import io.timemates.api.users.types.UserOuterClass import io.timemates.sdk.common.constructor.createOrThrow -import io.timemates.sdk.files.types.value.FileId +import io.timemates.sdk.users.profile.types.Avatar import io.timemates.sdk.users.profile.types.value.EmailAddress import io.timemates.sdk.users.profile.types.value.UserDescription import io.timemates.sdk.users.profile.types.value.UserId @@ -10,13 +10,14 @@ import io.timemates.sdk.users.profile.types.value.UserName import io.timemates.sdk.users.profile.types.User as SdkUser internal class UsersMapper { - fun grpcUserToSdkUser(user: User): SdkUser = with(user) { + fun grpcUserToSdkUser(user: UserOuterClass.User): SdkUser = with(user) { return SdkUser( id = UserId.createOrThrow(id), name = UserName.createOrThrow(name), description = UserDescription.createOrThrow(description), - avatarFileId = FileId.createOrThrow(avatarId), - emailAddress = email?.takeIf { it.isNotEmpty() }?.let { EmailAddress.createOrThrow(it) } + emailAddress = email?.takeIf { it.isNotEmpty() }?.let { EmailAddress.createOrThrow(it) }, + avatar = gravatarId?.let { Avatar.GravatarId.createOrThrow(it) } ?: + avatarId?.let { Avatar.FileId.createOrThrow(it) } ) } } \ No newline at end of file diff --git a/grpc-engine/src/main/proto/io/timemates/api/users/UsersService.proto b/grpc-engine/src/main/proto/io/timemates/api/users/UsersService.proto index 4aede02..4f7fc7d 100644 --- a/grpc-engine/src/main/proto/io/timemates/api/users/UsersService.proto +++ b/grpc-engine/src/main/proto/io/timemates/api/users/UsersService.proto @@ -3,38 +3,30 @@ syntax = "proto3"; import "io/timemates/api/users/requests/GetUsersRequest.proto"; import "io/timemates/api/users/requests/EditUserRequest.proto"; import "io/timemates/api/users/requests/EditEmailRequest.proto"; -import "io/timemates/api/users/requests/SetGravatarRequest.proto"; import "io/timemates/api/users/types/User.proto"; import "google/protobuf/empty.proto"; option java_package = "io.timemates.api.users"; service UsersService { - /** - * Gets users by given identifiers. - * - * Returns [User] or null - */ - rpc getUsers(GetUsersRequest) returns (Users); + /** + * Gets users by given identifiers. + * + * Returns [User] or null + */ + rpc getUsers(GetUsersRequest) returns (Users); - /** - * Sets user information (name / description / avatar file id). - * - * Returns [Status] - */ - rpc setUser(EditUserRequest) returns (google.protobuf.Empty); + /** + * Sets user information (name / description / avatar file id). + * + * Returns [Status] + */ + rpc setUser(EditUserRequest) returns (google.protobuf.Empty); - /** - * Sets new email for current user. - * - * Returns [Status] - */ - rpc setEmail(EditEmailRequest) returns (google.protobuf.Empty); - - /** - * Sets new gravatar for user avatar. - * - * Returns [Status] - */ - rpc setGravatar(SetGravatarRequest) returns (google.protobuf.Empty); + /** + * Sets new email for current user. + * + * Returns [Status] + */ + rpc setEmail(EditEmailRequest) returns (google.protobuf.Empty); } \ No newline at end of file diff --git a/grpc-engine/src/main/proto/io/timemates/api/users/requests/EditUserRequest.proto b/grpc-engine/src/main/proto/io/timemates/api/users/requests/EditUserRequest.proto index 9203208..aa19215 100644 --- a/grpc-engine/src/main/proto/io/timemates/api/users/requests/EditUserRequest.proto +++ b/grpc-engine/src/main/proto/io/timemates/api/users/requests/EditUserRequest.proto @@ -3,18 +3,20 @@ syntax = "proto3"; option java_package = "io.timemates.api.users.requests"; message EditUserRequest { - /** - * String identifier of 128 length. Can be null - * if there is no avatar. - */ - optional string avatarId = 1; - /** - * User's name - */ - optional string name = 2; + /** + * User's avatar id + */ + oneof avatar { + string avatarId = 1; + string gravatarId = 2; + } + /** + * User's name + */ + optional string name = 3; - /** - * User's description. - */ - optional string description = 3; + /** + * User's description. + */ + optional string description = 4; } \ No newline at end of file diff --git a/grpc-engine/src/main/proto/io/timemates/api/users/requests/SetGravatarRequest.proto b/grpc-engine/src/main/proto/io/timemates/api/users/requests/SetGravatarRequest.proto deleted file mode 100644 index e0da373..0000000 --- a/grpc-engine/src/main/proto/io/timemates/api/users/requests/SetGravatarRequest.proto +++ /dev/null @@ -1,7 +0,0 @@ -syntax = "proto3"; - -option java_package = "io.timemates.api.users.requests"; - -message SetGravatarRequest { - string email = 1; -} \ No newline at end of file diff --git a/grpc-engine/src/main/proto/io/timemates/api/users/types/User.proto b/grpc-engine/src/main/proto/io/timemates/api/users/types/User.proto index daebca0..b5b69c0 100644 --- a/grpc-engine/src/main/proto/io/timemates/api/users/types/User.proto +++ b/grpc-engine/src/main/proto/io/timemates/api/users/types/User.proto @@ -3,34 +3,37 @@ syntax = "proto3"; option java_package = "io.timemates.api.users.types"; message User { - /** - * User's identifier - */ - int64 id = 1; + /** + * User's identifier + */ + int64 id = 1; - /** - * String identifier of 128 length. Can be null - * if there is no avatar. - */ - optional string avatarId = 2; + /** + * String identifier of 128 length. Can be null + * if there is no avatar. + */ + oneof avatar { + string avatarId = 2; + string gravatarId = 3; + } - /** - * User's name - */ - string name = 3; + /** + * User's name + */ + string name = 4; - /** - * User's description. - */ - string description = 4; + /** + * User's description. + */ + string description = 5; - /** - * User's email up to 200 symbols. - * Can be null if there is no access. - */ - optional string email = 5; + /** + * User's email up to 200 symbols. + * Can be null if there is no access. + */ + optional string email = 6; } message Users { - repeated User users = 1; + repeated User users = 1; } \ No newline at end of file diff --git a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/ProfileApi.kt b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/ProfileApi.kt index 0558e02..eda56f8 100644 --- a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/ProfileApi.kt +++ b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/ProfileApi.kt @@ -7,7 +7,7 @@ import io.timemates.sdk.common.providers.getAsResult import io.timemates.sdk.common.types.Empty import io.timemates.sdk.users.profile.requests.EditProfileRequest import io.timemates.sdk.users.profile.requests.GetUsersRequest -import io.timemates.sdk.users.profile.requests.SetGravatarAvatarRequest +import io.timemates.sdk.users.profile.types.Avatar import io.timemates.sdk.users.profile.types.User import io.timemates.sdk.users.profile.types.value.EmailAddress import io.timemates.sdk.users.profile.types.value.UserDescription @@ -44,13 +44,11 @@ public class ProfileApi( public suspend fun editProfile( name: UserName? = null, description: UserDescription? = null, + avatar: Avatar? = null ): Result { return tokenProvider.getAsResult() - .flatMap { token -> engine.execute(EditProfileRequest(token, null, name, description)) } - } - - public suspend fun setGravatarAvatar(emailAddress: EmailAddress): Result { - return tokenProvider.getAsResult() - .flatMap { token -> engine.execute(SetGravatarAvatarRequest(token, emailAddress)) } + .flatMap { token -> engine.execute( + EditProfileRequest(token, name, description, avatar) + ) } } } diff --git a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/EditProfileRequest.kt b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/EditProfileRequest.kt index 1a5fabd..cbe97af 100644 --- a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/EditProfileRequest.kt +++ b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/EditProfileRequest.kt @@ -4,12 +4,13 @@ import io.timemates.sdk.authorization.types.value.AccessHash import io.timemates.sdk.common.types.Empty import io.timemates.sdk.common.types.TimeMatesRequest import io.timemates.sdk.files.types.value.FileId +import io.timemates.sdk.users.profile.types.Avatar import io.timemates.sdk.users.profile.types.value.UserDescription import io.timemates.sdk.users.profile.types.value.UserName public data class EditProfileRequest( val accessHash: AccessHash, - val avatarId: FileId?, val name: UserName?, val description: UserDescription?, + val avatar: Avatar? ) : TimeMatesRequest() \ No newline at end of file diff --git a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/SetGravatarAvatarRequest.kt b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/SetGravatarAvatarRequest.kt deleted file mode 100644 index 443d2c4..0000000 --- a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/requests/SetGravatarAvatarRequest.kt +++ /dev/null @@ -1,11 +0,0 @@ -package io.timemates.sdk.users.profile.requests - -import io.timemates.sdk.authorization.types.value.AccessHash -import io.timemates.sdk.common.types.Empty -import io.timemates.sdk.common.types.TimeMatesRequest -import io.timemates.sdk.users.profile.types.value.EmailAddress - -public data class SetGravatarAvatarRequest( - val accessHash: AccessHash, - val email: EmailAddress, -): TimeMatesRequest() \ No newline at end of file diff --git a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/Avatar.kt b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/Avatar.kt new file mode 100644 index 0000000..671d622 --- /dev/null +++ b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/Avatar.kt @@ -0,0 +1,30 @@ +package io.timemates.sdk.users.profile.types + +import io.timemates.sdk.common.constructor.CreationFailure +import io.timemates.sdk.common.constructor.Factory + +public sealed interface Avatar { + @JvmInline + public value class GravatarId private constructor(public val string: String) : Avatar { + public companion object : Factory() { + override fun create(input: String): Result { + return when { + input.isBlank() -> Result.failure(CreationFailure.ofBlank()) + else -> Result.success(GravatarId(input)) + } + } + } + } + + @JvmInline + public value class FileId private constructor(public val string: String) : Avatar { + public companion object : Factory() { + override fun create(input: String): Result { + return when { + input.isBlank() -> Result.failure(CreationFailure.ofBlank()) + else -> Result.success(FileId(input)) + } + } + } + } +} \ No newline at end of file diff --git a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/User.kt b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/User.kt index ebbd6d3..9f4b62f 100644 --- a/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/User.kt +++ b/sdk/src/commonMain/kotlin/io/timemates/sdk/users/profile/types/User.kt @@ -1,7 +1,6 @@ package io.timemates.sdk.users.profile.types import io.timemates.sdk.common.types.TimeMatesEntity -import io.timemates.sdk.files.types.value.FileId import io.timemates.sdk.users.profile.types.value.EmailAddress import io.timemates.sdk.users.profile.types.value.UserDescription import io.timemates.sdk.users.profile.types.value.UserId @@ -20,6 +19,6 @@ public data class User( val id: UserId, val name: UserName, val description: UserDescription, - val avatarFileId: FileId, val emailAddress: EmailAddress?, + val avatar: Avatar?, ) : TimeMatesEntity() \ No newline at end of file