-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #86 from Jike11/master
feat: Add web push hook functionality and configuration options
- Loading branch information
Showing
10 changed files
with
142 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package web_push | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"pmail/config" | ||
"pmail/dto/parsemail" | ||
"pmail/utils/context" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type WebPushHook struct { | ||
url string | ||
token string | ||
} | ||
|
||
// EmailData 用于存储解析后的邮件数据 | ||
type EmailData struct { | ||
From string `json:"from"` | ||
To []string `json:"to"` | ||
Subject string `json:"subject"` | ||
Body string `json:"body"` | ||
Token string `json:"token"` | ||
} | ||
|
||
func (w *WebPushHook) SendBefore(ctx *context.Context, email *parsemail.Email) { | ||
|
||
} | ||
|
||
func (w *WebPushHook) SendAfter(ctx *context.Context, email *parsemail.Email, err map[string]error) { | ||
|
||
} | ||
|
||
func (w *WebPushHook) ReceiveParseBefore(email []byte) { | ||
|
||
} | ||
|
||
func (w *WebPushHook) ReceiveParseAfter(email *parsemail.Email) { | ||
if w.url == "" { | ||
return | ||
} | ||
|
||
content := string(email.Text) | ||
|
||
if content == "" { | ||
content = email.Subject | ||
} | ||
|
||
webhookURL := w.url // 替换为您的 Webhook URL | ||
|
||
to := make([]string, len(email.To)) | ||
for i, user := range email.To { | ||
to[i] = user.EmailAddress | ||
} | ||
|
||
data := EmailData{ | ||
From: email.From.EmailAddress, | ||
To: to, | ||
Subject: email.Subject, | ||
Body: content, | ||
Token: w.token, | ||
} | ||
|
||
var ctx *context.Context = nil | ||
jsonData, err := json.Marshal(data) | ||
|
||
if err != nil { | ||
log.WithContext(ctx).Errorf("web push error %+v", err) | ||
} | ||
|
||
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData)) | ||
if err != nil { | ||
log.WithContext(ctx).Errorf("web push error %+v", err) | ||
} | ||
defer resp.Body.Close() | ||
} | ||
|
||
func NewWebPushHook() *WebPushHook { | ||
|
||
ret := &WebPushHook{ | ||
url: config.Instance.WebPushUrl, | ||
token: config.Instance.WebPushToken, | ||
} | ||
return ret | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package web_push | ||
|
||
import ( | ||
"pmail/config" | ||
"pmail/dto/parsemail" | ||
"testing" | ||
) | ||
|
||
func testInit() { | ||
|
||
config.Init() | ||
|
||
} | ||
func TestWebPushHook_ReceiveParseAfter(t *testing.T) { | ||
testInit() | ||
|
||
w := NewWebPushHook() | ||
w.ReceiveParseAfter(&parsemail.Email{Subject: "标题", Text: []byte("文本内容")}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters