-
Notifications
You must be signed in to change notification settings - Fork 0
/
member.go
53 lines (43 loc) · 1.47 KB
/
member.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
// Copyright 2021 The fallacy Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package fallacy
import (
"strings"
"maunium.net/go/mautrix/event"
"maunium.net/go/mautrix/id"
)
// isNewJoin checks if a membership event is really a new join.
func isNewJoin(ev event.Event) bool {
prev := ev.Unsigned.PrevContent
if ev.Content.AsMember().Membership == event.MembershipJoin && prev == nil {
return true
}
if m, ok := prev.Raw["membership"].(string); ok {
if m == "join" {
return false
}
}
return true
}
// WelcomeMember welcomes a member via their display name. The display name is
// calculated as per
// https://spec.matrix.org/v1.1/Client-server-api/#calculating-the-display-name-for-a-user.
func WelcomeMember(display string, sender id.UserID, roomID id.RoomID) (err error) {
senderStr := sender.String()
// if the name is just whitespace we can just use sender ID
if f := strings.Fields(display); len(f) == 0 {
display = senderStr
}
welcome := func(s string) string {
return strings.Join([]string{"Welcome", s + "!", "Howdy?"}, " ")
}
anchor := strings.Join([]string{"<a href='https://matrix.to/#/", senderStr, "'>", display, "</a>"}, "")
_, err = Client.SendMessageEvent(roomID, event.EventMessage, event.MessageEventContent{
Body: welcome(display),
Format: event.FormatHTML,
FormattedBody: welcome(anchor),
MsgType: event.MsgNotice,
})
return
}