Module: ActivityPub

Defined in:
lib/activitypub.rb,
lib/activitypub/uri.rb,
lib/activitypub/base.rb,
lib/activitypub/types.rb,
lib/activitypub/version.rb,
lib/activitypub/resolvers.rb,
lib/activitypub/webfinger.rb

Defined Under Namespace

Classes: Accept, Activity, Actor, Add, Announce, Application, Arrive, Article, Audio, Base, Block, Collection, CollectionPage, Create, Delete, Dislike, Document, Error, Event, Flag, Follow, Group, Hashtag, Ignore, Image, IntransitiveActivity, Invite, Join, Leave, Like, Link, Listen, Mention, Move, Note, Object, Offer, OrderedCollection, OrderedCollectionPage, Page, Person, Place, Profile, PropertyValue, Question, Reject, Relationship, Remove, Service, TentativeAccept, TentativeReject, Tombstone, Travel, URI, Undo, UnsafeResolver, Update, Video, View, WebFingerResult, WebResolver

Constant Summary collapse

VERSION =
"0.5.5"

Class Method Summary collapse

Class Method Details

.from_hash(h, resolver: nil, allow_unknown: false) ⇒ Object

Raises:



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/activitypub/base.rb', line 13

def self.from_hash(h, resolver: nil, allow_unknown: false)
  type = h&.dig("type")

  raise Error, "'type' attribute is required" if !type
  raise NameError, "'type' attribute with '::' is not allowed" if !type.index("::").nil?

  # FIXME: May need a way to override/handle custom namespaces.

  if allow_unknown && !ActivityPub.const_defined?(type)
    klass = ActivityPub::Object
  else
    klass = ActivityPub.const_get(type)
  end

  ob = klass ? klass.new : nil
  ob._resolver = resolver
  ob._raw = h

  # FIXME: Useful for debug. Add flag to allow enabling.
  # ob.instance_variable_set("@_raw",h)
  
  if ob
    context = h.dig("@context")
    ob.instance_variable_set("@_context", context) if context
    
    klass.ap_attributes.each do |attr|
      v = h.dig(attr.to_s)

      if v.is_a?(Hash) && v["type"]
        v = from_hash(v)
      elsif v.is_a?(Array)
        v = v.map do |av|
          av.is_a?(Hash) && av["type"] ? from_hash(av) : av
        end
      end

      if t = klass.ap_types[attr]
        v = t.new(v) if v && !v.kind_of?(ActivityPub::Base)
        v._resolver = ob._resolver if v
      end
      ob.instance_variable_set("@#{attr}", v) if !v.nil?
    end
  end

  ob
end

.from_json(json, resolver: nil, allow_unknown: false) ⇒ Object



9
10
11
# File 'lib/activitypub/base.rb', line 9

def self.from_json(json, resolver: nil, allow_unknown: false)
  from_hash(JSON.parse(json), resolver:, allow_unknown:)
end

.webfinger(acct) ⇒ Object



24
25
26
# File 'lib/activitypub/webfinger.rb', line 24

def self.webfinger(acct)
  WebFingerResult.new(WebFinger.discover!(acct))
end