Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ uses semantic versioning (`MAJOR.MINOR.PATCH`).

### Fixed

- **Changing a fronting preference no longer wipes your system profile.**
Flipping either switch under Settings, Fronting sent the new value along with
an instruction to clear your system's description, tag, colour, note, avatar
and banner. Those fields are emptied by sending nothing for them, and that
screen sends nothing for anything except the switch you touched, so the save
cleared them every time. Present since the preferences arrived in 1.5.0. If
you lost a description or a tag this way it cannot be recovered from the app;
the values are gone from the server too.

- **Turning quiet hours off on a notification channel now turns them off.**
Unticking the box reported success and left the window in place, because the
app never actually told the server the field had changed. The same fix stops
an unrelated channel edit from clearing a window you wanted to keep.

- **Sharing and custom fields no longer explain failures their own way.** The
screens that tell three kinds of refusal apart, so they can ask for a
password or say publishing is off, carried a second set of wordings for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import systems.lupine.sheaf.data.model.GroupUpdate
import systems.lupine.sheaf.data.model.MemberUpdate
import systems.lupine.sheaf.data.model.NotificationChannelUpdate
import systems.lupine.sheaf.data.model.SystemUpdate

/**
Expand Down Expand Up @@ -37,9 +38,25 @@ import systems.lupine.sheaf.data.model.SystemUpdate
internal class PatchWriter(
private val writer: JsonWriter,
private val anyAdapter: JsonAdapter<Any>,
/**
* Whether a null in a clearable field means "clear it".
*
* False for a body that carries only part of the form, where a null means
* the caller simply did not populate that field. Writing it anyway would
* clear something the user never touched, which is how toggling a front
* preference wiped the system's description, tag, colour, note, avatar and
* banner: that screen sends one boolean and nothing else.
*/
private val clearsUnsetFields: Boolean = true,
) {
/** Server clears the column on an explicit null, so always write it. */
/**
* Server clears the column on an explicit null.
*
* A set value is always written. A null is written only when this body is
* the whole form, because only then does null mean the user emptied it.
*/
fun clears(name: String, value: Any?) {
if (value == null && !clearsUnsetFields) return
val previous = writer.serializeNulls
writer.serializeNulls = true
writer.name(name)
Expand All @@ -64,7 +81,7 @@ class MemberUpdateJsonAdapter(moshi: Moshi) : JsonAdapter<MemberUpdate>() {
override fun toJson(writer: JsonWriter, value: MemberUpdate?) {
if (value == null) { writer.nullValue(); return }
writer.beginObject()
PatchWriter(writer, anyAdapter).apply {
PatchWriter(writer, anyAdapter, value.clearsUnsetFields).apply {
// NOT NULL server-side: name, privacy.
omitsWhenNull("name", value.name)
omitsWhenNull("privacy", value.privacy)
Expand Down Expand Up @@ -101,7 +118,7 @@ class GroupUpdateJsonAdapter(moshi: Moshi) : JsonAdapter<GroupUpdate>() {
override fun toJson(writer: JsonWriter, value: GroupUpdate?) {
if (value == null) { writer.nullValue(); return }
writer.beginObject()
PatchWriter(writer, anyAdapter).apply {
PatchWriter(writer, anyAdapter, value.clearsUnsetFields).apply {
omitsWhenNull("name", value.name)
omitsWhenNull("privacy", value.privacy)
clears("description", value.description)
Expand All @@ -124,13 +141,68 @@ class GroupUpdateJsonAdapter(moshi: Moshi) : JsonAdapter<GroupUpdate>() {
}
}

class NotificationChannelUpdateJsonAdapter(moshi: Moshi) : JsonAdapter<NotificationChannelUpdate>() {
private val anyAdapter: JsonAdapter<Any> by lazy { moshi.adapter(Any::class.java) }

override fun toJson(writer: JsonWriter, value: NotificationChannelUpdate?) {
if (value == null) { writer.nullValue(); return }
writer.beginObject()
PatchWriter(writer, anyAdapter).apply {
// quiet_hours is the one genuinely nullable field on a channel, and
// the server keys on whether it was in the request rather than on
// its value. Moshi dropped the null, so unticking "Quiet hours"
// sent an empty body, the server left the column alone, and the
// window came back on the next refresh.
//
// Written only when the user actually touched it. This body is a
// partial patch, unlike the member and group ones, so an
// unconditional null here would clear the window on every unrelated
// channel save.
if (value.quietHoursSet) clears("quiet_hours", value.quietHours)
// webhook_secret, group_rules and member_rules are absent from the
// server's _reject_explicit_null list, so a null would be accepted
// rather than refused. That makes omitting them load-bearing: these
// are sent only when the user edited them, and an explicit null
// would wipe a secret or replace every rule with nothing.
omitsWhenNull("webhook_secret", value.webhookSecret)
omitsWhenNull("group_rules", value.groupRules)
omitsWhenNull("member_rules", value.memberRules)
omitsWhenNull("name", value.name)
omitsWhenNull("destination_config", value.destinationConfig)
omitsWhenNull("base_all_members", value.baseAllMembers)
omitsWhenNull("base_include_private", value.baseIncludePrivate)
omitsWhenNull("trigger_on_start", value.triggerOnStart)
omitsWhenNull("trigger_on_stop", value.triggerOnStop)
omitsWhenNull("trigger_on_cofront_change", value.triggerOnCofrontChange)
omitsWhenNull("cofront_redaction", value.cofrontRedaction)
omitsWhenNull("payload_sensitivity", value.payloadSensitivity)
omitsWhenNull("debounce_seconds", value.debounceSeconds)
omitsWhenNull("aggregation_window_seconds", value.aggregationWindowSeconds)
}
writer.endObject()
}

override fun fromJson(reader: JsonReader) = requestOnly("NotificationChannelUpdate")

companion object {
/** Needs the Moshi instance, so it registers as a factory. */
val FACTORY = Factory { type, _, moshi ->
if (Types.getRawType(type) == NotificationChannelUpdate::class.java) {
NotificationChannelUpdateJsonAdapter(moshi)
} else {
null
}
}
}
}

class SystemUpdateJsonAdapter(moshi: Moshi) : JsonAdapter<SystemUpdate>() {
private val anyAdapter: JsonAdapter<Any> by lazy { moshi.adapter(Any::class.java) }

override fun toJson(writer: JsonWriter, value: SystemUpdate?) {
if (value == null) { writer.nullValue(); return }
writer.beginObject()
PatchWriter(writer, anyAdapter).apply {
PatchWriter(writer, anyAdapter, value.clearsUnsetFields).apply {
// NOT NULL server-side: name, privacy, show_member_created_date
// and both front preferences (alongside date_format, which this
// client does not send).
Expand Down
16 changes: 16 additions & 0 deletions sheaf/app/src/main/java/systems/lupine/sheaf/data/model/Models.kt
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ data class SystemUpdate(
// Only consulted when raising privacy to public is deferred.
val password: String? = null,
@Json(name = "totp_code") val totpCode: String? = null,
/** See [MemberUpdate.clearsUnsetFields]: default false is the safe answer. */
@Json(ignore = true) val clearsUnsetFields: Boolean = false,
)

// ── System Safety ─────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -650,6 +652,18 @@ data class MemberUpdate(
// Only consulted when the edit is a deferred exposure. Never stored.
val password: String? = null,
@Json(name = "totp_code") val totpCode: String? = null,
/**
* Whether an optional field left null means "clear it" or "leave it alone".
*
* Only a caller that owns the WHOLE form can mean the first: it populates
* every clearable field from its own state, so a null there really is the
* user emptying the box. A caller patching one setting populates nothing
* else, and clearing on its behalf wipes whatever the user had.
*
* Default false so that is the safe answer, and a screen that only sends
* part of the body cannot destroy the rest of it by omission.
*/
@Json(ignore = true) val clearsUnsetFields: Boolean = false,
)

// ── Fronts ────────────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -801,6 +815,8 @@ data class GroupUpdate(
// Popped server-side before persistence, never a group column.
val password: String? = null,
@Json(name = "totp_code") val totpCode: String? = null,
/** See [MemberUpdate.clearsUnsetFields]: default false is the safe answer. */
@Json(ignore = true) val clearsUnsetFields: Boolean = false,
)

@JsonClass(generateAdapter = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,14 @@ data class NotificationChannelRead(
*
* Note: backend rejects explicit null on most fields (only quiet_hours
* accepts null = clear). The UI keeps the ones it isn't editing out of
* the request entirely by leaving them as `null` here, which Moshi omits
* by default. Whenever the user toggles a control, the corresponding
* field is set to a non-null value.
* the request entirely by leaving them as `null` here. Whenever the user
* toggles a control, the corresponding field is set to a non-null value.
*
* Serialised by [NotificationChannelUpdateJsonAdapter], not by Moshi's
* generated one. Moshi drops nulls, which cannot express the clear: this
* class once relied on "null = clear quiet hours" while the field was never
* reaching the server at all, so unticking the window reported success and
* changed nothing.
*/
@JsonClass(generateAdapter = true)
data class NotificationChannelUpdate(
Expand All @@ -134,6 +139,18 @@ data class NotificationChannelUpdate(
@Json(name = "quiet_hours") val quietHours: QuietHoursSpec? = null,
@Json(name = "group_rules") val groupRules: List<GroupRuleSpec>? = null,
@Json(name = "member_rules") val memberRules: List<MemberRuleSpec>? = null,
/**
* Whether [quietHours] is an answer rather than an absence. Not serialised:
* it decides whether `quiet_hours` appears in the body at all.
*
* Needed because null means two different things here. The user turning
* the window off is a clear, and has to be sent; the user editing the
* channel's name while the window is untouched is an omission, and must
* not be. Without this they are the same value, and whichever meaning the
* adapter picked would break the other: dropping the null leaves quiet
* hours on forever, writing it always wipes them on any unrelated save.
*/
@Json(ignore = true) val quietHoursSet: Boolean = false,
)

@JsonClass(generateAdapter = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import systems.lupine.sheaf.data.api.CustomFieldValueSetJsonAdapter
import systems.lupine.sheaf.data.api.SystemUpdateJsonAdapter
import systems.lupine.sheaf.data.api.GroupUpdateJsonAdapter
import systems.lupine.sheaf.data.api.MemberUpdateJsonAdapter
import systems.lupine.sheaf.data.api.NotificationChannelUpdateJsonAdapter
import systems.lupine.sheaf.data.api.FrontUpdateJsonAdapter
import systems.lupine.sheaf.data.model.FrontUpdate
import systems.lupine.sheaf.data.api.RawJsonAdapter
Expand Down Expand Up @@ -57,6 +58,7 @@ object NetworkModule {
.add(MemberUpdateJsonAdapter.FACTORY)
.add(GroupUpdateJsonAdapter.FACTORY)
.add(SystemUpdateJsonAdapter.FACTORY)
.add(NotificationChannelUpdateJsonAdapter.FACTORY)
.addLast(KotlinJsonAdapterFactory())
.build()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ class GroupDetailViewModel @Inject constructor(
))
} else {
api.updateGroup(groupId!!, GroupUpdate(
clearsUnsetFields = true,
name = f.name.trim(),
description = f.description.takeIf { it.isNotBlank() },
color = f.color.takeIf { it.isNotBlank() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,9 @@ class MemberDetailViewModel @Inject constructor(
)).also { createdMemberId = it.id }
} else {
val update = MemberUpdate(
// The whole form, so a null here is the user emptying
// the box rather than this screen not knowing.
clearsUnsetFields = true,
name = f.name.trim(),
displayName = f.displayName.takeIf { it.isNotBlank() },
// Null clears it. That used to need an empty string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,11 @@ class ChannelDetailViewModel @Inject constructor(
debounceSeconds = draft.debounceSeconds,
aggregationWindowSeconds = draft.aggregationWindowSeconds,
// quietHours / groupRules / memberRules only sent when the
// user actively toggled them — otherwise we pass null and
// Moshi omits them from the wire JSON.
// user actively toggled them. For the two rule sets a null is
// enough to mean "untouched", since the adapter omits them. Quiet
// hours needs the flag as well: null there is also how the window
// is cleared, so the body has to say which of the two it means.
quietHoursSet = draft.quietHoursSet,
quietHours = if (draft.quietHoursSet) draft.quietHours else null,
groupRules = if (draft.groupRulesSet) draft.groupRules else null,
memberRules = if (draft.memberRulesSet) draft.memberRules else null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ class SystemEditViewModel @Inject constructor(
_state.update { it.copy(isSaving = true, error = null, stepUpError = null) }
runCatching {
api.updateOwnSystem(SystemUpdate(
// The whole profile form, so an empty box really is a clear.
clearsUnsetFields = true,
name = f.name.takeIf { it.isNotBlank() },
description = f.description.takeIf { it.isNotBlank() },
// Send empty string as-is so the backend's "empty clears"
Expand Down
Loading
Loading