summaryrefslogtreecommitdiff
path: root/internal/db/bundb/conversation.go
blob: 1a3958a798b5d69082b4267e1eb0e74bf25a38a0 (plain)
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
// GoToSocial
// Copyright (C) GoToSocial Authors admin@gotosocial.org
// SPDX-License-Identifier: AGPL-3.0-or-later
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package bundb

import (
	"context"
	"errors"
	"slices"

	"github.com/superseriousbusiness/gotosocial/internal/db"
	"github.com/superseriousbusiness/gotosocial/internal/gtscontext"
	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
	"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
	"github.com/superseriousbusiness/gotosocial/internal/id"
	"github.com/superseriousbusiness/gotosocial/internal/log"
	"github.com/superseriousbusiness/gotosocial/internal/paging"
	"github.com/superseriousbusiness/gotosocial/internal/state"
	"github.com/superseriousbusiness/gotosocial/internal/util"
	"github.com/uptrace/bun"
	"github.com/uptrace/bun/dialect"
)

type conversationDB struct {
	db    *bun.DB
	state *state.State
}

func (c *conversationDB) GetConversationByID(ctx context.Context, id string) (*gtsmodel.Conversation, error) {
	return c.getConversation(
		ctx,
		"ID",
		func(conversation *gtsmodel.Conversation) error {
			return c.db.
				NewSelect().
				Model(conversation).
				Where("? = ?", bun.Ident("id"), id).
				Scan(ctx)
		},
		id,
	)
}

func (c *conversationDB) GetConversationByThreadAndAccountIDs(ctx context.Context, threadID string, accountID string, otherAccountIDs []string) (*gtsmodel.Conversation, error) {
	otherAccountsKey := gtsmodel.ConversationOtherAccountsKey(otherAccountIDs)
	return c.getConversation(
		ctx,
		"ThreadID,AccountID,OtherAccountsKey",
		func(conversation *gtsmodel.Conversation) error {
			return c.db.
				NewSelect().
				Model(conversation).
				Where("? = ?", bun.Ident("thread_id"), threadID).
				Where("? = ?", bun.Ident("account_id"), accountID).
				Where("? = ?", bun.Ident("other_accounts_key"), otherAccountsKey).
				Scan(ctx)
		},
		threadID,
		accountID,
		otherAccountsKey,
	)
}

func (c *conversationDB) getConversation(
	ctx context.Context,
	lookup string,
	dbQuery func(conversation *gtsmodel.Conversation) error,
	keyParts ...any,
) (*gtsmodel.Conversation, error) {
	// Fetch conversation from cache with loader callback
	conversation, err := c.state.Caches.GTS.Conversation.LoadOne(lookup, func() (*gtsmodel.Conversation, error) {
		var conversation gtsmodel.Conversation

		// Not cached! Perform database query
		if err := dbQuery(&conversation); err != nil {
			return nil, err
		}

		return &conversation, nil
	}, keyParts...)
	if err != nil {
		// already processe
		return nil, err
	}

	if gtscontext.Barebones(ctx) {
		// Only a barebones model was requested.
		return conversation, nil
	}

	if err := c.populateConversation(ctx, conversation); err != nil {
		return nil, err
	}

	return conversation, nil
}

func (c *conversationDB) populateConversation(ctx context.Context, conversation *gtsmodel.Conversation) error {
	var (
		errs gtserror.MultiError
		err  error
	)

	if conversation.Account == nil {
		conversation.Account, err = c.state.DB.GetAccountByID(
			gtscontext.SetBarebones(ctx),
			conversation.AccountID,
		)
		if err != nil {
			errs.Appendf("error populating conversation owner account: %w", err)
		}
	}

	if conversation.OtherAccounts == nil {
		conversation.OtherAccounts, err = c.state.DB.GetAccountsByIDs(
			gtscontext.SetBarebones(ctx),
			conversation.OtherAccountIDs,
		)
		if err != nil {
			errs.Appendf("error populating other conversation accounts: %w", err)
		}
	}

	if conversation.LastStatus == nil && conversation.LastStatusID != "" {
		conversation.LastStatus, err = c.state.DB.GetStatusByID(
			gtscontext.SetBarebones(ctx),
			conversation.LastStatusID,
		)
		if err != nil {
			errs.Appendf("error populating conversation last status: %w", err)
		}
	}

	return errs.Combine()
}

