Class: Libertree::Model::Member
- Inherits:
-
Object
- Object
- Libertree::Model::Member
- Defined in:
- lib/libertree/model/member.rb
Class Method Summary collapse
- .create(*args) ⇒ Object
- .search(name:, include_old: false) ⇒ Object
-
.with_display_name(name) ⇒ Object
TODO: this is a temporary helper because with_handle no longer includes display name matches.
- .with_handle(h) ⇒ Object
Instance Method Summary collapse
- #account ⇒ Object
- #after_create ⇒ Object
- #after_update ⇒ Object
- #before_destroy ⇒ Object
- #comments(n = 10) ⇒ Object
- #delete_cascade ⇒ Object
- #dirty ⇒ Object
- #distribute ⇒ Object
- #groups ⇒ Object
- #handle ⇒ Object
- #local? ⇒ Boolean
- #name_display ⇒ Object
- #online? ⇒ Boolean
- #pools ⇒ Object
- #posts(opts = {}) ⇒ Object
-
#profile ⇒ Object
TODO: DB: association.
-
#server ⇒ Object
(also: #tree)
TODO: DB: association many_to_one :server.
- #springs ⇒ Object
- #username ⇒ Object
Class Method Details
.create(*args) ⇒ Object
126 127 128 129 130 |
# File 'lib/libertree/model/member.rb', line 126 def self.create(*args) member = super Profile.create( member_id: member.id ) member end |
.search(name:, include_old: false) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/libertree/model/member.rb', line 178 def self.search(name:, include_old: false) if include_old newness_clause = "TRUE" else newness_clause = %{ EXISTS ( SELECT 1 FROM posts po WHERE po.member_id = m.id AND po.time_created > NOW() - '30 days'::INTERVAL ) OR EXISTS ( SELECT 1 FROM comments c WHERE c.member_id = m.id AND c.time_created > NOW() - '30 days'::INTERVAL ) } end self.s( %{ SELECT m.* FROM members m LEFT OUTER JOIN accounts a ON (a.id = m.account_id) LEFT OUTER JOIN profiles p ON (m.id = p.member_id) WHERE ( m.username ILIKE '%' || ? || '%' OR a.username ILIKE '%' || ? || '%' OR p.name_display ILIKE '%' || ? || '%' ) AND ( #{newness_clause} ) }, name, name, name ) end |
.with_display_name(name) ⇒ Object
TODO: this is a temporary helper because with_handle no longer includes display name matches. This will eventually be removed when river :from queries no longer accept display names.
105 106 107 108 109 110 111 |
# File 'lib/libertree/model/member.rb', line 105 def self.with_display_name(name) self.qualify. join(:profiles, :member_id=>:id). where(Sequel.qualify(:profiles, :name_display) => name). limit(1). first end |
.with_handle(h) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/libertree/model/member.rb', line 68 def self.with_handle(h) if h =~ /^(.+?)@(.+?)$/ username = $1 host = $2 if host == Server.own_domain local = true end else username = h local = true end if local self.qualify. join(:accounts, :id=>:account_id). where(Sequel.qualify(:accounts, :username) => username). limit(1). first else # TODO: servers.name_given is no longer used. Remove it after # migrating user rivers/contact lists etc. self.qualify .join(:servers, :id=>:server_id) .where(Sequel.qualify(:members, :username) => username) .where( Sequel.or( Sequel.qualify(:servers, :domain) => host, Sequel.qualify(:servers, :name_given) => host ) ) .limit(1) .first end end |
Instance Method Details
#account ⇒ Object
45 46 47 |
# File 'lib/libertree/model/member.rb', line 45 def account @account ||= Account[self.account_id] end |
#after_create ⇒ Object
5 6 7 8 |
# File 'lib/libertree/model/member.rb', line 5 def after_create super self.distribute end |
#after_update ⇒ Object
10 11 12 13 |
# File 'lib/libertree/model/member.rb', line 10 def after_update super self.distribute end |
#before_destroy ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/libertree/model/member.rb', line 15 def before_destroy # TODO: expand later for more granularity: # - only abandon (not delete) posts and comments # - only empty posts if they contain a discussion # - only empty and anonymise comments # - etc. if self.local? Libertree::Model::Job.create_for_forests( { task: 'request:MEMBER-DELETE', params: { 'username' => self.account.username, } } ) end super end |
#comments(n = 10) ⇒ Object
151 152 153 |
# File 'lib/libertree/model/member.rb', line 151 def comments(n = 10) Comment.where(member_id: self.id).reverse_order(:id).limit(n) end |
#delete_cascade ⇒ Object
166 167 168 |
# File 'lib/libertree/model/member.rb', line 166 def delete_cascade DB.dbh[ "SELECT delete_cascade_member(?)", self.id ].get end |
#dirty ⇒ Object
170 171 172 173 174 175 176 |
# File 'lib/libertree/model/member.rb', line 170 def dirty @account = nil @name_display = nil @profile = nil @pools = nil @springs = nil end |
#distribute ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/libertree/model/member.rb', line 32 def distribute return if ! self.local? Libertree::Model::Job.create_for_forests( { task: 'request:MEMBER', params: { 'username' => self.account.username } } ) end |
#groups ⇒ Object
222 223 224 |
# File 'lib/libertree/model/member.rb', line 222 def groups Libertree::Model::GroupMember.where(member_id: self.id).map { |gm| gm.group } end |
#handle ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/libertree/model/member.rb', line 60 def handle if self.username && self.server self.username + "@#{self.server.name_display}" elsif account account.username + "@#{Server.own_domain}" end end |
#local? ⇒ Boolean
41 42 43 |
# File 'lib/libertree/model/member.rb', line 41 def local? ! self.account.nil? end |
#name_display ⇒ Object
56 57 58 |
# File 'lib/libertree/model/member.rb', line 56 def name_display @name_display ||= ( profile && profile.name_display || self.username ) end |
#online? ⇒ Boolean
162 163 164 |
# File 'lib/libertree/model/member.rb', line 162 def online? self.account && self.account.online? end |
#pools ⇒ Object
155 156 157 |
# File 'lib/libertree/model/member.rb', line 155 def pools @pools ||= Pool.where( member_id: self.id ).all end |
#posts(opts = {}) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/libertree/model/member.rb', line 132 def posts( opts = {} ) limit = opts.fetch(:limit, 30) time = Time.at( opts.fetch(:time, Time.now.to_f) ).strftime("%Y-%m-%d %H:%M:%S.%6N%z") time_clause = if opts[:newer] proc { time_created > time } else proc { time_created < time } end res = Post.where(member_id: self.id). where(&time_clause). reverse_order(:time_created). limit(limit) # optionally restrict to Internet visible posts res = res.where(visibility: 'internet') if opts[:public] res end |
#profile ⇒ Object
TODO: DB: association
122 123 124 |
# File 'lib/libertree/model/member.rb', line 122 def profile @profile ||= Profile[ member_id: self.id ] end |
#server ⇒ Object Also known as: tree
TODO: DB: association many_to_one :server
51 52 53 |
# File 'lib/libertree/model/member.rb', line 51 def server @server ||= Server[self.server_id] end |
#springs ⇒ Object
158 159 160 |
# File 'lib/libertree/model/member.rb', line 158 def springs @springs ||= Pool.where( member_id: self.id, sprung: true ).all end |
#username ⇒ Object
113 114 115 116 117 118 119 |
# File 'lib/libertree/model/member.rb', line 113 def username if val = super val elsif a = self.account a.username end end |