Skip to content

Commit 044e111

Browse files
committed
Make all sogs.config timespan variables store seconds
The sogs.ini values remain as days, but the value is converted to seconds now before storing in sogs.config.WHATEVER. This is less bug-prone: in particular, there was a bug in model.file.File.set_expiry that wasn't multiplying by 86400 when using the days value, which is now fixed (by eliminating the multiplication requirement).
1 parent c794afe commit 044e111

File tree

10 files changed

+38
-36
lines changed

10 files changed

+38
-36
lines changed

sogs/cleanup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def prune_files():
6868
def prune_message_history():
6969
count = query(
7070
"DELETE FROM message_history WHERE replaced < :t",
71-
t=time.time() - config.MESSAGE_HISTORY_PRUNE_THRESHOLD * 86400,
71+
t=time.time() - config.MESSAGE_HISTORY_PRUNE_THRESHOLD,
7272
).rowcount
7373

7474
if count > 0:
@@ -79,7 +79,7 @@ def prune_message_history():
7979
def prune_room_activity():
8080
count = query(
8181
"DELETE FROM room_users WHERE last_active < :t",
82-
t=time.time() - config.ROOM_ACTIVE_PRUNE_THRESHOLD * 86400,
82+
t=time.time() - config.ROOM_ACTIVE_PRUNE_THRESHOLD,
8383
).rowcount
8484

8585
if count > 0:

sogs/config.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
OMQ_LISTEN = 'tcp://*:22028'
1919
OMQ_INTERNAL = 'ipc://./omq.sock'
2020
LOG_LEVEL = 'WARNING'
21-
DM_EXPIRY_DAYS = 15
22-
UPLOAD_DEFAULT_EXPIRY_DAYS = 15
21+
DM_EXPIRY = 15 * 86400.0 # Seconds, but specified in config file as days
22+
UPLOAD_DEFAULT_EXPIRY = 15 * 86400.0 # Seconds, but specified in config file as days
2323
UPLOAD_FILENAME_MAX = 60
2424
UPLOAD_FILENAME_KEEP_PREFIX = 40
2525
UPLOAD_FILENAME_KEEP_SUFFIX = 17
2626
UPLOAD_FILE_MAX_SIZE = 6_000_000
2727
UPLOAD_FILENAME_BAD = re.compile(r"[^\w+\-.'()@\[\]]+")
28-
ROOM_ACTIVE_PRUNE_THRESHOLD = 60
29-
ROOM_DEFAULT_ACTIVE_THRESHOLD = 7
30-
MESSAGE_HISTORY_PRUNE_THRESHOLD = 30
28+
ROOM_ACTIVE_PRUNE_THRESHOLD = 60 * 86400.0 # Seconds, but specified in config file as days
29+
ROOM_DEFAULT_ACTIVE_THRESHOLD = 7 * 86400.0 # Seconds, but specified in config file as days
30+
MESSAGE_HISTORY_PRUNE_THRESHOLD = 30 * 86400.0 # Seconds, but specified in config file as days
3131
IMPORT_ADJUST_MS = 0
3232
PROFANITY_FILTER = False
3333
PROFANITY_SILENT = True
@@ -77,6 +77,9 @@ def path_exists(path):
7777
def val_or_none(v):
7878
return v or None
7979

80+
def days_to_seconds(v):
81+
return float(v) * 86400.0
82+
8083
truthy = ('y', 'yes', 'Y', 'Yes', 'true', 'True', 'on', 'On', '1')
8184
falsey = ('n', 'no', 'N', 'No', 'false', 'False', 'off', 'Off', '0')
8285
booly = truthy + falsey
@@ -105,18 +108,18 @@ def bool_opt(name):
105108
'http_show_recent': bool_opt('HTTP_SHOW_RECENT'),
106109
},
107110
'files': {
108-
'expiry': ('UPLOAD_DEFAULT_EXPIRY_DAYS', None, float),
111+
'expiry': ('UPLOAD_DEFAULT_EXPIRY', None, days_to_seconds),
109112
'max_size': ('UPLOAD_FILE_MAX_SIZE', None, int),
110113
'uploads_dir': ('UPLOAD_PATH', path_exists, val_or_none),
111114
},
112115
'rooms': {
113-
'active_threshold': ('ROOM_DEFAULT_ACTIVE_THRESHOLD', None, float),
114-
'active_prune_threshold': ('ROOM_ACTIVE_PRUNE_THRESHOLD', None, float),
116+
'active_threshold': ('ROOM_DEFAULT_ACTIVE_THRESHOLD', None, days_to_seconds),
117+
'active_prune_threshold': ('ROOM_ACTIVE_PRUNE_THRESHOLD', None, days_to_seconds),
115118
},
116-
'direct_messages': {'expiry': ('DM_EXPIRY_DAYS', None, float)},
119+
'direct_messages': {'expiry': ('DM_EXPIRY', None, days_to_seconds)},
117120
'users': {'require_blind_keys': bool_opt('REQUIRE_BLIND_KEYS')},
118121
'messages': {
119-
'history_prune_threshold': ('MESSAGE_HISTORY_PRUNE_THRESHOLD', None, float),
122+
'history_prune_threshold': ('MESSAGE_HISTORY_PRUNE_THRESHOLD', None, days_to_seconds),
120123
'profanity_filter': bool_opt('PROFANITY_FILTER'),
121124
'profanity_silent': bool_opt('PROFANITY_SILENT'),
122125
'profanity_custom': ('PROFANITY_CUSTOM', path_exists, val_or_none),

sogs/migrations/v_0_1_x.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ def ins_user(session_id):
368368
r=room_id,
369369
size=size,
370370
uploaded=timestamp,
371-
expiry=timestamp + 86400 * config.UPLOAD_DEFAULT_EXPIRY_DAYS,
371+
expiry=timestamp + config.UPLOAD_DEFAULT_EXPIRY,
372372
path=path,
373373
dbconn=conn,
374374
)
@@ -488,8 +488,8 @@ def ins_user(session_id):
488488
imported_activity, imported_active = 0, 0
489489

