Coverage for src/couchers/notifications/settings.py: 93%

55 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-04-16 15:13 +0000

1import logging 

2 

3from couchers.db import session_scope 

4from couchers.models import ( 

5 NotificationDelivery, 

6 NotificationDeliveryType, 

7 NotificationPreference, 

8 NotificationTopicAction, 

9) 

10from couchers.notifications.utils import enum_from_topic_action 

11from couchers.sql import couchers_select as select 

12from proto import notifications_pb2 

13 

14logger = logging.getLogger(__name__) 

15 

16 

17def get_preference(session, user_id: int, topic_action: NotificationTopicAction) -> list[NotificationDeliveryType]: 

18 """ 

19 Gets the user's preference from the DB or otherwise falls back to defaults 

20 

21 Must be done in session scope 

22 

23 Returns list of delivery types 

24 """ 

25 overrides = { 

26 res.delivery_type: res.deliver 

27 for res in session.execute( 

28 select(NotificationPreference) 

29 .where(NotificationPreference.user_id == user_id) 

30 .where(NotificationPreference.topic_action == topic_action) 

31 ) 

32 .scalars() 

33 .all() 

34 } 

35 return [dt for dt in NotificationDeliveryType if overrides.get(dt, dt in topic_action.defaults)] 

36 

37 

38def reset_preference(session, user_id, topic_action, delivery_type): 

39 current_pref = session.execute( 

40 select(NotificationPreference) 

41 .where(NotificationPreference.user_id == user_id) 

42 .where(NotificationPreference.topic_action == topic_action) 

43 .where(NotificationDelivery.delivery_type == delivery_type) 

44 ).scalar_one_or_none() 

45 if current_pref: 

46 session.delete(current_pref) 

47 session.flush() 

48 

49 

50class PreferenceNotUserEditableError(Exception): 

51 pass 

52 

53 

54def set_preference(session, user_id, topic_action: NotificationTopicAction, delivery_type, deliver): 

55 if not topic_action.user_editable: 

56 raise PreferenceNotUserEditableError() 

57 current_pref = session.execute( 

58 select(NotificationPreference) 

59 .where(NotificationPreference.user_id == user_id) 

60 .where(NotificationPreference.topic_action == topic_action) 

61 .where(NotificationPreference.delivery_type == delivery_type) 

62 ).scalar_one_or_none() 

63 if current_pref: 

64 current_pref.deliver = deliver 

65 else: 

66 session.add( 

67 NotificationPreference( 

68 user_id=user_id, 

69 topic_action=topic_action, 

70 delivery_type=delivery_type, 

71 deliver=deliver, 

72 ) 

73 ) 

74 session.flush() 

75 

76 

