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

55 statements  

« prev     ^ index     » next       coverage.py v7.6.10, created at 2025-03-11 15:27 +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 "chat", 

96 "Messaging", 

97 [ 

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

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

100 ], 

101 ), 

102 ( 

103 "reference", 

104 "References", 

105 [ 

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

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

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

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

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

111 ], 

112 ), 

113 ], 

114 ), 

115 ( 

116 "Community Features", 

117 [ 

118 ( 

119 "friend_request", 

120 "Friend requests", 

121 [ 

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

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

124 ], 

125 ), 

126 ( 

127 "event", 

128 "Events", 

129 [ 

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

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

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

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

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

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

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

137 ], 

138 ), 

139 ( 

140 "discussion", 

141 "Community discussions", 

142 [ 

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

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

145 ], 

146 ), 

147 ( 

148 "thread", 

149 "Threads, Comments, & Replies", 

150 [ 

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

152 ], 

153 ), 

154 ], 

155 ), 

156 ( 

157 "Account Settings", 

158 [ 

159 ( 

160 "onboarding", 

161 "Onboarding", 

162 [ 

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

164 ], 

165 ), 

166 ( 

167 "badge", 

168 "Updates to Badges on your profile", 

169 [ 

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

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

172 ], 

173 ), 

174 ( 

175 "donation", 

176 "Donations", 

177 [ 

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

179 ], 

180 ), 

181 ], 

182 ), 

183 ( 

184 "Account Security", 

185 [ 

186 ( 

187 "password", 

188 "Password change", 

189 [ 

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

191 ], 

192 ), 

193 ( 

194 "password_reset", 

195 "Password reset", 

196 [ 

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

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

199 ], 

200 ), 

201 ( 

202 "email_address", 

203 "Email address change", 

204 [ 

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

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

207 ], 

208 ), 

209 ( 

210 "account_deletion", 

211 "Account deletion", 

212 [ 

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

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

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

216 ], 

217 ), 

218 ( 

219 "api_key", 

220 "API keys", 

221 [ 

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

223 ], 

224 ), 

225 ( 

226 "phone_number", 

227 "Phone number change", 

228 [ 

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

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

231 ], 

232 ), 

233 ( 

234 "birthdate", 

235 "Birthdate change", 

236 [ 

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

238 ], 

239 ), 

240 ( 

241 "gender", 

242 "Displayed gender change", 

243 [ 

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

245 ], 

246 ), 

247 ( 

248 "modnote", 

249 "Moderator notes", 

250 [ 

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

252 ], 

253 ), 

254 ( 

255 "verification", 

256 "Verification", 

257 [ 

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

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

260 ], 

261 ), 

262 ], 

263 ), 

264] 

265 

266 

267def check_settings(): 

268 # check settings contain all actions+topics 

269 actions_by_topic = {} 

270 for t in NotificationTopicAction: 

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

272 

273 actions_by_topic_check = {} 

274 

275 for heading, group in settings_layout: 

276 for topic, name, items in group: 

277 actions = [] 

278 for action, description in items: 

279 actions.append(action) 

280 actions_by_topic_check[topic] = actions 

281 

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

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

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

285 ) 

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

287 

288 

289check_settings() 

290 

291 

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

293 with session_scope() as session: 

294 groups = [] 

295 for heading, group in settings_layout: 

296 topics = [] 

297 for topic, name, items in group: 

298 actions = [] 

299 for action, description in items: 

300 topic_action = enum_from_topic_action[topic, action] 

301 delivery_types = get_preference(session, user_id, topic_action) 

302 actions.append( 

303 notifications_pb2.NotificationItem( 

304 action=action, 

305 description=description, 

306 user_editable=topic_action.user_editable, 

307 push=NotificationDeliveryType.push in delivery_types, 

308 email=NotificationDeliveryType.email in delivery_types, 

309 digest=NotificationDeliveryType.digest in delivery_types, 

310 ) 

311 ) 

312 topics.append( 

313 notifications_pb2.NotificationTopic( 

314 topic=topic, 

315 name=name, 

316 items=actions, 

317 ) 

318 ) 

319 groups.append( 

320 notifications_pb2.NotificationGroup( 

321 heading=heading, 

322 topics=topics, 

323 ) 

324 ) 

325 return groups