ampelmaennchen.git

ref: master

matrix/rooms.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
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package matrix

import (
	"apiote.xyz/p/ampelmaennchen/config"
	"apiote.xyz/p/ampelmaennchen/db"

	"context"
	"errors"

	"apiote.xyz/p/gott/v2"
	"maunium.net/go/mautrix"
	"maunium.net/go/mautrix/appservice"
	"maunium.net/go/mautrix/event"
	"maunium.net/go/mautrix/id"
)

type createRoomResult struct {
	Intent         *appservice.IntentAPI
	CreateFunction func(*appservice.IntentAPI, string, string, id.RoomID) (id.RoomID, error)
	Kind           string
	Name           string
	Alias          string
	Parent         id.RoomID
	AvatarPath     string

	RoomID         id.RoomID
	AvatarPathInDB string
	ParentInDB     string
	AvatarUri      id.ContentURI
	AvatarUriInDB  string
}

func createSpace(intent *appservice.IntentAPI, name, alias string, spaceID id.RoomID) (id.RoomID, error) {
	zero := 0
	request := &mautrix.ReqCreateRoom{
		Name:   name,
		Preset: "public_chat",
		PowerLevelOverride: &event.PowerLevelsEventContent{
			EventsDefault: 100,
			InvitePtr:     &zero,
		},
		RoomAliasName: alias,
		Visibility:    "private",
		CreationContent: map[string]interface{}{
			"type": "m.space",
		},
		InitialState: []*event.Event{
			{
				Type: event.StateGuestAccess,
				Content: event.Content{
					Parsed: event.GuestAccessEventContent{
						GuestAccess: event.GuestAccessCanJoin,
					},
				},
			},
			{
				Type: event.StateHistoryVisibility,
				Content: event.Content{
					Parsed: event.HistoryVisibilityEventContent{
						HistoryVisibility: event.HistoryVisibilityWorldReadable,
					},
				},
			},
		},
	}

	response, err := intent.CreateRoom(context.Background(), request)
	if errors.Is(err, mautrix.MRoomInUse) {
		return id.RoomID(""), nil
	}
	return response.RoomID, err
}

func createPublicRoom(intent *appservice.IntentAPI, name, alias string, spaceID id.RoomID) (id.RoomID, error) {
	spaceIDString := spaceID.String()
	request := &mautrix.ReqCreateRoom{
		Name:          name,
		Preset:        "public_chat",
		RoomAliasName: alias,
		InitialState: []*event.Event{
			{
				Type: event.StateGuestAccess,
				Content: event.Content{
					Parsed: event.GuestAccessEventContent{
						GuestAccess: event.GuestAccessCanJoin,
					},
				},
			},
			{
				Type: event.StateHistoryVisibility,
				Content: event.Content{
					Parsed: event.HistoryVisibilityEventContent{
						HistoryVisibility: event.HistoryVisibilityWorldReadable,
					},
				},
			},
			{
				Type: event.StateSpaceParent,
				Content: event.Content{
					Parsed: event.SpaceParentEventContent{
						Via:       []string{"apiote.xyz"},
						Canonical: true,
					},
				},
				StateKey: &spaceIDString,
			},
		},
	}

	response, err := intent.CreateRoom(context.Background(), request)
	if errors.Is(err, mautrix.MRoomInUse) {
		return id.RoomID(""), nil
	}
	return response.RoomID, err
}

func createPrivateRoom(intent *appservice.IntentAPI, name, alias string, spaceID id.RoomID) (id.RoomID, error) {
	spaceIDString := spaceID.String()
	request := &mautrix.ReqCreateRoom{
		Name:          name,
		Preset:        "private_chat",
		RoomAliasName: alias,
		InitialState: []*event.Event{
			{
				Type: event.StateHistoryVisibility,
				Content: event.Content{
					Parsed: event.HistoryVisibilityEventContent{
						HistoryVisibility: event.HistoryVisibilityShared,
					},
				},
			},
			{
				Type: event.StateSpaceParent,
				Content: event.Content{
					Parsed: event.SpaceParentEventContent{
						Via:       []string{"apiote.xyz"},
						Canonical: true,
					},
				},
				StateKey: &spaceIDString,
			},
		},
	}

	response, err := intent.CreateRoom(context.Background(), request)
	if errors.Is(err, mautrix.MRoomInUse) {
		return id.RoomID(""), nil
	}
	return response.RoomID, err
}

func saveRoom(r createRoomResult) error {
	return db.SaveRoom(r.Kind, r.RoomID.String(), r.AvatarPathInDB, r.ParentInDB, r.AvatarUriInDB)
}

func getRoom(r createRoomResult) (createRoomResult, error) {
	roomID, avatarPathInDB, parentInDB, avatarUriInDB, err := db.GetRoom(r.Kind)
	if err != nil {
		return r, err
	}
	r.RoomID = id.RoomID(roomID)
	r.AvatarPathInDB = avatarPathInDB
	r.ParentInDB = parentInDB
	r.AvatarUriInDB = avatarUriInDB
	return r, nil
}

