From 173f69c854adf966d52b3767c4de43a0b1ce5b00 Mon Sep 17 00:00:00 2001
From: "Haelwenn (lanodan) Monnier" <contact@hacktivis.me>
Date: Mon, 15 Jun 2020 05:18:30 +0200
Subject: [PATCH] question_validator: fix for mastodon poll expiration

Mastodon activities do not have a "closed" field, this could be seen on
https://pouet.it/users/lanodan_tmp/statuses/104345126997708380
which runs Mastodon 3.1.4 (SDF runs 3.1.2)
---
 .../object_validators/question_validator.ex        | 10 ++++++++++
 lib/pleroma/web/mastodon_api/views/poll_view.ex    | 14 ++++++--------
 test/fixtures/mastodon-question-activity.json      |  1 -
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex
index f94d79352..605cb56f8 100644
--- a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex
+++ b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex
@@ -71,7 +71,17 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionValidator do
     |> changeset(data)
   end
 
+  def fix(data) do
+    cond do
+      is_binary(data["closed"]) -> data
+      is_binary(data["endTime"]) -> Map.put(data, "closed", data["endTime"])
+      true -> Map.drop(data, ["closed"])
+    end
+  end
+
   def changeset(struct, data) do
+    data = fix(data)
+
     struct
     |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf])
     |> cast_embed(:anyOf)
diff --git a/lib/pleroma/web/mastodon_api/views/poll_view.ex b/lib/pleroma/web/mastodon_api/views/poll_view.ex
index 73c990e2e..ce595ae8a 100644
--- a/lib/pleroma/web/mastodon_api/views/poll_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/poll_view.ex
@@ -40,15 +40,13 @@ defmodule Pleroma.Web.MastodonAPI.PollView do
   end
 
   defp end_time_and_expired(object) do
-    case object.data["closed"] || object.data["endTime"] do
-      end_time when is_binary(end_time) ->
-        end_time = NaiveDateTime.from_iso8601!(end_time)
-        expired = NaiveDateTime.compare(end_time, NaiveDateTime.utc_now()) == :lt
+    if object.data["closed"] do
+      end_time = NaiveDateTime.from_iso8601!(object.data["closed"])
+      expired = NaiveDateTime.compare(end_time, NaiveDateTime.utc_now()) == :lt
 
-        {Utils.to_masto_date(end_time), expired}
-
-      _ ->
-        {nil, false}
+      {Utils.to_masto_date(end_time), expired}
+    else
+      {nil, false}
     end
   end
 
diff --git a/test/fixtures/mastodon-question-activity.json b/test/fixtures/mastodon-question-activity.json
index ac329c7d5..3648b9f90 100644
--- a/test/fixtures/mastodon-question-activity.json
+++ b/test/fixtures/mastodon-question-activity.json
@@ -49,7 +49,6 @@
       "en": "<p>Why is Tenshi eating a corndog so cute?</p>"
     },
     "endTime": "2019-05-11T09:03:36Z",
-    "closed": "2019-05-11T09:03:36Z",
     "attachment": [],
     "tag": [],
     "replies": {
-- 
2.49.0