Class: Meshchat::Node
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Meshchat::Node
- Defined in:
- lib/meshchat/models/node.rb
Constant Summary collapse
- IPV4_WITH_PORT =
/((?:(?:^|\.)(?:\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])){4})(:\d*)?/
- DOMAIN_WITH_PORT =
/(https?:\/\/)?([\da-z\.-]+)\.?([a-z\.]{2,6})([\/\w \.-]*)*[^\/](:\d*)?/
Class Method Summary collapse
- .as_json ⇒ Object
- .as_sha512 ⇒ Object
-
.diff(theirs) ⇒ Array<-,+>
Nodes only we have, and nodes only they have.
-
.for(location: nil, uid: nil, node: nil) ⇒ Node
Try to find the node, given a location, or uid.
- .from_json(json) ⇒ Object
- .import_from_file(filename) ⇒ Object
- .public_key_from_uid(uid) ⇒ Object
- .sha_preimage ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_info ⇒ Object
- #as_json ⇒ Object
- #location ⇒ Object
- #location_is_web_socket? ⇒ Boolean
- #online ⇒ Object (also: #online?)
Class Method Details
.as_json ⇒ Object
37 38 39 40 41 42 43 44 |
# 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 ⇒ Object
32 33 34 35 |
# File 'lib/meshchat/models/node.rb', line 32 def as_sha512 digest = Digest::SHA512.new digest.hexdigest sha_preimage end |
.diff(theirs) ⇒ Array<-,+>
Returns nodes only we have, and nodes only they have.
61 62 63 64 65 66 67 |
# 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) ⇒ Node
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
88 89 90 91 92 93 94 95 96 97 98 99 100 |
# 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) ⇒ Object
46 47 48 49 50 51 52 53 |
# 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) ⇒ Object
69 70 71 72 73 74 75 76 77 |
# 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. end |
.public_key_from_uid(uid) ⇒ Object
55 56 57 |
# File 'lib/meshchat/models/node.rb', line 55 def public_key_from_uid(uid) find_by_uid(uid).try(:public_key) end |
.sha_preimage ⇒ Object
28 29 30 |
# File 'lib/meshchat/models/node.rb', line 28 def sha_preimage all.map(&:public_key).sort.join(',') end |
Instance Method Details
#==(other) ⇒ Object
103 104 105 106 107 108 109 110 111 |
# 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 ⇒ Object
136 137 138 |
# File 'lib/meshchat/models/node.rb', line 136 def as_info "#{alias_name}@#{location}" end |
#as_json ⇒ Object
127 128 129 130 131 132 133 134 |
# 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 ⇒ Object
118 119 120 121 |
# 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? ⇒ Boolean
123 124 125 |
# File 'lib/meshchat/models/node.rb', line 123 def location_is_web_socket? location.match(/wss?/).present? end |
#online ⇒ Object Also known as: online?
113 114 115 |
# File 'lib/meshchat/models/node.rb', line 113 def online on_relay? || on_local_network? end |