class Meshchat::Node

Constants

DOMAIN_WITH_PORT

rubular.com/r/WYT09ptct3

IPV4_WITH_PORT

Public Class Methods

as_json() click to toggle source
# File lib/meshchat/models/node.rb, line 37
def as_json
  # must also include ourselves
  # so that we can pass our own public key
  # to those who don't have it
  others = all.map(&:as_json)
  me = APP_CONFIG.user.identity_as_json
  others << me
end
as_sha512() click to toggle source
# File lib/meshchat/models/node.rb, line 32
def as_sha512
  digest = Digest::SHA512.new
  digest.hexdigest sha_preimage
end
diff(theirs) click to toggle source

@param [Array] theirs array of hashes representing node entries @return [Array<-,+>] nodes only we have, and nodes only they have

# File lib/meshchat/models/node.rb, line 61
def diff(theirs)
  ours = as_json
  we_only_have = ours - theirs
  they_only_have = theirs - ours

  [we_only_have, they_only_have]
end
for(location: nil, uid: nil, node: nil) click to toggle source

Try to find the node, given a location, or uid

TODO: do we want to also be able to find by relay address?

- this would be non-unique
- maybe finding should only happen via UID

@param [String] location - the local network address @param [String] uid - the node's UID @param [Node] node - the node @return [Node]

# File lib/meshchat/models/node.rb, line 88
def for(location: nil, uid: nil, node: nil)
  unless node
    node = Node.find_by_location_on_network(location) if location
    node = Node.find_by_uid(uid) if uid && !node
  end

  unless node && node.valid?
    msg =  I18n.t('node.not_found', name: location || uid || '')
    return Display.alert msg
  end

  node
end
from_json(json) click to toggle source
# File lib/meshchat/models/node.rb, line 46
def from_json(json)
  new(
    alias_name: json['alias'],
    location_on_network: json['location'],
    uid: json['uid'],
    public_key: json['publickey']
  )
end
import_from_file(filename) click to toggle source
# File lib/meshchat/models/node.rb, line 69
def import_from_file(filename)
  f = File.read(filename)
  hash = JSON.parse(f)
  n = from_json(hash)
  n.save
  n
rescue => e
  Display.alert e.message
end
public_key_from_uid(uid) click to toggle source
# File lib/meshchat/models/node.rb, line 55
def public_key_from_uid(uid)
  find_by_uid(uid).try(:public_key)
end
sha_preimage() click to toggle source
# File lib/meshchat/models/node.rb, line 28
def sha_preimage
  all.map(&:public_key).sort.join(',')
end

Public Instance Methods

==(other) click to toggle source
Calls superclass method
# File lib/meshchat/models/node.rb, line 103
def ==(other)
  result = false

  if other.is_a?(Hash)
    result = as_json.values_at(*other.keys) == other.values
  end

  result || super
end
as_info() click to toggle source
# File lib/meshchat/models/node.rb, line 136
def as_info
  "#{alias_name}@#{location}"
end
as_json() click to toggle source
# File lib/meshchat/models/node.rb, line 127
def as_json
  {
    'alias' => alias_name,
    'location' => location_on_network,
    'uid' => uid,
    'publickey' => public_key
  }
end
location() click to toggle source
# File lib/meshchat/models/node.rb, line 118
def location
  return location_of_relay if on_relay?
  location_on_network
end
location_is_web_socket?() click to toggle source
# File lib/meshchat/models/node.rb, line 123
def location_is_web_socket?
  location.match(/wss?/).present?
end
online() click to toggle source
# File lib/meshchat/models/node.rb, line 113
def online
  on_relay? || on_local_network?
end
Also aliased as: online?
online?()
Alias for: online