func (c *conversationDB) GetConversationsByOwnerAccountID(ctx context.Context, accountID string, page *paging.Page) ([]*gtsmodel.Conversation, error) {
	conversationLastStatusIDs, err := c.getAccountConversationLastStatusIDs(ctx, accountID, page)
	if err != nil {
		return nil, err
	}
	return c.getConversationsByLastStatusIDs(ctx, accountID, conversationLastStatusIDs)
}

func (c *conversationDB) getAccountConversationLastStatusIDs(ctx context.Context, accountID string, page *paging.Page) ([]string, error) {
	return loadPagedIDs(&c.state.Caches.GTS.ConversationLastStatusIDs, accountID, page, func() ([]string, error) {
		var conversationLastStatusIDs []string

		// Conversation last status IDs not in cache. Perform DB query.
		if _, err := c.db.
			NewSelect().
			Model((*gtsmodel.Conversation)(nil)).
			Column("last_status_id").
			Where("? = ?", bun.Ident("account_id"), accountID).
			OrderExpr("? DESC", bun.Ident("last_status_id")).
			Exec(ctx, &conversationLastStatusIDs); // nocollapse
		err != nil && !errors.Is(err, db.ErrNoEntries) {
			return nil, err
		}

		return conversationLastStatusIDs, nil
	})
}

func (c *conversationDB) getConversationsByLastStatusIDs(
	ctx context.Context,
	accountID string,
	conversationLastStatusIDs []string,
) ([]*gtsmodel.Conversation, error) {
	// Load all conversation IDs via cache loader callbacks.
	conversations, err := c.state.Caches.GTS.Conversation.LoadIDs2Part(
		"AccountID,LastStatusID",
		accountID,
		conversationLastStatusIDs,
		func(accountID string, uncached []string) ([]*gtsmodel.Conversation, error) {
			// Preallocate expected length of uncached conversations.
			conversations := make([]*gtsmodel.Conversation, 0, len(uncached))

			// Perform database query scanning the remaining (uncached) IDs.
			if err := c.db.NewSelect().
				Model(&conversations).
				Where("? = ?", bun.Ident("account_id"), accountID).
				Where("? IN (?)", bun.Ident("last_status_id"), bun.In(uncached)).
				Scan(ctx); err != nil {
				return nil, err
			}

			return conversations, nil
		},
	)
	if err != nil {
		return nil, err
	}

	// Reorder the conversations by their last status IDs to ensure correct order.
	getID := func(b *gtsmodel.Conversation) string { return b.ID }
	util.OrderBy(conversations, conversationLastStatusIDs, getID)

	if gtscontext.Barebones(ctx) {
		// no need to fully populate.
		return conversations, nil
	}

	// Populate all loaded conversations, removing those we fail to populate.
	conversations = slices.DeleteFunc(conversations, func(conversation *gtsmodel.Conversation) bool {
		if err := c.populateConversation(ctx, conversation); err != nil {
			log.Errorf(ctx, "error populating conversation %s: %v", conversation.ID, err)
			return true
		}
		return false
	})

	return conversations, nil
}

func (c *conversationDB) UpsertConversation(ctx context.Context, conversation *gtsmodel.Conversation, columns ...string) error {
	// If we're updating by column, ensure "updated_at" is included.
	if len(columns) > 0 {
		columns = append(columns, "updated_at")
	}

	return c.state.Caches.GTS.Conversation.Store(conversation, func() error {
		_, err := NewUpsert(c.db).
			Model(conversation).
			Constraint("id").
			Column(columns...).
			Exec(ctx)
		return err
	})
}

func (c *conversationDB) LinkConversationToStatus(ctx context.Context, conversationID string, statusID string) error {
	conversationToStatus := &gtsmodel.ConversationToStatus{
		ConversationID: conversationID,
		StatusID:       statusID,
	}

	if _, err := c.db.NewInsert().
		Model(conversationToStatus).
		Exec(ctx); // nocollapse
	err != nil {
		return err
	}
	return nil
}

