sprinkle a little documentation around lib, mostly just specifying return types
This commit is contained in:
parent
2947ee46f4
commit
2022187942
9 changed files with 46 additions and 11 deletions
|
|
@ -3,18 +3,21 @@
|
||||||
# the COPYRIGHT file.
|
# the COPYRIGHT file.
|
||||||
|
|
||||||
class PostVisibility < ActiveRecord::Base
|
class PostVisibility < ActiveRecord::Base
|
||||||
|
|
||||||
belongs_to :contact
|
belongs_to :contact
|
||||||
belongs_to :post
|
belongs_to :post
|
||||||
|
|
||||||
|
# Perform a batch import, given a set of contacts and a post
|
||||||
|
# @note performs a bulk insert in mySQL; performs linear insertions in postgres
|
||||||
|
# @param contacts [Array<Contact>] Recipients
|
||||||
|
# @param post [Post]
|
||||||
|
# @return [void]
|
||||||
def self.batch_import(contacts, post)
|
def self.batch_import(contacts, post)
|
||||||
if postgres?
|
if postgres?
|
||||||
# Take the naive approach to inserting our new visibilities for now.
|
|
||||||
contacts.each do |contact|
|
contacts.each do |contact|
|
||||||
PostVisibility.find_or_create_by_contact_id_and_post_id(contact.id, post.id)
|
PostVisibility.find_or_create_by_contact_id_and_post_id(contact.id, post.id)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
# Use a batch insert on mySQL.
|
|
||||||
new_post_visibilities = contacts.map do |contact|
|
new_post_visibilities = contacts.map do |contact|
|
||||||
PostVisibility.new(:contact_id => contact.id, :post_id => post.id)
|
PostVisibility.new(:contact_id => contact.id, :post_id => post.id)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -127,7 +127,7 @@ class Profile < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
|
|
||||||
# Constructs a full name by joining #first_name and #last_name
|
# Constructs a full name by joining #first_name and #last_name
|
||||||
# @returns [String] A full name
|
# @return [String] A full name
|
||||||
def construct_full_name
|
def construct_full_name
|
||||||
self.full_name = [self.first_name, self.last_name].join(' ').downcase
|
self.full_name = [self.first_name, self.last_name].join(' ').downcase
|
||||||
self.full_name
|
self.full_name
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ class AspectStream
|
||||||
@people ||= Person.all_from_aspects(aspect_ids, @user)
|
@people ||= Person.all_from_aspects(aspect_ids, @user)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# The first aspect in #aspects, given the stream is not for all aspects, or #aspects size is 1
|
||||||
# @note aspects.first is used for mobile. NOTE(this is a hack and should be fixed)
|
# @note aspects.first is used for mobile. NOTE(this is a hack and should be fixed)
|
||||||
# @return [Aspect,Symbol]
|
# @return [Aspect,Symbol]
|
||||||
def aspect
|
def aspect
|
||||||
|
|
|
||||||
|
|
@ -16,28 +16,36 @@ module Postzord
|
||||||
notify_users
|
notify_users
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Batch import visibilities for the recipients of the given @post
|
||||||
|
# @note performs a bulk insert into mySQL
|
||||||
|
# @return [void]
|
||||||
def create_visibilities
|
def create_visibilities
|
||||||
contacts = Contact.where(:user_id => @recipient_user_ids, :person_id => @post.author_id)
|
contacts = Contact.where(:user_id => @recipient_user_ids, :person_id => @post.author_id)
|
||||||
PostVisibility.batch_import(contacts, post)
|
PostVisibility.batch_import(contacts, post)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Issue websocket requests to all specified recipients
|
||||||
|
# @return [void]
|
||||||
def socket_to_users
|
def socket_to_users
|
||||||
@users.each do |user|
|
@users.each do |user|
|
||||||
@post.socket_to_user(user)
|
@post.socket_to_user(user)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Notify any mentioned users within the @post's text
|
||||||
|
# @return [void]
|
||||||
def notify_mentioned_users
|
def notify_mentioned_users
|
||||||
@post.mentions.each do |mention|
|
@post.mentions.each do |mention|
|
||||||
mention.notify_recipient
|
mention.notify_recipient
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Notify users of the new post
|
||||||
|
# return [void]
|
||||||
def notify_users
|
def notify_users
|
||||||
if @post.respond_to?(:notification_type)
|
return unless @post.respond_to?(:notification_type)
|
||||||
@users.each do |user|
|
@users.each do |user|
|
||||||
Notification.notify(user, @post, @post.author)
|
Notification.notify(user, @post, @post.author)
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -43,7 +43,6 @@ module Postzord
|
||||||
obj
|
obj
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
protected
|
protected
|
||||||
def salmon
|
def salmon
|
||||||
@salmon ||= Salmon::EncryptedSlap.from_xml(@salmon_xml, @user)
|
@salmon ||= Salmon::EncryptedSlap.from_xml(@salmon_xml, @user)
|
||||||
|
|
|
||||||
|
|
@ -29,6 +29,7 @@ module Postzord
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [Object]
|
||||||
def receive_relayable
|
def receive_relayable
|
||||||
if @object.parent.author.local?
|
if @object.parent.author.local?
|
||||||
# receive relayable object only for the owner of the parent object
|
# receive relayable object only for the owner of the parent object
|
||||||
|
|
@ -37,6 +38,7 @@ module Postzord
|
||||||
# notify everyone who can see the parent object
|
# notify everyone who can see the parent object
|
||||||
receiver = Postzord::Receiver::LocalPostBatch.new(nil, self.recipient_user_ids)
|
receiver = Postzord::Receiver::LocalPostBatch.new(nil, self.recipient_user_ids)
|
||||||
receiver.notify_users
|
receiver.notify_users
|
||||||
|
@object
|
||||||
end
|
end
|
||||||
|
|
||||||
# @return [Object]
|
# @return [Object]
|
||||||
|
|
@ -51,9 +53,9 @@ module Postzord
|
||||||
User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id }
|
User.all_sharing_with_person(@author).select('users.id').map!{ |u| u.id }
|
||||||
end
|
end
|
||||||
|
|
||||||
class RelayableObjectWithoutParent < StandardError ; ; end
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
# @return [Boolean]
|
||||||
def object_can_be_public_and_it_is_not?
|
def object_can_be_public_and_it_is_not?
|
||||||
@object.respond_to?(:public) && !@object.public?
|
@object.respond_to?(:public) && !@object.public?
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,9 @@
|
||||||
|
|
||||||
module Salmon
|
module Salmon
|
||||||
class EncryptedSlap < Slap
|
class EncryptedSlap < Slap
|
||||||
|
|
||||||
|
# Construct an encrypted header
|
||||||
|
# @return [String] Header XML
|
||||||
def header(person)
|
def header(person)
|
||||||
<<XML
|
<<XML
|
||||||
<encrypted_header>
|
<encrypted_header>
|
||||||
|
|
@ -12,16 +15,21 @@ module Salmon
|
||||||
XML
|
XML
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Decrypts an encrypted magic sig envelope
|
||||||
|
# @param key_hash [Hash] Contains 'key' (aes) and 'iv' values
|
||||||
|
# @param user [User]
|
||||||
def parse_data(key_hash, user)
|
def parse_data(key_hash, user)
|
||||||
user.aes_decrypt(super, key_hash)
|
user.aes_decrypt(super, key_hash)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Decrypts and parses out the salmon header
|
||||||
# @return [Nokogiri::Doc]
|
# @return [Nokogiri::Doc]
|
||||||
def salmon_header(doc, user)
|
def salmon_header(doc, user)
|
||||||
header = user.decrypt(doc.search('encrypted_header').text)
|
header = user.decrypt(doc.search('encrypted_header').text)
|
||||||
Nokogiri::XML(header)
|
Nokogiri::XML(header)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Encrypt the magic sig
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def self.payload(activity, user, aes_key_hash)
|
def self.payload(activity, user, aes_key_hash)
|
||||||
user.person.aes_encrypt(activity, aes_key_hash)
|
user.person.aes_encrypt(activity, aes_key_hash)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,10 @@
|
||||||
|
|
||||||
module Salmon
|
module Salmon
|
||||||
class MagicSigEnvelope
|
class MagicSigEnvelope
|
||||||
|
|
||||||
attr_accessor :data, :data_type, :encoding, :alg, :sig, :author
|
attr_accessor :data, :data_type, :encoding, :alg, :sig, :author
|
||||||
|
|
||||||
|
# @return [MagicSigEnvelope]
|
||||||
def self.parse(doc)
|
def self.parse(doc)
|
||||||
env = self.new
|
env = self.new
|
||||||
ns = {'me'=>'http://salmon-protocol.org/ns/magic-env'}
|
ns = {'me'=>'http://salmon-protocol.org/ns/magic-env'}
|
||||||
|
|
@ -27,6 +30,7 @@ module Salmon
|
||||||
env
|
env
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [MagicSigEnvelope]
|
||||||
def self.create(user, activity)
|
def self.create(user, activity)
|
||||||
env = MagicSigEnvelope.new
|
env = MagicSigEnvelope.new
|
||||||
env.author = user.person
|
env.author = user.person
|
||||||
|
|
@ -42,10 +46,12 @@ module Salmon
|
||||||
env
|
env
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [String]
|
||||||
def signable_string
|
def signable_string
|
||||||
[@data, Base64.urlsafe_encode64(@data_type),Base64.urlsafe_encode64(@encoding), Base64.urlsafe_encode64(@alg)].join(".")
|
[@data, Base64.urlsafe_encode64(@data_type),Base64.urlsafe_encode64(@encoding), Base64.urlsafe_encode64(@alg)].join(".")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [String]
|
||||||
def to_xml
|
def to_xml
|
||||||
<<ENTRY
|
<<ENTRY
|
||||||
<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
|
<me:env xmlns:me="http://salmon-protocol.org/ns/magic-env">
|
||||||
|
|
@ -57,14 +63,17 @@ module Salmon
|
||||||
ENTRY
|
ENTRY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [String]
|
||||||
def get_encoding
|
def get_encoding
|
||||||
'base64url'
|
'base64url'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [String]
|
||||||
def get_data_type
|
def get_data_type
|
||||||
'application/atom+xml'
|
'application/atom+xml'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [String]
|
||||||
def get_alg
|
def get_alg
|
||||||
'RSA-SHA256'
|
'RSA-SHA256'
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,6 @@ module Salmon
|
||||||
slap
|
slap
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
# @return [String]
|
# @return [String]
|
||||||
def self.payload(activity, user=nil, aes_key_hash=nil)
|
def self.payload(activity, user=nil, aes_key_hash=nil)
|
||||||
activity
|
activity
|
||||||
|
|
@ -62,6 +61,7 @@ module Salmon
|
||||||
doc.search('header')
|
doc.search('header')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [String] The constructed salmon, given a person
|
||||||
def xml_for(person)
|
def xml_for(person)
|
||||||
xml =<<ENTRY
|
xml =<<ENTRY
|
||||||
<?xml version='1.0' encoding='UTF-8'?>
|
<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
|
@ -72,10 +72,14 @@ module Salmon
|
||||||
ENTRY
|
ENTRY
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Wraps plaintext header in <header></header> tags
|
||||||
|
# @return [String] Header XML
|
||||||
def header(person)
|
def header(person)
|
||||||
"<header>#{plaintext_header}</header>"
|
"<header>#{plaintext_header}</header>"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# Generate a plaintext salmon header (unencrypted), sans <header></header> tags
|
||||||
|
# @return [String] Header XML (sans <header></header> tags)
|
||||||
def plaintext_header
|
def plaintext_header
|
||||||
header =<<HEADER
|
header =<<HEADER
|
||||||
<iv>#{iv}</iv>
|
<iv>#{iv}</iv>
|
||||||
|
|
@ -87,6 +91,7 @@ ENTRY
|
||||||
HEADER
|
HEADER
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @return [Person] Author of the salmon object
|
||||||
def author
|
def author
|
||||||
if @author.nil?
|
if @author.nil?
|
||||||
@author ||= Person.by_account_identifier @author_email
|
@author ||= Person.by_account_identifier @author_email
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue