Skip to content

Commit 37f8bac

Browse files
committed
Allow to unsubscribe from 1 list instead of all lists per default
1 parent 45fef86 commit 37f8bac

File tree

5 files changed

+70
-18
lines changed

5 files changed

+70
-18
lines changed

assets/js/views/login.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ async function doLogin() {
135135
console.log(resp);
136136

137137
if (!resp.success) {
138-
alert("Error");
138+
alert("Error [" + resp.error + "]");
139139
return;
140140
}
141141

nimletter.nimble

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Package
22

3-
version = "0.6.0"
3+
version = "0.6.1"
44
author = "ThomasTJdev"
55
description = "Newsletter"
66
license = "AGPL v3"

src/email/email_channel.nim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ proc sendPendingEmail(pendingEmail: PendingMailObj) =
9696
]
9797
), pendingEmail.mailID)
9898

99-
echo "Email being sent to " & userData[1]
99+
#echo "Email being sent to " & userData[1]
100100
# Send the email
101101
let sendData = sendMailMimeNow(
102102
contactID = pendingEmail.userID,

src/html/optin.nimf

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
#
100100
#
101101
#
102-
#proc nimfOptinUnubscribe(userUUID, name, email: string): string =
102+
#proc nimfOptinUnubscribe(userUUID, name, email: string, unsubscribeFromAll: bool = false): string =
103103
<html>
104104
<head>
105105
<title>Nimletter</title>
@@ -118,7 +118,11 @@
118118
<div class="mb30" style="display: flex;">
119119
<div>
120120
<p>Sorry to see you go, <b>${name}</b>!</p>
121-
<p>We have unsubscribed you from our list.</p>
121+
# if unsubscribeFromAll:
122+
<p>We have unsubscribed you from all our lists.</p>
123+
# else:
124+
<p>We have unsubscribed you from this list.</p>
125+
# end if
122126
</div>
123127
</div>
124128
# if userUUID != "":
@@ -134,6 +138,11 @@
134138
</div>
135139
</div>
136140
</div>
141+
# if not unsubscribeFromAll:
142+
<div>
143+
<p>Unsubscribe from all lists and all emails by clicking <a href="/unsubscribe?contactUUID=${userUUID}&unsubscribeFromAll=true">here</a>.</p>
144+
</div>
145+
# end if
137146
# end if
138147
</div>
139148
</main>

src/routes/routes_optin.nim

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,40 +196,83 @@ proc(request: Request) =
196196
if not userUUID.isValidUUID():
197197
resp Http200, nimfOptinUnubscribe("", "", "")
198198

199+
let unsubscribeFromAll = @"unsubscribeFromAll" == "true"
200+
201+
202+
# For streamlining the code
203+
var listID: string
204+
var listIdentifier: string
205+
var listUUID: string
206+
207+
# Used in callback event
208+
var listUUIDs: seq[string]
209+
var listIdentifiers: seq[string]
210+
211+
var onlyOneList: bool = false
199212
var userData: seq[string]
213+
200214
pg.withConnection conn:
201-
exec(conn, sqlUpdate(
202-
table = "contacts",
203-
data = [
204-
"double_opt_in",
205-
"double_opt_in_data",
206-
],
207-
where = ["uuid = ?"]
208-
), "false", "", userUUID)
209215

216+
# First get user info
210217
userData = getRow(conn, sqlSelect(table = "contacts", select = ["id", "name", "email"], where = ["uuid = ?"]), userUUID)
211218

219+
if not unsubscribeFromAll:
220+
# Now get listID from latest email from pending_emails
221+
let lastEmailListID = getValue(conn, sqlSelect(table = "pending_emails", select = ["list_id"], where = ["user_id = ?"], customSQL = "ORDER BY created_at DESC LIMIT 1"), userData[0])
222+
if lastEmailListID != "":
223+
listID = lastEmailListID
224+
let listData = getRow(conn, sqlSelect(table = "lists", select = ["id", "identifier", "uuid"], where = ["id = ?"]), listID)
225+
listUUID = listData[2]
226+
listIdentifier = listData[1]
227+
228+
else:
229+
exec(conn, sqlUpdate(
230+
table = "contacts",
231+
data = [
232+
"double_opt_in",
233+
"double_opt_in_data",
234+
],
235+
where = ["uuid = ?"]
236+
), "false", "", userUUID)
237+
238+
212239
#
213240
# Remove from lists
214241
#
215-
let listIds = getAllRows(conn, sqlSelect(table = "subscriptions", select = ["list_id"], where = ["user_id = ?"]), userData[0])
216-
for listId in listIds:
217-
discard contactRemoveFromList(userData[0], listId[0], "list")
242+
if not unsubscribeFromAll:
243+
listUUIDs.add(listUUID)
244+
listIdentifiers.add(listIdentifier)
245+
discard contactRemoveFromList(userData[0], listID, "list")
246+
else:
247+
let listIds = getAllRows(conn, sqlSelect(table = "subscriptions", select = ["list_id"], where = ["user_id = ?"]), userData[0])
248+
var tmpListIDs: seq[string]
249+
for listId in listIds:
250+
tmpListIDs.add(listId[0])
251+
discard contactRemoveFromList(userData[0], listId[0], "list")
252+
253+
# Get data to event hook
254+
if tmpListIDs.len() > 0:
255+
let listData = getAllRows(conn, sqlSelect(table = "lists", select = ["uuid", "identifier"], where = ["id = ANY(?::int[])"]), "{" & tmpListIDs.join(",") & "}")
256+
for listId in listData:
257+
listUUIDs.add(listId[0])
258+
listIdentifiers.add(listId[1])
218259

219260
if userData[0] == "":
220-
resp Http200, nimfOptinUnubscribe("", "", "")
261+
resp Http200, nimfOptinUnubscribe("", "", "", unsubscribeFromAll)
221262

222263
let data = %* {
223264
"success": true,
224265
"id": userData[0],
225266
"name": userData[1],
226267
"email": userData[2],
268+
"listUUIDs": listUUIDs,
269+
"listIdentifiers": listIdentifiers,
227270
"event": "contact_optedin"
228271
}
229272

230273
parseWebhookEvent(contact_optedin, data)
231274

232-
resp Http200, nimfOptinUnubscribe(userUUID, userData[1], userData[2])
275+
resp Http200, nimfOptinUnubscribe(userUUID, userData[1], userData[2], unsubscribeFromAll)
233276
)
234277

235278

0 commit comments

Comments
 (0)