Skip to content

Commit

Permalink
more validations & types
Browse files Browse the repository at this point in the history
  • Loading branch information
michelson committed Sep 12, 2023
1 parent 472cf01 commit 4ba46eb
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
91 changes: 57 additions & 34 deletions lib/activitypub/activity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,56 +6,79 @@ module ActivityPub
# This class provides methods for creating, validating, and processing activities
# in accordance with the ActivityPub standard.
class Activity
# The type of the activity, e.g., 'Create', 'Like', 'Follow', etc.
attr_accessor :type
attr_accessor :id, :type, :actor, :object, :target, :published, :to, :cc, :bcc, :context

# The actor performing the activity.
attr_accessor :actor

# The object of the activity, e.g., a post, a comment, etc.
attr_accessor :object

# Any additional properties can go here.
# ...

def initialize(type:, actor:, object:, **other_attributes)
@type = type
@actor = actor
@object = object
# Initializes a new Activity instance.
def initialize(attributes = {})
@id = attributes[:id]
@type = attributes[:type]
@actor = attributes[:actor]
@object = attributes[:object]
@target = attributes[:target]
@published = attributes[:published] || Time.now.utc.iso8601
@to = attributes[:to]
@cc = attributes[:cc]
@bcc = attributes[:bcc]
@context = attributes[:context]
end

# Handle other attributes as needed
other_attributes.each do |key, value|
instance_variable_set("@#{key}", value)
end
# Validate the activity attributes.
def valid?
validate_type && validate_actor && validate_object
end

# Convert the activity into a hash representation.
# Convert the Activity object into a hash representation.
def to_h
{
'@context': "https://www.w3.org/ns/activitystreams",
id: @id,
type: @type,
actor: @actor,
object: @object
# Add any other attributes as needed.
# ...
object: @object,
target: @target,
published: @published,
to: @to,
cc: @cc,
bcc: @bcc,
context: @context
}
end

# Convert the activity into a JSON representation.
def to_json(*args)
to_h.to_json(*args)
end

# Load an activity from a hash.
# Generate an Activity object from a given hash.
def self.from_h(hash)
new(
Activity.new(
id: hash["id"],
type: hash["type"],
actor: hash["actor"],
object: hash["object"]
# Handle any other attributes as needed.
# ...
object: hash["object"],
target: hash["target"],
published: hash["published"],
to: hash["to"],
cc: hash["cc"],
bcc: hash["bcc"],
context: hash["context"]
)
end

private

# Validate the type attribute. ActivityStreams specifies a set of core activity types.
# For simplicity, let's validate against a subset of them.
def validate_type
valid_types = %w[Create Update Delete Follow Like Add Remove Block Undo]
valid_types.include?(@type)
end

# Validate the actor attribute. For this example, we'll just ensure it's present.
def validate_actor
!@actor.nil? && !@actor.empty?
end

# Validate the object attribute. For this example, we'll ensure it's present.
def validate_object
!@object.nil? && !@object.empty?
end

# Additional validations and methods based on scenarios can be added here.
end
end

Expand Down
3 changes: 2 additions & 1 deletion spec/activity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
subject { described_class.new(type: "Like", actor: "https://example.com/users/alice", object: "https://example.com/posts/1") }

it "converts the activity to a JSON representation" do
json_output = subject.to_json
json_output = subject.to_h.to_json

parsed_output = JSON.parse(json_output)

expect(parsed_output["type"]).to eq("Like")
Expand Down

0 comments on commit 4ba46eb

Please sign in to comment.