GetStream / stream-swift

Swift client for Stream API
https://getstream.io
BSD 3-Clause "New" or "Revised" License
35 stars 26 forks source link

403 when adding activities to feed #15

Closed jetaggart closed 4 years ago

jetaggart commented 4 years ago

What did you do?

let client = Client(apiKey: apiKey, appId: appId, token: tokenForUser)
self.feed = client.flatFeed(feedSlug: "user")
let activity = PostActivity(actor: "user", verb: "post", object: UUID().uuidString, message: "message")
feed!.add(activity) {result in
    print(result)
}
=> failure(NotAllowedException[17] Status Code: 403, You don't have the permission to do this)

What did you expect to happen?

Activity is posted to feed

What happened instead?

I get a 404

Additional context

Is this caused because I'm using a string actor? The token is generated for the same user id (in this case "user") that the activity uses. When I use the User object, it works fine.

buh commented 4 years ago

@psylinse thanks! I'll check.

buh commented 4 years ago

Hi @psylinse sorry for the delay. Is it still an issue? The client should use an actor as the current user.

jetaggart commented 4 years ago

I was able to solve this by wrapping the string as a User. This is confusing to me as I'm not sure when a string type is allowed. I'm assuming it has to do with some actor type I need to register with Stream. For example, to get the same code to work in java I had to prefix the string with SU: (which I assume is Stream User?). The docs are not clear to me what exactly the actor is and what is allowed.

Here is my swift code:

let activity = PostActivity(actor: User(id: "user"), verb: "post", object: UUID().uuidString, message: "message")

and the related java code:

Activity.builder().actor("SU:user").verb("post").`object`(UUID.randomUUID().toString()).extraField("message", "message").build()
buh commented 4 years ago

@psylinse In Swift we use generics. It's a little bit hard at the beginning to setup, but then it's safer and easy to use. By default GetStream.Activity has a type:

public typealias Activity = EnrichedActivity<User, String, DefaultReaction>

For your PostActivity you can change the actor type as own User type and for the ObjectType. Then these types will be automatically encoded and decoded in the right way. The value of serialized User as SU:<id> should be as internal work, you shouldn't worry about it as for SO:<id>. Please let me know if you have more questions.