yavsc/Yavsc/Hubs/ChatHub.cs

157 lines
5.4 KiB
C#
Raw Normal View History

2016-05-17 18:22:18 +02:00
//
// MyHub.cs
//
// Author:
// Paul Schneider <paul@pschneider.fr>
//
// Copyright (c) 2016 GNU GPL
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using Microsoft.AspNet.SignalR;
2016-11-04 12:18:15 +01:00
using System.Threading.Tasks;
2016-11-01 23:22:46 +01:00
using System.Collections.Generic;
using System.Linq;
2016-05-17 18:22:18 +02:00
namespace Yavsc
{
2016-11-03 14:52:17 +01:00
using Models;
using Models.Chat;
2016-05-17 18:22:18 +02:00
2016-11-01 23:22:46 +01:00
public class ChatHub : Hub
2016-05-17 18:22:18 +02:00
{
2016-11-02 11:27:12 +01:00
2016-11-01 23:22:46 +01:00
public override Task OnConnected()
{
bool isAuth = false;
2016-11-02 11:27:12 +01:00
string userName = null;
2016-11-01 23:22:46 +01:00
if (Context.User != null)
{
isAuth = Context.User.Identity.IsAuthenticated;
userName = Context.User.Identity.Name;
2016-11-01 23:22:46 +01:00
var group = isAuth ?
"authenticated" : "anonymous";
// Log ("Cx: " + group);
Groups.Add(Context.ConnectionId, group);
2016-11-02 11:27:12 +01:00
if (isAuth)
{
using (var db = new ApplicationDbContext()) {
var user = db.Users.Single(u => u.UserName == userName);
if (user.Connections==null)
2018-01-02 16:35:03 +01:00
user.Connections = new List<ChatConnection>();
user.Connections.Add(new ChatConnection
2016-11-02 11:27:12 +01:00
{
2016-11-03 14:52:17 +01:00
ConnectionId = Context.ConnectionId,
2016-11-02 11:27:12 +01:00
UserAgent = Context.Request.Headers["User-Agent"],
Connected = true
});
db.SaveChanges();
2016-11-02 11:27:12 +01:00
}
2016-11-02 11:27:12 +01:00
}
2016-11-01 23:22:46 +01:00
}
else Groups.Add(Context.ConnectionId, "anonymous");
2016-11-02 11:27:12 +01:00
Clients.Group("authenticated").notify("connected", Context.ConnectionId, userName);
2016-05-17 18:22:18 +02:00
2016-11-01 23:22:46 +01:00
return base.OnConnected();
}
2016-11-02 11:27:12 +01:00
2016-11-01 23:22:46 +01:00
public override Task OnDisconnected(bool stopCalled)
{
2016-11-03 14:52:17 +01:00
2016-11-02 11:27:12 +01:00
string userName = Context.User?.Identity.Name;
2016-11-03 14:52:17 +01:00
Clients.Group("authenticated").notify("disconnected", Context.ConnectionId, userName);
2016-11-02 11:27:12 +01:00
if (userName != null)
{
using (var db = new ApplicationDbContext()) {
2016-11-03 14:52:17 +01:00
var cx = db.Connections.SingleOrDefault(c => c.ConnectionId == Context.ConnectionId);
2016-11-02 11:27:12 +01:00
if (cx != null)
{
if (stopCalled)
{
var user = db.Users.Single(u => u.UserName == userName);
2016-11-02 11:27:12 +01:00
user.Connections.Remove(cx);
}
else
{
cx.Connected = false;
}
db.SaveChanges();
2016-11-02 11:27:12 +01:00
}
}
}
2016-11-03 14:52:17 +01:00
2016-11-01 23:22:46 +01:00
return base.OnDisconnected(stopCalled);
}
public override Task OnReconnected()
{
2016-11-02 11:27:12 +01:00
string userName = Context.User?.Identity.Name;
if (userName != null)
{
using (var db = new ApplicationDbContext()) {
var user = db.Users.Single(u => u.UserName == userName);
2016-11-03 14:52:17 +01:00
2018-01-02 16:35:03 +01:00
if (user.Connections==null) user.Connections = new List<ChatConnection>();
2016-11-03 14:52:17 +01:00
var cx = user.Connections.SingleOrDefault(c => c.ConnectionId == Context.ConnectionId);
2016-11-02 11:27:12 +01:00
if (cx != null)
{
cx.Connected = true;
db.SaveChanges();
2016-11-02 11:27:12 +01:00
}
2018-01-02 16:35:03 +01:00
else cx = new ChatConnection { ConnectionId = Context.ConnectionId,
2016-11-03 14:52:17 +01:00
UserAgent = Context.Request.Headers["User-Agent"],
Connected = true };
2016-11-02 11:27:12 +01:00
}
}
2016-11-01 23:22:46 +01:00
return base.OnReconnected();
}
2016-05-17 18:22:18 +02:00
2016-11-01 23:22:46 +01:00
public void Send(string name, string message)
{
string uname = (Context.User != null) ?
$"[{Context.User.Identity.Name}]" :
$"({name})";
2016-11-01 23:22:46 +01:00
Clients.All.addMessage(uname, message);
}
2016-05-17 18:22:18 +02:00
2016-11-01 23:22:46 +01:00
[Authorize]
public void SendPV(string connectionId, string message)
{
var sender = Context.User.Identity.Name;
// TODO personal black|white list +
// Contact list allowed only +
// only pro
var hubCxContext = Clients.User(connectionId);
var cli = Clients.Client(connectionId);
cli.addPV(sender, message);
}
2016-11-03 14:52:17 +01:00
public void Abort()
2016-11-01 23:22:46 +01:00
{
2016-11-03 14:52:17 +01:00
using (var db = new ApplicationDbContext()) {
var cx = db.Connections.SingleOrDefault(c=>c.ConnectionId == Context.ConnectionId);
if (cx!=null) {
db.Connections.Remove(cx);
db.SaveChanges();
2016-11-03 14:52:17 +01:00
}
}
2016-11-03 14:52:17 +01:00
}
2016-11-01 23:22:46 +01:00
}
2016-05-17 18:22:18 +02:00
}