blib/lib/Chat/iFly.pm untested

filter: Statementstmt 0.0% 0 / 236 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 391 CC 20 SCAR 33.3
CC
20
Cov
0%
init
90.0
get_key
42.0
get_message_thread
12.0
CRAP 28.0
1 use strict;
2 use warnings;
3 package Chat::iFly;
4  
5 use HTTP::Thin;
6 use HTTP::Request::Common;
7 use JSON;
8 use URI;
9 use Ouch;
10 use Moo;
11  
12 my $convert_to_string = sub { "$_[0]" };
13  
14 =head1 NAME
15  
16 Chat::iFly - An interface to the iFlyChat service.
17  
18 =head1 SYNOPSIS
19  
20 use Chat::iFly;
21
22 my $chat = Chat::iFly->new(
23 api_key => 'afsdadfafdsadfsafsd',
24 static_asset_base_uri => '//www.myserver.com/ifly',
25 ajax_uri => '//www.myserver.com/chat/login'
26 );
27
28 my $user = {
29 id => 4321,
30 name => 'Joe Blow',
31 avatar_uri => '//www.myserver.com/uploads/joe.blow.avatar.jpg',
32 profile_uri => '//www.myserver.com/users/4321',
33 };
34
35 my $html_to_inline_into_page = $chat->render_html($user);
36 my $response_to_chat_login = $chat->render_ajax($user);
37  
38  
39 =head1 DESCRIPTION
40  
41 A wrapper needed to authenticate to L<iflychat.com>.
42  
43 =head2 Setup
44  
45 You'll need to go here L<https://iflychat.com/iflyapi/index> and register for an API Key. You'll specify that using C<api_key> passed to the constructor.
46  
47 You'll need to copy the C<public> folder from this distribution onto your web server somewhere. You'll specify where that is using the C<static_asset_base_uri> passed tot he constructor.
48  
49 You'll need to build 2 things into your web server:
50  
51 =over
52  
53 =item Inline HTML
54  
55 You'll need to inline the result of C<render_html> into any web page where you want the chat to appear.
56  
57 =item AJAX Method
58  
59 You'll need to set up an ajax method in your app that returns the result of C<render_ajax>. You'll pass the URL where that can be found into the constructor using C<ajax_uri>.
60  
61 =back
62  
63 And finally you need to call C<update_settings> to tell the iFly servers what your settings are.
64  
65  
66 =head1 METHODS
67  
68 The following methods are available.
69  
70 =head2 new ( params )
71  
72 Constructor.
73  
74 =over
75  
76 =item params
77  
78 A hash of parameters.
79  
80 =over
81  
82 =item api_key
83  
84 Required. The key generated on the iFly Chat Dashboard.
85  
86 =cut
87  
88 has api_key => (
89 is => 'rw',
90 required => 1,
91 );
92  
93 =item static_asset_base_uri
94  
95 Required. The URL where you have installed the static files found in the C<public> folder of the L<Chat::iFly> github repository.
96  
97 =cut
98  
99 has static_asset_base_uri => (
100 is => 'rw',
101 required => 1,
102 );
103  
104 =item ajax_uri
105  
106 Required. The URL where you have installed the response to the C<render_ajax> method.
107  
108 =cut
109  
110 has ajax_uri => (
111 is => 'rw',
112 required => 1,
113 );
114  
115 =item minimize_chat_user_list
116  
117 Defaults to C<2>. Must be 1 or 2. Minimize online user list by default. 2 means Yes. 1 means No.
118  
119 =cut
120  
121 has minimize_chat_user_list => (
122 is => 'rw',
123 default => sub { '2' },
124 coerce => $convert_to_string,
125 isa => sub {
126 ouch(442, 'minimize_chat_user_list must be 1 or 2', 'minimize_chat_user_list') unless ($_[0] ~~ [1,2]);
127 }
128 );
129  
130 =item enable_search_bar
131  
132 Defaults to C<1>. Must be 1 or 2. Show search bar in online user list. 1 means Yes. 2 means No.
133  
134 =cut
135  
136 has enable_search_bar => (
137 is => 'rw',
138 default => sub { '1' },
139 coerce => $convert_to_string,
140 isa => sub {
141 ouch(442, 'enable_search_bar must be 1 or 2', 'enable_search_bar') unless ($_[0] ~~ [1,2]);
142 }
143 );
144  
145 =item chat_topbar_color
146  
147 Defaults to C<#222222>. Choose the color of the top bar in the chat.
148  
149 =cut
150  
151 has chat_topbar_color => (
152 is => 'rw',
153 default => sub { '#222222' },
154 );
155  
156 =item chat_topbar_text_color
157  
158 Defaults to C<#FFFFFF>. Choose the color of the text in top bar in the chat.
159  
160 =cut
161  
162 has chat_topbar_text_color => (
163 is => 'rw',
164 default => sub { '#FFFFFF' },
165 );
166  
167 =item font_color
168  
169 Defaults to C<#222222>. Choose the color of the text in the chat.
170  
171 =cut
172  
173 has font_color => (
174 is => 'rw',
175 default => sub { '#222222' },
176 );
177  
178 =item chat_list_header
179  
180 Defaults to C<Chat>. This is the text that will appear in header of chat list.
181  
182 =cut
183  
184 has chat_list_header => (
185 is => 'rw',
186 default => sub { 'Chat' },
187 );
188  
189 =item support_chat_init_label
190  
191 Defaults to C<Chat with us>. The label for B<Start Chat> button, which when clicked upon will launch chat.
192  
193 =cut
194  
195 has support_chat_init_label => (
196 is => 'rw',
197 default => sub { 'Chat with us' },
198 );
199  
200 =item support_chat_box_header
201  
202 Defaults to C<Support>. This is the text that will appear as header of chat box.
203  
204 =cut
205  
206 has support_chat_box_header => (
207 is => 'rw',
208 default => sub { 'Support' },
209 );
210  
211 =item support_chat_box_company_name
212  
213 Defaults to C<Support Team>. Name of your team or company which the visitors will see in the chat box.
214  
215 =cut
216  
217 has support_chat_box_company_name => (
218 is => 'rw',
219 default => sub { 'Support Team' },
220 );
221  
222 =item support_chat_box_company_tagline
223  
224 Defaults to C<Ask us anything...>. Your team/company tagline.
225  
226 =cut
227  
228 has support_chat_box_company_tagline => (
229 is => 'rw',
230 default => sub { 'Ask us anything...' },
231 );
232  
233 =item support_chat_auto_greet_enable
234  
235 Defaults to 1. Must be 1 or 2. 1 means that the auto greeting is enabled.
236  
237 =cut
238  
239 has support_chat_auto_greet_enable => (
240 is => 'rw',
241 default => sub { '1' },
242 coerce => $convert_to_string,
243 isa => sub {
244 ouch(442, 'support_chat_auto_greet_enable must be 1 or 2', 'support_chat_auto_greet_enable') unless ($_[0] ~~ [1,2]);
245 }
246 );
247  
248 =item support_chat_auto_greet_message
249  
250 Defaults to C<Hi there! Welcome to our website. Let us know if you have any query!>. This is the text of an auto greeting message which will be displayed to visitors.
251  
252 =cut
253  
254 has support_chat_auto_greet_message => (
255 is => 'rw',
256 default => sub { 'Hi there! Welcome to our website. Let us know if you have any query!' },
257 );
258  
259  
260 =item support_chat_auto_greet_time
261  
262 Defaults to 1. The delay, in seconds, after which the first time visitors will be shown auto greeting message.
263  
264 =cut
265  
266 has support_chat_auto_greet_time => (
267 is => 'rw',
268 coerce => $convert_to_string,
269 default => sub { '1' },
270 );
271  
272 =item support_chat_init_label_off
273  
274 Defaults to C<Leave Message>. The label for B<Leave Message> button, which when clicked upon will offline form.
275  
276 =cut
277  
278 has support_chat_init_label_off => (
279 is => 'rw',
280 default => sub { 'Leave Message' },
281 );
282  
283 =item support_chat_offline_message_desc
284  
285 Defaults to C<Hello there. We are currently offline. Please leave us a message. Thanks.>. This is the description shown in Support Chat Offline window.
286  
287 =cut
288  
289 has support_chat_offline_message_desc => (
290 is => 'rw',
291 default => sub { 'Hello there. We are currently offline. Please leave us a message. Thanks.' },
292 );
293  
294 =item support_chat_offline_message_label
295  
296 Defaults to C<Message>. This is the label for the B<Message> textarea in Support Chat Offline window.
297  
298 =cut
299  
300 has support_chat_offline_message_label => (
301 is => 'rw',
302 default => sub { 'Message' },
303 );
304  
305 =item support_chat_offline_message_contact
306  
307 Defaults to C<Contact Details>. This is the label for the <i>Contact Details</i> textarea in Support Chat Offline window.
308  
309 =cut
310  
311 has support_chat_offline_message_contact => (
312 is => 'rw',
313 default => sub { 'Contact Details' },
314 );
315  
316 =item support_chat_offline_message_send_button
317  
318 Defaults to C<Send Message>. This is the label for the B<Send Button> textarea in Support Chat Offline window.
319  
320 =cut
321  
322 has support_chat_offline_message_send_button => (
323 is => 'rw',
324 default => sub { 'Send Message' },
325 );
326  
327 =item support_chat_offline_message_email
328  
329 Enter all email addresses (separated by comma) to which notification should be sent when a user leaves a message via Offline Form.
330  
331 =cut
332  
333 has support_chat_offline_message_email => (
334 is => 'rw',
335 default => sub { '' },
336 );
337  
338 =item go_online_label
339  
340 Defaults to C<Go Online>. Set this to change the label of the status of a user to an online state.
341  
342 =cut
343  
344 has go_online_label => (
345 is => 'rw',
346 default => sub { 'Go Online' },
347 );
348  
349 =item go_idle_label
350  
351 Defaults to C<Go Idle>. Set this to change the label of the status of a user to an idle state.
352  
353 =cut
354  
355 has go_idle_label => (
356 is => 'rw',
357 default => sub { 'Go Idle' },
358 );
359  
360 =item new_message_label
361  
362 Defaults to C<New chat message!>. Set this to change the label of the notification when a new chat message has come in.
363  
364 =cut
365  
366 has new_message_label => (
367 is => 'rw',
368 default => sub { 'New chat message!' },
369 );
370  
371 =item public_chatroom_header
372  
373 Defaults to C<Public Chatroom>. This is the text that will appear in header of public chatroom.
374  
375 =cut
376  
377 has public_chatroom_header => (
378 is => 'rw',
379 default => sub { 'Public Chatroom' },
380 );
381  
382 =item enable_chatroom
383  
384 Defaults to 1. Must be 1 or 2. 1 means that the public chatroom is enabled.
385  
386 =cut
387  
388 has enable_chatroom => (
389 is => 'rw',
390 default => sub { '1' },
391 coerce => $convert_to_string,
392 isa => sub {
393 ouch(442, 'enable_chatroom must be 1 or 2', 'enable_chatroom') unless ($_[0] ~~ [1,2]);
394 }
395 );
396  
397 =item theme
398  
399 Defaults to C<light>. Must be C<light> or C<dark>. Other options may exist in the future, or you could create your own.
400  
401 =cut
402  
403 has theme => (
404 is => 'rw',
405 default => sub { 'light' },
406 );
407  
408 =item notification_sound
409  
410 Defaults to 1. Must be 1 or 2. When set to 1 the chat will play a notification sound when a message is posted.
411  
412 =cut
413  
414 has notification_sound => (
415 is => 'rw',
416 default => sub { '1' },
417 coerce => $convert_to_string,
418 isa => sub {
419 ouch(442, 'notification_sound must be 1 or 2', 'notification_sound') unless ($_[0] ~~ [1,2]);
420 }
421 );
422  
423 =item sound_player_uri
424  
425 Defaults to C<static_asset_base_uri> + C</swf/sound.swf>. The URL to where the sound player is stored.
426  
427 =cut
428  
429 has sound_player_uri => (
430 is => 'rw',
431 default => sub { shift->static_asset_base_uri.'/swf/sound.swf' },
432 lazy => 1,
433 );
434  
435 =item sound_file_uri
436  
437 Defaults to C<static_asset_base_uri> + C</wav/notification.mp3>. The URL to where the notification sound is stored.
438  
439 =cut
440  
441 has sound_file_uri => (
442 is => 'rw',
443 default => sub { shift->static_asset_base_uri.'/wav/notification.mp3' },
444 lazy => 1,
445 );
446  
447 =item enable_smiley
448  
449 Defaults to 1. Must be 1 or 2. When set to 1 users will have access to emoticons.
450  
451 =cut
452  
453 has enable_smiley => (
454 is => 'rw',
455 default => sub { '1' },
456 coerce => $convert_to_string,
457 isa => sub {
458 ouch(442, 'enable_smiley must be 1 or 2', 'enable_smiley') unless ($_[0] ~~ [1,2]);
459 }
460 );
461  
462 =item smiley_uri
463  
464 Defaults to C<static_asset_base_uri> + C</smileys/very_emotional_emoticons-png/png-32x32/>. The URL to where the emoticons are stored.
465  
466 =cut
467  
468 has smiley_uri => (
469 is => 'rw',
470 default => sub { shift->static_asset_base_uri.'/smileys/very_emotional_emoticons-png/png-32x32/' },
471 lazy => 1,
472 );
473  
474 =item log_messages
475  
476 Defaults to 1. Must be 1 or 2. When set to 1 chat messages will be logged to the user's inbox. See C<get_message_inbox>.
477  
478 =cut
479  
480 has log_messages => (
481 is => 'rw',
482 coerce => $convert_to_string,
483 default => sub { '1' },
484 isa => sub {
485 ouch(442, 'log_messages must be 1 or 2', 'log_messages') unless ($_[0] ~~ [1,2]);
486 }
487 );
488  
489 =item anon_prefix
490  
491 Defaults to C<Guest>. A prefix that will be applied to anonymous generated usernames.
492  
493 =cut
494  
495 has anon_prefix => (
496 is => 'rw',
497 default => sub { 'Guest' },
498 );
499  
500 =item use_local_anonymous_names
501  
502 Perl boolean. When true guest names will be pulled from C<local_anonymous_names>, which is faster than consulting the remote server. When false a list of names will be polled from the iFly server.
503  
504 =cut
505  
506 has use_local_anonymous_names => (
507 is => 'rw',
508 default => sub { 1 },
509 );
510  
511 =item local_anonymous_names
512  
513 An array reference of local names. There's a default list of 50 or so English sounding names. For use with C<use_local_anonymous_names>.
514  
515 =cut
516  
517 has local_anonymous_names => (
518 is => 'rw',
519 default => sub { [qw(John Tim Tom Jason Fred Dave Mark James Don Ron Ed Sally Mary Sarah Kim Jim Nancy Wayne Bill Bob Karey Heather Victoria Becky Ana Larry Kayla Joe Tera Kevin Josh Chris Karen Maria Nadia Susan Mellisa Rebecca Bev Rachel Eddie Heidi Shana Shane Eric Erika)] },
520 lazy => 1,
521 );
522  
523 =item use_stop_word_list
524  
525 Defaults to 1. Must be 1. Whether to use C<stop_word_list> to filter user posts. 1 means don't filter. 2 means filter in public chat room. 3 means filter in private chats. 4 means filter in all chats.
526  
527 =cut
528  
529 has use_stop_word_list => (
530 is => 'rw',
531 default => sub { '1' },
532 coerce => $convert_to_string,
533 isa => sub {
534 ouch(442, 'use_stop_word_list must be 1, 2, 3 or 4', 'use_stop_word_list') unless ($_[0] ~~ [1,2,3,4]);
535 }
536 );
537  
538 =item stop_word_list
539  
540 A comma separated string of "bad" words. A list of about a hundred defaults this list.
541  
542 =cut
543  
544 has stop_word_list => (
545 is => 'rw',
546 default => sub { 'asshole,assholes,bastard,beastial,beastiality,beastility,bestial,bestiality,bitch,bitcher,bitchers,bitches,bitchin,bitching,blowjob,blowjobs,bullshit,clit,cock,cocks,cocksuck,cocksucked,cocksucker,cocksucking,cocksucks,cum,cummer,cumming,cums,cumshot,cunillingus,cunnilingus,cunt,cuntlick,cuntlicker,cuntlicking,cunts,cyberfuc,cyberfuck,cyberfucked,cyberfucker,cyberfuckers,cyberfucking,damn,dildo,dildos,dick,dink,dinks,ejaculate,ejaculated,ejaculates,ejaculating,ejaculatings,ejaculation,fag,fagging,faggot,faggs,fagot,fagots,fags,fart,farted,farting,fartings,farts,farty,felatio,fellatio,fingerfuck,fingerfucked,fingerfucker,fingerfuckers,fingerfucking,fingerfucks,fistfuck,fistfucked,fistfucker,fistfuckers,fistfucking,fistfuckings,fistfucks,fuck,fucked,fucker,fuckers,fuckin,fucking,fuckings,fuckme,fucks,fuk,fuks,gangbang,gangbanged,gangbangs,gaysex,goddamn,hardcoresex,horniest,horny,hotsex,jism,jiz,jizm,kock,kondum,kondums,kum,kumer,kummer,kumming,kums,kunilingus,lust,lusting,mothafuck,mothafucka,mothafuckas,mothafuckaz,mothafucked,mothafucker,mothafuckers,mothafuckin,mothafucking,mothafuckings,mothafucks,motherfuck,motherfucked,motherfucker,motherfuckers,motherfuckin,motherfucking,motherfuckings,motherfucks,niger,nigger,niggers,orgasim,orgasims,orgasm,orgasms,phonesex,phuk,phuked,phuking,phukked,phukking,phuks,phuq,pis,piss,pisser,pissed,pisser,pissers,pises,pisses,pisin,pissin,pising,pissing,pisof,pissoff,porn,porno,pornography,pornos,prick,pricks,pussies,pusies,pussy,pusy,pussys,pusys,slut,sluts,smut,spunk' },
547 );
548  
549 =item stop_links
550  
551 Defaults to 1. Must be 1, 2, 3, or 4. 1 means don't block links. 2 means block in public chatroom. 3 menas block in private chat rooms. 4 means block in all chats.
552  
553 =cut
554  
555 has stop_links => (
556 is => 'rw',
557 default => sub { '1' },
558 coerce => $convert_to_string,
559 isa => sub {
560 ouch(442, 'stop_links must be 1, 2, 3, or 4', 'stop_links') unless ($_[0] ~~ [1,2,3,4]);
561 }
562 );
563  
564 =item allow_anon_links
565  
566 Defaults to 2. Must be 1 or 2. 1 means apply C<stop_links> only to anonymous users. 2 means apply C<stop_links> to all users.
567  
568 =cut
569  
570 has allow_anon_links => (
571 is => 'rw',
572 default => sub { '2' },
573 coerce => $convert_to_string,
574 isa => sub {
575 ouch(442, 'allow_anon_links must be 1 or 2', 'allow_anon_links') unless ($_[0] ~~ [1,2]);
576 }
577 );
578  
579 =item allow_render_images
580  
581 Defaults to 1. Must be 1 or 2. When set to 1 images and video links will be rendered inline in the chat.
582  
583 =cut
584  
585 has allow_render_images => (
586 is => 'rw',
587 default => sub { '1' },
588 coerce => $convert_to_string,
589 isa => sub {
590 ouch(442, 'allow_render_images must be 1 or 2', 'allow_render_images') unless ($_[0] ~~ [1,2]);
591 }
592 );
593  
594 =item allow_user_font_color
595  
596 Defaults to 1. Must be 1 or 2. When set to 1 users can set their name color.
597  
598 =cut
599  
600 has allow_user_font_color => (
601 is => 'rw',
602 default => sub { '1' },
603 coerce => $convert_to_string,
604 isa => sub {
605 ouch(442, 'allow_user_font_color must be 1 or 2', 'allow_user_font_color') unless ($_[0] ~~ [1,2]);
606 }
607 );
608  
609 =item allow_single_message_delete
610  
611 Defaults to 1. Must be 1, 2, or 3. Allow users to delete messages selectively when in private conversation. 1 means allow all users. 2 means allow moderators. 3 means do not allow.
612  
613 =cut
614  
615 has allow_single_message_delete => (
616 is => 'rw',
617 default => sub { '2' },
618 coerce => $convert_to_string,
619 isa => sub {
620 ouch(442, 'allow_single_message_delete must be 1, 2, or 3', 'allow_single_message_delete') unless ($_[0] ~~ [1,2,3]);
621 }
622 );
623  
624 =item allow_clear_room_history
625  
626 Defaults to 1. Must be 1, 2, or 3. Allow users to clear all messages in a room. 1 means allow all users. 2 means allow moderators. 3 means do not allow.
627  
628 =cut
629  
630 has allow_clear_room_history => (
631 is => 'rw',
632 default => sub { '2' },
633 coerce => $convert_to_string,
634 isa => sub {
635 ouch(442, 'allow_clear_room_history must be 1, 2, or 3', 'allow_clear_room_history') unless ($_[0] ~~ [1,2,3]);
636 }
637 );
638  
639 =item rel
640  
641 Defaults to 2. Must be 1 or 2. When set to 2 the chat will be in "Community" mode, which means users can talk to each other. When set to 1 the chat is in "Support" mode and users can only talk to admins.
642  
643 =cut
644  
645 has show_admin_list => (
646 is => 'rw',
647 default => sub { '2' },
648 coerce => $convert_to_string,
649 isa => sub {
650 ouch(442, 'show_admin_list must be 1 or 2', 'show_admin_list') unless ($_[0] ~~ [1,2]);
651 }
652 );
653  
654 =item user_picture
655  
656 Defaults to 1. Must be 1 or 2. 1 means to enable user avatars.
657  
658 =cut
659  
660 has user_picture => (
661 is => 'rw',
662 default => sub { '1' },
663 coerce => $convert_to_string,
664 isa => sub {
665 ouch(442, 'user_picture must be 1 or 2', 'user_picture') unless ($_[0] ~~ [1,2]);
666 }
667 );
668  
669 =item no_users_html
670  
671 Determine how to render the list when no users are online. Defaults to:
672  
673 <div class="item-list"><ul><li class="drupalchatnousers even first last">No users online</li></ul></div>
674  
675 =cut
676  
677 has no_users_html => (
678 is => 'rw',
679 default => sub { '<div class="item-list"><ul><li class="drupalchatnousers even first last">No users online</li></ul></div>' },
680 );
681  
682 =item uri
683  
684 The base URI of the service you're interacting with. Defaults to C<https://api.iflychat.com>.
685  
686 =cut
687  
688 has uri => (
689 is => 'rw',
690 default => sub { 'https://api.iflychat.com' },
691 );
692  
693 =item port
694  
695 The port you're interacting with for iFly. Defaults to C<443>.
696  
697 =cut
698  
699 has port => (
700 is => 'rw',
701 default => sub { '443' },
702 coerce => $convert_to_string,
703 );
704  
705 =item agent
706  
707 A HTTP::Thin object.
708  
709 =back
710  
711 =cut
712  
713 has agent => (
714 is => 'ro',
715 required => 0,
716 lazy => 1,
717 builder => '_build_agent',
718 );
719  
720 sub _build_agent {
721 return HTTP::Thin->new( )
722 }
723  
724  
725 =head2 render_html( user )
726  
727 This method renders the HTML you need to inline into your web page to configure the chat.
728  
729 =over
730  
731 =item user
732  
733 A hash reference containing a user as defined in C<get_key>. However, you only need to pass this in if the user is registered with the site, not for anonymous users.
734  
735 =back
736  
737 =cut
738  
739 sub render_html {
740 my ($self, $user) = @_;
741 my $out = '<script type="text/javascript">Drupal={};Drupal.settings={};Drupal.settings.drupalchat={};Drupal.settings='.to_json($self->init($user)).';</script>';
742 $out .= '<script type="text/javascript" src="' . $self->static_asset_base_uri . '/js/ba-emotify.js"></script>';
743 $out .= '<script type="text/javascript" src="' . $self->static_asset_base_uri . '/js/jquery.titlealert.min.js"></script>';
744 $out .= '<script type="text/javascript" src="' . $self->static_asset_base_uri . '/js/iflychat.js"></script>';
745 return $out;
746 }
747  
748 =head2 render_ajax( user )
749  
750 This method renders the response that will log the user into the chat.
751  
752 B<NOTE:> For best performance and to ensure persistent user names for anonymous users you should cache the result of this method in your session management system.
753  
754 =over
755  
756 =item user
757  
758 Required. A hash reference containing a user definition. For an anonymous user call C<generate_anonymous_user>. Otherwise define the user as described in the C<get_key> method.
759  
760 =back
761  
762 =cut
763  
764 sub render_ajax {
765 my ($self, $user) = @_;
766 ouch(441, 'user is required', 'user') unless defined $user;
767 return to_json({(%{$user}, %{$self->get_key($user)})});
768 }
769  
770 =head2 init( user )
771  
772 This method is called by C<render_html> to generate the list of settings to initialize the chat. There should proably not be a reason for you to call it yourself.
773  
774 =over
775  
776 =item user
777  
778 A hash reference containing a user as defined in C<get_key>. However, you only need to pass this in if the user is registered with the site, not for anonymous users.
779  
780 =back
781  
782 =cut
783  
784 sub init {
785 my ($self, $user) = @_;
786 my %settings = (
787 username => (defined $user && exists $user->{name} && $user->{name}) ? $user->{name} : '',
788 uid => (defined $user && exists $user->{id} && $user->{id}) ? $user->{id} : '',
789 current_timestamp => time(),
790 polling_method => "3",
791 pollUrl => ' ',
792 sendUrl => ' ',
793 statusUrl => ' ',
794 status => "1",
795 goOnline => $self->go_online_label,
796 goIdle => $self->go_idle_label,
797 newMessage => $self->new_message_label,
798 images => $self->static_asset_base_uri.'/themes/'.$self->theme.'/images/',
799 sound => $self->sound_player_uri,
800 soundFile => $self->sound_file_uri,
801 noUsers => $self->no_users_html,
802 smileyURL => $self->smiley_uri,
803 addUrl => ' ',
804 guestPrefix => $self->anon_prefix,
805 allowSmileys => $self->enable_smiley,
806 iup => $self->user_picture,
807 open_chatlist_default => $self->minimize_chat_user_list,
808 useStopWordList => $self->use_stop_word_list,
809 blockHL => $self->stop_links,
810 allowAnonHL => $self->allow_anon_links,
811 renderImageInline => $self->allow_render_images,
812 searchBar => $self->enable_search_bar,
813 notificationSound => $self->notification_sound,
814 basePath => '/',
815 admin => (defined $user && exists $user->{is_admin} && $user->{is_admin}) ? "1" : "0",
816 session_key => '',
817 exurl => $self->ajax_uri,
818 external_host => $self->uri,
819 external_port => $self->port,
820 external_a_host => $self->uri,
821 external_a_port => $self->port,
822 upl => (defined $user && exists $user->{profile_uri} && $user->{profile_uri}) ? $user->{profile_uri} : '#',
823 );
824
825 if ($self->user_picture) {
826 $settings{default_up} = $self->static_asset_base_uri.'/themes/'.$self->theme.'/images/default_avatar.png';
827 $settings{up} = (defined $user && exists $user->{avatar_uri}) ? $user->{avatar_uri} : $settings{default_up};
828 $settings{default_cr} = $self->static_asset_base_uri.'/themes/'.$self->theme.'/images/default_room.png';
829 }
830
831 if ($self->show_admin_list) {
832 $settings{text_support_chat_init_label} = $self->support_chat_init_label;
833 $settings{text_support_chat_box_header} = $self->support_chat_box_header;
834 $settings{text_support_chat_box_company_name} = $self->support_chat_box_company_name;
835 $settings{text_support_chat_box_company_tagline} = $self->support_chat_box_company_tagline;
836 $settings{text_support_chat_auto_greet_enable} = $self->support_chat_auto_greet_enable;
837 $settings{text_support_chat_auto_greet_message} = $self->support_chat_auto_greet_message;
838 $settings{text_support_chat_auto_greet_time} = $self->support_chat_auto_greet_time;
839 $settings{text_support_chat_offline_message_label} = $self->support_chat_offline_message_label;
840 $settings{text_support_chat_offline_message_contact} = $self->support_chat_offline_message_contact;
841 $settings{text_support_chat_offline_message_send_button} = $self->support_chat_offline_message_send_button;
842 $settings{text_support_chat_offline_message_desc} = $self->support_chat_offline_message_desc;
843 $settings{text_support_chat_init_label_off} = $self->support_chat_init_label_off;
844 }
845
846 if ($self->use_stop_word_list == 2) {
847 $settings{stopWordList} = $self->stop_word_list;
848 }
849  
850 return { drupalchat => \%settings };
851 }
852  
853 =head2 generate_anonymous_user()
854  
855 Generates an anonymous user hash reference to be used with C<get_key> or C<render_ajax>.
856  
857 =cut
858  
859 sub generate_anonymous_user {
860 my $self = shift;
861 my %user = ( id => '0-'.time() );
862 if ($self->use_local_anonymous_names) {
863 my $names = $self->local_anonymous_names;
864 $user{name} = $self->anon_prefix.' '.$names->[rand @{$names}];
865 }
866 else {
867 $user{name} = $self->anon_prefix.' '.$self->fetch_anonymous_name;
868 }
869 return \%user;
870 }
871  
872 =head2 fetch_anonymous_name( )
873  
874 Retrieves a random name from the iFly servers for anonymous users. This is called by C<generate_anonymous_user> and probably doesn't need to be used by you.
875  
876 =cut
877  
878 sub fetch_anonymous_name {
879 my $self = shift;
880 return $self->get('/anam/v/usa');
881 }
882  
883  
884 =head2 get_key( user )
885  
886 This method is used to essentially log a user into the chat system. It generates a key that is used by the javascript to communicate back to the chat server.
887  
888 =over
889  
890 =item user
891  
892 A hash reference containing the definition of a user. If this is an anonymous user then generate it using C<generate_anonymous_user>.
893  
894 =over
895  
896 =item id
897  
898 The unique id of the user. It can be any alphanumeric string, and cannot contain special characters or start with a number. I recommend hex encoding your string and then prepending "ifly" on it to create a string that fits their ID scheme.
899  
900 =item name
901  
902 The name or username of the user.
903  
904 =item is_admin
905  
906 Defaults to 0. Can be set to 1 if the user should have chat admin privileges.
907  
908 =item custom_roles
909  
910 Defaults to C<normal>. Ignored entirely if C<is_admin> is set to 1. You can also pass in a hash of custom roles (not admin or normal) that will be used as CSS classes for styling. For example:
911  
912 {
913 1 => 'cool',
914 2 => 'slick',
915 }
916  
917 =item avatar_uri
918  
919 A URI string that references a picture used to identify the user.
920  
921 =item profile_uri
922  
923 A URI string that will link other users to this user's profile on the web site.
924  
925 =item relationships_set
926  
927 This allows you to set up buddy lists within the chat. It is a hash reference taking the form of:
928  
929 {
930 1 => {
931 name => 'friend',
932 plural => 'friends',
933 valid_uids => ['user_id_1', 'user_id_5', 'user_id_3']
934 },
935 2 => {
936 name => 'co-worker',
937 plural => 'co-workers',
938 valid_uids => ['user_id_3', 'user_id_4', 'user_id_2']
939 },
940 }
941  
942  
943 =back
944  
945 =back
946  
947 =cut
948  
949 sub get_key {
950 my ($self, $user) = @_;
951 my $result = from_json($self->post('/p/', {
952 api_key => $self->api_key,
953 uname => $user->{name},
954 uid => $user->{id} || 0,
955 image_path => $self->static_asset_base_uri.'/themes/'.$self->theme.'/images',
956 isLog => JSON::true,
957 whichTheme => 'blue',
958 enableStatus => JSON::true,
959 role => $user->{is_admin} ? 'admin' : ((exists $user->{custom_roles}) ? $user->{custom_roles} : 'normal'),
960 validState => ['available','offline','busy','idle'],
961 up => (exists $user->{avatar_uri}) ? $user->{avatar_uri} : $self->static_asset_base_uri.'/themes/'.$self->theme.'/images/default_avatar.png',
962 upl => (exists $user->{profile_uri}) ? $user->{profile_uri} : '#',
963 rel => (exists $user->{relationships_set}) ? 1 : undef,
964 valid_uids => $user->{relationships_set},
965 }));
966 $result->{uid} = $user->{id} || 0;
967 $result->{name} = $user->{name};
968 return $result;
969 }
970  
971 =head2 update_settings( )
972  
973 Updates the iFly servers with all the configured settings you passed into the constructor of this object. This must be called when you first start using this module, and also each time that you make changes to your settings.
974  
975 =cut
976  
977 sub update_settings {
978 my ($self) = @_;
979 return $self->post('/z/', {
980 api_key => $self->api_key,
981 enable_chatroom => $self->enable_chatroom,
982 theme => $self->theme,
983 notify_sound => $self->notification_sound,
984 smileys => $self->enable_smiley,
985 log_chat => $self->log_messages,
986 chat_topbar_color => $self->chat_topbar_color,
987 chat_topbar_text_color => $self->chat_topbar_text_color,
988 font_color => $self->font_color,
989 chat_list_header => $self->chat_list_header,
990 public_chatroom_header => $self->public_chatroom_header,
991 version => 'perl',
992 show_admin_list => $self->show_admin_list,
993 delmessage => $self->allow_clear_room_history,
994 clear => $self->allow_single_message_delete,
995 ufc => $self->allow_user_font_color,
996 guest_prefix => $self->anon_prefix,
997 use_stop_word_list => $self->use_stop_word_list,
998 stop_word_list => $self->stop_word_list,
999 });
1000 }
1001  
1002 =head2 get_message_thread(uid1, uid2)
1003  
1004 Returns a hash reference of a message thread between two users.
1005  
1006 =over
1007  
1008 =item uid1
1009  
1010 Required. The unique user id of the first user in the discussion.
1011  
1012 =item uid2
1013  
1014 Required. The unique user id of the second user in the discussion.
1015  
1016 =back
1017  
1018 =cut
1019  
1020 sub get_message_thread {
1021 my ($self, $uid1, $uid2) = @_;
1022 ouch(441, 'uid1 required', 'uid1') unless $uid1;
1023 ouch(441, 'uid2 required', 'uid2') unless $uid2;
1024 return from_json($self->post('/q/', { api_key => $self->api_key, uid1 => $uid1, uid2 => $uid2} ));
1025 }
1026  
1027 =head2 get_message_inbox(uid)
1028  
1029 Returns a hash reference containing the messages in the user's inbox.
1030  
1031 =over
1032  
1033 =item uid
1034  
1035 The unique user id of the user who's inbox you want to retrieve.
1036  
1037 =back
1038  
1039 =cut
1040  
1041 sub get_message_inbox {
1042 my ($self, $uid) = @_;
1043 ouch(441, 'uid required', 'uid') unless $uid;
1044 return from_json($self->post('/r/', { api_key => $self->api_key, uid1 => $uid} ));
1045 }
1046  
1047  
1048  
1049 =head2 get(path, params)
1050  
1051 Performs a C<GET> request, which is used for reading data from the service.
1052  
1053 =over
1054  
1055 =item path
1056  
1057 The path to the REST interface you wish to call.
1058  
1059 =item params
1060  
1061 A hash reference of parameters you wish to pass to the web service.
1062  
1063 =back
1064  
1065 =cut
1066  
1067 sub get {
1068 my ($self, $path, $params) = @_;
1069 my $uri = $self->_create_uri($path);
1070 $uri->query_form($params);
1071 return $self->_process_request( GET $uri );
1072 }
1073  
1074  
1075 =head2 post(path, params)
1076  
1077 Performs a C<POST> request, which is used for creating data in the service.
1078  
1079 =over
1080  
1081 =item path
1082  
1083 The path to the REST interface you wish to call.
1084  
1085 =item params
1086  
1087 A hash reference of parameters you wish to pass to the web service.
1088  
1089 =back
1090  
1091 =cut
1092  
1093 sub post {
1094 my ($self, $path, $params) = @_;
1095 my $uri = $self->_create_uri($path);
1096 return $self->_process_request( POST $uri->as_string, Content_Type => 'application/json', Content => to_json($params) );
1097 }
1098  
1099 sub _create_uri {
1100 my $self = shift;
1101 my $path = shift;
1102 return URI->new($self->uri.$path);
1103 }
1104  
1105 sub _process_request {
1106 my $self = shift;
1107 $self->_process_response($self->agent->request( @_ ));
1108 }
1109  
1110 sub _process_response {
1111 my $self = shift;
1112 my $response = shift;
1113 if ($response->is_success) {
1114 return $response->decoded_content;
1115 }
1116 else {
1117 warn $response->decoded_content;
1118 ouch $response->code, $response->message, $response->decoded_content;
1119 }
1120 }
1121  
1122 =head1 PREREQS
1123  
1124 L<HTTP::Thin>
1125 L<Ouch>
1126 L<HTTP::Request::Common>
1127 L<JSON>
1128 L<URI>
1129 L<Moo>
1130  
1131 =head1 SUPPORT
1132  
1133 =over
1134  
1135 =item Repository
1136  
1137 L<http://github.com/rizen/Chat-iFly>
1138  
1139 =item Bug Reports
1140  
1141 L<http://github.com/rizen/Chat-iFly/issues>
1142  
1143 =back
1144  
1145 =head1 AUTHOR
1146  
1147 JT Smith <jt_at_plainblack_dot_com>
1148  
1149 =head1 LEGAL
1150  
1151 This module is Copyright 2014 Plain Black Corporation. It is distributed under the same terms as Perl itself.
1152  
1153 =cut
1154  
1155  
1156 1;