-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.lua
More file actions
1021 lines (859 loc) · 26.5 KB
/
Copy pathCore.lua
File metadata and controls
1021 lines (859 loc) · 26.5 KB
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
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
local ADDON_NAME, ns = ...
local Core = CreateFrame("Frame")
ns.Core = Core
Core.addonName = ADDON_NAME
local ADDON_PLAIN_NAME = "BluePosts"
local ADDON_DISPLAY_NAME = "|cffffc757Blue|r|cff00b4ffPosts|r"
ns.ADDON_PLAIN_NAME = ADDON_PLAIN_NAME
ns.ADDON_DISPLAY_NAME = ADDON_DISPLAY_NAME
local GUILD_SHARE_POPUP = "BLUEPOSTS_CONFIRM_GUILD_SHARE"
local DEFAULT_DB = {
read = {},
minimap = {
hide = false,
showInCompartment = true,
},
window = {
point = "CENTER",
relativePoint = "CENTER",
x = 0,
y = 0,
width = 980,
height = 650,
maximized = false,
},
filters = {
category = "ALL",
region = "ALL",
},
showToasts = true,
toastSound = true,
toastDuration = 12,
toastPosition = "TOPCENTER",
toastOffsetX = 0,
toastOffsetY = 30,
autoMarkRead = true,
confirmGuildShare = true,
readerFontSize = 13,
resumeLastPost = true,
dateFormat = "MDY_SLASH",
showClassicPosts = false,
}
local RESETTABLE_SETTING_KEYS = {
"minimap",
"showToasts",
"toastSound",
"toastDuration",
"toastPosition",
"toastOffsetX",
"toastOffsetY",
"autoMarkRead",
"confirmGuildShare",
"readerFontSize",
"resumeLastPost",
"dateFormat",
"showClassicPosts",
}
local DATE_FORMATS = {
MDY_SLASH = "%m/%d/%Y %H:%M",
DMY_SLASH = "%d/%m/%Y %H:%M",
YMD_DASH = "%Y-%m-%d %H:%M",
YMD_SLASH = "%Y/%m/%d %H:%M",
DMY_DOT = "%d.%m.%Y %H:%M",
}
ns.THEME = {
bg = { 0.10, 0.10, 0.10, 0.85 },
panel = { 0.07, 0.07, 0.08, 0.88 },
rail = { 0.055, 0.055, 0.065, 0.92 },
void = { 0.28, 0.08, 0.45, 0.95 },
voidSoft = { 0.18, 0.06, 0.30, 0.55 },
gold = { 1.00, 0.78, 0.34, 1.00 },
blue = { 0.00, 0.70, 1.00, 1.00 },
text = { 0.88, 0.88, 0.86, 1.00 },
muted = { 0.62, 0.62, 0.64, 1.00 },
danger = { 1.00, 0.26, 0.26, 1.00 },
}
ns.CATEGORY_META = {
ALL = {
label = "All",
icon = "Interface\\Icons\\INV_Misc_Book_09",
},
NEWS = {
label = "News",
icon = "Interface\\Icons\\INV_Letter_15",
},
PTR = {
label = "PTR",
icon = "Interface\\Icons\\INV_Misc_Gear_01",
},
FIXES = {
label = "Fixes",
icon = "Interface\\Icons\\Trade_Engineering",
},
CLASS = {
label = "Classes",
icon = "Interface\\Icons\\INV_Misc_Book_11",
},
BLOG = {
label = "Blog",
icon = "Interface\\Icons\\INV_Misc_Note_01",
},
}
local CLASS_NAMES = {
["DEATH KNIGHT"] = true,
["DEMON HUNTER"] = true,
["DRUID"] = true,
["DRACTHYR"] = true,
["EVOKER"] = true,
["HUNTER"] = true,
["MAGE"] = true,
["MONK"] = true,
["PALADIN"] = true,
["PRIEST"] = true,
["ROGUE"] = true,
["SHAMAN"] = true,
["WARLOCK"] = true,
["WARRIOR"] = true,
}
ns.CLASS_NAMES = CLASS_NAMES
local CATEGORY_FILTER_KEYS = {
ALL = true,
NEWS = true,
PTR = true,
FIXES = true,
CLASS = true,
}
local CATEGORY_KEYS = {
NEWS = true,
PTR = true,
FIXES = true,
CLASS = true,
}
local REGION_FILTER_KEYS = {
ALL = true,
EU = true,
US = true,
}
local function NormalizeRegionFilter(region)
region = tostring(region or ""):upper()
if region == "NA" then
region = "US"
end
return REGION_FILTER_KEYS[region] and region or nil
end
local function NormalizeCategoryFilter(category)
category = tostring(category or ""):upper()
if category == "BLOG" then
category = "NEWS"
end
return CATEGORY_FILTER_KEYS[category] and category or nil
end
local function NormalizeCategoryKey(category)
category = tostring(category or ""):upper()
if category == "BLOG" then
category = "NEWS"
end
return CATEGORY_KEYS[category] and category or nil
end
local function DetectClientRegion()
local regionID = GetCurrentRegion and GetCurrentRegion()
if regionID == 3 or (_G.LE_REGION_EU and regionID == _G.LE_REGION_EU) then
return "EU"
end
if regionID == 1
or (_G.LE_REGION_AMERICAS and regionID == _G.LE_REGION_AMERICAS)
or (_G.LE_REGION_US and regionID == _G.LE_REGION_US) then
return "US"
end
local portal
if C_CVar and C_CVar.GetCVar then
portal = C_CVar.GetCVar("portal")
elseif GetCVar then
portal = GetCVar("portal")
end
return NormalizeRegionFilter(portal)
end
local function CopyDefaults(src, dst)
for key, value in pairs(src) do
if type(value) == "table" then
if type(dst[key]) ~= "table" then
dst[key] = {}
end
CopyDefaults(value, dst[key])
elseif dst[key] == nil then
dst[key] = value
end
end
end
local function CopyValue(value)
if type(value) ~= "table" then
return value
end
local copy = {}
for key, child in pairs(value) do
copy[key] = CopyValue(child)
end
return copy
end
local function ResetValue(defaultValue, targetValue)
if type(defaultValue) == "table" and type(targetValue) == "table" then
wipe(targetValue)
for key, child in pairs(defaultValue) do
targetValue[key] = CopyValue(child)
end
return targetValue
end
return CopyValue(defaultValue)
end
local function Trim(value)
if value == nil then
return ""
end
return tostring(value):match("^%s*(.-)%s*$") or ""
end
local function Contains(haystack, needle)
return haystack:find(needle, 1, true) ~= nil
end
local function NormalizeText(value)
return Trim(value):upper()
end
local function NormalizeDateFormat(formatKey)
formatKey = tostring(formatKey or ""):upper()
return DATE_FORMATS[formatKey] and formatKey or "MDY_SLASH"
end
local function IsClassicPost(post)
if type(post) ~= "table" then
return false
end
local haystack = NormalizeText((post.title or "") .. " " .. (post.category or ""))
return Contains(haystack, "CLASSIC")
or Contains(haystack, "SEASON OF DISCOVERY")
or Contains(haystack, "HARDCORE")
end
local function GetCategoryKey(post)
local title = (post.title or ""):lower()
local category = (post.category or ""):lower()
local haystack = category .. " " .. title
if Contains(haystack, "hotfix") or Contains(haystack, "fixes") then
return "FIXES"
end
if Contains(haystack, "ptr") or Contains(haystack, "public test") then
return "PTR"
end
if Contains(haystack, "death knight") or Contains(haystack, "demon hunter") or Contains(haystack, "evoker") or Contains(haystack, "class") then
return "CLASS"
end
if Contains(category, "blog") or Contains(category, "blogs") then
return "NEWS"
end
return "NEWS"
end
local function GetPostRegion(post)
local category = post and (post.category or "") or ""
local title = post and (post.title or "") or ""
local haystack = category .. " " .. title
if haystack:find("%(EU%)") then
return "EU"
end
if haystack:find("%(US%)") then
return "US"
end
return "OTHER"
end
local function PostMatchesRegionFilter(post, regionFilter)
regionFilter = NormalizeRegionFilter(regionFilter) or "ALL"
if regionFilter == "ALL" then
return true
end
local postRegion = NormalizeRegionFilter(post and post.region) or GetPostRegion(post)
return postRegion == regionFilter
end
local function FormatDate(timestamp, formatKey)
timestamp = tonumber(timestamp) or 0
if timestamp <= 0 then
return ""
end
return date(DATE_FORMATS[NormalizeDateFormat(formatKey)], timestamp)
end
ns.FormatDate = FormatDate
ns.IsClassicPost = IsClassicPost
ns.NormalizeText = NormalizeText
ns.GetCategoryKey = GetCategoryKey
ns.GetPostRegion = GetPostRegion
ns.NormalizeCategoryFilter = NormalizeCategoryFilter
ns.NormalizeRegionFilter = NormalizeRegionFilter
function Core:Print(message)
DEFAULT_CHAT_FRAME:AddMessage(ADDON_DISPLAY_NAME .. " " .. tostring(message))
end
function Core:GetDefaultRegionFilter()
return self.detectedRegion or "ALL"
end
function Core:GetDateFormat()
return NormalizeDateFormat(self.db and self.db.dateFormat)
end
function Core:FormatDate(timestamp)
return FormatDate(timestamp, self:GetDateFormat())
end
function Core:IsClassicPost(post)
return IsClassicPost(post)
end
function Core:IsPostVisible(post)
if not post then
return false
end
return not self.db or self.db.showClassicPosts == true or not IsClassicPost(post)
end
function Core:GetVisiblePostCount()
local count = 0
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post) then
count = count + 1
end
end
return count
end
function Core:RefreshFormattedDates()
for _, post in ipairs(self.posts or {}) do
post.dateText = self:FormatDate(post.timestamp)
end
end
function Core:SetDateFormat(formatKey)
if not self.db then
return
end
self.db.dateFormat = NormalizeDateFormat(formatKey)
self:RefreshFormattedDates()
if ns.UI and ns.UI.RefreshDateDisplays then
ns.UI:RefreshDateDisplays()
end
end
function Core:SetShowClassicPosts(enabled)
if not self.db then
return
end
self.db.showClassicPosts = enabled ~= false
if ns.UI and ns.UI.ApplyPostVisibilityPreferences then
ns.UI:ApplyPostVisibilityPreferences()
end
end
function Core:GetRegionFilter()
local filters = self.db and self.db.filters
return NormalizeRegionFilter(filters and filters.region) or self:GetDefaultRegionFilter()
end
function Core:SetRegionFilter(region, automatic)
if not self.db then
return
end
self.db.filters = self.db.filters or {}
region = NormalizeRegionFilter(region) or self:GetDefaultRegionFilter()
self.db.filters.region = region
self.db.regionFilterAutoRegion = automatic and region ~= "ALL" and region or nil
end
function Core:PostMatchesRegionFilter(post, regionFilter)
return PostMatchesRegionFilter(post, regionFilter)
end
function Core:InitializeDB()
local hadRegionFilter = BluePostsDB
and BluePostsDB.filters
and BluePostsDB.filters.region ~= nil
BluePostsDB = BluePostsDB or {}
self.detectedRegion = DetectClientRegion()
CopyDefaults(DEFAULT_DB, BluePostsDB)
self.db = BluePostsDB
if BluePostsDB.filters then
BluePostsDB.filters.unreadOnly = nil
end
BluePostsDB.filters = BluePostsDB.filters or {}
BluePostsDB.filters.category = NormalizeCategoryFilter(BluePostsDB.filters.category) or "ALL"
BluePostsDB.dateFormat = NormalizeDateFormat(BluePostsDB.dateFormat)
BluePostsDB.showClassicPosts = BluePostsDB.showClassicPosts == true
local defaultRegion = self:GetDefaultRegionFilter()
local savedRegion = NormalizeRegionFilter(BluePostsDB.filters.region)
local autoRegion = NormalizeRegionFilter(BluePostsDB.regionFilterAutoRegion)
if not hadRegionFilter then
self:SetRegionFilter(defaultRegion, defaultRegion ~= "ALL")
elseif autoRegion and savedRegion == autoRegion then
if defaultRegion ~= "ALL" and defaultRegion ~= autoRegion then
self:SetRegionFilter(defaultRegion, true)
else
BluePostsDB.filters.region = savedRegion
end
elseif savedRegion then
BluePostsDB.filters.region = savedRegion
BluePostsDB.regionFilterAutoRegion = nil
else
self:SetRegionFilter(defaultRegion, defaultRegion ~= "ALL")
end
end
function Core:NormalizeData()
self.posts = {}
self.postsByID = {}
self.categories = {}
self.regions = {}
local raw = BluePosts_Data or {}
local source = raw.posts or raw
self.packageTimestamp = tonumber(raw.package_timestamp or raw.generated_at) or 0
self.packageNewPostIDs = {}
self.packageNewPostLookup = {}
for _, postID in ipairs(raw.new_post_ids or {}) do
postID = Trim(postID)
if postID ~= "" then
self.packageNewPostIDs[#self.packageNewPostIDs + 1] = postID
self.packageNewPostLookup[postID] = true
end
end
for key, post in pairs(source) do
if type(post) == "table" and post.title then
local id = tostring(post.id or key)
local normalized = {}
for postKey, value in pairs(post) do
normalized[postKey] = value
end
normalized.id = id
normalized.post_key = normalized.post_key or normalized.postKey or id
normalized.title = Trim(normalized.title)
normalized.category = Trim(normalized.category)
normalized.url = Trim(normalized.url or normalized.source_url)
normalized.timestamp = tonumber(normalized.timestamp) or 0
normalized.dateText = self:FormatDate(normalized.timestamp)
normalized.categoryKey = NormalizeCategoryKey(normalized.categoryKey) or GetCategoryKey(normalized)
normalized.region = normalized.region or GetPostRegion(normalized)
normalized.content = type(normalized.content) == "table" and normalized.content or {}
normalized.isPackagedNew = self.packageNewPostLookup[id] == true
table.insert(self.posts, normalized)
self.postsByID[id] = normalized
self.categories[normalized.categoryKey] = (self.categories[normalized.categoryKey] or 0) + 1
self.regions[normalized.region] = (self.regions[normalized.region] or 0) + 1
end
end
table.sort(self.posts, function(left, right)
if left.timestamp == right.timestamp then
return (left.title or "") < (right.title or "")
end
return (left.timestamp or 0) > (right.timestamp or 0)
end)
end
function Core:GetPost(id)
return self.postsByID and self.postsByID[id]
end
function Core:IsRead(post)
if not post then
return false
end
return self.db and self.db.read and self.db.read[post.id] == true
end
function Core:SetRead(post, read)
if not post or not self.db then
return
end
self.db.read[post.id] = read and true or nil
if ns.UI then
ns.UI:RefreshPostList()
ns.UI:UpdateToolbar()
ns.UI:RefreshSettingsPanel()
end
end
function Core:SetAllRead(read)
if not self.db or not self.db.read then
return
end
if read then
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post) then
self.db.read[post.id] = true
end
end
else
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post) then
self.db.read[post.id] = nil
end
end
end
if ns.UI then
ns.UI:RefreshPostList()
ns.UI:UpdateToolbar()
ns.UI:RefreshSettingsPanel()
end
end
function Core:ResetSettingsToDefaults()
if not self.db then
return
end
for _, key in ipairs(RESETTABLE_SETTING_KEYS) do
self.db[key] = ResetValue(DEFAULT_DB[key], self.db[key])
end
self.db.dateFormat = NormalizeDateFormat(self.db.dateFormat)
self:RefreshFormattedDates()
self:UpdateMinimapVisibility()
if ns.UI then
if ns.UI.ApplyPostVisibilityPreferences then
ns.UI:ApplyPostVisibilityPreferences()
end
if ns.UI.RefreshDateDisplays then
ns.UI:RefreshDateDisplays()
end
if ns.UI.ApplyToastPosition then
ns.UI:ApplyToastPosition()
end
if ns.UI.RefreshSettingsPanel then
ns.UI:RefreshSettingsPanel()
end
end
self:Print("Settings reset to defaults.")
end
function Core:GetUnreadCount()
local count = 0
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post) and not self:IsRead(post) then
count = count + 1
end
end
return count
end
function Core:IsPackagedNewPost(post)
if type(post) == "table" then
if post.isPackagedNew ~= nil then
return post.isPackagedNew == true
end
post = post.id
end
post = Trim(post)
return post ~= "" and self.packageNewPostLookup and self.packageNewPostLookup[post] == true or false
end
function Core:GetNewestUnreadPost(regionFilter)
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post) and PostMatchesRegionFilter(post, regionFilter) and not self:IsRead(post) then
return post
end
end
return nil
end
function Core:GetNewestRecentPost(regionFilter)
local now = time()
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post)
and PostMatchesRegionFilter(post, regionFilter)
and post.timestamp
and post.timestamp > 0
and (now - post.timestamp) <= 86400 then
return post
end
end
return nil
end
function Core:GetNewestPackagedUnreadPost(regionFilter)
if not next(self.packageNewPostLookup or {}) then
return nil
end
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post)
and self:IsPackagedNewPost(post)
and PostMatchesRegionFilter(post, regionFilter)
and not self:IsRead(post) then
return post
end
end
return nil
end
function Core:GetToastPreviewPost()
if ns.UI and ns.UI.selectedPost and self:IsPostVisible(ns.UI.selectedPost) then
return ns.UI.selectedPost
end
local regionFilter = self:GetRegionFilter()
return self:GetNewestPackagedUnreadPost(regionFilter)
or self:GetNewestUnreadPost(regionFilter)
or self:GetNewestRecentPost(regionFilter)
or (function()
for _, post in ipairs(self.posts or {}) do
if self:IsPostVisible(post) then
return post
end
end
end)()
end
function Core:ShowToast(post, rememberToastID)
if not post or not self:IsPostVisible(post) or not ns.UI or not ns.UI.ShowToast then
return false
end
local shown = ns.UI:ShowToast(post)
if shown and rememberToastID ~= false and self.db then
self.db.lastToastID = post.id
end
return shown and true or false
end
function Core:Show(postID)
if ns.UI then
ns.UI:Show()
if postID then
ns.UI:SelectPost(postID)
end
end
end
function Core:Hide()
if ns.UI then
ns.UI:Hide()
end
end
function Core:Toggle()
if ns.UI and ns.UI.frame and ns.UI.frame:IsShown() then
self:Hide()
else
self:Show()
end
end
function Core:RegisterGuildSharePopup()
if not StaticPopupDialogs or StaticPopupDialogs[GUILD_SHARE_POPUP] then
return
end
StaticPopupDialogs[GUILD_SHARE_POPUP] = {
text = "Share this Blue Post with your guild?\n\n%s",
button1 = "Share",
button2 = "Cancel",
OnAccept = function(_, post)
Core:AnnounceGuild(post)
end,
timeout = 0,
whileDead = true,
hideOnEscape = true,
preferredIndex = 3,
}
end
function Core:ConfirmAnnounceGuild(post)
if not post then
return
end
if not IsInGuild() then
self:Print("You are not in a guild.")
return
end
if self.db and self.db.confirmGuildShare == false then
self:AnnounceGuild(post)
return
end
if StaticPopup_Show then
StaticPopup_Show(GUILD_SHARE_POPUP, post.title or "Blue Post", nil, post)
return
end
self:AnnounceGuild(post)
end
function Core:AnnounceGuild(post)
if not post then
return
end
if not IsInGuild() then
self:Print("You are not in a guild.")
return
end
local title = post.title or "Blue post"
if #title > 120 then
title = title:sub(1, 117) .. "..."
end
local message = ("%s: %s - %s"):format(ADDON_PLAIN_NAME, title, post.url or "")
if #message > 255 then
message = message:sub(1, 252) .. "..."
end
SendChatMessage(message, "GUILD")
self:Print("Shared with guild.")
end
function Core:InitializeBroker()
local dataBroker
local dbIcon
if LibStub then
dataBroker = LibStub("LibDataBroker-1.1", true)
dbIcon = LibStub("LibDBIcon-1.0", true)
end
if dataBroker then
self.ldb = dataBroker:NewDataObject("BluePosts", {
type = "launcher",
icon = "Interface\\Icons\\INV_Letter_15",
label = ADDON_DISPLAY_NAME,
text = ADDON_DISPLAY_NAME,
OnClick = function(_, button)
if button == "LeftButton" then
self:Toggle()
end
end,
OnTooltipShow = function(tooltip)
tooltip:AddLine(ADDON_DISPLAY_NAME)
tooltip:AddLine("Left click: open", 0.8, 0.8, 0.8)
tooltip:AddLine(("Unread: %d"):format(self:GetUnreadCount()), 0.0, 0.7, 1.0)
end,
})
end
if self.ldb and dbIcon then
self.dbIcon = dbIcon
self.db.minimap.showInCompartment = true
dbIcon:Register("BluePosts", self.ldb, self.db.minimap)
if dbIcon.AddButtonToCompartment then
dbIcon:AddButtonToCompartment("BluePosts")
end
if self.db.minimap.hide then
dbIcon:Hide("BluePosts")
end
return
end
self:CreateFallbackMinimapButton()
end
function Core:CreateFallbackMinimapButton()
if self.db.minimap.hide or not Minimap then
return
end
local button = CreateFrame("Button", "BluePosts_MinimapButton", Minimap, "BackdropTemplate")
button:SetSize(32, 32)
button:SetPoint("TOPLEFT", Minimap, "TOPLEFT", -2, -2)
button:SetFrameStrata("MEDIUM")
button:RegisterForClicks("LeftButtonUp")
button.icon = button:CreateTexture(nil, "ARTWORK")
button.icon:SetAllPoints(button)
button.icon:SetTexture("Interface\\Icons\\INV_Letter_15")
button.icon:SetTexCoord(0.08, 0.92, 0.08, 0.92)
button:SetHighlightTexture("Interface\\Minimap\\UI-Minimap-ZoomButton-Highlight")
button:SetScript("OnClick", function()
self:Toggle()
end)
button:SetScript("OnEnter", function()
GameTooltip:SetOwner(button, "ANCHOR_LEFT")
GameTooltip:AddLine(ADDON_DISPLAY_NAME)
GameTooltip:AddLine("Left click: open", 0.8, 0.8, 0.8)
GameTooltip:Show()
end)
button:SetScript("OnLeave", GameTooltip_Hide)
self.fallbackMinimapButton = button
end
function Core:ApplyMBBMinimapVisibility(button, hidden)
if not button or not button.oshow or not button.ohide then
return false
end
-- MBB wraps Show/Hide and keeps the originals on oshow/ohide.
local name = button:GetName()
local excluded = MBB_IsInArray and MBB_Exclude and MBB_IsInArray(MBB_Exclude, name)
button.isvisible = not hidden
if hidden then
button.ohide(button)
elseif excluded or MBB_IsShown == 1 then
button.oshow(button)
else
button.ohide(button)
end
if MBB_SetPositions then
MBB_SetPositions()
end
return true
end
function Core:UpdateMinimapVisibility()
local hidden = self.db and self.db.minimap and self.db.minimap.hide == true
if self.dbIcon then
local minimapButton = self.dbIcon.GetMinimapButton and self.dbIcon:GetMinimapButton("BluePosts")
local handledByMBB = self:ApplyMBBMinimapVisibility(minimapButton, hidden)
if hidden then
self.dbIcon:Hide("BluePosts")
else
self.dbIcon:Show("BluePosts")
end
if self.dbIcon.Refresh then
self.dbIcon:Refresh("BluePosts", self.db.minimap)
end
if minimapButton and not handledByMBB then
if hidden then
minimapButton:Hide()
else
minimapButton:Show()
end
end
elseif self.fallbackMinimapButton then
self.fallbackMinimapButton:SetShown(not hidden)
elseif not hidden then
self:CreateFallbackMinimapButton()
end
if ns.UI and ns.UI.RefreshSettingsPanel then
ns.UI:RefreshSettingsPanel()
end
end
function Core:HandleSlash(message)
local command = Trim(message):lower()
if command == "reset" then
if ns.UI then
ns.UI:ResetPosition()
end
return
end
if command == "minimap" then
self.db.minimap.hide = not self.db.minimap.hide
self:UpdateMinimapVisibility()
self:Print(self.db.minimap.hide and "Minimap button hidden." or "Minimap button visible.")
return
end
if command == "toasts" then
self.db.showToasts = not self.db.showToasts
if ns.UI and ns.UI.RefreshSettingsPanel then
ns.UI:RefreshSettingsPanel()
end
self:Print(self.db.showToasts and "Toasts enabled." or "Toasts disabled.")
return
end
if command == "toasttest" then
local post = self:GetToastPreviewPost()
if not post then
self:Print("No post available for toast preview.")
return
end
if not self:ShowToast(post, false) then
self:Print("Toast preview failed.")
end
return
end
if command == "settings" or command == "options" then
self:Show()
if ns.UI and ns.UI.ShowSettings then
ns.UI:ShowSettings()
end
return
end
self:Toggle()
end
function Core:RegisterSlashCommands()
SLASH_BLUEPOSTS1 = "/blueposts"
SLASH_BLUEPOSTS2 = "/bp"
SlashCmdList.BLUEPOSTS = function(message)
self:HandleSlash(message)
end
end
function Core:MaybeShowLoginToast()
if not self.db or self.db.showToasts == false then
return
end
local regionFilter = self:GetRegionFilter()
local post = self:GetNewestPackagedUnreadPost(regionFilter)
if not post or self.db.lastToastID == post.id then
return
end
C_Timer.After(2.0, function()
if self:IsPostVisible(post) and not self:IsRead(post) and PostMatchesRegionFilter(post, self:GetRegionFilter()) then
self:ShowToast(post)
end
end)
end
function Core:OnAddonLoaded(addonName)
if addonName ~= ADDON_NAME then
return
end
self:InitializeDB()
self:RegisterGuildSharePopup()