490490
# Don't import rows we're going to immediately prune:
491-
import_cutoff = time.time() - config.ROOM_ACTIVE_PRUNE_THRESHOLD * 86400
492-
active_cutoff = time.time() - config.ROOM_DEFAULT_ACTIVE_THRESHOLD * 86400
491+
import_cutoff = time.time() - config.ROOM_ACTIVE_PRUNE_THRESHOLD
492+
active_cutoff = time.time() - config.ROOM_DEFAULT_ACTIVE_THRESHOLD
493493
n_activity = rconn.execute(
494494
"SELECT COUNT(*) FROM user_activity WHERE last_active > ?", (import_cutoff,)
495495
).fetchone()[0]

sogs/model/file.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,14 @@ def read_base64(self):
124124
def set_expiry(self, duration=None, forever=False):
125125
"""
126126
Updates the file expiry to `duration` seconds from now, or to unlimited if `forever` is
127-
True. If duration is None (and not using forever) then the default expiry will be used.
127+
True. If duration is None (and not using forever) then the default expiry (relative to the
128+
current time) will be used.
128129
"""
129130
expiry = (
130131
None
131132
if forever
132133
else time.time()
133-
+ (duration if duration is not None else config.UPLOAD_DEFAULT_EXPIRY_DAYS)
134+
+ (duration if duration is not None else config.UPLOAD_DEFAULT_EXPIRY)
134135
)
135136
query("UPDATE files SET expiry = :when WHERE id = :f", when=expiry, f=self.id)
136137
self.expiry = expiry
@@ -139,7 +140,7 @@ def set_expiry(self, duration=None, forever=False):
139140
def reset_expiries(file_ids: List[int]):
140141
query(
141142
"UPDATE files SET expiry = uploaded + :exp WHERE id IN :ids",
142-
exp=config.UPLOAD_DEFAULT_EXPIRY_DAYS * 86400.0,
143+
exp=config.UPLOAD_DEFAULT_EXPIRY,
143144
ids=file_ids,
144145
bind_expanding=['ids'],
145146
)

sogs/model/message.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def __init__(self, row=None, *, sender=None, recip=None, data=None):
3636
sender=sender.id,
3737
recipient=recip.id,
3838
data=data,
39-
expiry=time.time() + config.DM_EXPIRY_DAYS * 86400,
39+
expiry=time.time() + config.DM_EXPIRY,
4040
)
4141
# sanity check
4242
assert row is not None

sogs/model/room.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,10 @@ def default_upload(self, upload: bool):
374374
query("UPDATE rooms SET upload = :upload WHERE id = :r", r=self.id, upload=upload)
375375
self._refresh(perms=True)
376376

377-
def active_users(self, cutoff=config.ROOM_DEFAULT_ACTIVE_THRESHOLD * 86400):
377+
def active_users(self, cutoff=config.ROOM_DEFAULT_ACTIVE_THRESHOLD):
378378
"""
379379
Queries the number of active users in the past `cutoff` seconds. Defaults to
380-
config.ROOM_DEFAULT_ACTIVE_THRESHOLD days. Note that room activity records are periodically
380+
config.ROOM_DEFAULT_ACTIVE_THRESHOLD. Note that room activity records are periodically
381381
removed, so going beyond config.ROOM_ACTIVE_PRUNE_THRESHOLD days is useless.
382382
"""
383383

@@ -669,7 +669,7 @@ def _own_files(self, msg_id: int, files: List[int], user):
669669
AND expiry IS NOT NULL
670670
""",
671671
m=msg_id,
672-
exp=time.time() + config.UPLOAD_DEFAULT_EXPIRY_DAYS * 86400.0,
672+
exp=time.time() + config.UPLOAD_DEFAULT_EXPIRY,
673673
ids=files,
674674
r=self.id,
675675
u=user.id,
@@ -1276,7 +1276,7 @@ def upload_file(
12761276
uploader: User,
12771277
*,
12781278
filename: Optional[str] = None,
1279-
lifetime: Optional[float] = config.UPLOAD_DEFAULT_EXPIRY_DAYS * 86400.0,
1279+
lifetime: Optional[float] = config.UPLOAD_DEFAULT_EXPIRY,
12801280
):
12811281
"""
12821282
Uploads a file to this room. The uploader must have write and upload permissions.

