aboutsummaryrefslogtreecommitdiff
path: root/BackEnd/Timeline/SignalRHub/TimelineHub.cs
blob: 1925a992b832c6efdedad62565a766a9f12e2c67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using System;
using System.Threading.Tasks;
using Timeline.Auth;
using Timeline.Services;
using Timeline.Services.Timeline;
using Timeline.Services.User;

namespace Timeline.SignalRHub
{
    public class TimelineHub : Hub<ITimelineClient>
    {
        private readonly ILogger<TimelineHub> _logger;
        private readonly ITimelineService _timelineService;

        public TimelineHub(ILogger<TimelineHub> logger, ITimelineService timelineService)
        {
            _logger = logger;
            _timelineService = timelineService;
        }

        [Obsolete("Use overload with owner.")]
        public static string GenerateTimelinePostChangeListeningGroupName(string timelineName)
        {
            return $"timeline-post-change-{timelineName}";
        }

        public static string GenerateTimelinePostChangeListeningGroupName(string owner, string timeline)
        {
            return $"v2-timeline-post-change-{owner}/{timeline}";
        }

        [Obsolete("Use v2.")]
        public async Task SubscribeTimelinePostChange(string timelineName)
        {
            try
            {
                var timelineId = await _timelineService.GetTimelineIdByNameAsync(timelineName);
                var user = Context.User;
                if (!user.HasPermission(UserPermission.AllTimelineManagement) && !await _timelineService.HasReadPermissionAsync(timelineId, user.GetOptionalUserId()))
                    throw new HubException(Resource.MessageForbidden);

                var group = GenerateTimelinePostChangeListeningGroupName(timelineName);
                await Groups.AddToGroupAsync(Context.ConnectionId, group);
                _logger.LogInformation(Resource.LogSubscribeTimelinePostChange, Context.ConnectionId, group);
            }
            catch (ArgumentException)
            {
                throw new HubException(Resource.MessageTimelineNameInvalid);
            }
            catch (EntityNotExistException)
            {
                throw new HubException(Resource.MessageTimelineNotExist);
            }
        }

        public async Task SubscribeTimelinePostChangeV2(string owner, string timeline)
        {
            try
            {
                var timelineId = await _timelineService.GetTimelineIdAsync(owner, timeline);
                var user = Context.User;
                if (!user.HasPermission(UserPermission.AllTimelineManagement) && !await _timelineService.HasReadPermissionAsync(timelineId, user.GetOptionalUserId()))
                    throw new HubException(Resource.MessageForbidden);

                var group = GenerateTimelinePostChangeListeningGroupName(owner, timeline);
                await Groups.AddToGroupAsync(Context.ConnectionId, group);
                _logger.LogInformation(Resource.LogSubscribeTimelinePostChange, Context.ConnectionId, group);
            }
            catch (ArgumentException)
            {
                throw new HubException(Resource.MessageTimelineNameInvalid);
            }
            catch (EntityNotExistException)
            {
                throw new HubException(Resource.MessageTimelineNotExist);
            }
        }

        [Obsolete("Use v2.")]
        public async Task UnsubscribeTimelinePostChange(string timelineName)
        {
            var group = GenerateTimelinePostChangeListeningGroupName(timelineName);
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, group);
            _logger.LogInformation(Resource.LogUnsubscribeTimelinePostChange, Context.ConnectionId, group);
        }

        public async Task UnsubscribeTimelinePostChangeV2(string owner, string timeline)
        {
            var group = GenerateTimelinePostChangeListeningGroupName(owner, timeline);
            await Groups.RemoveFromGroupAsync(Context.ConnectionId, group);
            _logger.LogInformation(Resource.LogUnsubscribeTimelinePostChange, Context.ConnectionId, group);
        }
    }
}