File size: 1,696 Bytes
13555f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package ws

import (
	"sync"
	"sync/atomic"
	"time"

	mmModel "github.com/mattermost/mattermost/server/public/model"
)

type PluginAdapterClient struct {
	inactiveAt int64
	webConnID  string
	userID     string
	teams      []string
	blocks     []string
	mu         sync.RWMutex
}

func (pac *PluginAdapterClient) isActive() bool {
	return atomic.LoadInt64(&pac.inactiveAt) == 0
}

func (pac *PluginAdapterClient) hasExpired(threshold time.Duration) bool {
	return !mmModel.GetTimeForMillis(atomic.LoadInt64(&pac.inactiveAt)).Add(threshold).After(time.Now())
}

func (pac *PluginAdapterClient) subscribeToTeam(teamID string) {
	pac.mu.Lock()
	defer pac.mu.Unlock()

	pac.teams = append(pac.teams, teamID)
}

func (pac *PluginAdapterClient) unsubscribeFromTeam(teamID string) {
	pac.mu.Lock()
	defer pac.mu.Unlock()

	newClientTeams := []string{}
	for _, id := range pac.teams {
		if id != teamID {
			newClientTeams = append(newClientTeams, id)
		}
	}
	pac.teams = newClientTeams
}

func (pac *PluginAdapterClient) unsubscribeFromBlock(blockID string) {
	pac.mu.Lock()
	defer pac.mu.Unlock()

	newClientBlocks := []string{}
	for _, id := range pac.blocks {
		if id != blockID {
			newClientBlocks = append(newClientBlocks, id)
		}
	}
	pac.blocks = newClientBlocks
}

func (pac *PluginAdapterClient) isSubscribedToTeam(teamID string) bool {
	pac.mu.RLock()
	defer pac.mu.RUnlock()

	for _, id := range pac.teams {
		if id == teamID {
			return true
		}
	}

	return false
}

//nolint:unused
func (pac *PluginAdapterClient) isSubscribedToBlock(blockID string) bool {
	pac.mu.RLock()
	defer pac.mu.RUnlock()

	for _, id := range pac.blocks {
		if id == blockID {
			return true
		}
	}

	return false
}