From d8bed0ff635d99bcd75207e5ee78fac6d3f186a4 Mon Sep 17 00:00:00 2001 From: provable_ascent Date: Thu, 27 Apr 2023 05:22:12 +0000 Subject: [PATCH 1/2] Make UserNote comment default to the empty string. This make the behavior consistent between when UserNote doesn't exist and when comment is null. The current behavior may return null in APIs, which misleads some clients doing feature detection into thinking the server does not support comments. For example, see https://codeberg.org/husky/husky/issues/92 --- lib/pleroma/user_note.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/user_note.ex b/lib/pleroma/user_note.ex index 5e82d359f..ff4981cb7 100644 --- a/lib/pleroma/user_note.ex +++ b/lib/pleroma/user_note.ex @@ -31,7 +31,7 @@ defmodule Pleroma.UserNote do UserNote |> where(source_id: ^source.id, target_id: ^target.id) |> Repo.one() do - note.comment + note.comment || "" else _ -> "" end From 9c4203632da7b6e0bd7edbe6e93dc97e748819cc Mon Sep 17 00:00:00 2001 From: provable_ascent Date: Fri, 12 May 2023 02:18:24 +0000 Subject: [PATCH 2/2] Add user_note_test.exs. --- test/pleroma/user_note_test.exs | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 test/pleroma/user_note_test.exs diff --git a/test/pleroma/user_note_test.exs b/test/pleroma/user_note_test.exs new file mode 100644 index 000000000..7d818196e --- /dev/null +++ b/test/pleroma/user_note_test.exs @@ -0,0 +1,39 @@ +# Pleroma: A lightweight social networking server +# Copyright © 2017-2021 Pleroma Authors +# SPDX-License-Identifier: AGPL-3.0-only + +defmodule Pleroma.UserNoteTest do + alias Pleroma.UserNote + + use Pleroma.DataCase, async: false + import Pleroma.Factory + + describe "show/2" do + setup do + {:ok, users: insert_list(2, :user)} + end + + test "if record does not exist, returns empty string", %{users: [user1, user2]} do + comment = UserNote.show(user1, user2) + + assert comment == "" + end + + test "if record exists with comment == nil, returns empty string", %{users: [user1, user2]} do + UserNote.create(user1, user2, nil) + + comment = UserNote.show(user1, user2) + + assert comment == "" + end + + test "if record exists with non-nil comment, returns comment", %{users: [user1, user2]} do + expected_comment = "hello" + UserNote.create(user1, user2, expected_comment) + + comment = UserNote.show(user1, user2) + + assert comment == expected_comment + end + end +end