]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/name.js
Merge pull request #520 from rabuzarus/20180206_-_membersince_frio_support
[friendica-addons.git] / jappixmini / jappix / js / name.js
1 /*
2
3 Jappix - An open social platform
4 These are the buddy name related JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 29/04/11
11
12 */
13
14 // Gets an user name for buddy add tool
15 function getAddUserName(xid) {
16         var iq = new JSJaCIQ();
17         iq.setType('get');
18         iq.setTo(xid);
19         
20         iq.appendNode('vCard', {'xmlns': NS_VCARD});
21         
22         con.send(iq, handleAddUserName);
23 }
24
25 // Handles an user name for buddy add tool
26 function handleAddUserName(iq) {
27         // Was it an obsolete request?
28         if(!exists('.add-contact-name-get[data-for=' + escape(bareXID(getStanzaFrom(iq))) + ']'))
29                 return false;
30         
31         // Reset the waiting item
32         $('.add-contact-name-get').hide().removeAttr('data-for');
33         
34         // Get the names
35         if(iq.getType() == 'result') {
36                 var full_name = generateBuddyName(iq)[0];
37                 
38                 if(full_name)
39                         $('.add-contact-name').val(full_name);
40         }
41         
42         return false;
43 }
44
45 // Generates the good buddy name from a vCard IQ reply
46 function generateBuddyName(iq) {
47         // Get the IQ content
48         var xml = $(iq.getNode()).find('vCard');
49         
50         // Get the full name & the nickname
51         var pFull = xml.find('FN:first').text();
52         var pNick = xml.find('NICKNAME:first').text();
53         
54         // No full name?
55         if(!pFull) {
56                 // Get the given name
57                 var pN = xml.find('N:first');
58                 var pGiven = pN.find('GIVEN:first').text();
59                 
60                 if(pGiven) {
61                         pFull = pGiven;
62                         
63                         // Get the family name (optional)
64                         var pFamily = pN.find('FAMILY:first').text();
65                         
66                         if(pFamily)
67                                 pFull += ' ' + pFamily;
68                 }
69         }
70         
71         return [pFull, pNick];
72 }
73
74 // Returns the given XID buddy name
75 function getBuddyName(xid) {
76         // Initialize
77         var cname, bname;
78         
79         // Cut the XID resource
80         xid = bareXID(xid);
81         
82         // This is me?
83         if(isAnonymous() && !xid)
84                 bname = _e("You");
85         else if(xid == getXID())
86                 bname = getName();
87         
88         // Not me!
89         else {
90                 cname = $('#buddy-list .buddy[data-xid=' + escape(xid) + ']:first .buddy-name').html();
91                 
92                 // If the complete name exists
93                 if(cname)
94                         bname = cname.revertHtmlEnc();
95                 
96                 // Else, we just get the nickname of the buddy
97                 else
98                         bname = getXIDNick(xid);
99         }
100         
101         return bname;
102 }
103
104 // Gets the nickname of the user
105 function getNick() {
106         // Try to read the user nickname
107         var nick = getDB('profile', 'nick');
108         
109         // No nick?
110         if(!nick)
111                 nick = con.username;
112         
113         return nick;
114 }
115
116 // Gets the full name of the user
117 function getName() {
118         // Try to read the user name
119         var name = getDB('profile', 'name');
120         
121         // No name? Use the nickname instead!
122         if(!name)
123                 name = getNick();
124         
125         return name;
126 }
127
128 // Gets the MUC nickname of the user
129 function getMUCNick(id) {
130         return unescape($('#' + id).attr('data-nick'));
131 }