forked from 500px/500pxPublisher.lrplugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path500pxUser.lua
executable file
·825 lines (727 loc) · 27.3 KB
/
500pxUser.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
-- Lightroom SDK
local LrBinding = import "LrBinding"
local LrColor = import "LrColor"
local LrDate = import "LrDate"
local LrDialogs = import "LrDialogs"
local LrFunctionContext = import "LrFunctionContext"
local LrHttp = import "LrHttp"
local LrTasks = import "LrTasks"
local LrView = import "LrView"
local LrErrors = import "LrErrors"
-- Common shortcuts
local bind = LrView.bind
-- logging
local logger = import "LrLogger"("500pxPublisher")
logger:enable("logfile")
-- 500px Plugin
require "500pxAPI"
PxUser = {}
local function booleanToNumber( value )
if not value then
return 0
else
return 1
end
end
local function storedCredentialsAreValid( propertyTable )
-- might want to check the length of oauth token and secret
return propertyTable.username and string.len( propertyTable.username ) > 0
and propertyTable.credentials and propertyTable.credentials.oauth_token
and propertyTable.credentials.oauth_token_secret
end
local function notLoggedIn( propertyTable )
propertyTable.credentials = nil
propertyTable.userId = nil
propertyTable.username = nil
propertyTable.validAccount = false
propertyTable.collections = {}
propertyTable.accountStatus = "Not logged in"
propertyTable.loginButtonTitle = "Login"
propertyTable.loginButtonEnabled = true
end
local doingLogin = false
local function getCollectionsAndPhotos( publishService )
local publishedCollections = publishService:getChildCollections()
local photos = {}
local collectionsById = {}
local collectionsByName = {}
for _, publishedCollection in ipairs( publishedCollections ) do
local publishedPhotos = publishedCollection:getPublishedPhotos()
local collection = {}
for _, publishedPhoto in ipairs( publishedPhotos ) do
local photo = publishedPhoto:getPhoto()
local photoId = photo:getPropertyForPlugin( _PLUGIN, "photoId" )
if photoId then
-- logger:trace( "Photo id in Published photos " .. photoId )
photos[ photoId ] = { photo = photo, edited = publishedPhoto:getEditedFlag() }
collection[ photoId ] = photo
end
end
local collectionId = publishedCollection:getRemoteId()
if collectionId then
collectionsById[ tostring( collectionId ) ] = { collection = publishedCollection, photos = collection }
else
collectionsByName[ publishedCollection:getName() ] = { collection = publishedCollection, photos = collection }
end
end
return collectionsById, collectionsByName, photos
end
function PxUser.sync( propertyTable )
local publishService = propertyTable.LR_publishService
local e = false
local function doSync( context, progressScope )
LrDialogs.attachErrorDialogToFunctionContext( context )
logger:trace( "sync all" )
local LrPathUtils = import "LrPathUtils"
local LrFileUtils = import "LrFileUtils"
local collectionsById, collectionsByName, publishedPhotos = getCollectionsAndPhotos( publishService )
local profileCollection = collectionsByName[ "Profile" ]
if profileCollection then
if profileCollection.collection:getCollectionInfoSummary().isDefaultCollection then
profileCollection.collection:setName( "Library" )
collectionsByName[ "Library" ] = collectionsByName[ "Profile" ]
collectionsByName[ "Public Profile" ] = nil
end
end
local collections = {}
local nInCollections = 0
local success, obj = PxAPI.getCollections( propertyTable )
if not success then
e = true
obj = { collections = {} }
end
for _, collectionObj in ipairs( obj.collections ) do
collections[ tostring( collectionObj.id ) ] = collectionObj
for _, __ in ipairs( collectionObj.photos ) do
nInCollections = nInCollections + 1
end
nInCollections = nInCollections + 1
end
-- Sync the photos
local nPages = 0
local page = 0
local args = {
feature = "user_library",
username = propertyTable.username,
sort = "created_at",
rpp = 1,
image_size=0,
}
local dir = LrPathUtils.child(LrPathUtils.child( LrPathUtils.getStandardFilePath( "pictures" ), "500px" ), propertyTable.username )
LrFileUtils.createAllDirectories( dir )
-- collect photos for "Profile" and "Library" collections
local allPhotos = {}
local profilePhotos = {}
local nPhotos
local i = 0
args.page = 1
local success, obj = PxAPI.getPhotos( propertyTable.credentials, args)
if not success then
e = true
obj = { total_items = 0 }
end
nPhotos = obj.total_items
while true and nPhotos ~= 0 do
if progressScope:isCanceled() then
progressScope:setCaption("Cancelling...")
break
end
args.page = nPhotos - page
if args.page == 0 then args.page = 1 end
local success, obj = PxAPI.getPhotos( propertyTable.credentials, args )
if not success then
e = true
break
end
nPhotos = obj.total_items
npages = obj.total_pages
for _, photoObj in ipairs( obj.photos ) do
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.sync", function()
local photoInfo = publishedPhotos[ tostring( photoObj.id ) ]
if not photoInfo then
--import the photo
local filename = string.format( "%i-%i", photoObj.id, math.floor(LrDate.currentTime() + 0.5) )
local file = LrFileUtils.chooseUniqueFileName( LrPathUtils.child( dir, LrPathUtils.addExtension( filename, "jpg" ) ) )
local response, headers = LrHttp.get( photoObj.image_url, { } )
if headers and headers.status == 200 and response then
logger:trace( "Got photo from the server")
local fh = io.open( file, "wb+" )
fh:write( response )
fh:flush()
fh:close()
local photo = publishService.catalog:addPhoto( file )
photo:setPropertyForPlugin( _PLUGIN, "photoId", tostring( photoObj.id ) )
photoInfo = { photo = photo, edited = false }
publishedPhotos[ tostring( photoObj.id ) ] = photoInfo
else
logger:trace( "No photo from the server")
e = true
end
else
photoInfo.published = true
end
--update title, description, category
if photoInfo then
local photo = photoInfo.photo
if not photoInfo.edited then
photo:setRawMetadata( "title", photoObj.name or "" )
photo:setRawMetadata( "caption", photoObj.description or "" )
if photoObj.category and photoObj.category > 0 then
photo:setPropertyForPlugin( _PLUGIN, "category", photoObj.category )
end
photo:setPropertyForPlugin( _PLUGIN, "nsfw", booleanToNumber(photoObj.nsfw) )
end
photo:setPropertyForPlugin( _PLUGIN, "publishedUUID", photo:getRawMetadata( "uuid" ) )
photo:setPropertyForPlugin( _PLUGIN, "views", tostring( photoObj.times_viewed ) or "0" )
photo:setPropertyForPlugin( _PLUGIN, "favorites", tostring( photoObj.favorites_count ) or "0" )
photo:setPropertyForPlugin( _PLUGIN, "votes", tostring( photoObj.votes_count ) or "0" )
photo:setPropertyForPlugin( _PLUGIN, "license_type", photoObj.license_type)
--add to "Library" collection
table.insert( allPhotos, photoObj )
if not photoObj.privacy then
table.insert( profilePhotos, photoObj )
end
end
end)
if PluginInit then PluginInit.unlock() end
LrTasks.yield()
end
progressScope:setPortionComplete( i / ( nInCollections + nPhotos ) )
i = i + 1
--if obj.total_pages <= obj.current_page then break end
page = page + 1
if obj.total_pages <= page or obj.total_pages == 0 then break end
end
-- ...add in the "Profile" and "Library" collection
collections[ "profile" ] = { title = "Public Profile", id = nil, photos = profilePhotos }
collections[ "nil" ] = { title = "Library", id = nil, photos = allPhotos }
-- delete all published collections that no longer exist
for id, collectionInfo in pairs( collectionsById ) do
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.sync", function()
if not collections[ id ] then
collectionInfo.collection:delete()
end
end )
if PluginInit then PluginInit.unlock() end
end
for id, collectionObj in pairs( collections ) do
local collection
local photos
local collectionSettings
local new = false
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.sync", function()
if collectionsById[ id ] then
-- collection has been published before, update it's name
local collectionInfo = collectionsById[ id ]
collection = collectionInfo.collection
photos = collectionInfo.photos
collection:setName( collectionObj.title )
collectionSettings = collection:getCollectionInfoSummary().collectionSettings or {}
elseif collectionsByName[ collectionObj.title ] then
-- collection has not been published yet, set it's remote id
local collectionInfo = collectionsByName[ collectionObj.title ]
collection = collectionInfo.collection
photos = collectionInfo.photos
collectionSettings = collection:getCollectionInfoSummary().collectionSettings or {}
else
-- collection doesn't exist, create it
collection = publishService:createPublishedCollection( collectionObj.title, nil, true )
photos = {}
collection:setRemoteId( id )
collectionSettings = { toCommunity = propertyTable.toCommunity }
new = true
end
-- always update remote url and collection settings
local collectionUrl
if collectionObj.title == "Public Profile" then
collectionUrl = "http://500px.com/" .. propertyTable.username
elseif collectionObj.title == "Library" or collectionObj.title == "Organizer" then
collectionUrl = "http://500px.com/organizer"
collectionSettings.toCommunity = false
else
if collectionObj.kind == "profile" then
collectionUrl = PxAPI.makeCollectionUrl( propertyTable.username, collectionObj.path )
else
collectionUrl = "http://".. propertyTable.username ..".500px.com/".. collectionObj.path
end
--update collection settings (ie: path)
collectionSettings.path = collectionObj.path
end
collection:setCollectionSettings( collectionSettings )
collection:setRemoteUrl( collectionUrl )
end )
if PluginInit then PluginInit.unlock() end
LrTasks.yield()
-- add photos to the collection
for _, photoObj in ipairs( collectionObj.photos ) do
local photoInfo = publishedPhotos[ tostring( photoObj.id ) ]
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.sync", function()
if photoInfo then
photos[ photoInfo.photo ] = true
collection:addPhotoByRemoteId( photoInfo.photo, string.format("%i-%s", photoObj.id, tostring( collectionObj.id ) ), string.format( "http://500px.com/photo/%i", photoObj.id ), not photoInfo.edited )
progressScope:setPortionComplete( i / ( nInCollections + nPhotos ) )
end
end)
if PluginInit then PluginInit.unlock() end
i = i + 1
LrTasks.yield()
end
-- delete photos that were deleted from the web
local photosToRemove = {}
if not new then
if not progressScope:isCanceled() then
for _, publishedPhoto in ipairs( collection:getPublishedPhotos() ) do
if not photos[ publishedPhoto:getPhoto() ] then
table.insert( photosToRemove, publishedPhoto:getPhoto() )
end
end
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.sync", function()
collection:removePhotos( photosToRemove )
end )
if PluginInit then PluginInit.unlock() end
end
end
progressScope:setPortionComplete( i / ( nInCollections + nPhotos ) )
i = i + 1
LrTasks.yield()
end
end
LrFunctionContext.postAsyncTaskWithContext( "sync", function( context )
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.defaults", function()
local profileCollection = publishService:createPublishedCollection( "Public Profile", nil, true )
profileCollection:setCollectionSettings( { toCommunity = true } )
profileCollection:setRemoteUrl( "http://500px.com/" .. propertyTable.username )
for _, collection in ipairs( publishService:getChildCollections() ) do
if collection:getName() == "Profile" or collection:getName() == "Library" or collection:getName() == "Organizer" then
collection:setName( "Library" )
profileCollection:setCollectionSettings( { toCommunity = false } )
end
end
end )
if PluginInit then PluginInit.unlock() end
local progressScope = LrDialogs.showModalProgressDialog( {
title = "Sync with 500px",
caption = "Fetching photos and collections from 500px. This could take a while.",
cannotCancel = false,
functionContext = context,
} )
doSync( context, progressScope )
end )
if e then
LrErrors.throwUserError( "Something went wrong while syncing your photos. Some of your photos may not have been imported. Please try again later." )
end
end
function PxUser.syncCollections( propertyTable )
local publishService = propertyTable.LR_publishService
local function doSync( context, progressScope )
LrDialogs.attachErrorDialogToFunctionContext( context )
logger:trace( "sync collections" )
local collectionsById, collectionsByName, publishedPhotos = getCollectionsAndPhotos( publishService )
-- rename the profile collection...
local profileCollection = collectionsByName[ "Profile" ]
if profileCollection then
if profileCollection.collection:getCollectionInfoSummary().isDefaultCollection then
profileCollection.collection:setName( "Library" )
collectionsByName[ "Library" ] = collectionsByName[ "Profile" ]
collectionsByName[ "Public Profile" ] = nil
end
end
local collections = {}
local success, obj = PxAPI.getCollections( propertyTable )
local n = 0
for _, collectionObj in ipairs( obj.collections ) do
collections[ tostring( collectionObj.id ) ] = collectionObj
n = n + 1
end
-- ...add in the "Profile" and "Library" collection
collections[ "profile" ] = { title = "Public Profile", id = nil, photos = profilePhotos }
collections[ "nil" ] = { title = "Library", id = nil, photos = allPhotos }
-- delete all published collections that no longer exist
for id, collectionInfo in pairs( collectionsById ) do
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.sync", function()
if not collections[ id ] then
collectionInfo.collection:delete()
end
end )
if PluginInit then PluginInit.unlock() end
end
local i = 0
for id, collectionObj in pairs( collections ) do
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.sync", function()
local collection
local photos
local collectionSettings
if collectionsById[ id ] then
-- collection has been published before, update it's name
local collectionInfo = collectionsById[ id ]
collection = collectionInfo.collection
photos = collectionInfo.photos
collection:setName( collectionObj.title )
collectionSettings = collection:getCollectionInfoSummary().collectionSettings or {}
elseif collectionsByName[ collectionObj.title ] then
-- collection has not been published yet, set it's remote id
local collectionInfo = collectionsByName[ collectionObj.title ]
collection = collectionInfo.collection
photos = collectionInfo.photos
collection:setRemoteId( id )
collectionSettings = collection:getCollectionInfoSummary().collectionSettings or {}
else
-- collection doesn't exist, create it
collection = publishService:createPublishedCollection( collectionObj.title, false, nil )
photos = {}
collection:setRemoteId( id )
collectionSettings = { toCommunity = propertyTable.toCommunity }
end
-- always update remote url and collection settings
local collectionUrl
if collectionObj.title == "Public Profile" then
collectionUrl = "http://500px.com/" .. propertyTable.username
elseif collectionObj.title == "Library" then
collectionUrl = "http://500px.com/organizer"
else
collectionUrl = PxAPI.makeCollectionUrl( propertyTable.username, collectionObj.path )
--update collection settings (ie: path)
collectionSettings.path = collectionObj.path
end
collection:setRemoteUrl( collectionUrl )
collection:setCollectionSettings( collectionSettings )
end)
if PluginInit then PluginInit.unlock() end
progressScope:setPortionComplete( i / n )
LrTasks.yield()
end
end
LrFunctionContext.postAsyncTaskWithContext( "sync", function( context )
if PluginInit then PluginInit.lock() end
publishService.catalog:withWriteAccessDo( "sync.defaults", function()
local profileCollection = publishService:createPublishedCollection( "Public Profile", nil, true )
profileCollection:setCollectionSettings( { toCommunity = true } )
profileCollection:setRemoteUrl( "http://500px.com/" .. propertyTable.username )
for _, collection in ipairs( publishService:getChildCollections() ) do
if collection:getName() == "Profile" or collection:getName() == "Library" then
collection:setName( "Library" )
profileCollection:setCollectionSettings( { toCommunity = false } )
end
end
end )
if PluginInit then PluginInit.unlock() end
local progressScope = LrDialogs.showModalProgressDialog( {
title = "Sync with 500px",
caption = "Fetching collections from 500px.",
cannotCancel = true,
functionContext = context,
} )
doSync( context, progressScope )
end )
end
function PxUser.login( propertyTable )
if doingLogin then return end
doingLogin = true
LrFunctionContext.postAsyncTaskWithContext( "500px login", function( context )
if not propertyTable.LR_editingExistingPublishConnection then
notLoggedIn( propertyTable )
end
propertyTable.accountStatus = "Logging in..."
propertyTable.LoginButtonEnabled = false
LrDialogs.attachErrorDialogToFunctionContext( context )
context:addCleanupHandler( function()
doingLogin = false
if not storedCredentialsAreValid( propertyTable ) then
notLoggedIn( propertyTable )
end
-- error dialog?
end )
local userinfo = LrBinding.makePropertyTable( context )
local f = LrView.osFactory()
local contents = f:column {
bind_to_object = userinfo,
place = "overlapping",
fill = 1,
f:picture {
value = _PLUGIN:resourceId( "login.png" )
},
f:column {
spacing = f:control_spacing(),
f:spacer {
height = 170,
},
f:row {
spacing = f:label_spacing(),
f:static_text {
title = "Username:",
alignment = "right",
width = 100,
},
f:edit_field {
width = 300,
value = bind "username",
},
},
f:row {
spacing = f:label_spacing(),
f:static_text {
title = "Password:",
alignment = "right",
width = 100,
},
f:password_field {
width = 300,
value = bind "password",
}
},
},
}
local action = LrDialogs.presentModalDialog( {
title = "Login to 500px.",
contents = contents,
actionVerb = "Login!"
} )
if action == "cancel" then return end
propertyTable.accountStatus = "Waiting for response from 500px..."
propertyTable.username = userinfo.username
propertyTable.credentials = PxAPI.login( userinfo )
PxUser.updateUserStatusTextBindings( propertyTable )
end )
end
function PxUser.register( propertyTable )
if doingLogin then return end
doingLogin = true
LrFunctionContext.postAsyncTaskWithContext( "500px signup", function( context )
if not propertyTable.LR_editingExistingPublishConnection then
notLoggedIn( propertyTable )
end
propertyTable.accountStatus = "Signing up..."
propertyTable.LoginButtonEnabled = false
LrDialogs.attachErrorDialogToFunctionContext( context )
context:addCleanupHandler( function()
doingLogin = false
if not storedCredentialsAreValid( propertyTable ) then
notLoggedIn( propertyTable )
end
-- error dialog?
end )
local userinfo = LrBinding.makePropertyTable( context )
userinfo.tos = true
local f = LrView.osFactory()
local contents = f:view {
margin = 0,
place = "overlapping",
bind_to_object = userinfo,
spacing = f:control_spacing(),
fill = 1,
f:picture {
value = _PLUGIN:resourceId( "registration.png" )
},
f:column {
fill = 1,
f:spacer {
height = 350,
},
f:row {
f:static_text {
title = "500px is a photographic community powered by creative people from all over the world that lets you share and discover inspiring photographs.",
width = 390,
height_in_lines = 5,
},
f:column {
place_horizontal = 1,
spacing = f:control_spacing(),
f:row {
spacing = f:label_spacing(),
f:static_text {
title = "Username:",
alignment = "right",
width = 65,
place_horizontal = 1,
},
f:edit_field {
width = 300,
value = bind "username",
},
},
f:row {
spacing = f:label_spacing(),
f:static_text {
title = "E-mail Address:",
alignment = "right",
width = 120,
},
f:edit_field {
width = 300,
value = bind "email"
},
},
f:row {
spacing = f:label_spacing(),
f:static_text {
title = "Password:",
alignment = "right",
width = 120,
},
f:password_field {
width = 300,
value = bind "password",
}
},
f:row {
spacing = f:label_spacing(),
f:spacer {
width = 120,
},
f:checkbox {
title = "I agree with 500px's",
value = bind "tos",
},
f:static_text {
title = "Terms of Service",
font = "<system/bold>",
text_color = LrColor( "blue" ),
mouse_down = function() LrHttp.openUrlInBrowser( "http://500px.com/terms" ) end
},
},
},
},
},
}
local action = LrDialogs.presentModalDialog( {
title = "Sign up with 500px.",
contents = contents,
actionVerb = "Sign Up!",
accessoryView = f:row {
f:push_button {
title = "Learn More...",
action = function() LrHttp.openUrlInBrowser( "http://500px.com/about" ) end
}
}
} )
if action == "cancel" then return end
if not userinfo.tos then
LrDialogs.message( "You must accept 500px's Terms of Service." )
return
end
if not userinfo.password then
LrDialogs.message( "You must choose a password" )
return
elseif string.len( userinfo.password ) < 6 then
LrDialogs.message( "Your password must be at least 6 characters long." )
return
end
if not userinfo.email or not string.match( userinfo.email, "%w+@[%w.]+%w" ) then
LrDialogs.message( "You must provide an email address." )
return
end
if not userinfo.username or string.len( userinfo.username ) == 0 then
LrDialogs.message( "You must choose a username." )
return
end
propertyTable.accountStatus = "Waiting for response from 500px..."
propertyTable.username = userinfo.username
local success, obj = PxAPI.register( userinfo )
if success then
propertyTable.credentials = obj
PxUser.updateUserStatusTextBindings( propertyTable )
elseif obj.email then
LrDialogs.message( "Unable to Create an Account", "The email address is not valid or has already been used. Please use a different email address." )
elseif obj.username then
LrDialogs.message( "Unable to Create an Account", "This username has already been used. Please change your username." )
else
LrDialogs.message( "Unable to Create an Account", "Something went wrong, try again later." )
end
end )
end
function PxUser.verifyLogin( propertyTable )
local function updateStatus()
LrTasks.startAsyncTask( function()
if storedCredentialsAreValid( propertyTable ) then
propertyTable.accountStatus = string.format( "Logged in as %s", propertyTable.username )
logger:trace("User logged in")
if propertyTable.LR_editingExistingPublishConnection then
propertyTable.loginButtonEnabled = false
propertyTable.loginButtonTitle = "Login"
else
propertyTable.loginButtonEnabled = true
propertyTable.loginButtonTitle = "Switch User"
end
if not propertyTable.validAccount then
propertyTable.validAccount = true
end
elseif not storedCredentialsAreValid( propertyTable ) then
logger:trace("Credential are not valid")
notLoggedIn( propertyTable )
end
PxUser.updateUserStatusTextBindings( propertyTable )
end )
end
propertyTable:addObserver( "credentials", updateStatus )
updateStatus()
end
function PxUser.updateUserStatusTextBindings( propertyTable )
if propertyTable.credentials then
LrFunctionContext.postAsyncTaskWithContext( "500px account status" , function( context )
context:addFailureHandler( function()
propertyTable.accountStatus = string.format( "Login failed, was logged in as %s", propertyTable.username )
propertyTable.loginButtonTitle = "Login"
propertyTable.loginButtonEnabled = true
propertyTable.validAccount = false
propertyTable.isUserAwesome = false
if propertyTable.LR_editingExistingPublishCollection then
propertyTable.accountTypeMessage = "Could not verify this 500px account. Please login again. Note that you can not change the 500px account for an existing publish connection. You must login to the same account."
else
propertyTable.accountTypeMessage = "Login with your 500px account or sign up to create a new one."
end
end )
local success, obj = PxAPI.getUser( propertyTable )
-- if obj == "banned" then
-- propertyTable.accountStatus = string.format( "Login failed, user '%s' is banned or deactivated.", propertyTable.username )
-- -- else
-- -- propertyTable.accountStatus = string.format( "Login failed, was logged in as %s", propertyTable.username )
-- end
local userinfo = obj.user
if not propertyTable.userId then
propertyTable.username = userinfo.username
propertyTable.domain = userinfo.domain
propertyTable.userId = userinfo.id
propertyTable.uploadLimit = userinfo.upload_limit
elseif propertyTable.userId ~= userinfo.id and propertyTable.LR_editingExistingPublishConnection then
LrDialogs.message( "You can not change 500px accounts on an existing publish connection. Please login again with the account you used when you first created this connection." )
end
if not propertyTable.LR_isExportForPublish then
local status, obj = PxAPI.getCollections( propertyTable )
local collections = {}
local n = 0
for i, collection in ipairs( obj.collections ) do
collections[ collection.id ] = collection
n = n + 1
end
propertyTable.collections = collections
propertyTable.nCollections = n
end
local awesome = (userinfo.upgrade_status == 2)
if propertyTable.isUserAwesome ~= awesome then
propertyTable.isUserAwesome = awesome
end
local plus = (userinfo.upgrade_status == 1)
if propertyTable.isUserPlus ~= plus then
propertyTable.isUserPlus = plus
end
if userinfo and userinfo.upgrade_status == 0 then
propertyTable.accountTypeMessage = "You have a free account. You can upload 10 images per 7 day period. Upgrade to Plus or Awesome to have unlimited uploads, collections and more."
elseif userinfo and userinfo.upgrade_status == 1 then
propertyTable.accountTypeMessage = "You have a Plus account. You have unlimited uploads and sets. To create a portfolio, upgrade to Awesome."
elseif userinfo then
propertyTable.accountTypeMessage = "You are awesome and have unlimited uploads and sets."
end
end )
else
notLoggedIn( propertyTable )
propertyTable.accountTypeMessage = "Login with your 500px account or sign up to create a new account."
end
end