func createRoom(r createRoomResult) (createRoomResult, error) {
	if r.RoomID.String() == "" {
		roomID, err := r.CreateFunction(r.Intent, r.Name, r.Alias, r.Parent)
		if err != nil {
			return r, err
		}
		r.RoomID = roomID
		return r, nil
	}
	return r, nil
}

func uploadRoomAvatar(r createRoomResult) (createRoomResult, error) {
	if r.AvatarPathInDB != r.AvatarPath {
		uri, err := uploadImage(r.Intent, r.AvatarPath)
		if err != nil {
			return r, err
		}
		r.AvatarUri = uri
	}
	return r, nil
}

func setRoomAvatar(r createRoomResult) error {
	if !r.AvatarUri.IsEmpty() && r.AvatarUriInDB != r.AvatarUri.String() {
		_, err := r.Intent.SendStateEvent(context.Background(), r.RoomID, event.StateRoomAvatar, "", &event.RoomAvatarEventContent{
			URL: r.AvatarUri,
		})
		r.AvatarPathInDB = r.AvatarPath
		return err
	}
	return nil
}

func setRoomParent(r createRoomResult) error {
	if r.ParentInDB != r.Parent.String() {
		_, err := r.Intent.SendStateEvent(context.Background(), r.Parent, event.StateSpaceChild, r.RoomID.String(), &event.SpaceChildEventContent{
			Via:       []string{"apiote.xyz"},
			Suggested: true,
		})
		r.ParentInDB = r.Parent.String()
		return err
	}
	return nil
}

func CreatePublicRoom(as *appservice.AppService, cfg config.MatrixConfig, spaceID id.RoomID) error {
	botuser := id.NewUserID("ampelmaennchen", cfg.Homeserver.Domain)
	intent := as.Intent(botuser)

	r := gott.R[createRoomResult]{
		S: createRoomResult{
			Intent:         intent,
			CreateFunction: createPublicRoom,
			Kind:           "public",
			Name:           cfg.Rooms.PublicRoomName,
			Alias:          cfg.Rooms.PublicRoomAlias,
			AvatarPath:     cfg.Rooms.PublicRoomAvatarPath,
			Parent:         spaceID,
		},
	}.
		Bind(getRoom).
		Bind(createRoom).
		Tee(saveRoom).
		Bind(uploadRoomAvatar).
		Tee(setRoomAvatar).
		Tee(saveRoom).
		Tee(setRoomParent).
		Tee(saveRoom)

	return r.E
}

func CreatePatronsRoom(as *appservice.AppService, cfg config.MatrixConfig, spaceID id.RoomID) error {
	botuser := id.NewUserID("ampelmaennchen", cfg.Homeserver.Domain)
	intent := as.Intent(botuser)

	r := gott.R[createRoomResult]{
		S: createRoomResult{
			Intent:         intent,
			CreateFunction: createPrivateRoom,
			Kind:           "patrons",
			Name:           cfg.Rooms.PatronsRoomName,
			Alias:          cfg.Rooms.PatronsRoomAlias,
			AvatarPath:     cfg.Rooms.PatronsRoomAvatarPath,
			Parent:         spaceID,
		},
	}.
		Bind(getRoom).
		Bind(createRoom).
		Tee(saveRoom).
		Bind(uploadRoomAvatar).
		Tee(setRoomAvatar).
		Tee(saveRoom).
		Tee(setRoomParent).
		Tee(saveRoom)

	return r.E
}

func CreateHeadOfficeRoom(as *appservice.AppService, cfg config.MatrixConfig, spaceID id.RoomID) error {
	botuser := id.NewUserID("ampelmaennchen", cfg.Homeserver.Domain)
	intent := as.Intent(botuser)

	r := gott.R[createRoomResult]{
		S: createRoomResult{
			Intent:         intent,
			CreateFunction: createPrivateRoom,
			Kind:           "headoffice",
			Name:           cfg.Rooms.HeadOfficeRoomName,
			Alias:          cfg.Rooms.HeadOfficeRoomAlias,
			AvatarPath:     cfg.Rooms.HeadOfficeRoomAvatarPath,
			Parent:         spaceID,
		},
	}.
		Bind(getRoom).
		Bind(createRoom).
		Tee(saveRoom).
		Bind(uploadRoomAvatar).
		Tee(setRoomAvatar).
		Tee(saveRoom).
		Tee(setRoomParent).
		Tee(saveRoom)

	return r.E
}

func CreateSpace(as *appservice.AppService, cfg config.MatrixConfig) (id.RoomID, error) {
	botuser := id.NewUserID("ampelmaennchen", cfg.Homeserver.Domain)
	intent := as.Intent(botuser)

	r := gott.R[createRoomResult]{
		S: createRoomResult{
			Intent:         intent,
			CreateFunction: createSpace,
			Kind:           "space",
			Name:           cfg.Rooms.SpaceName,
			Alias:          cfg.Rooms.SpaceAlias,
			AvatarPath:     cfg.Rooms.SpaceAvatarPath,
			Parent:         "",
		},
	}.
		Bind(getRoom).
		Bind(createRoom).
		Tee(saveRoom).
		Bind(uploadRoomAvatar).
		Tee(setRoomAvatar).
		Tee(saveRoom).
		Tee(setRoomParent).
		Tee(saveRoom)

	return r.S.RoomID, r.E
}