func (c *conversationDB) DeleteConversationByID(ctx context.Context, id string) error {
	// Load conversation into cache before attempting a delete,
	// as we need it cached in order to trigger the invalidate
	// callback. This in turn invalidates others.
	_, err := c.GetConversationByID(gtscontext.SetBarebones(ctx), id)
	if err != nil {
		if errors.Is(err, db.ErrNoEntries) {
			// not an issue.
			err = nil
		}
		return err
	}

	// Drop this now-cached conversation on return after delete.
	defer c.state.Caches.GTS.Conversation.Invalidate("ID", id)

	// Finally delete conversation from DB.
	_, err = c.db.NewDelete().
		Model((*gtsmodel.Conversation)(nil)).
		Where("? = ?", bun.Ident("id"), id).
		Exec(ctx)
	return err
}

func (c *conversationDB) DeleteConversationsByOwnerAccountID(ctx context.Context, accountID string) error {
	defer func() {
		// Invalidate any cached conversations and conversation IDs owned by this account on return.
		// Conversation invalidate hooks only invalidate the conversation ID cache,
		// so we don't need to load all conversations into the cache to run invalidation hooks,
		// as with some other object types (blocks, for example).
		c.state.Caches.GTS.Conversation.Invalidate("AccountID", accountID)
		// In case there were no cached conversations,
		// explicitly invalidate the user's conversation last status ID cache.
		c.state.Caches.GTS.ConversationLastStatusIDs.Invalidate(accountID)
	}()

	return c.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
		// Delete conversations matching the account ID.
		deletedConversationIDs := []string{}
		if err := tx.NewDelete().
			Model((*gtsmodel.Conversation)(nil)).
			Where("? = ?", bun.Ident("account_id"), accountID).
			Returning("?", bun.Ident("id")).
			Scan(ctx, &deletedConversationIDs); // nocollapse
		err != nil {
			return gtserror.Newf("error deleting conversations for account %s: %w", accountID, err)
		}

		// Delete any conversation-to-status links matching the deleted conversation IDs.
		if _, err := tx.NewDelete().
			Model((*gtsmodel.ConversationToStatus)(nil)).
			Where("? IN (?)", bun.Ident("conversation_id"), bun.In(deletedConversationIDs)).
			Exec(ctx); // nocollapse
		err != nil {
			return gtserror.Newf("error deleting conversation-to-status links for account %s: %w", accountID, err)
		}

		return nil
	})
}

