GetStream / swift-activity-feed

Stream Swift iOS Activity Feed Components
https://getstream.io/
BSD 3-Clause "New" or "Revised" License
30 stars 17 forks source link

JSON decoding error #12

Closed AndrewRudyk closed 4 years ago

AndrewRudyk commented 4 years ago

Hello. I have lates version Swift-activity-feed (2.2.3). Initialization and UserRegistration is successful. When I am trying to load Activity I have an error: "JSON decoding error: The data couldn’t be read because it isn’t in the correct format.." JSON in logs is readable and has all the info about my Activitys(Posts).

I use code for request:

guard let feedId = FeedId(feedSlug: "feed_one_flat") else { return }
let timelineFlatFeed = Client.shared.flatFeed(feedId)
let presenter = FlatFeedPresenter<GetStreamActivityFeed.Activity>(flatFeed: timelineFlatFeed)
presenter.load { (error) in
       if let error = error {
            print("error: ", error)
        } else {
            print(presenter.items)
        }
}

We are trying to send an object in actor property on the backend. Here is code snippet from the backend:

const stream = require('getstream');
const client = stream.connect(
  process.env.GET_STREAM_API_KEY,
  process.env.GET_STREAM_API_SECRET,
  process.env.GET_STREAM_APP_ID,
);
const getClient = () => client;
const getFeed = (id) => client.feed('feed_one_flat', id);
const activity = {
    actor: {
      id: posterProfileId,
      data: {
        name: null,
        profileImage: null,
      },
    },
    verb: 'add',
    object: postId,
    foreign_id: postId,
    time: createdAt,
    postId,
    posterProfileId,
    descriptionText,
    titleText,
    postType,
  };
  const userFeed = getFeed(posterProfileId);
  const result = await userFeed.addActivity(activity);

In the mobile app we are getting this response and the same error. It look likes actor is still treated as a string, not an object:

{
  "results" : [
    {
      "id" : "35a9e590-bac2-11ea-8080-80003bb39bb0",
      "reaction_counts" : {

      },
      "videoIds" : [
        "PwwzKECB01I01eM4cZsR6m500003ZOFIsagjj8lzSjtZt6E"
      ],
      "verb" : "add",
      "descriptionText" : "dt",
      "posterProfileId" : "w75K3rXL9EF4iuy7rkrW",
      "time" : "2020-06-30T11:09:47.497000",
      "postType" : "BandsPost",
      "latest_reactions" : {

      },
      "imageIds" : [
        "WhctNcSzztTfDQM2KuHC"
      ],
      "object" : "c4c29Pt49gENVQmgzcF3",
      "actor" : "{\"data\":{\"name\":null,\"profileImage\":null},\"id\":\"w75K3rXL9EF4iuy7rkrW\"}",
      "origin" : null,
      "latest_reactions_extra" : {

      },
      "own_reactions" : {

      },
      "postId" : "c4c29Pt49gENVQmgzcF3",
      "target" : "",
      "titleText" : "tt",
      "foreign_id" : "c4c29Pt49gENVQmgzcF3"
    }
  ],
  "next" : "",
  "duration" : "9.05ms"
}

Actor property: "actor" : "{"data":{"name":null,"profileImage":null},"id":"w75K3rXL9EF4iuy7rkrW"}",

b-onc commented 4 years ago

Hello @AndrewRudyk Firstly, the correct place for this question is stream-js repo since the problem arises from how the user is sent from js backend. If the user is written as a literal (as you've did), stream-js fails to convert it to a valid stream object and encodes as string. There are 2 solutions here:

  1. Get the user object as a valid stream object:
    const actor = client.user(posterProfileId).get();
    const activity = {
    actor: actor,
    ..
    }
  2. Embed the actor id as SU:<actor Id> and on client, request it with enrich: true (this is by default):
    const activity = {
    actor: "SU:"+posterProfileId,
    ..
    }

    I'm not a js developer so apologizes for any errors in my js code. I'd suggest going with 1, since it's the safer option.

AndrewRudyk commented 4 years ago

Thanks) We will try now your advice.

AndrewRudyk commented 4 years ago

Second solution is working. Thanks.