Changeset View
Changeset View
Standalone View
Standalone View
src/main/java/net/wildfyre/users/LoggedUser.kt
- This file was added.
/* | |||||
* Copyright 2019 Wildfyre.net | |||||
* | |||||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||||
* you may not use this file except in compliance with the License. | |||||
* You may obtain a copy of the License at | |||||
* | |||||
* http://www.apache.org/licenses/LICENSE-2.0 | |||||
* | |||||
* Unless required by applicable law or agreed to in writing, software | |||||
* distributed under the License is distributed on an "AS IS" BASIS, | |||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||||
* See the License for the specific language governing permissions and | |||||
* limitations under the License. | |||||
*/ | |||||
package net.wildfyre.users | |||||
import com.eclipsesource.json.JsonObject | |||||
import net.wildfyre.api.Internal | |||||
import net.wildfyre.areas.Areas | |||||
import net.wildfyre.descriptors.NoSuchEntityException | |||||
import net.wildfyre.http.IssueInTransferException | |||||
import net.wildfyre.http.Method.PATCH | |||||
import net.wildfyre.http.Request | |||||
import net.wildfyre.posts.Post | |||||
/** | |||||
* Represents the user you are logging-in with. | |||||
*/ | |||||
class LoggedUser internal constructor(id: Int) : User(id) { | |||||
// Documentation is inherited from User | |||||
override val isEditable: Boolean | |||||
get() = true | |||||
/** | |||||
* The user's biography. | |||||
* | |||||
* See [set] for more information on the setter. | |||||
* | |||||
* @see [User.bio] | |||||
*/ | |||||
override var bio: String | |||||
get() = super.bio | |||||
public set(bio) = set(bio = bio) | |||||
/** | |||||
* The user's avatar. | |||||
* | |||||
* See [set] for more information on the setter. | |||||
* | |||||
* @see [User.avatar] | |||||
*/ | |||||
override var avatar: String? | |||||
get() = super.avatar | |||||
public set(avatar) = set(avatar = avatar) | |||||
/** | |||||
* The user's name. | |||||
* | |||||
* See [set] for more information on the setter. | |||||
* | |||||
* @see [User.name] | |||||
*/ | |||||
override var name: String | |||||
get() = super.name | |||||
public set(name) = set(username = name) | |||||
/** | |||||
* Changes the username, bio or avatar of the user. | |||||
* | |||||
* The changes will be reflected to the current object immediately, and the query will be sent to the server. | |||||
* | |||||
* You can select which fields to update and which fields to keep by giving `null` to any unchanged field. | |||||
* If you only specify null values, this method returns without doing anything. | |||||
* | |||||
* @param username the user's name | |||||
* @param bio the user's bio | |||||
* @param avatar the user's avatar (URL path) | |||||
*/ | |||||
// no 'operator' keyword: this is NOT to be used as user[username, bio] = avatar! | |||||
@JvmOverloads | |||||
fun set(username: String? = null, bio: String? = null, avatar: String? = null) { | |||||
val json = JsonObject() | |||||
username?.let { | |||||
super.name = username | |||||
json["name"] = username | |||||
} | |||||
bio?.let { | |||||
super.bio = bio | |||||
json["bio"] = bio | |||||
} | |||||
avatar?.let { | |||||
super.avatar = avatar | |||||
json["avatar"] = avatar | |||||
} | |||||
// Nothing to do if neither was specified | |||||
if (json.isEmpty) | |||||
return | |||||
Internal.submit { | |||||
// Send query to server | |||||
try { | |||||
Request(PATCH, "/users/") | |||||
.addToken(Internal.token()) | |||||
.addJson(json) | |||||
.getJson() | |||||
this.update() | |||||
} catch (e: IssueInTransferException) { | |||||
throw RuntimeException("Something unforeseen happened during the edition of the user.", e) | |||||
} catch (e: NoSuchEntityException) { | |||||
throw RuntimeException("Trying to edit a client that does not exist... this should not happen.", e) | |||||
} catch (e: Request.CantConnectException) { | |||||
Internal.throwCantConnect(e) | |||||
} | |||||
} | |||||
} | |||||
/** | |||||
* The posts created by this user, in every Area. | |||||
* @see postsList | |||||
*/ | |||||
val posts: Sequence<Post> | |||||
get() = Areas.collection().asSequence() | |||||
.flatMap { it.ownPosts().asSequence() } | |||||
/** | |||||
* The posts created by this user, in every Area, as a List. | |||||
* @see posts | |||||
*/ | |||||
val postsList: List<Post> | |||||
get() = posts.toList() | |||||
} |