Skip to content
This repository has been archived by the owner on Nov 21, 2024. It is now read-only.

Add a new plug in to add running MUC's and private chats with roster items #165

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions available-chats/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Available Chats Plugin
A plugin to join public rooms and start private chats with roster entries.

## Usage
Include the JavaScript and CSS files:

```HTML
<script type="text/javascript" src="candyshop/available-chats/candy.js"></script>
```

To enable the Available Chats Plugin, just add one of the ´init´ methods to your bootstrap (uses conference.your.domain as MUC domain):

```JavaScript
CandyShop.AvailableChats.init();
```

To set the MUC domain use
```JavaScript
CandyShop.AvailableChats.init({ domain: "chats.capulet.net" });
```

To set a MUC subdomain use (end with a dot ("."); will use the connection's domain as base.
```JavaScript
CandyShop.AvailableChats.init({ domain: "chats." });
```

## Screenshot
![Screenshot](https://github.com/amiadogroup/candy-plugins/raw/master/available-rooms/screenshot.png)
231 changes: 231 additions & 0 deletions available-chats/candy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
/** File: candy.js
* Candy: show available rooms on conference server and add roster items
* for private chats.
* Fork of available-rooms plugin.
*
* Authors:
* - Jonatan Männchen <[email protected]>
* - Georg Jansing <[email protected]>
*
* Copyright:
* - (c) 2012 Amiado Group AG. All rights reserved.
* - (c) 2016 Georg Jansing. All rights reserved.
*/

/**
* TODO: Properly translate ("Kontake", "Chatraum")
*/

/* global Candy, jQuery, Strophe */

var CandyShop = (function(self) { return self; }(CandyShop || {}));

CandyShop.AvailableChats = (function(self, Candy, $) {
self.about = {
name: 'Candy Plugin Available Chats',
version: '1.0',
};

self._options = {
domain: null, /** Prefix (e.g. "conference") or complete MUC Domain
* to list rooms from. If domain ends with a dot (".")
* it will be interpreted as a prefix, otherweise as
* full domain. */
}

/** Private chat or MUC */
self.RoomTypes = {
CONFERENCE: 1,
PRIVATE: 2,
};

/** Array: rooms
* all rooms
*
* Contains:
* (Object List) rooms
* (String) jid
* (String) name
* (Integer) people (number of participants)
* (Integer) type (private chat or muc)
*/
self.rooms = [];

/** Function: init
* Initializes the available-rooms plugin with the default settings.
*/
self.init = function(options) {
$.extend(true, self._options, options);

$(Candy).on('candy:core.chat.connection', function(e, args) {
if (args.status === Strophe.Status.CONNECTED ||
args.status === Strophe.Status.ATTACHED) {
// Load rooms
self.loadRooms();

// Do it again all 10 seconds
setInterval(self.loadRooms, 10000);
}
});

// Add Handler
$(Candy).on('candy:view.message.before-send', function(e, args) {
// (strip colors)
// if it matches '/list', show rooms and don't send anything
if (args.message.replace(/\|c:\d+\|/, '').toLowerCase() === '/list') {
self.showRooms();
args.message = '';
}
});
$(Candy).on('candy:view.room.after-add', self.loadRooms);
};

/** Function: loadRooms
* Load all public rooms
*/
self.loadRooms = function () {
if(! self._options.domain) {
self._options.domain = "conference." + Candy.Core.getConnection().domain;
} else if (self._options.domain.charAt(self._options.domain.length - 1) == ".") {
self._options.domain = self._options.domain +
Candy.Core.getConnection().domain;
}

Candy.Core.getConnection().muc.listRooms(self._options.domain, function(roomsData) {
CandyShop.AvailableChats.rooms = [];
$.each($(roomsData).find('item'), function(item, room) {
var allreadyIn = false;
$.each(Candy.Core.getRooms(), function(item, roomSearch) {
if(roomSearch.getJid() === $(room).attr('jid')) {
allreadyIn = true;
return false;
}
});
if (! allreadyIn) {
var name = $(room).attr('name');
var people = 0;
var pos = name.indexOf("(");
if (pos != -1) {
name = name.substr(0, name.indexOf('(') - 1);
people = name.substr(pos + 1, name.length - pos - 2);
}

CandyShop.AvailableChats.rooms.push({
jid: $(room).attr('jid'),
name: name,
people: people,
type: CandyShop.AvailableChats.RoomTypes.CONFERENCE,
});
}
});

// Add roster items
$.each(Candy.Core.getRoster().items, function(jid, contact) {
CandyShop.AvailableChats.rooms.push({
jid: jid,
name: contact.data.name,
people: 2,
type: CandyShop.AvailableChats.RoomTypes.PRIVATE,
});
});
/** Sorting: private chats first, then MUC rooms, ordered by number of participants */

CandyShop.AvailableChats.rooms = CandyShop.AvailableChats.rooms.sort(function(a, b) {
if (a.people === b.people) {
return a.name < b.name ? -1 : 1;
} else if (a.type == CandyShop.AvailableChats.RoomTypes.PRIVATE) {
return -1;
} else if (b.type == CandyShop.AvailableChats.RoomTypes.PRIVATE) {
return 1;
} else {
return a.people < b.people ? 1 : -1;
}
});
CandyShop.AvailableChats.placePlusTab();
});
};

/** Function: placePlusTab
* placeTheTab
*/
self.placePlusTab = function() {
if(self.rooms.length > 0) {
if($('#add-room').length > 0) {
$('#add-room').parent().remove();
}
$('#chat-tabs').children().last().after('<li class="roomtype-add"><a id="add-room" href="javascript:;" class="label" style="padding-right: 10px;">+</a></li>');
$('#add-room').click(self.showRooms);
} else {
if($('#add-room').length > 0) {
$('#add-room').parent().remove();
}
}
};

/** Function: showRooms
* Show all public rooms
*/
self.showRooms = function() {
// get the element
var elem = $('#add-room');

// blur the field
elem.blur();

// get the necessary items
var menu = $('#context-menu'),
content = $('ul', menu);

// clear the content if needed
content.empty();

// add the matches to the list
for(var i in self.rooms) {
var room = self.rooms[i];
var people = "";
if (room.type == CandyShop.AvailableChats.RoomTypes.PRIVATE) {
people = ' (Kontakt)';
} else {
if (room.people > 0) {
people = ' (Chatraum, ' + room.people + ' Personen)';
} else {
people = ' (Chatraum)';
}
}

content.append('<li class="available-room-option" data-jid="'+ self.rooms[i].jid +'">' + self.rooms[i].name + people + '</li>');
}

content.find('li').click(self.joinChanel);

var pos = elem.offset(),
posLeft = Candy.Util.getPosLeftAccordingToWindowBounds(menu, pos.left + 7),
posTop = Candy.Util.getPosTopAccordingToWindowBounds(menu, pos.top);

menu.css({'left': posLeft.px, 'top': '7px', backgroundPosition: posLeft.backgroundPositionAlignment + ' ' + posTop.backgroundPositionAlignment});
menu.fadeIn('fast');
};

/** Function: joinChanel
* Show all public rooms
*
* Parameters:
* (Event) e
*/
self.joinChanel = function(e) {
$('#context-menu').hide();
if ($(e.currentTarget).attr('data-room-type') == CandyShop.AvailableChats.RoomTypes.PRIVATE) {
// Start private chat
Candy.View.Pane.PrivateRoom.open($(e.currentTarget).attr('data-jid'), $(e.currentTarget).attr("data-room-name"), true, true);
} else {
// Start muc
Candy.Core.Action.Jabber.Room.Join($(e.currentTarget).attr('data-jid'));
}
if($('#add-room').length > 0) {
$('#add-room').parent().remove();
}
e.preventDefault();
};

return self;
}(CandyShop.AvailableChats || {}, Candy, jQuery));
Binary file added available-chats/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion available-rooms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Include the JavaScript and CSS files:
<script type="text/javascript" src="candyshop/available-rooms/candy.js"></script>
```

To enable the Paint Plugin, just add one of the ´init´ methods to your bootstrap:
To enable the Available Rooms Plugin, just add one of the ´init´ methods to your bootstrap:

```JavaScript
CandyShop.AvailableRooms.init();
Expand Down
33 changes: 25 additions & 8 deletions available-rooms/candy.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
*
* Authors:
* - Jonatan Männchen <[email protected]>
* - Georg Jansing <[email protected]>
*
* Copyright:
* - (c) 2012 Amiado Group AG. All rights reserved.
* - (c) 2016 Georg Jansing. All rights reserved.
*/

/* global Candy, jQuery, Strophe */
Expand All @@ -20,16 +22,17 @@ CandyShop.AvailableRooms = (function(self, Candy, $) {
* (Object List) rooms
* (String) jid
* (String) name
* (Integer) person
* (Integer) people (number of participants)
*/
self.rooms = [];

/** Function: init
* Initializes the available-rooms plugin with the default settings.
*/
self.init = function(){
$(Candy.Core.Event).on('candy:core.chat.connection', function(e, args) {
if(args.status === Strophe.Status.ATTACHED) {
$(Candy).on('candy:core.chat.connection', function(e, args) {
if(args.status === Strophe.Status.CONNECTED ||
args.status === Strophe.Status.ATTACHED) {
// Load rooms
self.loadRooms();

Expand All @@ -39,15 +42,15 @@ CandyShop.AvailableRooms = (function(self, Candy, $) {
});

// Add Handler
$(Candy.View.Pane).bind('candy:view.message.beforeSend', function(e, args) {
$(Candy).on('candy:view.message.before-send', function(e, args) {
// (strip colors)
// if it matches '/list', show rooms and don't send anything
if (args.message.replace(/\|c:\d+\|/, '').toLowerCase() === '/list') {
self.showRooms();
args.message = '';
}
});
$(Candy.View.Pane).bind('candy:view.room.afterAdd', self.loadRooms);
$(Candy).on('candy:view.room.after-add', self.loadRooms);
};

/** Function: loadRooms
Expand All @@ -65,10 +68,18 @@ CandyShop.AvailableRooms = (function(self, Candy, $) {
}
});
if(!allreadyIn) {
var name = $(room).attr('name');
var people = 0;
var pos = name.indexOf("(");
if (pos != -1) {
name = name.substr(0, name.indexOf('(') - 1);
people = name.substr(pos + 1, name.length - pos - 2);
}

CandyShop.AvailableRooms.rooms.push({
jid: $(room).attr('jid'),
name: $(room).attr('name').substr(0, $(room).attr('name').indexOf('(') - 1),
people: $(room).attr('name').substr($(room).attr('name').indexOf('(') + 1, $(room).attr('name').length - $(room).attr('name').indexOf('(') - 2)
name: name,
people: people,
});
}
});
Expand Down Expand Up @@ -119,7 +130,13 @@ CandyShop.AvailableRooms = (function(self, Candy, $) {

// add the matches to the list
for(var i in self.rooms) {
content.append('<li class="available-room-option" data-jid="'+ self.rooms[i].jid +'">' + self.rooms[i].name + ' (' + self.rooms[i].people + ' Personen)</li>');
var room = self.rooms[i];
var people = "";

if (room.people > 0) {
people = " (" + room.people + " Personen)";
}
content.append('<li class="available-room-option" data-jid="'+ self.rooms[i].jid +'">' + self.rooms[i].name + people + '</li>');
}

content.find('li').click(self.joinChanel);
Expand Down