77settings_layout = [ 

78 ( 

79 "Core Features", 

80 [ 

81 ( 

82 "host_request", 

83 "Host requests", 

84 [ 

85 ("create", "Someone sends you a host request"), 

86 ("accept", "Someone accepts your host request"), 

87 ("confirm", "Someone confirms their host request"), 

88 ("reject", "Someone declines your host request"), 

89 ("cancel", "Someone cancels their host request"), 

90 ("message", "Someone sends a message in a host request"), 

91 ("missed_messages", "You miss messages in a host request"), 

92 ], 

93 ), 

94 ( 

95 "activeness", 

96 "Activity Check-in", 

97 [ 

98 ("probe", "Check in to see if you are still hosting after a long period of inactivity"), 

99 ], 

100 ), 

101 ( 

102 "chat", 

103 "Messaging", 

104 [ 

105 ("message", "Someone sends you a message"), 

106 ("missed_messages", "You miss messages in a chat"), 

107 ], 

108 ), 

109 ( 

110 "reference", 

111 "References", 

112 [ 

113 ("receive_hosted", "You receive a reference from someone who hosted you"), 

114 ("receive_surfed", "You receive a reference from someone you hosted"), 

115 ("receive_friend", "You received a reference from a friend"), 

116 ("reminder_hosted", "Reminder to write a reference to someone you hosted"), 

117 ("reminder_surfed", "Reminder to write a reference to someone you surfed with"), 

118 ], 

119 ), 

120 ], 

121 ), 

122 ( 

123 "Community Features", 

124 [ 

125 ( 

126 "friend_request", 

127 "Friend requests", 

128 [ 

129 ("create", "Someone sends you a friend request"), 

130 ("accept", "Someone accepts your friend request"), 

131 ], 

132 ), 

133 ( 

134 "event", 

135 "Events", 

136 [ 

137 ("create_approved", "An event that is approved by the moderators is created in your community"), 

138 ("create_any", "A user creates any event in your community (not checked by an admin)"), 

139 ("update", "An event you are attending is updated"), 

140 ("comment", "Someone comments on an event you are organizing or attending"), 

141 ("cancel", "An event you are attending is cancelled"), 

142 ("delete", "An event you are attending is deleted"), 

143 ("invite_organizer", "Someone invites you to co-organize an event"), 

144 ], 

145 ), 

146 ( 

147 "discussion", 

148 "Community discussions", 

149 [ 

150 ("create", "Someone creates a discussion in one of your communities"), 

151 ("comment", "Someone comments on a discussion you authored"), 

152 ], 

153 ), 

154 ( 

155 "thread", 

156 "Threads, Comments, & Replies", 

157 [ 

158 ("reply", "Someone replies to your comment"), 

159 ], 

160 ), 

161 ], 

162 ), 

163 ( 

164 "Account Settings", 

165 [ 

166 ( 

167 "onboarding", 

168 "Onboarding", 

169 [ 

170 ("reminder", "Reminder to complete your profile after signing up"), 

171 ], 

172 ), 

173 ( 

174 "badge", 

175 "Updates to Badges on your profile", 

176 [ 

177 ("add", "A badge is added to your account"), 

178 ("remove", "A badge is removed from your account"), 

179 ], 

180 ), 

181 ( 

182 "donation", 

183 "Donations", 

184 [ 

185 ("received", "Your donation is received"), 

186 ], 

187 ), 

188 ], 

189 ), 

190 ( 

191 "Account Security", 

192 [ 

193 ( 

194 "password", 

195 "Password change", 

196 [ 

197 ("change", "Your password is changed"), 

198 ], 

199 ), 

200 ( 

201 "password_reset", 

202 "Password reset", 

203 [ 

204 ("start", "Password reset is initiated"), 

205 ("complete", "Password reset is completed"), 

206 ], 

207 ), 

208 ( 

209 "email_address", 

210 "Email address change", 

211 [ 

212 ("change", "Email change is initiated"), 

213 ("verify", "Your new email is verified"), 

214 ], 

215 ), 

216 ( 

217 "account_deletion", 

218 "Account deletion", 

219 [ 

220 ("start", "You initiate account deletion"), 

221 ("complete", "Your account is deleted"), 

222 ("recovered", "Your account is recovered (undeleted)"), 

223 ], 

224 ), 

225 ( 

226 "api_key", 

227 "API keys", 

228 [ 

229 ("create", "An API key is created for your account"), 

230 ], 

231 ), 

232 ( 

233 "phone_number", 

234 "Phone number change", 

235 [ 

236 ("change", "Your phone number is changed"), 

237 ("verify", "Your phone number is verified"), 

238 ], 

239 ), 

240 ( 

241 "birthdate", 

242 "Birthdate change", 

243 [ 

244 ("change", "Your birthdate is changed"), 

245 ], 

246 ), 

247 ( 

248 "gender", 

249 "Displayed gender change", 

250 [ 

251 ("change", "The gender displayed on your profile is changed"), 

252 ], 

253 ), 

254 ( 

255 "modnote", 

256 "Moderator notes", 

257 [ 

258 ("create", "You receive a moderator note"), 

259 ], 

260 ), 

261 ( 

262 "verification", 

263 "Verification", 

264 [ 

265 ("sv_fail", "Strong Verification fails"), 

266 ("sv_success", "Strong Verification succeeds"), 

267 ], 

268 ), 

269 ], 

270 ), 

271] 

272 

273 

274def check_settings(): 

275 # check settings contain all actions+topics 

276 actions_by_topic = {} 

277 for t in NotificationTopicAction: 

278 actions_by_topic[t.topic] = actions_by_topic.get(t.topic, []) + [t.action] 

279 

280 actions_by_topic_check = {} 

281 

282 for heading, group in settings_layout: 

283 for topic, name, items in group: 

284 actions = [] 

285 for action, description in items: 

286 actions.append(action) 

287 actions_by_topic_check[topic] = actions 

288 

289 for topic, actions in actions_by_topic.items(): 

290 assert sorted(actions) == sorted(actions_by_topic_check[topic]), ( 

291 f"Expected {actions} == {actions_by_topic_check[topic]} for {topic}" 

292 ) 

293 assert sorted(actions_by_topic.keys()) == sorted(actions_by_topic_check.keys()) 

294 

295 

296check_settings() 

297 

298 

299def get_user_setting_groups(user_id) -> list[notifications_pb2.NotificationGroup]: 

300 with session_scope() as session: 

301 groups = [] 

302 for heading, group in settings_layout: 

303 topics = [] 

304 for topic, name, items in group: 

305 actions = [] 

306 for action, description in items: 

307 topic_action = enum_from_topic_action[topic, action] 

308 delivery_types = get_preference(session, user_id, topic_action) 

309 actions.append( 

310 notifications_pb2.NotificationItem( 

311 action=action, 

312 description=description, 

313 user_editable=topic_action.user_editable, 

314 push=NotificationDeliveryType.push in delivery_types, 

315 email=NotificationDeliveryType.email in delivery_types, 

316 digest=NotificationDeliveryType.digest in delivery_types, 

317 ) 

318 ) 

319 topics.append( 

320 notifications_pb2.NotificationTopic( 

321 topic=topic, 

322 name=name, 

323 items=actions, 

324 ) 

325 ) 

326 groups.append( 

327 notifications_pb2.NotificationGroup( 

328 heading=heading, 

329 topics=topics, 

330 ) 

331 ) 

332 return groups