Changeset View
Changeset View
Standalone View
Standalone View
src/main/java/net/wildfyre/users/User.kt
- This file was added.
/* | |||||
* Copyright 2018 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.descriptors.CacheManager | |||||
import net.wildfyre.descriptors.Descriptor | |||||
import net.wildfyre.descriptors.NoSuchEntityException | |||||
import net.wildfyre.http.IssueInTransferException | |||||
import net.wildfyre.http.Method.GET | |||||
import net.wildfyre.http.Request | |||||
import net.wildfyre.utils.ProgrammingException | |||||
import java.net.MalformedURLException | |||||
import java.net.URL | |||||
import java.util.* | |||||
open class User | |||||
internal constructor(id: Int) : Descriptor() { | |||||
//region Fields | |||||
/** | |||||
* The ID of this user. | |||||
* @return The ID of this user, a positive integer. | |||||
*/ | |||||
val ID = id | |||||
get() { | |||||
use() | |||||
return field | |||||
} | |||||
/** | |||||
* The name of this user. | |||||
*/ | |||||
open var name: String = "" | |||||
get() { | |||||
use() | |||||
return field | |||||
} | |||||
protected set | |||||
/** | |||||
* Can this user be edited? | |||||
* | |||||
* Will always return `false` for a [User] and `true` for a [LoggedUser]. | |||||
*/ | |||||
open val isEditable: Boolean | |||||
get() = false | |||||
/** | |||||
* A [String] that contains the URL of the avatar of the user, | |||||
* or `null` if the user doesn't have an avatar. | |||||
* | |||||
* @see avatarUrl | |||||
*/ | |||||
open var avatar: String? = null | |||||
get() { | |||||
use() | |||||
return field | |||||
} | |||||
protected set | |||||
/** | |||||
* The avatar of the user, as an [URL] object. | |||||
* If the user doesn't have an avatar, this is `null`. | |||||
* | |||||
* @see avatar | |||||
*/ | |||||
val avatarUrl: URL? | |||||
get() = if (avatar != null) { | |||||
try { | |||||
URL(avatar) | |||||
} catch (e: MalformedURLException) { | |||||
throw ProgrammingException("URLs are sent by the server and therefore shouldn't be malformed!", e) | |||||
} | |||||
} else null | |||||
/** | |||||
* The user's biography. | |||||
*/ | |||||
open var bio = "" | |||||
get() { | |||||
use() | |||||
return field | |||||
} | |||||
protected set | |||||
/** | |||||
* Is this user banned? | |||||
*/ | |||||
var isBanned: Boolean = false | |||||
get() { | |||||
use() | |||||
return field | |||||
} | |||||
protected set | |||||
//endregion | |||||
//region Updating | |||||
@Throws(NoSuchEntityException::class, Request.CantConnectException::class) | |||||
override fun update() { | |||||
if (Users.getCached(this.ID) == null) { | |||||
System.err.println("User.update: The user $ID is not even in the cache, aborting update early.") | |||||
return | |||||
} | |||||
try { | |||||
val values = Request(GET, "/users/$ID/") | |||||
.getJson() as JsonObject | |||||
// Use the old value as default value: if nothing is specified, keep the old value | |||||
name = values.getString("name", null) | |||||
?: throw ProgrammingException("Missing 'name' in $values.") | |||||
avatar = if(values["avatar"].isNull) null | |||||
else values["avatar"].asString() | |||||
bio = values.getString("bio", "") | |||||
isBanned = values.getBoolean("banned", false) | |||||
val user = values.getInt("user", ID) | |||||
if (user != ID) | |||||
throw RuntimeException("The ID has changed! $ID -> $user") | |||||
this.use() | |||||
} catch (e: IssueInTransferException) { | |||||
e.json?.let { json -> | |||||
if (json.asObject().getString("detail", null) == "Not found.") { | |||||
Users.users.remove(this.ID) | |||||
throw NoSuchEntityException("The requested user does not exist!", this) | |||||
} | |||||
} | |||||
} | |||||
} | |||||
override fun cacheManager(): CacheManager { | |||||
return Users.cacheManager() | |||||
} | |||||
fun asLogged() = this as LoggedUser | |||||
//endregion | |||||
//region Generated | |||||
override fun toString(): String { | |||||
return "User{" + "ID=" + ID + | |||||
", name='" + name + '\''.toString() + | |||||
", avatar='" + avatar + '\''.toString() + | |||||
", bio='" + bio + '\''.toString() + | |||||
", isBanned=" + isBanned + | |||||
'}'.toString() | |||||
} | |||||
override fun equals(other: Any?): Boolean { | |||||
if (this === other) return true | |||||
if (other == null || javaClass != other.javaClass) return false | |||||
val user = other as User | |||||
return ID == user.ID && | |||||
isBanned == user.isBanned && | |||||
name == user.name && | |||||
avatar == user.avatar && | |||||
bio == user.bio | |||||
} | |||||
override fun hashCode(): Int { | |||||
return Objects.hash(ID) | |||||
} | |||||
//endregion | |||||
companion object { | |||||
/** | |||||
* Creates a new User object. | |||||
* @param id the ID of the user | |||||
* @return A new User object. | |||||
*/ | |||||
@JvmStatic | |||||
@JvmName("create") // Otherwise Kotlin wants to call this "create$wflibjava" | |||||
internal fun create(id: Int): User | |||||
= if (Users.isMyID(id)) LoggedUser(id) else User(id) | |||||
} | |||||
} |