This repository was archived by the owner on May 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathutils.go
94 lines (82 loc) · 2.76 KB
/
utils.go
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
// Copyright 2017 Michael Telatynski <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package mxclient
import "github.com/matrix-org/gomatrix"
// Keeping here in case it becomes used again.
//func ConcatEventsSlices(slices ...[]gomatrix.Event) []gomatrix.Event {
// var totalLen int
// for _, s := range slices {
// totalLen += len(s)
// }
// tmp := make([]gomatrix.Event, totalLen)
// var i int
// for _, s := range slices {
// i += copy(tmp[i:], s)
// }
// return tmp
//}
// ReverseEventsCopy returns a copy of the input slice with all elements in reverse order.
func ReverseEventsCopy(events []gomatrix.Event) []gomatrix.Event {
var newEvents []gomatrix.Event
for i := len(events) - 1; i >= 0; i-- {
newEvents = append(newEvents, events[i])
}
return newEvents
}
// UnwrapRespError takes an error and if it is a HTTPError returns the WrappedError.RespError it contains
func UnwrapRespError(err error) (respErr gomatrix.RespError, respErrOk bool) {
if err, ok := err.(gomatrix.HTTPError); ok {
respErr, respErrOk = err.WrappedError.(gomatrix.RespError)
}
return
}
var textForRespError = map[string]string{
"M_GUEST_ACCESS_FORBIDDEN": "This Room does not exist or does not permit guests to access it.",
}
// TextForRespError returns a string representation of the RespError if known, defaulting to the Err field of the RespError.
func TextForRespError(respErr gomatrix.RespError) string {
if msg, ok := textForRespError[respErr.ErrCode]; ok {
return msg
}
return respErr.Err + " (" + respErr.ErrCode + ")"
}
// ShouldHideEvent returns a bool the event should be ignored in the timeline view, mimicking riot-web
func ShouldHideEvent(ev gomatrix.Event) bool {
// m.room.create ?
// we want to hide all unknowns +:
// m.room.redaction
// m.room.aliases
// m.room.canonical_alias
if ev.StateKey == nil {
// Message Event
if ev.Type == "m.room.message" {
return false
}
} else {
// State Event
if ev.Type == "m.room.history_visibility" ||
ev.Type == "m.room.join_rules" ||
ev.Type == "m.room.member" ||
ev.Type == "m.room.power_levels" ||
ev.Type == "m.room.name" ||
ev.Type == "m.room.topic" ||
ev.Type == "m.room.avatar" {
return false
}
if ev.Type == "im.vector.modular.widgets" {
return false
}
}
return true
}