0

I'm trying to setup stream-framework the one here not the newer getstream. I've setup the Redis server and the environment properly, the issue I'm facing is in creating the activities for a user.

I've been trying to create activities, following the documentation to add an activity but it gives me an error message as follows:

...
  File "/Users/.../stream_framework/activity.py", line 110, in serialization_id
    if self.object_id >= 10 ** 10 or self.verb.id >= 10 ** 3:
AttributeError: 'int' object has no attribute 'id'

Here is the code

from stream_framework.activity import Activity
from stream_framework.feeds.redis import RedisFeed


class PinFeed(RedisFeed):
    key_format = 'feed:normal:%(user_id)s'

class UserPinFeed(PinFeed):
    key_format = 'feed:user:%(user_id)s'

feed = UserPinFeed(13)
print(feed)

activity = Activity(
    actor=13, # Thierry's user id
    verb=1, # The id associated with the Pin verb
    object=1, # The id of the newly created Pin object
)

feed.add(activity) # Error at this line

I think there is something missing in the documentation or maybe I'm doing something wrong. I'll be very grateful if anyone helps me get the stream framework working properly.

Saad
  • 3,340
  • 2
  • 10
  • 32

2 Answers2

1

The documentation is inconsistent. The verb you pass to the activity should be (an instance of?*) a subclass of stream_framework.verbs.base.Verb. Check out this documentation page on custom verbs and the tests for this class.

The following should fix the error you posted:

from stream_framework.activity import Activity
from stream_framework.feeds.redis import RedisFeed
from stream_framework.verbs import register
from stream_framework.verbs.base import Verb

class PinFeed(RedisFeed):
    key_format = 'feed:normal:%(user_id)s'

class UserPinFeed(PinFeed):
    key_format = 'feed:user:%(user_id)s'

class Pin(Verb):
    id = 5
    infinitive = 'pin'
    past_tense = 'pinned'

register(Pin)

feed = UserPinFeed(13)

activity = Activity(
    actor=13,
    verb=Pin,
    object=1,
)

feed.add(activity)

I quickly looked over the code for Activity and it looks like passing ints for actor and object should work. However, it is possible that these parameters are also outdated in the documentation.


* The tests pass in classes as verb. However, the Verb base class has the methods serialize and __str__ that can only be meaningfully invoked if you have an object of this class. So I'm still unsure which is required here. It seems like in the current state, the framework never calls these methods, so classes still work, but I feel like the author originally intended to pass instances.

He3lixxx
  • 3,263
  • 1
  • 12
  • 31
  • You are not wrong but the problem is with the actor and your code is giving me the same error. Please try to run your code or is it working for you?. Thanks – Saad May 29 '21 at 14:52
  • I can not test my code as I have neither `stream_framework` nor redis installed. The original error you posted was that the activity can not access `verb.id`, now you say the error has to do with the `actor`. Can you add the error you get after fixing the verb to your question? – He3lixxx May 29 '21 at 14:58
  • It is very simple to install... and also it is a bad practice to post an answer that you yourself don't know if it works or not. – Saad May 29 '21 at 15:04
  • Thanks for sharing this, I've tested your code and I think I've found the main issue to the problem your code indeed is correct. I'll share the same after a complete investigation. – Saad May 29 '21 at 15:24
0

With the help of great answer by @He3lixxx, I was able to solve it partially. As the package is no more maintained, the package installs the latest Redis client for python which was creating too many issues so by installation redis-2.10.5 if using stream-framework-1.3.7, should fix the issue.

I would also like to add a complete guide to properly add activity to a user feed.

Key points:

  • If you are not using feed manager, then make sure to first insert the activity before you add it to the user with feed.insert_activity(activity) method.

  • In case of getting feeds with feed[:] throws an error something like below:

    File "/Users/.../stream_framework/activity.py", line 44, in get_hydrated
          activity = activities[int(self.serialization_id)]
      KeyError: 16223026351730000000001005L
    

    then you need to clear data for that user using the key format for it in my case the key is feed:user:13 for user 13, delete it with DEL feed:user:13, In case if that doesn't fix the issue then you can FLUSHALL which will delete everything from Redis.

Sample code:

from stream_framework.activity import Activity
from stream_framework.feeds.redis import RedisFeed
from stream_framework.verbs import register
from stream_framework.verbs.base import Verb

class PinFeed(RedisFeed):
    key_format = 'feed:normal:%(user_id)s'

class UserPinFeed(PinFeed):
    key_format = 'feed:user:%(user_id)s'

class Pin(Verb):
    id = 5
    infinitive = 'pin'
    past_tense = 'pinned'
register(Pin)


feed = UserPinFeed(13)
print(feed[:])

activity = Activity(
    actor=13,
    verb=Pin,
    object=1)

feed.insert_activity(activity)
activity_id = feed.add(activity)

print(activity_id)
print(feed[:])
Saad
  • 3,340
  • 2
  • 10
  • 32