73 |
$self ? return $self : return undef; |
$self ? return $self : return undef; |
74 |
} |
} |
75 |
|
|
76 |
|
=head2 add_member_to_list |
77 |
|
|
78 |
|
Add new member to list |
79 |
|
|
80 |
|
$nos->add_member_to_list( |
81 |
|
list => "My list", |
82 |
|
email => "john.doe@example.com", |
83 |
|
name => "John A. Doe", |
84 |
|
); |
85 |
|
|
86 |
|
C<name> parametar is optional. |
87 |
|
|
88 |
|
Return true if user is added. |
89 |
|
|
90 |
|
=cut |
91 |
|
|
92 |
|
sub add_member_to_list { |
93 |
|
my $self = shift; |
94 |
|
|
95 |
|
my $arg = {@_}; |
96 |
|
|
97 |
|
my $email = $arg->{'email'} || confess "can't add user without e-mail"; |
98 |
|
my $name = $arg->{'name'} || ''; |
99 |
|
confess "need list name" unless ($arg->{'list'}); |
100 |
|
|
101 |
|
if (! Email::Valid->address($email)) { |
102 |
|
warn "SKIPPING $name <$email>"; |
103 |
|
return 0; |
104 |
|
} |
105 |
|
|
106 |
|
print "# $name <$email>\n"; |
107 |
|
|
108 |
|
my $lists = $self->{'loader'}->find_class('lists'); |
109 |
|
my $users = $self->{'loader'}->find_class('users'); |
110 |
|
my $user_list = $self->{'loader'}->find_class('user_list'); |
111 |
|
|
112 |
|
my $list = $lists->find_or_create({ |
113 |
|
name => $arg->{'list'}, |
114 |
|
}) || croak "can't add list ",$arg->{'list'},"\n"; |
115 |
|
|
116 |
|
my $this_user = $users->find_or_create({ |
117 |
|
email => $email, |
118 |
|
full_name => $name, |
119 |
|
}) || croak "can't find or create member\n"; |
120 |
|
|
121 |
|
my $user_on_list = $user_list->find_or_create({ |
122 |
|
user_id => $this_user->id, |
123 |
|
list_id => $list->id, |
124 |
|
}) || croak "can't add user to list"; |
125 |
|
|
126 |
|
$list->dbi_commit; |
127 |
|
$this_user->dbi_commit; |
128 |
|
$user_on_list->dbi_commit; |
129 |
|
|
130 |
|
return 1; |
131 |
|
} |
132 |
|
|
133 |
|
=head2 add_message_to_queue |
134 |
|
|
135 |
|
Adds message to one list's queue for later sending. |
136 |
|
|
137 |
|
$nos->add_message_to_queue( |
138 |
|
list => 'My list', |
139 |
|
message => 'From: My list <mylist@example.com> |
140 |
|
To: John A. Doe <john.doe@example.com> |
141 |
|
|
142 |
|
This is example message |
143 |
|
', |
144 |
|
); |
145 |
|
|
146 |
|
On success returns ID of newly created (or existing) message. |
147 |
|
|
148 |
|
=cut |
149 |
|
|
150 |
|
sub add_message_to_queue { |
151 |
|
my $self = shift; |
152 |
|
|
153 |
|
my $args = {@_}; |
154 |
|
|
155 |
|
my $list_name = $args->{'list'} || confess "need list name"; |
156 |
|
my $message_text = $args->{'message'} || croak "need message"; |
157 |
|
|
158 |
|
my $lists = $self->{'loader'}->find_class('lists'); |
159 |
|
|
160 |
|
my $this_list = $lists->search( |
161 |
|
name => $list_name, |
162 |
|
)->first || croak "can't find list $list_name"; |
163 |
|
|
164 |
|
my $messages = $self->{'loader'}->find_class('messages'); |
165 |
|
|
166 |
|
my $this_message = $messages->find_or_create({ |
167 |
|
message => $message_text |
168 |
|
}) || croak "can't insert message"; |
169 |
|
|
170 |
|
$this_message->dbi_commit() || croak "can't add message"; |
171 |
|
|
172 |
|
my $queue = $self->{'loader'}->find_class('queue'); |
173 |
|
|
174 |
|
$queue->find_or_create({ |
175 |
|
message_id => $this_message->id, |
176 |
|
list_id => $this_list->id, |
177 |
|
}) || croak "can't add message ",$this_message->id," to list ",$this_list->id, ": ",$this_list->name; |
178 |
|
|
179 |
|
$queue->dbi_commit || croak "can't add message to list ",$this_list->name; |
180 |
|
|
181 |
|
return $this_message->id; |
182 |
|
} |
183 |
|
|
184 |
|
|
185 |
=head2 send_queued_messages |
=head2 send_queued_messages |
186 |
|
|
187 |
Send queued messages or just ones for selected list |
Send queued messages or just ones for selected list |
188 |
|
|
189 |
$noc->send_queued_messages("my list"); |
$nos->send_queued_messages("My list"); |
190 |
|
|
191 |
=cut |
=cut |
192 |
|
|