sogs/routes/legacy.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,7 @@ def handle_one_compact_poll(req):
260260
}
261261

262262

263-
def process_legacy_file_upload_for_room(
264-
user, room, lifetime=config.UPLOAD_DEFAULT_EXPIRY_DAYS * 86400
265-
):
263+
def process_legacy_file_upload_for_room(user, room, lifetime=config.UPLOAD_DEFAULT_EXPIRY):
266264
"""
267265
Uploads a file, posted by user, into the given room. `lifetime` controls how long (in seconds)
268266
the file will be stored before expiry, and can be None for uploads (such as room images) that

sogs/routes/rooms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def get_room_info(room):
2424
'message_sequence': room.message_sequence,
2525
'created': room.created,
2626
'active_users': room.active_users(),
27-
'active_users_cutoff': int(config.ROOM_DEFAULT_ACTIVE_THRESHOLD * 86400),
27+
'active_users_cutoff': int(config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
2828
'moderators': mods,
2929
'admins': admins,
3030
'read': room.check_read(g.user),

tests/test_dm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,20 +72,20 @@ def test_dm_send(client, blind_user, blind_user2):
7272
assert r.status_code == 201
7373
data = r.json
7474
assert -1 < data.pop('posted_at') - time.time() < 1
75-
assert -1 < data.pop('expires_at') - config.DM_EXPIRY_DAYS * 86400 - time.time() < 1
75+
assert -1 < data.pop('expires_at') - config.DM_EXPIRY - time.time() < 1
7676
assert data == {k: v for k, v in msg_expected.items() if k != 'message'}
7777

7878
r = sogs_get(client, '/inbox', blind_user2)
7979
assert r.status_code == 200
8080
assert len(r.json) == 1
8181
data = r.json[0]
8282
assert -1 < data.pop('posted_at') - time.time() < 1
83-
assert -1 < data.pop('expires_at') - config.DM_EXPIRY_DAYS * 86400 - time.time() < 1
83+
assert -1 < data.pop('expires_at') - config.DM_EXPIRY - time.time() < 1
8484
assert data == msg_expected
8585

8686
r = sogs_get(client, '/outbox', blind_user)
8787
assert len(r.json) == 1
8888
data = r.json[0]
8989
assert -1 < data.pop('posted_at') - time.time() < 1
90-
assert -1 < data.pop('expires_at') - config.DM_EXPIRY_DAYS * 86400 - time.time() < 1
90+
assert -1 < data.pop('expires_at') - config.DM_EXPIRY - time.time() < 1
9191
assert data == msg_expected

tests/test_room_routes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def test_list(client, room, room2, user, user2, admin, mod, global_mod, global_a
3939
"message_sequence": 0,
4040
"created": room2.created,
4141
"active_users": 0,
42-
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
42+
"active_users_cutoff": int(sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
4343
"moderators": [],
4444
"admins": [],
4545
"read": True,
@@ -60,7 +60,7 @@ def test_list(client, room, room2, user, user2, admin, mod, global_mod, global_a
6060
"message_sequence": 0,
6161
"created": room.created,
6262
"active_users": 0,
63-
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
63+
"active_users_cutoff": int(sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
6464
"moderators": [mod.session_id],
6565
"admins": [admin.session_id],
6666
"read": True,
@@ -76,7 +76,7 @@ def test_list(client, room, room2, user, user2, admin, mod, global_mod, global_a
7676
"message_sequence": 0,
7777
"created": room3.created,
7878
"active_users": 0,
79-
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
79+
"active_users_cutoff": int(sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
8080
"moderators": [],
8181
"admins": [],
8282
"read": False,
@@ -98,7 +98,7 @@ def test_list(client, room, room2, user, user2, admin, mod, global_mod, global_a
9898
"message_sequence": 0,
9999
"created": room4.created,
100100
"active_users": 0,
101-
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
101+
"active_users_cutoff": int(sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
102102
"moderators": [],
103103
"admins": [],
104104
"read": False,
@@ -201,7 +201,7 @@ def test_updates(client, room, user, user2, mod, admin, global_mod, global_admin
201201
"message_sequence": 0,
202202
"created": room.created,
203203
"active_users": 0,
204-
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
204+
"active_users_cutoff": int(sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
205205
"moderators": [mod.session_id],
206206
"admins": [admin.session_id],
207207
"read": True,
@@ -387,7 +387,7 @@ def test_polling(client, room, user, user2, mod, admin, global_mod, global_admin
387387
"message_sequence": 0,
388388
"created": room.created,
389389
"active_users": 1,
390-
"active_users_cutoff": int(86400 * sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
390+
"active_users_cutoff": int(sogs.config.ROOM_DEFAULT_ACTIVE_THRESHOLD),
391391
"moderators": [mod.session_id],
392392
"admins": [admin.session_id],
393393
"read": True,

0 commit comments

Comments
 (0)