• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bpatrik / pigallery2 / 17530592184

07 Sep 2025 03:41PM UTC coverage: 65.073% (+0.07%) from 65.007%
17530592184

push

github

bpatrik
fix tests #1015

1261 of 2199 branches covered (57.34%)

Branch coverage included in aggregate %.

4658 of 6897 relevant lines covered (67.54%)

4419.67 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.07
/src/backend/model/database/SessionManager.ts
1
import * as crypto from 'crypto';
1✔
2
import {ContextUser, SessionContext} from '../SessionContext';
1✔
3
import {SearchQueryUtils} from '../../../common/SearchQueryUtils';
1✔
4
import {Config} from '../../../common/config/private/Config';
1✔
5
import {ANDSearchQuery, SearchQueryDTO, SearchQueryTypes} from '../../../common/entities/SearchQueryDTO';
1✔
6
import {SharingEntity} from './enitites/SharingEntity';
7
import {ObjectManagers} from '../ObjectManagers';
1✔
8
import {Logger} from '../../Logger';
1✔
9
import {Utils} from '../../../common/Utils';
10

11
const LOG_TAG = '[SessionManager]';
1✔
12

13
export class SessionManager {
1✔
14

15
  public static readonly NO_PROJECTION_KEY = 'pr:' + crypto.createHash('md5').update('No Key').digest('hex');
1✔
16

17
  private getQueryForUser(user: ContextUser) {
18
    let blockQuery = user.overrideAllowBlockList ? user.blockQuery : Config.Users.blockQuery;
322✔
19
    const allowQuery = user.overrideAllowBlockList ? user.allowQuery : Config.Users.allowQuery;
322✔
20

21
    if (SearchQueryUtils.isQueryEmpty(allowQuery) && SearchQueryUtils.isQueryEmpty(blockQuery)) {
322✔
22
      return null;
277✔
23
    }
24

25
    if (!SearchQueryUtils.isQueryEmpty(blockQuery)) {
45✔
26
      blockQuery = SearchQueryUtils.negate(blockQuery);
8✔
27
    }
28
    let query = !SearchQueryUtils.isQueryEmpty(allowQuery) ? allowQuery : blockQuery;
45✔
29
    if (!SearchQueryUtils.isQueryEmpty(allowQuery) && !SearchQueryUtils.isQueryEmpty(blockQuery)) {
45✔
30
      query = {
4✔
31
        type: SearchQueryTypes.AND,
32
        list: [
33
          allowQuery,
34
          blockQuery
35
        ]
36
      } as ANDSearchQuery;
37
    }
38
    return query;
45✔
39

40
  }
41

42
  public buildAllowListForSharing(sharing: SharingEntity): SearchQueryDTO {
43
    const creatorQuery = this.getQueryForUser(sharing.creator);
16✔
44
    let finalQuery = sharing.searchQuery;
16✔
45
    if (creatorQuery) {
16✔
46
      finalQuery = {
4✔
47
        type: SearchQueryTypes.AND,
48
        list: [
49
          creatorQuery,
50
          sharing.searchQuery
51
        ]
52
      } as ANDSearchQuery;
53
    }
54
    return finalQuery;
16✔
55
  }
56

57
  public createProjectionKey(q: SearchQueryDTO) {
58
    const canonical = SearchQueryUtils.stringifyForComparison(q);
46✔
59
    return 'pr:' + crypto.createHash('md5').update(canonical).digest('hex');
46✔
60
  }
61

62
  public async buildContext(user: ContextUser): Promise<SessionContext> {
63
    const context = new SessionContext();
306✔
64
    context.user = user;
306✔
65
    context.user.projectionKey = SessionManager.NO_PROJECTION_KEY;
306✔
66
    let finalQuery = this.getQueryForUser(user);
306✔
67

68
    if (finalQuery) {
306✔
69
      // Build the Brackets-based query
70
      context.projectionQuery = await ObjectManagers.getInstance().SearchManager.prepareAndBuildWhereQuery(finalQuery);
41✔
71
      context.hasDirectoryProjection = ObjectManagers.getInstance().SearchManager.hasDirectoryQuery(finalQuery);
41✔
72
      if (context.hasDirectoryProjection) {
41✔
73
        context.projectionQueryForSubDir = await ObjectManagers.getInstance().SearchManager.prepareAndBuildWhereQuery(finalQuery, true, {directory: 'directories'});
21✔
74
      }
75
      context.user.projectionKey = this.createProjectionKey(finalQuery);
41✔
76
      if (SearchQueryUtils.isQueryEmpty(finalQuery)) {
41!
77
        Logger.silly(LOG_TAG, 'Empty Projection query.');
×
78
      } else {
79
        Logger.silly(LOG_TAG, 'Projection query: ' + JSON.stringify(finalQuery));
41✔
80
      }
81
    }
82
    return context;
306✔
83
  }
84

85
  async getAvailableUserSessions(): Promise<SessionContext[]> {
86
    // List all users and build a session context for each
87
    const users = await ObjectManagers.getInstance().UserManager.find({} as any);
×
88
    const sessions: SessionContext[] = [];
×
89
    for (const u of users as unknown as ContextUser[]) {
×
90
      try {
×
91
        const ctx = await this.buildContext(u);
×
92
        sessions.push(ctx);
×
93
      } catch (e) {
94
        // Log and continue on individual user context build failure to ensure we return other sessions
95
        Logger.warn(LOG_TAG, 'Failed to build session context for user id=' + (u as any)?.id + ': ' + (e as Error)?.message);
×
96
      }
97
    }
98
    return sessions;
×
99
  }
100
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc