| 1 |
// Copyright (c) 2020 Tulir Asokan |
| 2 |
// |
| 3 |
// This Source Code Form is subject to the terms of the Mozilla Public |
| 4 |
// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 5 |
// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 |
|
| 7 |
package event |
| 8 |
|
| 9 |
import ( |
| 10 |
"sync" |
| 11 |
|
| 12 |
"maunium.net/go/mautrix/id" |
| 13 |
) |
| 14 |
|
| 15 |
// PowerLevelsEventContent represents the content of a m.room.power_levels state event content. |
| 16 |
// https://spec.matrix.org/v1.2/client-server-api/#mroompower_levels |
| 17 |
type PowerLevelsEventContent struct { |
| 18 |
usersLock sync.RWMutex |
| 19 |
Users map[id.UserID]int `json:"users,omitempty"` |
| 20 |
UsersDefault int `json:"users_default,omitempty"` |
| 21 |
|
| 22 |
eventsLock sync.RWMutex |
| 23 |
Events map[string]int `json:"events,omitempty"` |
| 24 |
EventsDefault int `json:"events_default,omitempty"` |
| 25 |
|
| 26 |
StateDefaultPtr *int `json:"state_default,omitempty"` |
| 27 |
|
| 28 |
InvitePtr *int `json:"invite,omitempty"` |
| 29 |
KickPtr *int `json:"kick,omitempty"` |
| 30 |
BanPtr *int `json:"ban,omitempty"` |
| 31 |
RedactPtr *int `json:"redact,omitempty"` |
| 32 |
HistoricalPtr *int `json:"historical,omitempty"` |
| 33 |
} |
| 34 |
|
| 35 |
func (pl *PowerLevelsEventContent) Invite() int { |
| 36 |
if pl.InvitePtr != nil { |
| 37 |
return *pl.InvitePtr |
| 38 |
} |
| 39 |
return 50 |
| 40 |
} |
| 41 |
|
| 42 |
func (pl *PowerLevelsEventContent) Kick() int { |
| 43 |
if pl.KickPtr != nil { |
| 44 |
return *pl.KickPtr |
| 45 |
} |
| 46 |
return 50 |
| 47 |
} |
| 48 |
|
| 49 |
func (pl *PowerLevelsEventContent) Ban() int { |
| 50 |
if pl.BanPtr != nil { |
| 51 |
return *pl.BanPtr |
| 52 |
} |
| 53 |
return 50 |
| 54 |
} |
| 55 |
|
| 56 |
func (pl *PowerLevelsEventContent) Redact() int { |
| 57 |
if pl.RedactPtr != nil { |
| 58 |
return *pl.RedactPtr |
| 59 |
} |
| 60 |
return 50 |
| 61 |
} |
| 62 |
|
| 63 |
func (pl *PowerLevelsEventContent) Historical() int { |
| 64 |
if pl.HistoricalPtr != nil { |
| 65 |
return *pl.HistoricalPtr |
| 66 |
} |
| 67 |
return 100 |
| 68 |
} |
| 69 |
|
| 70 |
func (pl *PowerLevelsEventContent) StateDefault() int { |
| 71 |
if pl.StateDefaultPtr != nil { |
| 72 |
return *pl.StateDefaultPtr |
| 73 |
} |
| 74 |
return 50 |
| 75 |
} |
| 76 |
|
| 77 |
func (pl *PowerLevelsEventContent) GetUserLevel(userID id.UserID) int { |
| 78 |
pl.usersLock.RLock() |
| 79 |
defer pl.usersLock.RUnlock() |
| 80 |
level, ok := pl.Users[userID] |
| 81 |
if !ok { |
| 82 |
return pl.UsersDefault |
| 83 |
} |
| 84 |
return level |
| 85 |
} |
| 86 |
|
| 87 |
func (pl *PowerLevelsEventContent) SetUserLevel(userID id.UserID, level int) { |
| 88 |
pl.usersLock.Lock() |
| 89 |
defer pl.usersLock.Unlock() |
| 90 |
if level == pl.UsersDefault { |
| 91 |
delete(pl.Users, userID) |
| 92 |
} else { |
| 93 |
pl.Users[userID] = level |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
func (pl *PowerLevelsEventContent) EnsureUserLevel(userID id.UserID, level int) bool { |
| 98 |
existingLevel := pl.GetUserLevel(userID) |
| 99 |
if existingLevel != level { |
| 100 |
pl.SetUserLevel(userID, level) |
| 101 |
return true |
| 102 |
} |
| 103 |
return false |
| 104 |
} |
| 105 |
|
| 106 |
func (pl *PowerLevelsEventContent) GetEventLevel(eventType Type) int { |
| 107 |
pl.eventsLock.RLock() |
| 108 |
defer pl.eventsLock.RUnlock() |
| 109 |
level, ok := pl.Events[eventType.String()] |
| 110 |
if !ok { |
| 111 |
if eventType.IsState() { |
| 112 |
return pl.StateDefault() |
| 113 |
} |
| 114 |
return pl.EventsDefault |
| 115 |
} |
| 116 |
return level |
| 117 |
} |
| 118 |
|
| 119 |
func (pl *PowerLevelsEventContent) SetEventLevel(eventType Type, level int) { |
| 120 |
pl.eventsLock.Lock() |
| 121 |
defer pl.eventsLock.Unlock() |
| 122 |
if (eventType.IsState() && level == pl.StateDefault()) || (!eventType.IsState() && level == pl.EventsDefault) { |
| 123 |
delete(pl.Events, eventType.String()) |
| 124 |
} else { |
| 125 |
pl.Events[eventType.String()] = level |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func (pl *PowerLevelsEventContent) EnsureEventLevel(eventType Type, level int) bool { |
| 130 |
existingLevel := pl.GetEventLevel(eventType) |
| 131 |
if existingLevel != level { |
| 132 |
pl.SetEventLevel(eventType, level) |
| 133 |
return true |
| 134 |
} |
| 135 |
return false |
| 136 |
} |