func (c *conversationDB) DeleteStatusFromConversations(ctx context.Context, statusID string) error {
	// SQL returning the current time.
	var nowSQL string
	switch c.db.Dialect().Name() {
	case dialect.SQLite:
		nowSQL = "DATE('now')"
	case dialect.PG:
		nowSQL = "NOW()"
	default:
		log.Panicf(nil, "db conn %s was neither pg nor sqlite", c.db)
	}

	updatedConversationIDs := []string{}
	deletedConversationIDs := []string{}

	if err := c.db.RunInTx(ctx, nil, func(ctx context.Context, tx bun.Tx) error {
		// Delete this status from conversation-to-status links.
		if _, err := tx.NewDelete().
			Model((*gtsmodel.ConversationToStatus)(nil)).
			Where("? = ?", bun.Ident("status_id"), statusID).
			Exec(ctx); // nocollapse
		err != nil {
			return gtserror.Newf("error deleting conversation-to-status links while deleting status %s: %w", statusID, err)
		}

		// Note: Bun doesn't currently support CREATE TABLE … AS SELECT … so we need to use raw queries here.

		// Create a temporary table with all statuses other than the deleted status
		// in each conversation for which the deleted status is the last status
		// (if there are such statuses).
		conversationStatusesTempTable := "conversation_statuses_" + id.NewULID()
		if _, err := tx.NewRaw(
			"CREATE TEMPORARY TABLE ? AS ?",
			bun.Ident(conversationStatusesTempTable),
			tx.NewSelect().
				ColumnExpr(
					"? AS ?",
					bun.Ident("conversations.id"),
					bun.Ident("conversation_id"),
				).
				ColumnExpr(
					"? AS ?",
					bun.Ident("conversation_to_statuses.status_id"),
					bun.Ident("id"),
				).
				Column("statuses.created_at").
				Table("conversations").
				Join("LEFT JOIN ?", bun.Ident("conversation_to_statuses")).
				JoinOn(
					"? = ?",
					bun.Ident("conversations.id"),
					bun.Ident("conversation_to_statuses.conversation_id"),
				).
				JoinOn(
					"? != ?",
					bun.Ident("conversation_to_statuses.status_id"),
					statusID,
				).
				Join("LEFT JOIN ?", bun.Ident("statuses")).
				JoinOn(
					"? = ?",
					bun.Ident("conversation_to_statuses.status_id"),
					bun.Ident("statuses.id"),
				).
				Where(
					"? = ?",
					bun.Ident("conversations.last_status_id"),
					statusID,
				),
		).
			Exec(ctx); // nocollapse
		err != nil {
			return gtserror.Newf("error creating conversationStatusesTempTable while deleting status %s: %w", statusID, err)
		}

		// Create a temporary table with the most recently created status in each conversation
		// for which the deleted status is the last status (if there is such a status).
		latestConversationStatusesTempTable := "latest_conversation_statuses_" + id.NewULID()
		if _, err := tx.NewRaw(
			"CREATE TEMPORARY TABLE ? AS ?",
			bun.Ident(latestConversationStatusesTempTable),
			tx.NewSelect().
				Column(
					"conversation_statuses.conversation_id",
					"conversation_statuses.id",
				).
				TableExpr(
					"? AS ?",
					bun.Ident(conversationStatusesTempTable),
					bun.Ident("conversation_statuses"),
				).
				Join(
					"LEFT JOIN ? AS ?",
					bun.Ident(conversationStatusesTempTable),
					bun.Ident("later_statuses"),
				).
				JoinOn(
					"? = ?",
					bun.Ident("conversation_statuses.conversation_id"),
					bun.Ident("later_statuses.conversation_id"),
				).
				JoinOn(
					"? > ?",
					bun.Ident("later_statuses.created_at"),
					bun.Ident("conversation_statuses.created_at"),
				).
				Where("? IS NULL", bun.Ident("later_statuses.id")),
		).
			Exec(ctx); // nocollapse
		err != nil {
			return gtserror.Newf("error creating latestConversationStatusesTempTable while deleting status %s: %w", statusID, err)
		}

		// For every conversation where the given status was the last one,
		// reset its last status to the most recently created in the conversation other than that one,
		// if there is such a status.
		// Return conversation IDs for invalidation.
		if err := tx.NewUpdate().
			Model((*gtsmodel.Conversation)(nil)).
			SetColumn("last_status_id", "?", bun.Ident("latest_conversation_statuses.id")).
			SetColumn("updated_at", "?", bun.Safe(nowSQL)).
			TableExpr("? AS ?", bun.Ident(latestConversationStatusesTempTable), bun.Ident("latest_conversation_statuses")).
			Where("?TableAlias.? = ?", bun.Ident("id"), bun.Ident("latest_conversation_statuses.conversation_id")).
			Where("? IS NOT NULL", bun.Ident("latest_conversation_statuses.id")).
			Returning("?TableName.?", bun.Ident("id")).
			Scan(ctx, &updatedConversationIDs); // nocollapse
		err != nil {
			return gtserror.Newf("error rolling back last status for conversation while deleting status %s: %w", statusID, err)
		}

		// If there is no such status, delete the conversation.
		// Return conversation IDs for invalidation.
		if err := tx.NewDelete().
			Model((*gtsmodel.Conversation)(nil)).
			Where(
				"? IN (?)",
				bun.Ident("id"),
				tx.NewSelect().
					Table(latestConversationStatusesTempTable).
					Column("conversation_id").
					Where("? IS NULL", bun.Ident("id")),
			).
			Returning("?", bun.Ident("id")).
			Scan(ctx, &deletedConversationIDs); // nocollapse
		err != nil {
			return gtserror.Newf("error deleting conversation while deleting status %s: %w", statusID, err)
		}

		// Clean up.
		for _, tempTable := range []string{
			conversationStatusesTempTable,
			latestConversationStatusesTempTable,
		} {
			if _, err := tx.NewDropTable().Table(tempTable).Exec(ctx); err != nil {
				return gtserror.Newf(
					"error dropping temporary table %s after deleting status %s: %w",
					tempTable,
					statusID,
					err,
				)
			}
		}

		return nil
	}); err != nil {
		return err
	}

	updatedConversationIDs = append(updatedConversationIDs, deletedConversationIDs...)
	c.state.Caches.GTS.Conversation.InvalidateIDs("ID", updatedConversationIDs)

	return nil
}