Skip to content

Commit

Permalink
feat (#29): adapt gravatar avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
sliderzxc authored Jul 31, 2023
1 parent bff1a74 commit d2c379b
Show file tree
Hide file tree
Showing 11 changed files with 108 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

/**
Expand Down Expand Up @@ -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 }
Expand All @@ -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<EditEmailRequest>()

is CreateTimerRequest -> timersService.createTimer(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
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
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) }
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

This file was deleted.

49 changes: 26 additions & 23 deletions grpc-engine/src/main/proto/io/timemates/api/users/types/User.proto
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,13 +44,11 @@ public class ProfileApi(
public suspend fun editProfile(
name: UserName? = null,
description: UserDescription? = null,
avatar: Avatar? = null
): Result<Empty> {
return tokenProvider.getAsResult()
.flatMap { token -> engine.execute(EditProfileRequest(token, null, name, description)) }
}

public suspend fun setGravatarAvatar(emailAddress: EmailAddress): Result<Empty> {
return tokenProvider.getAsResult()
.flatMap { token -> engine.execute(SetGravatarAvatarRequest(token, emailAddress)) }
.flatMap { token -> engine.execute(
EditProfileRequest(token, name, description, avatar)
) }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Empty>()

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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<GravatarId, String>() {
override fun create(input: String): Result<GravatarId> {
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<FileId, String>() {
override fun create(input: String): Result<FileId> {
return when {
input.isBlank() -> Result.failure(CreationFailure.ofBlank())
else -> Result.success(FileId(input))
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()

0 comments on commit d2c379b

Please sign in to comment.